Skip to content

Commit 8ae1fbe

Browse files
committed
Added JavaDoc and re-added the callback functionality to fix #101
Added more comments to the sample
1 parent 7f4b52a commit 8ae1fbe

File tree

14 files changed

+212
-92
lines changed

14 files changed

+212
-92
lines changed

core/src/main/java/eu/bittrade/libs/steemj/SteemJ.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
import eu.bittrade.libs.steemj.communication.BlockAppliedCallback;
7878
import eu.bittrade.libs.steemj.communication.CallbackHub;
7979
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
80-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
80+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
8181
import eu.bittrade.libs.steemj.configuration.SteemJConfig;
8282
import eu.bittrade.libs.steemj.enums.AssetSymbolType;
8383
import eu.bittrade.libs.steemj.enums.DiscussionSortType;

core/src/main/java/eu/bittrade/libs/steemj/apis/follow/FollowApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import eu.bittrade.libs.steemj.base.models.AccountName;
1515
import eu.bittrade.libs.steemj.base.models.Permlink;
1616
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
17-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
17+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
1818
import eu.bittrade.libs.steemj.enums.RequestMethods;
1919
import eu.bittrade.libs.steemj.enums.SteemApiType;
2020
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;

core/src/main/java/eu/bittrade/libs/steemj/apis/market/history/MarketHistoryApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import eu.bittrade.libs.steemj.apis.market.history.model.OrderBook;
1111
import eu.bittrade.libs.steemj.base.models.TimePointSec;
1212
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
13-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
13+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
1414
import eu.bittrade.libs.steemj.enums.RequestMethods;
1515
import eu.bittrade.libs.steemj.enums.SteemApiType;
1616
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;

core/src/main/java/eu/bittrade/libs/steemj/apis/network/broadcast/NetworkBroadcastApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import eu.bittrade.libs.steemj.base.models.SignedTransaction;
44
import eu.bittrade.libs.steemj.communication.CommunicationHandler;
5-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
5+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
66
import eu.bittrade.libs.steemj.enums.RequestMethods;
77
import eu.bittrade.libs.steemj.enums.SteemApiType;
88
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;

core/src/main/java/eu/bittrade/libs/steemj/communication/AbstractClient.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.io.IOException;
44
import java.net.URI;
55

6-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
7-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCResponse;
6+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
7+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse;
88
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
99
import eu.bittrade.libs.steemj.exceptions.SteemResponseException;
1010

@@ -15,27 +15,41 @@
1515
*/
1616
public abstract class AbstractClient {
1717
/**
18+
* Use this method to send a <code>requestObject</code> to the
19+
* <code>endpointUri</code> and to receive an answer.
1820
*
1921
* @param requestObject
22+
* The object to send.
2023
* @param endpointUri
24+
* The endpoint to connect and send to.
2125
* @param sslVerificationDisabled
22-
* @return The response
26+
* Define if the SSL verification should be disabled.
27+
* @return The response returned by the Steem Node wrapped in a
28+
* {@link JsonRPCResponse} object.
2329
* @throws SteemCommunicationException
30+
* In case of communication problems.
2431
* @throws SteemResponseException
2532
* If the answer received from the node is no valid JSON.
2633
*/
2734
public abstract JsonRPCResponse invokeAndReadResponse(JsonRPCRequest requestObject, URI endpointUri,
2835
boolean sslVerificationDisabled) throws SteemCommunicationException, SteemResponseException;
2936

3037
/**
38+
* Use this method to handle callbacks.
3139
*
3240
* @param rawJsonResponse
41+
* A {@link JsonRPCResponse} instance wrapping a potential
42+
* callback.
43+
* @throws SteemCommunicationException
44+
* If the <code>rawJsonResponse</code> is not a callback.
3345
*/
34-
protected abstract void handleCallback(JsonRPCResponse rawJsonResponse);
46+
protected abstract void handleCallback(JsonRPCResponse rawJsonResponse) throws SteemCommunicationException;
3547

3648
/**
49+
* Use this method to close the connection of this client.
3750
*
3851
* @throws IOException
52+
* If the connection can't be closed.
3953
*/
4054
public abstract void closeConnection() throws IOException;
4155
}

