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