Skip to content

Commit 0f285f0

Browse files
committed
Merge branch 'master' of https://github.com/forresthopkinsa/StompProtocolAndroid into forresthopkins-feature/deep-refactor
# Conflicts: # lib/src/main/java/ua/naiksoftware/stomp/OkHttpConnectionProvider.java # lib/src/main/java/ua/naiksoftware/stomp/WebSocketsConnectionProvider.java # lib/src/main/java/ua/naiksoftware/stomp/client/StompClient.java
2 parents fae04e6 + 965f127 commit 0f285f0

File tree

14 files changed

+476
-364
lines changed

14 files changed

+476
-364
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
*.iml
22
.gradle
33
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
4+
.idea
65
.DS_Store
76
/build
8-
/captures
7+
/captures

build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
maven {
7+
url "https://maven.google.com"
8+
}
69
}
710
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0'
9-
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
10-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
11+
classpath 'com.android.tools.build:gradle:3.0.1'
12+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1113

1214
// NOTE: Do not place your application dependencies here; they belong
1315
// in the individual module build.gradle files
@@ -18,7 +20,6 @@ allprojects {
1820
repositories {
1921
jcenter()
2022
maven { url "https://jitpack.io" }
21-
maven { url "http://clojars.org/repo" }
2223
}
2324
}
2425

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Thu Feb 23 17:37:13 EET 2017
1+
#Tue Sep 05 08:26:08 MST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

lib/build.gradle

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
apply plugin: 'com.android.library'
2-
apply plugin: 'me.tatarka.retrolambda'
32
apply plugin: 'com.github.dcendents.android-maven'
43

5-
group='com.github.NaikSoftware'
4+
group='com.github.forresthopkinsa'
65

