From 4bd6a61ae07c3bf13aa052462122aeb7dbf3241b Mon Sep 17 00:00:00 2001 From: Greg Moulds Date: Mon, 24 Apr 2023 16:16:24 -0400 Subject: [PATCH 1/3] [BCLOUD-4723] Added support for string-based authenticate() method (cherry picked from commit 74d81b7fdd26e5c3125b433a3826e1318af65e11) --- README.md | 2 -- .../bitheads/brainclouds2s/BrainCloudS2S.java | 18 +++++++++++------- .../RunWithTimeoutAutoAuthTest.java | 2 -- .../brainclouds2s/RunWithTimeoutTest.java | 2 -- .../brainclouds2s/SyncAutoAuthTest.java | 2 -- .../com/bitheads/brainclouds2s/SyncTest.java | 2 -- .../brainclouds2s/TestFixtureBase.java | 8 ++------ 7 files changed, 13 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index c5a0448..c995e46 100644 --- a/README.md +++ b/README.md @@ -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 = ""; String serverName = ""; diff --git a/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java b/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java index e21d044..e3f6ec2 100644 --- a/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java +++ b/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutAutoAuthTest.java b/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutAutoAuthTest.java index 662093e..19d0a7f 100644 --- a/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutAutoAuthTest.java +++ b/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutAutoAuthTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.fail; -import java.util.Date; - import org.json.JSONObject; import org.junit.Test; diff --git a/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutTest.java b/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutTest.java index 47dcbbf..8cf76fd 100644 --- a/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutTest.java +++ b/src/test/java/com/bitheads/brainclouds2s/RunWithTimeoutTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.fail; -import java.util.Date; - import org.json.JSONObject; import org.junit.Test; diff --git a/src/test/java/com/bitheads/brainclouds2s/SyncAutoAuthTest.java b/src/test/java/com/bitheads/brainclouds2s/SyncAutoAuthTest.java index ad596da..60fc38c 100644 --- a/src/test/java/com/bitheads/brainclouds2s/SyncAutoAuthTest.java +++ b/src/test/java/com/bitheads/brainclouds2s/SyncAutoAuthTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.fail; -import java.util.Date; - import org.json.JSONObject; import org.junit.Test; diff --git a/src/test/java/com/bitheads/brainclouds2s/SyncTest.java b/src/test/java/com/bitheads/brainclouds2s/SyncTest.java index 671a80a..2c7b415 100644 --- a/src/test/java/com/bitheads/brainclouds2s/SyncTest.java +++ b/src/test/java/com/bitheads/brainclouds2s/SyncTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.fail; -import java.util.Date; - import org.json.JSONObject; import org.junit.Test; diff --git a/src/test/java/com/bitheads/brainclouds2s/TestFixtureBase.java b/src/test/java/com/bitheads/brainclouds2s/TestFixtureBase.java index 6020fe0..fa74db2 100644 --- a/src/test/java/com/bitheads/brainclouds2s/TestFixtureBase.java +++ b/src/test/java/com/bitheads/brainclouds2s/TestFixtureBase.java @@ -1,5 +1,7 @@ package com.bitheads.brainclouds2s; +import static org.junit.Assert.fail; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -7,17 +9,11 @@ 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 { From ac12d4110d0a42372aa99adf1d7745e2aa9ab72e Mon Sep 17 00:00:00 2001 From: Greg Moulds Date: Mon, 1 May 2023 14:46:19 -0400 Subject: [PATCH 2/3] [BCLOUD-4723] Code cleanup and json library version upgrade --- pom.xml | 2 +- .../bitheads/brainclouds2s/BrainCloudS2S.java | 64 ++++++++++++------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 555b30f..ce0c697 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.json json - 20180130 + 20230227 junit diff --git a/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java b/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java index e3f6ec2..f9ec628 100644 --- a/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java +++ b/src/main/java/com/bitheads/brainclouds2s/BrainCloudS2S.java @@ -387,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(); @@ -401,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()); @@ -582,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(); @@ -599,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 { @@ -610,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()); + } + } + } } From bfb5719344910c3fbdbb8e6a0432e8da2776e45a Mon Sep 17 00:00:00 2001 From: Greg Moulds Date: Tue, 2 May 2023 10:16:16 -0400 Subject: [PATCH 3/3] [BCLOUD-4723] Version bump --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ce0c697..78354a3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.bitheads brainclouds2s - 4.14.0 + 4.14.1 jar