Moved the NetworkService to the graphenej library
This commit is contained in:
parent
628faf6109
commit
e9c920d469
6 changed files with 137 additions and 132 deletions
|
@ -1,6 +1,14 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cy.agorise.graphenej">
|
package="cy.agorise.graphenej">
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="1" />
|
<uses-sdk android:minSdkVersion="1" />
|
||||||
<application/>
|
|
||||||
|
<application>
|
||||||
|
<service
|
||||||
|
android:name=".api.android.NetworkService"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="true"></service>
|
||||||
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
|
@ -1,4 +1,4 @@
|
||||||
package com.luminiasoft.labs.sample;
|
package cy.agorise.graphenej.api.android;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
@ -6,7 +6,6 @@ import android.content.SharedPreferences;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
@ -20,11 +19,11 @@ import java.util.HashMap;
|
||||||
import cy.agorise.graphenej.RPC;
|
import cy.agorise.graphenej.RPC;
|
||||||
import cy.agorise.graphenej.api.ApiAccess;
|
import cy.agorise.graphenej.api.ApiAccess;
|
||||||
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
|
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
|
||||||
import cy.agorise.graphenej.api.android.RxBus;
|
|
||||||
import cy.agorise.graphenej.api.bitshares.Nodes;
|
import cy.agorise.graphenej.api.bitshares.Nodes;
|
||||||
import cy.agorise.graphenej.api.calls.ApiCallable;
|
import cy.agorise.graphenej.api.calls.ApiCallable;
|
||||||
import cy.agorise.graphenej.models.ApiCall;
|
import cy.agorise.graphenej.models.ApiCall;
|
||||||
import cy.agorise.graphenej.models.JsonRpcResponse;
|
import cy.agorise.graphenej.models.JsonRpcResponse;
|
||||||
|
import io.reactivex.annotations.Nullable;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
@ -32,7 +31,7 @@ import okhttp3.WebSocket;
|
||||||
import okhttp3.WebSocketListener;
|
import okhttp3.WebSocketListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service in charge of mantaining a connection to the full node.
|
* Service in charge of maintaining a connection to the full node.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class NetworkService extends Service {
|
public class NetworkService extends Service {
|
||||||
|
@ -69,127 +68,6 @@ public class NetworkService extends Service {
|
||||||
|
|
||||||
private Gson gson = new Gson();
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
private WebSocketListener mWebSocketListener = new WebSocketListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(WebSocket webSocket, Response response) {
|
|
||||||
super.onOpen(webSocket, response);
|
|
||||||
mWebSocket = webSocket;
|
|
||||||
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED));
|
|
||||||
|
|
||||||
if(!isLoggedIn){
|
|
||||||
Log.d(TAG,"About to send login request");
|
|
||||||
ArrayList<Serializable> loginParams = new ArrayList<>();
|
|
||||||
loginParams.add(mUsername);
|
|
||||||
loginParams.add(mPassword);
|
|
||||||
ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, ++mCurrentId);
|
|
||||||
mLastCall = RPC.CALL_LOGIN;
|
|
||||||
sendMessage(loginCall.toJsonString());
|
|
||||||
}else{
|
|
||||||
Log.d(TAG,"Already logged in");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMessage(WebSocket webSocket, String text) {
|
|
||||||
super.onMessage(webSocket, text);
|
|
||||||
Log.v(TAG,"< "+text);
|
|
||||||
JsonRpcResponse<?> response = gson.fromJson(text, JsonRpcResponse.class);
|
|
||||||
|
|
||||||
// We will only handle messages that relate to the login and API accesses here.
|
|
||||||
if(response.result != null){
|
|
||||||
if(mLastCall == RPC.CALL_LOGIN){
|
|
||||||
isLoggedIn = true;
|
|
||||||
|
|
||||||
checkNextRequestedApiAccess();
|
|
||||||
}else if(mLastCall == RPC.CALL_DATABASE){
|
|
||||||
// Deserializing integer response
|
|
||||||
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
|
||||||
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
|
||||||
|
|
||||||
// Storing the "database" api id
|
|
||||||
mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result);
|
|
||||||
|
|
||||||
checkNextRequestedApiAccess();
|
|
||||||
}else if(mLastCall == RPC.CALL_HISTORY){
|
|
||||||
// Deserializing integer response
|
|
||||||
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
|
||||||
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
|
||||||
|
|
||||||
// Storing the "history" api id
|
|
||||||
mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result);
|
|
||||||
|
|
||||||
checkNextRequestedApiAccess();
|
|
||||||
}else if(mLastCall == RPC.CALL_NETWORK_BROADCAST){
|
|
||||||
// Deserializing integer response
|
|
||||||
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
|
||||||
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
|
||||||
|
|
||||||
// Storing the "network_broadcast" api access
|
|
||||||
mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result);
|
|
||||||
|
|
||||||
// All calls have been handled at this point
|
|
||||||
mLastCall = "";
|
|
||||||
}else{
|
|
||||||
Log.d(TAG,"New unhandled message");
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
Log.w(TAG,"Error.Msg: "+response.error.message);
|
|
||||||
}
|
|
||||||
RxBus.getBusInstance().send(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkNextRequestedApiAccess(){
|
|
||||||
if( (mRequestedApis & ApiAccess.API_DATABASE) == ApiAccess.API_DATABASE &&
|
|
||||||
mApiIds.get(ApiAccess.API_DATABASE) == null){
|
|
||||||
// If we need the "database" api access and we don't yet have it
|
|
||||||
|
|
||||||
ApiCall apiCall = new ApiCall(1, RPC.CALL_DATABASE, null, RPC.VERSION, ++mCurrentId);
|
|
||||||
mLastCall = RPC.CALL_DATABASE;
|
|
||||||
sendMessage(apiCall.toJsonString());
|
|
||||||
} else if( (mRequestedApis & ApiAccess.API_HISTORY) == ApiAccess.API_HISTORY &&
|
|
||||||
mApiIds.get(ApiAccess.API_HISTORY) == null){
|
|
||||||
// If we need the "history" api access and we don't yet have it
|
|
||||||
|
|
||||||
ApiCall apiCall = new ApiCall(1, RPC.CALL_HISTORY, null, RPC.VERSION, ++mCurrentId);
|
|
||||||
mLastCall = RPC.CALL_HISTORY;
|
|
||||||
sendMessage(apiCall.toJsonString());
|
|
||||||
}else if( (mRequestedApis & ApiAccess.API_NETWORK_BROADCAST) == ApiAccess.API_NETWORK_BROADCAST &&
|
|
||||||
mApiIds.get(ApiAccess.API_NETWORK_BROADCAST) == null){
|
|
||||||
// If we need the "network_broadcast" api access and we don't yet have it
|
|
||||||
|
|
||||||
ApiCall apiCall = new ApiCall(1, RPC.CALL_NETWORK_BROADCAST, null, RPC.VERSION, ++mCurrentId);
|
|
||||||
mLastCall = RPC.CALL_NETWORK_BROADCAST;
|
|
||||||
sendMessage(apiCall.toJsonString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
|
||||||
super.onClosed(webSocket, code, reason);
|
|
||||||
Log.d(TAG,"onClosed");
|
|
||||||
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED));
|
|
||||||
|
|
||||||
isLoggedIn = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
|
||||||
super.onFailure(webSocket, t, response);
|
|
||||||
Log.e(TAG,"onFailure. Msg: "+t.getMessage());
|
|
||||||
isLoggedIn = false;
|
|
||||||
if(response != null){
|
|
||||||
Log.e(TAG,"Response: "+response.message());
|
|
||||||
}
|
|
||||||
for(StackTraceElement element : t.getStackTrace()){
|
|
||||||
Log.v(TAG,String.format("%s#%s:%d", element.getClassName(), element.getMethodName(), element.getLineNumber()));
|
|
||||||
}
|
|
||||||
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED));
|
|
||||||
mSocketIndex++;
|
|
||||||
connect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
@ -260,4 +138,125 @@ public class NetworkService extends Service {
|
||||||
return NetworkService.this;
|
return NetworkService.this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private WebSocketListener mWebSocketListener = new WebSocketListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(WebSocket webSocket, Response response) {
|
||||||
|
super.onOpen(webSocket, response);
|
||||||
|
mWebSocket = webSocket;
|
||||||
|
|
||||||
|
// Notifying all listeners about the new connection status
|
||||||
|
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED));
|
||||||
|
|
||||||
|
// If we're not yet logged in, we should do it now
|
||||||
|
if(!isLoggedIn){
|
||||||
|
ArrayList<Serializable> loginParams = new ArrayList<>();
|
||||||
|
loginParams.add(mUsername);
|
||||||
|
loginParams.add(mPassword);
|
||||||
|
ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, ++mCurrentId);
|
||||||
|
mLastCall = RPC.CALL_LOGIN;
|
||||||
|
sendMessage(loginCall.toJsonString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(WebSocket webSocket, String text) {
|
||||||
|
super.onMessage(webSocket, text);
|
||||||
|
Log.v(TAG,"< "+text);
|
||||||
|
JsonRpcResponse<?> response = gson.fromJson(text, JsonRpcResponse.class);
|
||||||
|
|
||||||
|
// We will only handle messages that relate to the login and API accesses here.
|
||||||
|
if(response.result != null){
|
||||||
|
if(mLastCall == RPC.CALL_LOGIN){
|
||||||
|
isLoggedIn = true;
|
||||||
|
|
||||||
|
checkNextRequestedApiAccess();
|
||||||
|
}else if(mLastCall == RPC.CALL_DATABASE){
|
||||||
|
// Deserializing integer response
|
||||||
|
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
||||||
|
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
||||||
|
|
||||||
|
// Storing the "database" api id
|
||||||
|
mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result);
|
||||||
|
|
||||||
|
checkNextRequestedApiAccess();
|
||||||
|
}else if(mLastCall == RPC.CALL_HISTORY){
|
||||||
|
// Deserializing integer response
|
||||||
|
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
||||||
|
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
||||||
|
|
||||||
|
// Storing the "history" api id
|
||||||
|
mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result);
|
||||||
|
|
||||||
|
checkNextRequestedApiAccess();
|
||||||
|
}else if(mLastCall == RPC.CALL_NETWORK_BROADCAST){
|
||||||
|
// Deserializing integer response
|
||||||
|
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
|
||||||
|
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
|
||||||
|
|
||||||
|
// Storing the "network_broadcast" api access
|
||||||
|
mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result);
|
||||||
|
|
||||||
|
// All calls have been handled at this point
|
||||||
|
mLastCall = "";
|
||||||
|
}else{
|
||||||
|
Log.d(TAG,"New unhandled message");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
Log.w(TAG,"Error.Msg: "+response.error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
RxBus.getBusInstance().send(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkNextRequestedApiAccess(){
|
||||||
|
if( (mRequestedApis & ApiAccess.API_DATABASE) == ApiAccess.API_DATABASE &&
|
||||||
|
mApiIds.get(ApiAccess.API_DATABASE) == null){
|
||||||
|
// If we need the "database" api access and we don't yet have it
|
||||||
|
|
||||||
|
ApiCall apiCall = new ApiCall(1, RPC.CALL_DATABASE, null, RPC.VERSION, ++mCurrentId);
|
||||||
|
mLastCall = RPC.CALL_DATABASE;
|
||||||
|
sendMessage(apiCall.toJsonString());
|
||||||
|
} else if( (mRequestedApis & ApiAccess.API_HISTORY) == ApiAccess.API_HISTORY &&
|
||||||
|
mApiIds.get(ApiAccess.API_HISTORY) == null){
|
||||||
|
// If we need the "history" api access and we don't yet have it
|
||||||
|
|
||||||
|
ApiCall apiCall = new ApiCall(1, RPC.CALL_HISTORY, null, RPC.VERSION, ++mCurrentId);
|
||||||
|
mLastCall = RPC.CALL_HISTORY;
|
||||||
|
sendMessage(apiCall.toJsonString());
|
||||||
|
}else if( (mRequestedApis & ApiAccess.API_NETWORK_BROADCAST) == ApiAccess.API_NETWORK_BROADCAST &&
|
||||||
|
mApiIds.get(ApiAccess.API_NETWORK_BROADCAST) == null){
|
||||||
|
// If we need the "network_broadcast" api access and we don't yet have it
|
||||||
|
|
||||||
|
ApiCall apiCall = new ApiCall(1, RPC.CALL_NETWORK_BROADCAST, null, RPC.VERSION, ++mCurrentId);
|
||||||
|
mLastCall = RPC.CALL_NETWORK_BROADCAST;
|
||||||
|
sendMessage(apiCall.toJsonString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
|
super.onClosed(webSocket, code, reason);
|
||||||
|
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED));
|
||||||
|
|
||||||
|
isLoggedIn = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
|
super.onFailure(webSocket, t, response);
|
||||||
|
Log.e(TAG,"onFailure. Msg: "+t.getMessage());
|
||||||
|
isLoggedIn = false;
|
||||||
|
if(response != null){
|
||||||
|
Log.e(TAG,"Response: "+response.message());
|
||||||
|
}
|
||||||
|
for(StackTraceElement element : t.getStackTrace()){
|
||||||
|
Log.v(TAG,String.format("%s#%s:%d", element.getClassName(), element.getMethodName(), element.getLineNumber()));
|
||||||
|
}
|
||||||
|
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED));
|
||||||
|
mSocketIndex++;
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
|
@ -18,8 +18,6 @@
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<service android:name=".NetworkService" />
|
|
||||||
|
|
||||||
<activity android:name=".SecondActivity"></activity>
|
<activity android:name=".SecondActivity"></activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
import cy.agorise.graphenej.api.ApiAccess;
|
import cy.agorise.graphenej.api.ApiAccess;
|
||||||
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
|
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
|
||||||
|
import cy.agorise.graphenej.api.android.NetworkService;
|
||||||
import cy.agorise.graphenej.api.android.RxBus;
|
import cy.agorise.graphenej.api.android.RxBus;
|
||||||
import cy.agorise.graphenej.api.calls.GetBlock;
|
import cy.agorise.graphenej.api.calls.GetBlock;
|
||||||
import cy.agorise.graphenej.models.JsonRpcResponse;
|
import cy.agorise.graphenej.models.JsonRpcResponse;
|
||||||
|
|
|
@ -7,7 +7,7 @@ import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import cy.agorise.graphenej.api.ApiAccess;
|
import cy.agorise.graphenej.api.android.NetworkService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sample application class
|
* Sample application class
|
||||||
|
@ -45,8 +45,6 @@ public class SampleApplication extends Application implements Application.Activi
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
Intent intent = new Intent(this, NetworkService.class);
|
Intent intent = new Intent(this, NetworkService.class);
|
||||||
int requestedApis = ApiAccess.API_DATABASE | ApiAccess.API_HISTORY | ApiAccess.API_NETWORK_BROADCAST;
|
|
||||||
intent.putExtra(NetworkService.KEY_REQUESTED_APIS, requestedApis);
|
|
||||||
startService(intent);
|
startService(intent);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -4,12 +4,13 @@ import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import cy.agorise.graphenej.api.ApiAccess;
|
import cy.agorise.graphenej.api.ApiAccess;
|
||||||
|
import cy.agorise.graphenej.api.android.NetworkService;
|
||||||
|
|
||||||
public class SecondActivity extends AppCompatActivity {
|
public class SecondActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue