enum
This commit is contained in:
@@ -27,16 +27,14 @@ public abstract class Socket extends Emitter {
|
||||
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private static final int OPENING = 0;
|
||||
private static final int OPEN = 1;
|
||||
private static final int CLOSING = 2;
|
||||
private static final int CLOSED = 3;
|
||||
private static final Map<Integer, String> STATE_MAP = new HashMap<Integer, String>() {{
|
||||
put(OPENING, "opening");
|
||||
put(OPEN, "open");
|
||||
put(CLOSING, "closing");
|
||||
put(CLOSED, "closed");
|
||||
}};
|
||||
private enum ReadyState {
|
||||
OPENING, OPEN, CLOSING, CLOSED;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on successful connection.
|
||||
@@ -98,7 +96,6 @@ public abstract class Socket extends Emitter {
|
||||
private int port;
|
||||
private int policyPort;
|
||||
private int prevBufferLen;
|
||||
private int readyState = -1;
|
||||
private long pingInterval;
|
||||
private long pingTimeout;
|
||||
private String id;
|
||||
@@ -114,6 +111,7 @@ public abstract class Socket extends Emitter {
|
||||
private Future pingTimeoutTimer;
|
||||
private Future pingIntervalTimer;
|
||||
|
||||
private ReadyState readyState;
|
||||
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
|
||||
@@ -179,7 +177,7 @@ public abstract class Socket extends Emitter {
|
||||
EventThread.exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Socket.this.readyState = OPENING;
|
||||
Socket.this.readyState = ReadyState.OPENING;
|
||||
Transport transport = Socket.this.createTransport(Socket.this.transports.get(0));
|
||||
Socket.this.setTransport(transport);
|
||||
transport.open();
|
||||
@@ -297,7 +295,7 @@ public abstract class Socket extends Emitter {
|
||||
@Override
|
||||
public void run() {
|
||||
if (failed[0]) return;
|
||||
if (self.readyState == CLOSED || self.readyState == CLOSING) {
|
||||
if (self.readyState == ReadyState.CLOSED || self.readyState == ReadyState.CLOSING) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -354,12 +352,12 @@ public abstract class Socket extends Emitter {
|
||||
|
||||
private void onOpen() {
|
||||
logger.fine("socket open");
|
||||
this.readyState = OPEN;
|
||||
this.readyState = ReadyState.OPEN;
|
||||
this.emit(EVENT_OPEN);
|
||||
this.onopen();
|
||||
this.flush();
|
||||
|
||||
if (this.readyState == OPEN && this.upgrade && this.transport instanceof Polling) {
|
||||
if (this.readyState == ReadyState.OPEN && this.upgrade && this.transport instanceof Polling) {
|
||||
logger.fine("starting upgrade probes");
|
||||
for (String upgrade: this.upgrades) {
|
||||
this.probe(upgrade);
|
||||
@@ -368,7 +366,7 @@ public abstract class Socket extends Emitter {
|
||||
}
|
||||
|
||||
private void onPacket(Packet packet) {
|
||||
if (this.readyState == OPENING || this.readyState == OPEN) {
|
||||
if (this.readyState == ReadyState.OPENING || this.readyState == ReadyState.OPEN) {
|
||||
logger.fine(String.format("socket received: type '%s', data '%s'", packet.type, packet.data));
|
||||
|
||||
this.emit(EVENT_PACKET, packet);
|
||||
@@ -389,7 +387,7 @@ public abstract class Socket extends Emitter {
|
||||
this.onmessage(packet.data);
|
||||
}
|
||||
} else {
|
||||
logger.fine(String.format("packet received with socket readyState '%s'", STATE_MAP.get(this.readyState)));
|
||||
logger.fine(String.format("packet received with socket readyState '%s'", this.readyState));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,7 +428,7 @@ public abstract class Socket extends Emitter {
|
||||
EventThread.exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (self.readyState == CLOSED) return;
|
||||
if (self.readyState == ReadyState.CLOSED) return;
|
||||
self.onClose("ping timeout");
|
||||
}
|
||||
});
|
||||
@@ -485,7 +483,7 @@ public abstract class Socket extends Emitter {
|
||||
}
|
||||
|
||||
private void flush() {
|
||||
if (this.readyState != CLOSED && this.transport.writable &&
|
||||
if (this.readyState != ReadyState.CLOSED && this.transport.writable &&
|
||||
!this.upgrading && this.writeBuffer.size() != 0) {
|
||||
logger.fine(String.format("flushing %d packets in socket", this.writeBuffer.size()));
|
||||
this.prevBufferLen = this.writeBuffer.size();
|
||||
@@ -552,7 +550,7 @@ public abstract class Socket extends Emitter {
|
||||
EventThread.exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Socket.this.readyState == OPENING || Socket.this.readyState == OPEN) {
|
||||
if (Socket.this.readyState == ReadyState.OPENING || Socket.this.readyState == ReadyState.OPEN) {
|
||||
Socket.this.onClose("forced close");
|
||||
logger.fine("socket closing - telling transport to close");
|
||||
Socket.this.transport.close();
|
||||
@@ -575,7 +573,7 @@ public abstract class Socket extends Emitter {
|
||||
}
|
||||
|
||||
private void onClose(String reason, Exception desc) {
|
||||
if (this.readyState == OPENING || this.readyState == OPEN) {
|
||||
if (this.readyState == ReadyState.OPENING || this.readyState == ReadyState.OPEN) {
|
||||
logger.fine(String.format("socket close with reason: %s", reason));
|
||||
if (this.pingIntervalTimer != null) {
|
||||
this.pingIntervalTimer.cancel(true);
|
||||
@@ -590,7 +588,7 @@ public abstract class Socket extends Emitter {
|
||||
Socket.this.callbackBuffer.clear();
|
||||
}
|
||||
});
|
||||
this.readyState = CLOSED;
|
||||
this.readyState = ReadyState.CLOSED;
|
||||
this.emit(EVENT_CLOSE, reason, desc);
|
||||
this.onclose();
|
||||
this.id = null;
|
||||
|
||||
@@ -5,21 +5,18 @@ import com.github.nkzawa.emitter.Emitter;
|
||||
import com.github.nkzawa.engineio.parser.Packet;
|
||||
import com.github.nkzawa.engineio.parser.Parser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Transport extends Emitter {
|
||||
|
||||
protected static final int OPENING = 0;
|
||||
protected static final int OPEN = 1;
|
||||
protected static final int CLOSED = 2;
|
||||
protected static final int PAUSED = 3;
|
||||
protected static final Map<Integer, String> STATE_MAP = new HashMap<Integer, String>() {{
|
||||
put(OPENING, "opening");
|
||||
put(OPEN, "open");
|
||||
put(CLOSED, "closed");
|
||||
put(PAUSED, "paused");
|
||||
}};
|
||||
protected enum ReadyState {
|
||||
OPENING, OPEN, CLOSED, PAUSED;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String EVENT_OPEN = "open";
|
||||
public static final String EVENT_CLOSE = "close";
|
||||
@@ -34,11 +31,11 @@ public abstract class Transport extends Emitter {
|
||||
protected boolean secure;
|
||||
protected boolean timestampRequests;
|
||||
protected int port;
|
||||
protected int readyState = -1;
|
||||
protected String path;
|
||||
protected String hostname;
|
||||
protected String timestampParam;
|
||||
|
||||
protected ReadyState readyState;
|
||||
|
||||
public Transport(Options opts) {
|
||||
this.path = opts.path;
|
||||
@@ -61,8 +58,8 @@ public abstract class Transport extends Emitter {
|
||||
exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Transport.this.readyState == CLOSED || Transport.this.readyState < 0) {
|
||||
Transport.this.readyState = OPENING;
|
||||
if (Transport.this.readyState == ReadyState.CLOSED || Transport.this.readyState == null) {
|
||||
Transport.this.readyState = ReadyState.OPENING;
|
||||
Transport.this.doOpen();
|
||||
}
|
||||
}
|
||||
@@ -74,7 +71,7 @@ public abstract class Transport extends Emitter {
|
||||
exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Transport.this.readyState == OPENING || Transport.this.readyState == OPEN) {
|
||||
if (Transport.this.readyState == ReadyState.OPENING || Transport.this.readyState == ReadyState.OPEN) {
|
||||
Transport.this.doClose();
|
||||
Transport.this.onClose();
|
||||
}
|
||||
@@ -87,7 +84,7 @@ public abstract class Transport extends Emitter {
|
||||
exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (Transport.this.readyState == OPEN) {
|
||||
if (Transport.this.readyState == ReadyState.OPEN) {
|
||||
Transport.this.write(packets);
|
||||
} else {
|
||||
throw new RuntimeException("Transport not open");
|
||||
@@ -97,7 +94,7 @@ public abstract class Transport extends Emitter {
|
||||
}
|
||||
|
||||
protected void onOpen() {
|
||||
this.readyState = OPEN;
|
||||
this.readyState = ReadyState.OPEN;
|
||||
this.writable = true;
|
||||
this.emit(EVENT_OPEN);
|
||||
}
|
||||
@@ -111,7 +108,7 @@ public abstract class Transport extends Emitter {
|
||||
}
|
||||
|
||||
protected void onClose() {
|
||||
this.readyState = CLOSED;
|
||||
this.readyState = ReadyState.CLOSED;
|
||||
this.emit(EVENT_CLOSE);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,13 +38,13 @@ abstract public class Polling extends Transport {
|
||||
public void run() {
|
||||
final Polling self = Polling.this;
|
||||
|
||||
Polling.this.readyState = PAUSED;
|
||||
Polling.this.readyState = ReadyState.PAUSED;
|
||||
|
||||
final Runnable pause = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.fine("paused");
|
||||
self.readyState = PAUSED;
|
||||
self.readyState = ReadyState.PAUSED;
|
||||
onPause.run();
|
||||
}
|
||||
};
|
||||
@@ -100,7 +100,7 @@ abstract public class Polling extends Transport {
|
||||
Parser.decodePayload(data, new Parser.DecodePayloadCallback() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
if (self.readyState == OPENING) {
|
||||
if (self.readyState == ReadyState.OPENING) {
|
||||
self.onOpen();
|
||||
}
|
||||
|
||||
@@ -114,14 +114,14 @@ abstract public class Polling extends Transport {
|
||||
}
|
||||
});
|
||||
|
||||
if (this.readyState != CLOSED) {
|
||||
if (this.readyState != ReadyState.CLOSED) {
|
||||
this.polling = false;
|
||||
this.emit(EVENT_POLL_COMPLETE);
|
||||
|
||||
if (this.readyState == OPEN) {
|
||||
if (this.readyState == ReadyState.OPEN) {
|
||||
this.poll();
|
||||
} else {
|
||||
logger.fine(String.format("ignoring poll - transport state '%s'", STATE_MAP.get(this.readyState)));
|
||||
logger.fine(String.format("ignoring poll - transport state '%s'", this.readyState));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user