Skip to content

Commit 682223c

Browse files
committed
Sachin | Changed logging levels for socket, tested reconnection functionality.
Mode logger to be local, changed disableLogging method.
1 parent c7ba825 commit 682223c

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

src/main/java/io/github/sac/Socket.java

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
package io.github.sac;
22

3-
import com.neovisionaries.ws.client.*;
4-
import org.json.JSONException;
5-
import org.json.JSONObject;
6-
3+
import com.neovisionaries.ws.client.OpeningHandshakeException;
4+
import com.neovisionaries.ws.client.StatusLine;
5+
import com.neovisionaries.ws.client.WebSocket;
6+
import com.neovisionaries.ws.client.WebSocketAdapter;
7+
import com.neovisionaries.ws.client.WebSocketException;
8+
import com.neovisionaries.ws.client.WebSocketFactory;
9+
import com.neovisionaries.ws.client.WebSocketFrame;
10+
import com.neovisionaries.ws.client.WebSocketState;
711
import java.io.IOException;
8-
import java.util.*;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Timer;
17+
import java.util.TimerTask;
918
import java.util.concurrent.atomic.AtomicInteger;
10-
import java.util.logging.LogManager;
19+
import java.util.logging.Level;
1120
import java.util.logging.Logger;
21+
import org.json.JSONException;
22+
import org.json.JSONObject;
1223

1324
/**
1425
* Created by sachin on 13/11/16.
1526
*/
1627

1728
public class Socket extends Emitter {
1829

19-
20-
private final static Logger LOGGER = Logger.getLogger(Socket.class.getName());
30+
private final Logger logger = Logger.getLogger(Socket.class.getName());
2131

2232
private AtomicInteger counter;
2333
private String URL;
@@ -79,6 +89,9 @@ public void setListener(BasicListener listener) {
7989
this.listener = listener;
8090
}
8191

92+
public Logger getLogger(){
93+
return logger;
94+
}
8295
/**
8396
* used to set up TLS/SSL connection to server for more details visit neovisionaries websocket client
8497
*/
@@ -149,7 +162,7 @@ public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception
149162
/**
150163
* Message retrieval mechanism goes here
151164
*/
152-
LOGGER.info("Message :" + object.toString());
165+
logger.info("Message :" + object.toString());
153166

154167

155168
try {
@@ -191,14 +204,14 @@ public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception
191204
if (fn != null) {
192205
fn.call((String) objects[0], object.opt("error"), object.opt("data"));
193206
} else {
194-
LOGGER.info("ack function is null with rid " + rid);
207+
logger.warning("ack function is null with rid " + rid);
195208
}
196209
}
197210
}
198211
break;
199212
}
200213
} catch (Exception e) {
201-
e.printStackTrace();
214+
logger.severe(e.toString());
202215
}
203216

204217
}
@@ -208,14 +221,13 @@ public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception
208221

209222
@Override
210223
public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
211-
LOGGER.info("On close frame got called");
224+
logger.warning("On close frame got called");
212225
super.onCloseFrame(websocket, frame);
213226
}
214227

215228
@Override
216229
public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
217-
LOGGER.info("Got send error");
218-
230+
logger.severe("Error while sending data " + cause.toString());
219231
super.onSendError(websocket, cause, frame);
220232
}
221233

@@ -427,7 +439,7 @@ public void connect() {
427439
try {
428440
ws = factory.createSocket(URL);
429441
} catch (IOException e) {
430-
e.printStackTrace();
442+
logger.severe(e.toString());
431443
}
432444
ws.addExtension("permessage-deflate; client_max_window_bits");
433445
for (Map.Entry<String, String> entry : headers.entrySet()) {
@@ -442,16 +454,17 @@ public void connect() {
442454
// A violation against the WebSocket protocol was detected
443455
// during the opening handshake.
444456

457+
logger.severe(e.toString());
445458
// Status line.
446459
StatusLine sl = e.getStatusLine();
447-
LOGGER.info("=== Status Line ===");
448-
LOGGER.info("HTTP Version = \n" + sl.getHttpVersion());
449-
LOGGER.info("Status Code = \n" + sl.getStatusCode());
450-
LOGGER.info("Reason Phrase = \n" + sl.getReasonPhrase());
460+
logger.info("=== Status Line ===");
461+
logger.info("HTTP Version = \n" + sl.getHttpVersion());
462+
logger.info("Status Code = \n" + sl.getStatusCode());
463+
logger.info("Reason Phrase = \n" + sl.getReasonPhrase());
451464

452465
// HTTP headers.
453466
Map<String, List<String>> headers = e.getHeaders();
454-
LOGGER.info("=== HTTP Headers ===");
467+
logger.info("=== HTTP Headers ===");
455468
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
456469
// Header name.
457470
String name = entry.getKey();
@@ -461,17 +474,16 @@ public void connect() {
461474

462475
if (values == null || values.size() == 0) {
463476
// Print the name only.
464-
LOGGER.info(name);
477+
logger.info(name);
465478
continue;
466479
}
467480

468481
for (String value : values) {
469482
// Print the name and the value.
470-
LOGGER.info(name + value + "\n");
483+
logger.info(name + value + "\n");
471484
}
472485
}
473486
} catch (WebSocketException e) {
474-
// Failed to establish a WebSocket connection.
475487
listener.onConnectError(Socket.this, e);
476488
reconnect();
477489
}
@@ -482,7 +494,7 @@ public void connectAsync() {
482494
try {
483495
ws = factory.createSocket(URL);
484496
} catch (IOException e) {
485-
e.printStackTrace();
497+
logger.severe(e.toString());
486498
}
487499
ws.addExtension("permessage-deflate; client_max_window_bits");
488500
for (Map.Entry<String, String> entry : headers.entrySet()) {
@@ -495,13 +507,13 @@ public void connectAsync() {
495507

496508
private void reconnect() {
497509
if (strategy == null) {
498-
LOGGER.info("Unable to reconnect: reconnection is null");
510+
logger.warning("Unable to reconnect: reconnection is null");
499511
return;
500512
}
501513

502514
if (strategy.areAttemptsComplete()) {
503515
strategy.setAttemptsMade(0);
504-
LOGGER.info("Unable to reconnect: max reconnection attempts reached");
516+
logger.warning("Unable to reconnect: max reconnection attempts reached");
505517
return;
506518
}
507519

@@ -510,7 +522,7 @@ private void reconnect() {
510522
@Override
511523
public void run() {
512524
if (strategy == null) {
513-
LOGGER.info("Unable to reconnect: reconnection is null");
525+
logger.warning("Unable to reconnect: reconnection is null");
514526
return;
515527
}
516528
strategy.processValues();
@@ -546,7 +558,7 @@ public Boolean isconnected() {
546558
}
547559

548560
public void disableLogging() {
549-
LogManager.getLogManager().reset();
561+
logger.setLevel(Level.OFF);
550562
}
551563

552564
/**
@@ -596,4 +608,10 @@ public void unsubscribe(Ack ack) {
596608
channels.remove(this);
597609
}
598610
}
611+
612+
@Override
613+
protected void finalize() throws Throwable {
614+
ws.disconnect("Client socket garbage collected, closing connection");
615+
super.finalize();
616+
}
599617
}

0 commit comments

Comments
 (0)