Skip to content

Commit bc73c36

Browse files
committed
backport latest client changes from nbusy
1 parent 64b3b8e commit bc73c36

9 files changed

Lines changed: 115 additions & 80 deletions

File tree

src/main/java/neptulon/client/Conn.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package neptulon.client;
22

3+
import neptulon.client.callbacks.ConnCallback;
4+
import neptulon.client.callbacks.ResCallback;
5+
36
/**
47
* Neptulon connection interface: https://github.com/neptulon/neptulon
58
*/
@@ -26,14 +29,11 @@ public interface Conn {
2629
void middleware(Middleware mw);
2730

2831
/**
29-
* Connects to the given WebSocket server.
30-
*/
31-
void connect();
32-
33-
/**
34-
* Whether the connection is established.
32+
* Connects to the given Neptulon server.
33+
*
34+
* @param handler Handler for connection/disconnection events.
3535
*/
36-
boolean isConnected();
36+
void connect(ConnCallback handler);
3737

3838
/**
3939
* Returns the remote network address.
@@ -43,12 +43,12 @@ public interface Conn {
4343
/**
4444
* Sends a JSON-RPC request through the connection with an auto generated request ID.
4545
*/
46-
<T> void sendRequest(String method, T params, ResHandler resHandler);
46+
<T> void sendRequest(String method, T params, ResCallback cb);
4747

4848
/**
4949
* Sends a JSON-RPC request through the connection, with array params and auto generated request ID.
5050
*/
51-
void sendRequestArr(String method, ResHandler handler, Object... params);
51+
void sendRequestArr(String method, ResCallback cb, Object... params);
5252

5353
/**
5454
* Closes the connection.

src/main/java/neptulon/client/ConnImpl.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package neptulon.client;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45

56
import java.io.IOException;
67
import java.util.ArrayList;
@@ -11,6 +12,8 @@
1112
import java.util.concurrent.TimeUnit;
1213
import java.util.logging.Logger;
1314

15+
import neptulon.client.callbacks.ConnCallback;
16+
import neptulon.client.callbacks.ResCallback;
1417
import okhttp3.OkHttpClient;
1518
import okhttp3.Request;
1619
import okhttp3.RequestBody;
@@ -26,14 +29,15 @@
2629
*/
2730
public class ConnImpl implements Conn, WebSocketListener {
2831
private static final Logger logger = Logger.getLogger(ConnImpl.class.getSimpleName());
29-
private final Gson gson = new Gson();
32+
private final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").create();
3033
private final OkHttpClient client;
3134
private final Request request;
3235
private final WebSocketCall wsCall;
3336
private final List<Middleware> middleware = new ArrayList<>();
34-
private final Map<String, ResHandler> resHandlers = new HashMap<>();
37+
private final Map<String, ResCallback> resCallbacks = new HashMap<>();
3538
private WebSocket ws;
3639
private boolean connected;
40+
private ConnCallback connCallback;
3741

3842
/**
3943
* Initializes a new connection with given server URL.
@@ -96,31 +100,27 @@ public void middleware(Middleware mw) {
96100
// handleRequest(method, .....) { if isClientConn... else exception } // same goes for go-client
97101

98102
@Override
99-
public void connect() {
103+
public void connect(ConnCallback handler) {
100104
// enqueue this listener implementation to initiate the WebSocket connection
105+
connCallback = handler;
101106
wsCall.enqueue(this);
102107
}
103108

104-
@Override
105-
public boolean isConnected() {
106-
return ws != null && connected;
107-
}
108-
109109
@Override
110110
public void remoteAddr() {
111111

112112
}
113113

114114
@Override
115-
public <T> void sendRequest(String method, T params, ResHandler handler) {
115+
public <T> void sendRequest(String method, T params, ResCallback cb) {
116116
String id = UUID.randomUUID().toString();
117117
neptulon.client.Request r = new neptulon.client.Request<>(id, method, params);
118118
send(r);
119-
resHandlers.put(id, handler);
119+
resCallbacks.put(id, cb);
120120
}
121121

122122
@Override
123-
public void sendRequestArr(String method, ResHandler handler, Object... params) {
123+
public void sendRequestArr(String method, ResCallback cb, Object... params) {
124124

125125
}
126126

@@ -143,12 +143,15 @@ public void onOpen(WebSocket webSocket, Response response) {
143143
ws = webSocket;
144144
connected = true;
145145
logger.info("WebSocket connected.");
146+
connCallback.connected();
146147
}
147148

148149
@Override
149150
public void onFailure(IOException e, Response response) {
150151
connected = false;
151-
logger.warning("WebSocket connection closed with error: " + e.getMessage());
152+
String reason = e.getMessage();
153+
logger.warning("WebSocket connection closed with error: " + reason);
154+
connCallback.disconnected(reason);
152155
}
153156

154157
@Override
@@ -158,7 +161,7 @@ public void onMessage(ResponseBody message) throws IOException {
158161
Message msg = gson.fromJson(msgStr, Message.class);
159162
if (msg.method == null || msg.method.isEmpty()) {
160163
// handle response message
161-
resHandlers.get(msg.id).execute(gson, msg);
164+
resCallbacks.get(msg.id).handleResponse(new ResCtx(msg.id, msg.result, msg.error, gson));
162165
return;
163166
}
164167

src/main/java/neptulon/client/Middleware.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
public interface Middleware {
77
void handler(ReqCtx ctx);
88
}
9-

src/main/java/neptulon/client/ReqCtx.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ public ReqCtx(ConnImpl conn, String id, String method, JsonElement params, List<
3030
this.gson = gson;
3131
}
3232

33-
public Conn getConn() {
34-
return conn;
35-
}
36-
3733
public String getID() {
3834
return id;
3935
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package neptulon.client;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonElement;
5+
6+
/**
7+
* Response context.
8+
*/
9+
public class ResCtx {
10+
private final String id;
11+
private final JsonElement result; // result parameters
12+
private final Message.ResError error; // response error (if any)
13+
private final Gson gson;
14+
15+
public ResCtx(String id, JsonElement result, Message.ResError error, Gson gson) {
16+
this.id = id;
17+
this.result = result;
18+
this.error = error;
19+
this.gson = gson;
20+
}
21+
22+
public String getID() {
23+
return id;
24+
}
25+
26+
27+
public <T> T getResult(Class<T> classOfT) {
28+
return gson.fromJson(result, classOfT);
29+
}
30+
}

src/main/java/neptulon/client/ResHandler.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package neptulon.client.callbacks;
2+
3+
/**
4+
* Callback for connection event.
5+
*/
6+
public interface ConnCallback {
7+
void connected();
8+
void disconnected(String reason);
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package neptulon.client.callbacks;
2+
3+
import neptulon.client.ResCtx;
4+
5+
/**
6+
* Callback for responses.
7+
*/
8+
public interface ResCallback {
9+
void handleResponse(ResCtx ctx);
10+
}
11+
Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package neptulon.client;
22

3+
import neptulon.client.callbacks.ConnCallback;
4+
import neptulon.client.callbacks.ResCallback;
35
import neptulon.client.middleware.Echo;
46
import neptulon.client.middleware.Logger;
57
import neptulon.client.middleware.Router;
8+
69
import org.junit.Test;
710

811
import java.util.concurrent.CountDownLatch;
9-
10-
import static org.hamcrest.MatcherAssert.assertThat;
11-
import static org.hamcrest.core.IsEqual.equalTo;
12+
import java.util.concurrent.TimeUnit;
1213

1314
public class NeptulonTest {
1415
private static final String URL = "ws://127.0.0.1:3001";
1516

17+
private boolean isTravis() {
18+
return System.getenv().containsKey("TRAVIS");
19+
}
20+
21+
private class EchoMessage {
22+
final String message;
23+
24+
EchoMessage(String message) {
25+
this.message = message;
26+
}
27+
}
28+
29+
/**
30+
* External client test case in line with the Neptulon external client test case specs and event flow.
31+
*/
1632
@Test
17-
public void connect() throws InterruptedException {
33+
public void testExternalClient() throws InterruptedException {
1834
if (isTravis()) {
1935
return;
2036
}
@@ -25,51 +41,38 @@ public void connect() throws InterruptedException {
2541
router.request("echo", new Echo());
2642
conn.middleware(router);
2743

28-
conn.connect();
29-
Thread.sleep(100);
30-
assertThat("Connection was not established in time.", conn.isConnected());
31-
32-
final CountDownLatch counter = new CountDownLatch(2); // todo: add one more for ws.onClose
33-
34-
conn.sendRequest("echo", new EchoMessage("Hello from Java client!"), new ResHandler<Object>() {
44+
final CountDownLatch connCounter = new CountDownLatch(1);
45+
conn.connect(new ConnCallback() {
3546
@Override
36-
public Class<Object> getType() {
37-
return Object.class;
47+
public void connected() {
48+
connCounter.countDown();
3849
}
3950

4051
@Override
41-
public void handler(Response<Object> res) {
42-
System.out.println("Received 'echo' response: " + res.result);
43-
counter.countDown();
52+
public void disconnected(String reason) {
4453
}
4554
});
55+
connCounter.await(3, TimeUnit.SECONDS);
4656

47-
conn.sendRequest("close", new EchoMessage("Bye from Java client!"), new ResHandler<Object>() {
57+
final CountDownLatch msgCounter = new CountDownLatch(2);
58+
conn.sendRequest("echo", new EchoMessage("Hello from Java client!"), new ResCallback() {
4859
@Override
49-
public Class<Object> getType() {
50-
return Object.class;
60+
public void handleResponse(ResCtx ctx) {
61+
Object res = ctx.getResult(Object.class);
62+
System.out.println("Received 'echo' response: " + res);
63+
msgCounter.countDown();
5164
}
52-
65+
});
66+
conn.sendRequest("close", new EchoMessage("Bye from Java client!"), new ResCallback() {
5367
@Override
54-
public void handler(Response<Object> res) {
55-
System.out.println("Received 'close' response: " + res.result);
56-
counter.countDown();
68+
public void handleResponse(ResCtx ctx) {
69+
Object res = ctx.getResult(Object.class);
70+
System.out.println("Received 'close' response: " + res);
71+
msgCounter.countDown();
5772
}
5873
});
74+
msgCounter.await(3, TimeUnit.SECONDS);
5975

60-
counter.await();
6176
conn.close();
6277
}
63-
64-
private boolean isTravis() {
65-
return System.getenv().containsKey("TRAVIS");
66-
}
67-
68-
private class EchoMessage {
69-
final String message;
70-
71-
EchoMessage(String message) {
72-
this.message = message;
73-
}
74-
}
75-
}
78+
}

0 commit comments

Comments
 (0)