Skip to content

Commit

Permalink
Merge pull request #98 from NaikSoftware/forresthopkins-feature/deep-…
Browse files Browse the repository at this point in the history
…refactor

Forresthopkins/DrStranges deep refactor
  • Loading branch information
NaikSoftware authored Apr 3, 2018
2 parents fae04e6 + d67eaeb commit f02fdae
Show file tree
Hide file tree
Showing 17 changed files with 469 additions and 373 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.idea
.DS_Store
/build
/captures
/captures
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -18,7 +21,7 @@ allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
maven { url "http://clojars.org/repo" }
google()
}
}

Expand Down
5 changes: 2 additions & 3 deletions example-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion '26.0.2'

defaultConfig {
applicationId "ua.naiksoftware.stompclientexample"
Expand All @@ -29,7 +28,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'org.java-websocket:java-websocket:1.3.2'
compile 'org.java-websocket:Java-WebSocket:1.3.6'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.naiksoftware.stompclientexample;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import retrofit2.http.POST;
import retrofit2.http.Query;
Expand All @@ -10,5 +11,5 @@
public interface ExampleRepository {

@POST("hello-convert-and-send")
Flowable<Void> sendRestEcho(@Query("msg") String message);
Completable sendRestEcho(@Query("msg") String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.java_websocket.WebSocket;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import io.reactivex.FlowableTransformer;
import io.reactivex.CompletableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -56,7 +54,7 @@ public void disconnectStomp(View view) {
}

public void connectStomp(View view) {
mStompClient = Stomp.over(WebSocket.class, "ws://" + ANDROID_EMULATOR_LOCALHOST
mStompClient = Stomp.over(Stomp.ConnectionProvider.JWS, "ws://" + ANDROID_EMULATOR_LOCALHOST
+ ":" + RestClient.SERVER_PORT + "/example-endpoint/websocket");

mStompClient.lifecycle()
Expand Down Expand Up @@ -91,7 +89,7 @@ public void connectStomp(View view) {
public void sendEchoViaStomp(View v) {
mStompClient.send("/topic/hello-msg-mapping", "Echo STOMP " + mTimeFormat.format(new Date()))
.compose(applySchedulers())
.subscribe(aVoid -> {
.subscribe(() -> {
Log.d(TAG, "STOMP echo send successfully");
}, throwable -> {
Log.e(TAG, "Error send STOMP echo", throwable);
Expand All @@ -103,7 +101,7 @@ public void sendEchoViaRest(View v) {
mRestPingDisposable = RestClient.getInstance().getExampleRepository()
.sendRestEcho("Echo REST " + mTimeFormat.format(new Date()))
.compose(applySchedulers())
.subscribe(aVoid -> {
.subscribe(() -> {
Log.d(TAG, "REST echo send successfully");
}, throwable -> {
Log.e(TAG, "Error send REST echo", throwable);
Expand All @@ -122,8 +120,8 @@ private void toast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

protected <T> FlowableTransformer<T, T> applySchedulers() {
return tFlowable -> tFlowable
protected CompletableTransformer applySchedulers() {
return upstream -> upstream
.unsubscribeOn(Schedulers.newThread())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Feb 23 17:37:13 EET 2017
#Tue Sep 05 08:26:08 MST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
12 changes: 5 additions & 7 deletions lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
apply plugin: 'com.android.library'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.github.dcendents.android-maven'

group='com.github.NaikSoftware'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
minSdkVersion 16
Expand All @@ -30,12 +28,12 @@ android {


dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile "io.reactivex.rxjava2:rxjava:2.1.2"
implementation "io.reactivex.rxjava2:rxjava:2.1.8"
// Supported transports
provided "org.java-websocket:java-websocket:1.3.2"
provided 'com.squareup.okhttp3:okhttp:3.8.0'
compileOnly 'org.java-websocket:Java-WebSocket:1.3.6'
compileOnly 'com.squareup.okhttp3:okhttp:3.9.1'

implementation 'com.android.support:support-annotations:27.1.0'
}

task sourcesJar(type: Jar) {
Expand Down

This file was deleted.

5 changes: 2 additions & 3 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ua.naiksoftware.stomp">
<manifest package="com.github.forresthopkinsa">

<application/>
<application />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package ua.naiksoftware.stomp;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;

/**
* Created by forresthopkinsa on 8/8/2017.
* <p>
* Created because there was a lot of shared code between JWS and OkHttp connection providers.
*/

abstract class AbstractConnectionProvider implements ConnectionProvider {

private static final String TAG = AbstractConnectionProvider.class.getSimpleName();

@NonNull
private final PublishSubject<LifecycleEvent> mLifecycleStream;
@NonNull
private final PublishSubject<String> mMessagesStream;

AbstractConnectionProvider() {
mLifecycleStream = PublishSubject.create();
mMessagesStream = PublishSubject.create();
}

@NonNull
@Override
public Observable<String> messages() {
return mMessagesStream.startWith(initSocket().toObservable());
}

/**
* Simply close socket.
* <p>
* For example:
* <pre>
* webSocket.close();
* </pre>
*/
abstract void rawDisconnect();

@Override
public Completable disconnect() {
return Completable
.fromAction(this::rawDisconnect);
}

private Completable initSocket() {
return Completable
.fromAction(this::createWebSocketConnection);
}

// Doesn't do anything at all, only here as a stub
public Completable setHeartbeat(int ms) {
return Completable.complete();
}

/**
* Most important method: connects to websocket and notifies program of messages.
* <p>
* See implementations in OkHttpConnectionProvider and WebSocketsConnectionProvider.
*/
abstract void createWebSocketConnection();

@NonNull
@Override
public Completable send(String stompMessage) {
return Completable.fromCallable(() -> {
if (getSocket() == null) {
throw new IllegalStateException("Not connected yet");
} else {
Log.d(TAG, "Send STOMP message: " + stompMessage);
rawSend(stompMessage);
return null;
}
});
}

/**
* Just a simple message send.
* <p>
* For example:
* <pre>
* webSocket.send(stompMessage);
* </pre>
*
* @param stompMessage message to send
*/
abstract void rawSend(String stompMessage);

/**
* Get socket object.
* Used for null checking; this object is expected to be null when the connection is not yet established.
* <p>
* For example:
* <pre>
* return webSocket;
* </pre>
*/
@Nullable
abstract Object getSocket();

void emitLifecycleEvent(@NonNull LifecycleEvent lifecycleEvent) {
Log.d(TAG, "Emit lifecycle event: " + lifecycleEvent.getType().name());
mLifecycleStream.onNext(lifecycleEvent);
}

void emitMessage(String stompMessage) {
Log.d(TAG, "Emit STOMP message: " + stompMessage);
mMessagesStream.onNext(stompMessage);
}

@NonNull
@Override
public Observable<LifecycleEvent> lifecycle() {
return mLifecycleStream;
}
}
16 changes: 13 additions & 3 deletions lib/src/main/java/ua/naiksoftware/stomp/ConnectionProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ua.naiksoftware.stomp;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Observable;

/**
* Created by naik on 05.05.16.
Expand All @@ -10,17 +12,25 @@ public interface ConnectionProvider {
/**
* Subscribe this for receive stomp messages
*/
Flowable<String> messages();
Observable<String> messages();

/**
* Sending stomp messages via you ConnectionProvider.
* onError if not connected or error detected will be called, or onCompleted id sending started
* TODO: send messages with ACK
*/
Flowable<Void> send(String stompMessage);
Completable send(String stompMessage);

/**
* Subscribe this for receive #LifecycleEvent events
*/
Flowable<LifecycleEvent> getLifecycleReceiver();
Observable<LifecycleEvent> lifecycle();

/**
* Disconnects from server. This is basically a Callable.
* Automatically emits Lifecycle.CLOSE
*/
Completable disconnect();

Completable setHeartbeat(int ms);
}
Loading

0 comments on commit f02fdae

Please sign in to comment.