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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ The resulting jar file is in **target/brainclouds2s-x.x.x.jar**

``` java
import com.bitheads.braincloud.s2s.Brainclouds2s;
import org.json.JSONArray;
import org.json.JSONObject;

String appId = "<Your app ID>";
String serverName = "<Your Server defined name>";
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.bitheads</groupId>
<artifactId>brainclouds2s</artifactId>
<version>4.14.0</version>
<version>4.14.1</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
<version>20230227</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
82 changes: 52 additions & 30 deletions src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -24,13 +22,10 @@ public class BrainCloudS2S implements Runnable {

public static final String DEFAULT_S2S_URL = "https://api.braincloudservers.com/s2sdispatcher";

private static final long NO_PACKET_EXPECTED = -1;

private static final int CLIENT_NETWORK_ERROR_TIMEOUT = 90001;
private static final int MESSAGE_CONTENT_INVALID_JSON = 40614;
private static final int SERVER_SESSION_EXPIRED = 40365;
private static final int INVALID_REQUEST = 40001;
private static final int BAD_REQUEST = 400;
private static final int CLIENT_NETWORK_ERROR = 900;

private static final int STATE_DISCONNECTED = 0;
Expand All @@ -49,7 +44,7 @@ public class BrainCloudS2S implements Runnable {
private String _sessionId = null;
private long _packetId;

private ScheduledFuture _heartbeatTimer = null;
private ScheduledFuture<?> _heartbeatTimer = null;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

private boolean _loggingEnabled = false;
Expand Down Expand Up @@ -260,6 +255,15 @@ private void failAllRequests(JSONObject message) {
}
}


public void authenticate(IS2SCallBackString callback) {

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


/*
* Authenticate with brainCloud. If autoAuth is set to false, which is
* the default, this must be called successfully before doing other
Expand Down Expand Up @@ -383,9 +387,7 @@ private void sendRawRequest(JSONObject jsonRequest, IS2SCallback callback) {
postData = body.getBytes("UTF-8");
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));

if (_loggingEnabled) {
LogString("OUTGOING: " + jsonRequest.toString(2) + ", t: " + new Date().toString());
}
logRequest(jsonRequest);

connection.connect();

Expand All @@ -397,29 +399,24 @@ private void sendRawRequest(JSONObject jsonRequest, IS2SCallback callback) {
String responseBody = readResponseBody(connection);
synchronized(_lock) {
_responseCode = connection.getResponseCode();

_jsonResponse = null;
// if (_responseCode == HttpURLConnection.HTTP_OK) {
try {
_jsonResponse = new JSONObject(responseBody);
} catch (JSONException e) {
_jsonResponse = null;
}
// }
_jsonResponse = new JSONObject(responseBody);
}
} catch (java.net.SocketTimeoutException e) {
}
catch (java.net.SocketTimeoutException e) {
LogString("TIMEOUT t: " + new Date().toString());
if (callback != null) {
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, CLIENT_NETWORK_ERROR_TIMEOUT, "Network error");
}
return;
} catch (JSONException e) {
LogString("JSON ERROR " + e.getMessage() + " t: " + new Date().toString());
}
catch (JSONException e) {
LogString("JSON ERROR parsing response: " + e.getMessage() + " t: " + new Date().toString());
if (callback != null) {
_jsonResponse = generateError(CLIENT_NETWORK_ERROR, INVALID_REQUEST, e.getMessage());
}
return;
} catch (IOException e) {
}
catch (IOException e) {
try {
int status_code = (connection != null) ? connection.getResponseCode() : CLIENT_NETWORK_ERROR;
_jsonResponse = generateError(status_code, INVALID_REQUEST, e.getMessage());
Expand Down Expand Up @@ -578,12 +575,7 @@ public void runCallbacks(long timeoutMS) {

if (response != null && request != null) {

// to avoid taking the json parsing hit even when logging is disabled
if (_loggingEnabled) {
try {
LogString("INCOMING (" + responseCode + "): " + response.toString(2) + ", t: " + new Date().toString());
} catch (JSONException e) { }
}
logResponse(response, responseCode);

// Start heartbeat
startHeartbeat();
Expand All @@ -595,7 +587,7 @@ public void runCallbacks(long timeoutMS) {
break; // We got a response, leave.
}

if (java.lang.System.currentTimeMillis() - startTime > timeoutMS)
if (System.currentTimeMillis() - startTime > timeoutMS)
break;

try {
Expand All @@ -606,4 +598,34 @@ public void runCallbacks(long timeoutMS) {
}
}
}


private void logRequest(JSONObject jsonRequest) {

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


private void logResponse(JSONObject response, long responseCode) {

// to avoid taking the json parsing hit even when logging is disabled
if (_loggingEnabled) {
try {
String msg = new StringBuilder("INCOMING (")
.append(responseCode).append("): ").append(response.toString(2))
.append(", t: ").append(new Date().toString())
.toString();
LogString(msg);
}
catch (JSONException e) {
LogString("JSON ERROR parsing response: " + e.getMessage() + " t: " + new Date().toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.Assert.fail;

import java.util.Date;

import org.json.JSONObject;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.Assert.fail;

import java.util.Date;

import org.json.JSONObject;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.Assert.fail;

import java.util.Date;

import org.json.JSONObject;
import org.junit.Test;

Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/bitheads/brainclouds2s/SyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.Assert.fail;

import java.util.Date;

import org.json.JSONObject;
import org.junit.Test;

Expand Down
8 changes: 2 additions & 6 deletions src/test/java/com/bitheads/brainclouds2s/TestFixtureBase.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.bitheads.brainclouds2s;

import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.bitheads.brainclouds2s.BrainCloudS2S;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

public class TestFixtureBase {

Expand Down