Skip to content

Commit ac82dc0

Browse files
authored
Merge pull request #6 from nitram22/master
Extension allowing the use of a different certstream server
2 parents ab89693 + 6d093bf commit ac82dc0

5 files changed

Lines changed: 144 additions & 15 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package example;
2+
3+
import com.google.gson.Gson;
4+
import io.calidog.certstream.CertStream;
5+
6+
/**
7+
* Demo Client that prints out a message of a certstream-server with a given URI
8+
* The server address of the CaliDog Server is entered here for demo purposes
9+
*/
10+
public class ExampleClientAlternativeServer {
11+
12+
public static void main (String[] args){
13+
14+
CertStream.onMessageAlternativeServer(msg -> System.out.println(new Gson().toJson(msg)), "wss://certstream.calidog.io");
15+
16+
}
17+
18+
}

src/io/calidog/certstream/BoringParts.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public BoringParts(CertStreamStringMessageHandler messageHandler, long sleepBefo
4343
webSocketClient = webSocketClientSupplier.get();
4444
}
4545

46+
/**
47+
* Constructor for the connection to an alternative server
48+
* @param serverURI String representation of the alternative server's address
49+
*/
50+
public BoringParts(CertStreamStringMessageHandler messageHandler, long sleepBeforeReconnect, String serverURI){
51+
this.sleepBeforeReconnect = sleepBeforeReconnect;
52+
this.webSocketClientSupplier = () -> defaultImplFactory.make(messageHandler, serverURI);
53+
this.webSocketClient = webSocketClientSupplier.get();
54+
}
55+
4656
//todo reconnection logic
4757
@Override
4858
public void onClose(int i, String s, boolean b) {

src/io/calidog/certstream/CertStream.java

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@ public static void onMessageString(Consumer<String> handler)
2626
{
2727
BoringParts theBoringParts = new BoringParts(handler::accept);
2828

29-
while (theBoringParts.isNotClosed())
30-
{
31-
Thread.yield();
32-
try {
33-
Thread.sleep(1000);
34-
} catch (InterruptedException e) {}
29+
runThread(theBoringParts);
30+
31+
}).start();
32+
}
33+
34+
/**
35+
* @param handler A {@link Consumer<String>} that we'll
36+
* run in a Thread that stays alive as long
37+
* as the WebSocket stays open
38+
* @param serverURI Address of the server to which a WebSocket
39+
* connection is established
40+
*/
41+
public static void onMessageStringAlternativeServer(Consumer<String> handler, String serverURI){
42+
new Thread(() -> {
43+
BoringParts theBoringParts = new BoringParts(handler::accept, 0 , serverURI);
44+
45+
runThread(theBoringParts);
3546

36-
if (Thread.interrupted())
37-
{
38-
break;
39-
}
40-
}
4147
}).start();
4248
}
4349

@@ -80,4 +86,60 @@ public static void onMessage(CertStreamMessageHandler handler)
8086
handler.onMessage(fullMsg);
8187
});
8288
}
89+
90+
/**
91+
* @param handler A {@link Consumer<CertStreamMessage>} that we'll
92+
* run in a Thread that stays alive as long
93+
* as the WebSocket stays open
94+
* @param serverURI Address of the server to which a WebSocket
95+
* connection is established
96+
*/
97+
public static void onMessageAlternativeServer (CertStreamMessageHandler handler, String serverURI){
98+
onMessageStringAlternativeServer(string -> {
99+
CertStreamMessagePOJO msg;
100+
101+
try {
102+
msg = new Gson().fromJson(string, CertStreamMessagePOJO.class);
103+
104+
if (msg.messageType.equalsIgnoreCase("heartbeat")) {
105+
return;
106+
}
107+
} catch (JsonSyntaxException e) {
108+
System.out.println(e.getMessage());
109+
logger.warn("onMessageAlternativeServer had an exception parsing some json", e);
110+
return;
111+
}
112+
113+
CertStreamMessage fullMsg;
114+
115+
try {
116+
fullMsg = CertStreamMessage.fromPOJO(msg);
117+
} catch (CertificateException e){
118+
logger.warn("Encountered a CertificateException", e);
119+
return;
120+
}
121+
122+
handler.onMessage(fullMsg);
123+
}, serverURI);
124+
}
125+
126+
/**
127+
* Runs thread until WebSocket is closed
128+
* @param theBoringParts Websocket connection that is checked
129+
* to see if it has been closed
130+
*/
131+
public static void runThread(BoringParts theBoringParts) {
132+
while (theBoringParts.isNotClosed())
133+
{
134+
Thread.yield();
135+
try {
136+
Thread.sleep(1000);
137+
} catch (InterruptedException e) {}
138+
139+
if (Thread.interrupted())
140+
{
141+
break;
142+
}
143+
}
144+
}
83145
}