core/src/main/java/eu/bittrade/libs/steemj/communication/CommunicationHandler.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import com.fasterxml.jackson.databind.module.SimpleModule;
1919

2020
import eu.bittrade.libs.steemj.base.models.serializer.BooleanSerializer;
21-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
22-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCResponse;
21+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
22+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse;
2323
import eu.bittrade.libs.steemj.configuration.SteemJConfig;
2424
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
2525
import eu.bittrade.libs.steemj.exceptions.SteemResponseException;
@@ -34,11 +34,14 @@
3434
public class CommunicationHandler {
3535
private static final Logger LOGGER = LoggerFactory.getLogger(CommunicationHandler.class);
3636

37-
/** */
37+
/**
38+
* A preconfigured mapper instance used for de-/serialization of Json
39+
* objects.
40+
*/
3841
private static ObjectMapper mapper = getObjectMapper();
39-
/** */
42+
/** A counter for failed connection tries. */
4043
private int numberOfConnectionTries = 0;
41-
/** */
44+
/** The client used to send requests. */
4245
private AbstractClient client;
4346

4447
/**
@@ -53,9 +56,12 @@ public CommunicationHandler() throws SteemCommunicationException {
5356
}
5457

5558
/**
56-
* @throws SteemCommunicationException
57-
*
59+
* Initialize a new <code>client</code> by selecting one of the configured
60+
* endpoints.
5861
*
62+
* @throws SteemCommunicationException
63+
* If no {@link AbstractClient} implementation for the given
64+
* schema is available.
5965
*/
6066
public void initializeNewClient() throws SteemCommunicationException {
6167
if (client != null) {

core/src/main/java/eu/bittrade/libs/steemj/communication/HttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import org.slf4j.Logger;
1717
import org.slf4j.LoggerFactory;
1818

19-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
20-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCResponse;
19+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
20+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse;
2121
import eu.bittrade.libs.steemj.configuration.SteemJConfig;
2222
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
2323

core/src/main/java/eu/bittrade/libs/steemj/communication/WebsocketClient.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020

21-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCRequest;
22-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCResponse;
21+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCRequest;
22+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse;
2323
import eu.bittrade.libs.steemj.configuration.SteemJConfig;
2424
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
2525
import eu.bittrade.libs.steemj.exceptions.SteemResponseException;
@@ -34,15 +34,18 @@
3434
public class WebsocketClient extends AbstractClient {
3535
private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketClient.class);
3636

37-
/** */
37+
/** The client. */
3838
private ClientManager client;
39-
/** */
39+
/** A {@link CountDownLatch} used to indicate that a message is expected. */
4040
private CountDownLatch responseCountDownLatch;
41-
/** */
41+
/** The current session. */
4242
private Session session;
43-
/** */
43+
/**
44+
* The {@link WebsocketEndpoint} instance that will handle the incoming
45+
* messages.
46+
*/
4447
private WebsocketEndpoint websocketEndpoint;
45-
/** */
48+
/** The endpoint this client instance is currently connected to. */
4649
private URI currentEndpointUri;
4750

4851
/**
@@ -109,18 +112,8 @@ public JsonRPCResponse invokeAndReadResponse(JsonRPCRequest requestObject, URI e
109112
}
110113

111114
@Override
112-
protected void handleCallback(JsonRPCResponse response) {
113-
// try {
114-
// NotificationDTO response = mapper.readValue(message, NotificationDTO.class);
115-
116-
// Make sure that the inner result object is a BlockHeader.
117-
// CallbackHub.getInstance().getCallbackByUuid(Integer.valueOf(response.getParams()[0].toString()))
118-
// .onNewBlock(mapper.convertValue(((ArrayList<Object>) (response.getParams()[1])).get(0),
119-
// SignedBlockHeader.class));
120-
// } catch (IOException e) {
121-
// TODO Auto-generated catch block
122-
// LOGGER.error("Could not parse callback {}.", e);
123-
//}
115+
protected void handleCallback(JsonRPCResponse response) throws SteemCommunicationException {
116+
response.handleCallback();
124117
}
125118

126119
@Override
@@ -132,23 +125,29 @@ public void closeConnection() throws IOException {
132125
}
133126

134127
/**
128+
* Get the {@link CountDownLatch} used by this instance to change its
129+
* counter.
135130
*
136-
* @return
131+
* @return The {@link CountDownLatch} used by this instance.
137132
*/
138133
protected CountDownLatch getResponseCountDownLatch() {
139134
return this.responseCountDownLatch;
140135
}
141136

142137
/**
143-
* @return the session
138+
* Get the current {@link Session}.
139+
*
140+
* @return The session used by this instance.
144141
*/
145142
protected Session getSession() {
146143
return session;
147144
}
148145

149146
/**
147+
* Update the {@link Session} this instance should use.
148+
*
150149
* @param session
151-
* the session to set
150+
* The session to set.
152151
*/
153152
protected void setSession(Session session) {
154153
this.session = session;

core/src/main/java/eu/bittrade/libs/steemj/communication/WebsocketEndpoint.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313

14-
import eu.bittrade.libs.steemj.communication.dto.JsonRPCResponse;
14+
import eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse;
15+
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
1516
import eu.bittrade.libs.steemj.exceptions.SteemResponseException;
1617

1718
/**
@@ -22,23 +23,30 @@
2223
public class WebsocketEndpoint extends Endpoint implements MessageHandler.Whole<String> {
2324
private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketEndpoint.class);
2425

25-
/** */
26+
/** The latest response received from a Steem Node. */
2627
private String latestResponse;
27-
/** */
28+
/** The {@link WebsocketClient} whose session object should be updated. */
2829
private WebsocketClient websocketClient;
2930

3031
/**
32+
* Create a new {@link WebsocketEndpoint} instance.
3133
*
3234
* @param websocketClient
35+
* The @link WebsocketClient} whose session object should be
36+
* updated.
3337
*/
3438
public WebsocketEndpoint(WebsocketClient websocketClient) {
3539
this.websocketClient = websocketClient;
3640
}
3741

3842
/**
43+
* Pull the latest response.
3944
*
40-
* @return
45+
* @return In case a response has already been received a new
46+
* {@link JsonRPCResponse} instance that wraps the response,
47+
* otherwise the method will return <code>null</code>.
4148
* @throws SteemResponseException
49+
* In case the response can not be parsed as a tree.
4250
*/
4351
protected JsonRPCResponse getLatestResponse() throws SteemResponseException {
4452
try {
@@ -74,6 +82,24 @@ public void onOpen(Session session, EndpointConfig config) {
7482
public void onMessage(String message) {
7583
latestResponse = message;
7684

77-
this.websocketClient.getResponseCountDownLatch().countDown();
85+
if (this.websocketClient.getResponseCountDownLatch().getCount() > 0) {
86+
this.websocketClient.getResponseCountDownLatch().countDown();
87+
} else {
88+
/*
89+
* The client does not wait for an answer so this is probably a
90+
* callback.
91+
*/
92+
try {
93+
JsonRPCResponse response = getLatestResponse();
94+
95+
if (response.isCallback()) {
96+
this.websocketClient.handleCallback(response);
97+
}
98+
} catch (SteemCommunicationException | SteemResponseException e) {
99+
// Sadly it is not possible to throw an exception here, so the
100+
// only useful thing we can do is to log it.
101+
LOGGER.error("Tried to handle a potential callback and failed.", e);
102+
}
103+
}
78104
}
79105
}

core/src/main/java/eu/bittrade/libs/steemj/communication/dto/NotificationDTO.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

core/src/main/java/eu/bittrade/libs/steemj/communication/dto/JsonRPCRequest.java renamed to core/src/main/java/eu/bittrade/libs/steemj/communication/jrpc/JsonRPCRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package eu.bittrade.libs.steemj.communication.dto;
1+
package eu.bittrade.libs.steemj.communication.jrpc;
22

33
import java.util.Random;
44

0 commit comments

Comments
 (0)