Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.bitheads</groupId>
<artifactId>brainclouds2s</artifactId>
<version>4.14.2</version>
<version>4.14.3</version>
<packaging>jar</packaging>
<name>brainCloud S2S Java</name>
<description>The Java library for brainCloud S2S</description>
Expand Down Expand Up @@ -64,7 +64,6 @@
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<source>8</source>
</configuration>
<executions>
<execution>
Expand Down
136 changes: 87 additions & 49 deletions src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand All @@ -28,16 +30,15 @@ public class BrainCloudS2S implements Runnable {
private static final int CLIENT_NETWORK_ERROR = 900;
private static final int CLIENT_NETWORK_ERROR_TIMEOUT = 90001;
private static final int SERVER_SESSION_EXPIRED = 40365;
private static final int INVALID_REQUEST = 40001;
private static final int INVALID_RESPONSE = 40002;

private static final int STATE_DISCONNECTED = 0;
private static final int STATE_AUTHENTICATING = 1;
private static final int STATE_CONNECTED = 2;

private static final String MESSAGES = "messages";
private static final String DATA = "data";
private static final String SERVER_SECRET = "serverSecret";
private static final String MESSAGES = "messages";
private static final String DATA = "data";
private static final String SERVER_SECRET = "serverSecret";

private boolean _isInitialized = false;
private int _state = STATE_DISCONNECTED;
Expand Down Expand Up @@ -232,7 +233,7 @@ private JSONObject extractResponseAt(int index, JSONObject json) {
response = messageResponses.getJSONObject(0);
}
} catch (Exception e) {
LogString("Error decoding response " + e);
logException("Error decoding response", e);
response = json;
}
return response;
Expand All @@ -246,7 +247,7 @@ private JSONObject generateError(int statusCode, int reasonCode, String statusMe
jsonError.put("severity", "ERROR");
jsonError.put("status_message", statusMessage);
} catch (JSONException je) {
LogString("Error encoding error " + je.getMessage());
logException("Error encoding error", je);
}
return jsonError;
}
Expand All @@ -256,17 +257,15 @@ private void failAllRequests(JSONObject message) {
_requestQueue.clear();
for (int i = 0; i < queue.size(); i++) {
Request request = queue.get(i);
if (request.getCallback() != null) {
request.getCallback().s2sCallback(this, message);
}
callCallback(request.getCallback(), this, message);
}
}


public void authenticate(IS2SCallBackString callback) {

authenticate((IS2SCallback)(context, jsonData) -> {
callback.s2sCallback(context, jsonData.toString());
callCallback(callback, context, jsonData.toString());
});
}

Expand Down Expand Up @@ -323,24 +322,18 @@ public void authenticate(IS2SCallback callback) {
_state = STATE_DISCONNECTED;
}

if (callback != null) {
callback.s2sCallback(context, message);
}
callCallback(callback, context, message);
} else {
failAllRequests(null);
disconnect();

if (callback != null) {
callback.s2sCallback(context, null);
}
callCallback(callback, context, null);
}
} catch (JSONException e) {
failAllRequests(null);
disconnect();

if (callback != null) {
callback.s2sCallback(context, null);
}
callCallback(callback, context, null);
}
});
}
Expand Down Expand Up @@ -412,14 +405,14 @@ private void sendRawRequest(JSONObject jsonRequest, IS2SCallback callback) {
}
}
catch (SocketTimeoutException e) {
LogString("TIMEOUT sending request: " + e.getMessage() + " t: " + new Date().toString());
logException("TIMEOUT sending request", e);
synchronized(_lock) {
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, CLIENT_NETWORK_ERROR_TIMEOUT, "Network timeout error");
}
return;
}
catch (IOException e) {
LogString("ERROR sending request: " + e.getMessage() + " t: " + new Date().toString());
logException("ERROR sending request", e);
synchronized(_lock) {
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, CLIENT_NETWORK_ERROR, "Network error");
}
Expand All @@ -435,11 +428,11 @@ private void sendRawRequest(JSONObject jsonRequest, IS2SCallback callback) {
_jsonResponse = new JSONObject(responseBody);
}
catch (IOException e) {
LogString("ERROR reading response code: " + e.getMessage() + " t: " + new Date().toString());
logException("ERROR reading response code", e);
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, e.getMessage());
}
catch (JSONException e) {
LogString("ERROR parsing response json [error:" + e.getMessage() + " body:" + responseBody + "] t: " + new Date().toString());
logException("ERROR parsing response json [body:" + responseBody + "]", e);
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, e.getMessage());
}
}
Expand Down Expand Up @@ -476,21 +469,15 @@ private void sendRequest(JSONObject jsonRequest, IS2SCallback callback) {
}
_retryCount = 0;

