-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathPolling.java
218 lines (179 loc) · 6.69 KB
/
Polling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package io.socket.engineio.client.transports;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.Transport;
import io.socket.engineio.parser.Packet;
import io.socket.engineio.parser.Parser;
import io.socket.parseqs.ParseQS;
import io.socket.thread.EventThread;
import io.socket.yeast.Yeast;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
abstract public class Polling extends Transport {
private static final Logger logger = Logger.getLogger(Polling.class.getName());
public static final String NAME = "polling";
public static final String EVENT_POLL = "poll";
public static final String EVENT_POLL_COMPLETE = "pollComplete";
private boolean polling;
public Polling(Options opts) {
super(opts);
this.name = NAME;
}
protected void doOpen() {
this.poll();
}
public void pause(final Runnable onPause) {
EventThread.exec(new Runnable() {
@Override
public void run() {
final Polling self = Polling.this;
Polling.this.readyState = ReadyState.PAUSED;
final Runnable pause = new Runnable() {
@Override
public void run() {
logger.fine("paused");
self.readyState = ReadyState.PAUSED;
onPause.run();
}
};
if (Polling.this.polling || !Polling.this.writable) {
final int[] total = new int[]{0};
if (Polling.this.polling) {
logger.fine("we are currently polling - waiting to pause");
total[0]++;
Polling.this.once(EVENT_POLL_COMPLETE, new Emitter.Listener() {
@Override
public void call(Object... args) {
logger.fine("pre-pause polling complete");
if (--total[0] == 0) {
pause.run();
}
}
});
}
if (!Polling.this.writable) {
logger.fine("we are currently writing - waiting to pause");
total[0]++;
Polling.this.once(EVENT_DRAIN, new Emitter.Listener() {
@Override
public void call(Object... args) {
logger.fine("pre-pause writing complete");
if (--total[0] == 0) {
pause.run();
}
}
});
}
} else {
pause.run();
}
}
});
}
private void poll() {
logger.fine("polling");
this.polling = true;
this.doPoll();
this.emit(EVENT_POLL);
}
@Override
protected void onData(String data) {
_onData(data);
}
@Override
protected void onData(byte[] data) {
_onData(data);
}
private void _onData(Object data) {
final Polling self = this;
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("polling got data %s", data));
}
Parser.DecodePayloadCallback callback = new Parser.DecodePayloadCallback() {
@Override
public boolean call(Packet packet, int index, int total) {
if (self.readyState == ReadyState.OPENING && Packet.OPEN.equals(packet.type)) {
self.onOpen();
}
if (Packet.CLOSE.equals(packet.type)) {
self.onClose();
return false;
}
self.onPacket(packet);
return true;
}
};
Parser.decodePayload((String) data, callback);
if (this.readyState != ReadyState.CLOSED) {
this.polling = false;
this.emit(EVENT_POLL_COMPLETE);
if (this.readyState == ReadyState.OPEN) {
this.poll();
} else {
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("ignoring poll - transport state '%s'", this.readyState));
}
}
}
}
protected void doClose() {
final Polling self = this;
Emitter.Listener close = new Emitter.Listener() {
@Override
public void call(Object... args) {
logger.fine("writing close packet");
self.write(new Packet[]{new Packet(Packet.CLOSE)});
}
};
if (this.readyState == ReadyState.OPEN) {
logger.fine("transport open - closing");
close.call();
} else {
// in case we're trying to close while
// handshaking is in progress (engine.io-client GH-164)
logger.fine("transport not open - deferring close");
this.once(EVENT_OPEN, close);
}
}
protected void write(Packet[] packets) {
final Polling self = this;
this.writable = false;
final Runnable callbackfn = new Runnable() {
@Override
public void run() {
self.writable = true;
self.emit(EVENT_DRAIN);
}
};
Parser.encodePayload(packets, new Parser.EncodeCallback<String>() {
@Override
public void call(String data) {
self.doWrite(data, callbackfn);
}
});
}
protected String uri() {
Map<String, String> query = this.query;
if (query == null) {
query = new HashMap<String, String>();
}
String schema = this.secure ? "https" : "http";
String port = "";
if (this.timestampRequests) {
query.put(this.timestampParam, Yeast.yeast());
}
String derivedQuery = ParseQS.encode(query);
if (this.port > 0 && (("https".equals(schema) && this.port != 443)
|| ("http".equals(schema) && this.port != 80))) {
port = ":" + this.port;
}
if (derivedQuery.length() > 0) {
derivedQuery = "?" + derivedQuery;
}
boolean ipv6 = this.hostname.contains(":");
return schema + "://" + (ipv6 ? "[" + this.hostname + "]" : this.hostname) + port + this.path + derivedQuery;
}
abstract protected void doWrite(String data, Runnable fn);
abstract protected void doPoll();
}