76
android {
87
compileSdkVersion 25
9-
buildToolsVersion "25.0.1"
108

119
defaultConfig {
1210
minSdkVersion 16
@@ -32,10 +30,13 @@ android {
3230
dependencies {
3331
compile fileTree(include: ['*.jar'], dir: 'libs')
3432
testCompile 'junit:junit:4.12'
35-
compile "io.reactivex.rxjava2:rxjava:2.1.2"
33+
compile 'io.reactivex:rxjava:1.3.0'
34+
compile 'net.sourceforge.streamsupport:streamsupport:1.5.5'
35+
compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.5.5'
3636
// Supported transports
37-
provided "org.java-websocket:java-websocket:1.3.2"
38-
provided 'com.squareup.okhttp3:okhttp:3.8.0'
37+
compile 'org.java-websocket:Java-WebSocket:1.3.6'
38+
compile 'com.squareup.okhttp3:okhttp:3.8.1'
39+
implementation 'com.android.support:support-annotations:24.2.0'
3940
}
4041

4142
task sourcesJar(type: Jar) {

lib/src/androidTest/java/ua/naiksoftware/stomp/ApplicationTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/src/main/AndroidManifest.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="ua.naiksoftware.stomp">
1+
<manifest package="com.github.forresthopkinsa">
32

4-
<application/>
3+
<application />
54

65
</manifest>
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package ua.naiksoftware.stomp;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
import android.util.Log;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
import rx.Completable;
10+
import rx.Observable;
11+
import rx.subjects.BehaviorSubject;
12+
import rx.subjects.PublishSubject;
13+
14+
/**
15+
* Created by forresthopkinsa on 8/8/2017.
16+
* <p>
17+
* Created because there was a lot of shared code between JWS and OkHttp connection providers.
18+
*/
19+
20+
abstract class AbstractConnectionProvider implements ConnectionProvider {
21+
22+
private static final String TAG = AbstractConnectionProvider.class.getSimpleName();
23+
24+
@NonNull
25+
private final PublishSubject<LifecycleEvent> mLifecycleStream;
26+
@NonNull
27+
private final PublishSubject<String> mMessagesStream;
28+
final BehaviorSubject<Boolean> mConnectionStream;
29+
30+
AbstractConnectionProvider() {
31+
mLifecycleStream = PublishSubject.create();
32+
mMessagesStream = PublishSubject.create();
33+
mConnectionStream = BehaviorSubject.create(false);
34+
}
35+
36+
@NonNull
37+
@Override
38+
public Observable<String> messages() {
39+
return mMessagesStream.startWith(initSocket().toObservable());
40+
}
41+
42+
/**
43+
* Simply close socket.
44+
* <p>
45+
* For example:
46+
* <pre>
47+
* webSocket.close();
48+
* </pre>
49+
*/
50+
abstract void rawDisconnect();
51+
52+
@Override
53+
public Completable disconnect() {
54+
Observable<Boolean> ex = Observable.error(new IllegalStateException("Attempted to disconnect when already disconnected"));
55+
56+
Completable block = mConnectionStream
57+
.first(isConnected -> isConnected)
58+
.timeout(1, TimeUnit.SECONDS, ex)
59+
.toCompletable();
60+
61+
return Completable
62+
.fromAction(this::rawDisconnect)
63+
.startWith(block);
64+
}
65+
66+
private Completable initSocket() {
67+
Observable<Boolean> ex = Observable.error(new IllegalStateException("Attempted to connect when already connected"));
68+
69+
Completable block = mConnectionStream
70+
.first(isConnected -> !isConnected)
71+
.timeout(1, TimeUnit.SECONDS, ex)
72+
.toCompletable();
73+
74+
return Completable
75+
.fromAction(this::createWebSocketConnection)
76+
.startWith(block);
77+
}
78+
79+
// Doesn't do anything at all, only here as a stub
80+
public Completable setHeartbeat(int ms) {
81+
return Completable.complete();
82+
}
83+
84+
/**
85+
* Most important method: connects to websocket and notifies program of messages.
86+
* <p>
87+
* See implementations in OkHttpConnectionProvider and WebSocketsConnectionProvider.
88+
*/
89+
abstract void createWebSocketConnection();
90+
91+
@NonNull
92+
@Override
93+
public Completable send(String stompMessage) {
94+
return Completable.fromCallable(() -> {
95+
if (getSocket() == null) {
96+
throw new IllegalStateException("Not connected yet");
97+
} else {
98+
Log.d(TAG, "Send STOMP message: " + stompMessage);
99+
rawSend(stompMessage);
100+
return null;
101+
}
102+
});
103+
}
104+
105+
/**
106+
* Just a simple message send.
107+
* <p>
108+
* For example:
109+
* <pre>
110+
* webSocket.send(stompMessage);
111+
* </pre>
112+
*
113+
* @param stompMessage message to send
114+
*/
115+
abstract void rawSend(String stompMessage);
116+
117+
/**
118+
* Get socket object.
119+
* Used for null checking; this object is expected to be null when the connection is not yet established.
120+
* <p>
121+
* For example:
122+
* <pre>
123+
* return webSocket;
124+
* </pre>
125+
*/
126+
@Nullable
127+
abstract Object getSocket();
128+
129+
void emitLifecycleEvent(@NonNull LifecycleEvent lifecycleEvent) {
130+
Log.d(TAG, "Emit lifecycle event: " + lifecycleEvent.getType().name());
131+
mLifecycleStream.onNext(lifecycleEvent);
132+
if (lifecycleEvent.getType().equals(LifecycleEvent.Type.CLOSED))
133+
mConnectionStream.onNext(false);
134+
}
135+
136+
void emitMessage(String stompMessage) {
137+
Log.d(TAG, "Emit STOMP message: " + stompMessage);
138+
mMessagesStream.onNext(stompMessage);
139+
}
140+
141+
@NonNull
142+
@Override
143+
public Observable<LifecycleEvent> getLifecycleReceiver() {
144+
return mLifecycleStream;
145+
}
146+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ua.naiksoftware.stomp;
22

3-
import io.reactivex.Flowable;
3+
import rx.Completable;
4+
import rx.Observable;
45

56
/**
67
* Created by naik on 05.05.16.
@@ -10,17 +11,25 @@ public interface ConnectionProvider {
1011
/**
1112
* Subscribe this for receive stomp messages
1213
*/
13-
Flowable<String> messages();
14+
Observable<String> messages();
1415

1516
/**
1617
* Sending stomp messages via you ConnectionProvider.
1718
* onError if not connected or error detected will be called, or onCompleted id sending started
1819
* TODO: send messages with ACK
1920
*/
20-
Flowable<Void> send(String stompMessage);
21+
Completable send(String stompMessage);
2122

2223
/**
2324
* Subscribe this for receive #LifecycleEvent events
2425
*/
25-
Flowable<LifecycleEvent> getLifecycleReceiver();
26+
Observable<LifecycleEvent> getLifecycleReceiver();
27+
28+
/**
29+
* Disconnects from server. This is basically a Callable.
30+
* Automatically emits Lifecycle.CLOSE
31+
*/
32+
Completable disconnect();
33+
34+
Completable setHeartbeat(int ms);
2635
}

0 commit comments

Comments
 (0)