src/io/calidog/certstream/CertStreamClientImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public CertStreamClientImpl(CertStreamClient client) {
2727
this.connect();
2828
}
2929

30+
/**
31+
* Constructor for alternative Server
32+
* @param serverURI String representation of server-address
33+
*/
34+
public CertStreamClientImpl(CertStreamClient client, String serverURI) throws URISyntaxException {
35+
super(new URI(serverURI));
36+
this.client = client;
37+
this.connect();
38+
}
39+
3040
public void onOpen(ServerHandshake serverHandshake) {
3141
client.onOpen(serverHandshake);
3242
}

src/io/calidog/certstream/CertStreamClientImplFactory.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.java_websocket.handshake.ServerHandshake;
44

5+
import java.net.URISyntaxException;
6+
57
/**
68
* A bundler around a functional interface. This is the little bit of magic
79
* that lets you just pass in a {@link java.util.function.Consumer} and have
@@ -30,12 +32,39 @@ public CertStreamClientImplFactory(CertStreamOpenHandler openHandler,
3032

3133
/**
3234
* @param messageHandler The handler for onMessage
33-
* @return a complete CertStreamClientImpl made out of a function
34-
* and the handlers this Object was initialized with
35+
* @return CertStreamClientImpl connected to the
36+
* default sever ("wss://certstream.calidog.io")
3537
*/
3638
public CertStreamClientImpl make(CertStreamStringMessageHandler messageHandler)
3739
{
38-
return new CertStreamClientImpl(new CertStreamClient() {
40+
return new CertStreamClientImpl(createClient(messageHandler));
41+
}
42+
43+
/**
44+
*
45+
* @param messageHandler The handler for onMessage
46+
* @param serverURI String representation of the address of the alternative server
47+
* @return CertStreamClientImpl connected to an
48+
* alternative Certstream-Server
49+
*/
50+
public CertStreamClientImpl make(CertStreamStringMessageHandler messageHandler, String serverURI){
51+
CertStreamClientImpl certStreamClient = null;
52+
try {
53+
certStreamClient = new CertStreamClientImpl(createClient(messageHandler), serverURI);
54+
} catch (URISyntaxException e) {
55+
e.printStackTrace();
56+
}
57+
return certStreamClient;
58+
}
59+
60+
/**
61+
*
62+
* @param messageHandler The handler for onMessage
63+
* @return a complete CertStreamClientImpl made out of a function
64+
* and the handlers this Object was initialized with
65+
*/
66+
public CertStreamClient createClient(CertStreamStringMessageHandler messageHandler){
67+
return new CertStreamClient() {
3968
@Override
4069
public void onOpen(ServerHandshake serverHandshake) {
4170
openHandler.onOpen(serverHandshake);
@@ -55,7 +84,7 @@ public void onClose(int i, String s, boolean b) {
5584
public void onError(Exception e) {
5685
errorHandler.onError(e);
5786
}
58-
});
87+
};
5988
}
6089

6190

0 commit comments

Comments
 (0)