if (callback != null) {
callback.s2sCallback(this, jsonResponse);
}
callCallback(callback, this, jsonResponse);
}
catch (JSONException jse) {
LogString("JSON ERROR in response: " + jse.getMessage() + " t: " + new Date().toString());
if (callback != null) {
callback.s2sCallback(this, generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, jse.getMessage()));
}
logException("JSON ERROR in response", jse);
callCallback(callback, this, generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, jse.getMessage()));
}
catch (Exception e) {
LogString("ERROR in response: " + e.getMessage() + " t: " + new Date().toString());
if (callback != null) {
callback.s2sCallback(this, generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, e.getMessage()));
}
logException("ERROR processing response", e);
callCallback(callback, this, generateError(CLIENT_NETWORK_ERROR, INVALID_RESPONSE, e.getMessage()));
}

// Do next request in queue
Expand Down Expand Up @@ -535,14 +522,43 @@ private String readResponseBody(HttpURLConnection connection) {
return responseBody;
}

private void LogString(String s) {
if (_loggingEnabled) {
// for now use System.out as unit tests do not support android.util.log class
System.out.println("#BCC " + s);
}
}

@Override
private void logString(String s) {
logString(s, Instant.now());
}


private void logException(String msg, Exception e) {

String buf = new StringBuilder()
.append(msg)
.append(" [e:")
.append(e.getClass().getSimpleName())
.append(" m:")
.append(e.getMessage())
.append(']')
.toString();

logString(buf, Instant.now());
}


private void logString(String s, Instant timestamp) {

if (_loggingEnabled) {
// for now use System.out as unit tests do not support android.util.log class
String log = new StringBuilder("#BCC ")
.append(DateTimeFormatter.ISO_INSTANT.format(timestamp))
.append(' ')
.append(s)
.toString();

System.out.println(log);
}
}


@Override
public void run() {
if (_sessionId != null) {
JSONObject heartbeatMsg = new JSONObject();
Expand Down Expand Up @@ -608,9 +624,7 @@ public void runCallbacks(long timeoutMS) {
// Start heartbeat
startHeartbeat();

if (request.getCallback() != null) {
request.getCallback().s2sCallback(this, response);
}
callCallback(request.getCallback(), this, response);

break; // We got a response, leave.
}
Expand All @@ -628,14 +642,39 @@ public void runCallbacks(long timeoutMS) {
}


private void callCallback(IS2SCallback callback, BrainCloudS2S context, JSONObject json) {

if (callback != null) {
try {
callback.s2sCallback(this, json);
}
catch (Exception e) {
logException("WARN error in callback", e);
}
}
}


private void callCallback(IS2SCallBackString callback, BrainCloudS2S context, String message) {

if (callback != null) {
try {
callback.s2sCallback(this, message);
}
catch (Exception e) {
logException("WARN error in callback", e);
}
}
}


private void logRequest(JSONObject request) {

if (_loggingEnabled) {
String msg = new StringBuilder("OUTGOING: ")
.append(request.toString(2))
.append(", t: ").append(new Date().toString())
.toString();
LogString(msg);
logString(msg);
}
}

Expand All @@ -647,12 +686,11 @@ private void logResponse(JSONObject response, long responseCode) {
try {
String msg = new StringBuilder("INCOMING (")
.append(responseCode).append("): ").append(response.toString(2))
.append(", t: ").append(new Date().toString())
.toString();
LogString(msg);
logString(msg);
}
catch (JSONException e) {
LogString("JSON ERROR parsing response: " + e.getMessage() + " t: " + new Date().toString());
logException("JSON ERROR parsing response", e);
}
}
}
Expand Down