This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
/
connect.js
372 lines (327 loc) · 11.6 KB
/
connect.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Collab module for the Cloud9 that uses collab
*
* @copyright 2013, Ajax.org B.V.
*/
define(function(require, exports, module) {
main.consumes = [
"c9", "Plugin", "ext", "vfs", "dialog.question",
"installer"
];
main.provides = ["collab.connect"];
return main;
function main(options, imports, register) {
var Plugin = imports.Plugin;
var c9 = imports.c9;
var ext = imports.ext;
var vfs = imports.vfs;
var installer = imports.installer;
var question = imports["dialog.question"];
/***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes);
var emit = plugin.getEmitter();
var clientId;
// 0 - production
// 1 - development
// 2 - tracing
var debug = options.debug;
// var markup = require("text!./connect.xml");
var collab;
var collabInstalled = !options.isSSH;
var connecting = false;
var connected = false;
var isMaster = null;
var fatalError = false;
var CONNECT_TIMEOUT = 30000; // 30 seconds
var IDLE_PERIOD = 300000; // 5 minutes
var connectMsg;
var connectTimeout;
var stream;
// Idle state handling
var focussed = true;
var reportedIdle = false;
var idleTimeout;
var loaded = false;
function load() {
if (!installer.isInstalled("c9.ide.collab", function() {
load();
})) return;
if (loaded) return;
loaded = true;
if (c9.connected)
connect();
window.addEventListener("focus", updateIdleWithFocus);
window.addEventListener("blur", updateIdleWithBlur);
c9.on("connect", connect);
c9.on("disconnect", onDisconnect);
}
function updateIdleWithFocus() {
focussed = true;
clearTimeout(idleTimeout);
if (!connected || !reportedIdle)
return;
send("USER_STATE", { state: "online" });
reportedIdle = false;
}
function updateIdleWithBlur() {
focussed = false;
if (reportedIdle)
return;
clearTimeout(idleTimeout);
idleTimeout = setTimeout(function() {
if (!connected)
return;
reportedIdle = true;
send("USER_STATE", { state: "idle" });
}, IDLE_PERIOD);
}
function updateIdleStatus() {
if (document.hasFocus())
updateIdleWithFocus();
else
updateIdleWithBlur();
}
var extended = false;
function extendCollab(callback) {
if (collab)
return callback();
var t = debug && Date.now();
if (extended)
return plugin.once("available", callback);
extended = true;
ext.loadRemotePlugin("collab", {
file: "c9.ide.collab/server/collab-server.js"
}, function(err, api) {
if (!api) {
extended = false;
return callback(err);
}
if (debug)
console.log("loaded collab server in ", Date.now() - t, "ms");
collab = api;
emit.sticky("available");
callback();
});
}
function onDisconnect() {
if (connected || connecting)
emit("disconnect");
else
console.log("Collab already disconnected");
connecting = connected = extended = false;
emit.unsticky("available");
collab = null;
if (stream) {
stream.$close();
stream = null;
}
clearTimeout(connectTimeout);
}
/***** Methods *****/
function connect() {
if (fatalError)
return;
if (connected) {
console.log("Collab already connected, ignoring reconnection attempt");
return;
}
if (connecting)
return;
connecting = true;
console.log("Collab connecting");
emit("connecting");
connectTimeout = setTimeout(function() {
if (stream) {
stream.$close();
stream = null;
}
connecting = false;
if (!connected) {
console.warn("[OT] Collab connect timed out ! - retrying ...");
connect();
}
}, CONNECT_TIMEOUT);
extendCollab(function(err) {
if (err)
return console.error("COLLAB CONNECT ERR", err);
if (collabInstalled)
return doConnect();
// sshCheckInstall();
});
}
function doConnect() {
// socket.id
clientId = vfs.id;
collab.connect({
basePath: options.basePath,
clientId: clientId
}, function (err, meta) {
if (err) {
fatalError = err.code === "EFATAL";
console.error("COLLAB connect failed", err);
if (fatalError)
promptInstaller(err);
return;
}
stream = meta.stream;
var isClosed = false;
stream.once("data", onConnect);
stream.once("end", function () {
console.log("COLLAB STREAM END");
onClose();
});
stream.once("close", function() {
console.log("COLLAB STREAM CLOSE");
onClose();
});
stream.$close = onClose;
function onData(data) {
data = JSON.parse(data);
if (debug)
console.log("[OT] RECEIVED FROM SERVER", data);
emit("message", data);
}
function onConnect(data) {
if (isClosed || !collab)
return onClose();
data = JSON.parse(data);
if (debug)
console.log("[OT] RECEIVED FROM SERVER", data);
if (data.type !== "CONNECT")
return console.log("[OT] Waiting for connect event, skipping message", data);
connected = true;
connecting = false;
isMaster = meta.isMaster;
connectMsg = data;
console.log("COLLAB connected -", meta.isMaster ? "MASTER" : "SLAVE");
emit("connect", data);
stream.on("data", onData);
clearTimeout(connectTimeout);
updateIdleStatus();
}
function onClose() {
if (isClosed)
return;
if (stream) {
stream.off("data", onData);
stream.destroy();
}
isClosed = true;
onDisconnect();
setTimeout(function() {
c9.connected && connect();
}, 1000);
}
});
}
function send(msg) {
if (typeof arguments[0] !== "object")
msg = { type: arguments[0], data: arguments[1] };
if (!connected)
return console.log("Collab not connected - SKIPPING ", msg);
if (debug)
console.log("[OT] SENDING TO SERVER", msg);
collab.send(clientId, msg);
}
function promptInstaller(err) {
question.show("Missing collab dependencies",
err.message,
"Cloud9 detected you are missing one or more collab dependencies." +
" Would you like to open the installer to update to the latest version?",
function() { // Yes
installer.reinstall("c9.ide.collab");
},
function() { // No
// Do nothing
},
{
yes: "Update",
no: "Not now"
}
);
}
/***** Lifecycle *****/
plugin.on("load", function() {
load();
});
plugin.on("enable", function() {
});
plugin.on("disable", function() {
});
plugin.on("unload", function() {
loaded = false;
});
// Make sure the available event is always called
plugin.on("newListener", function(event, listener) {
if (event == "connect" && connected && connectMsg)
listener(null, connectMsg);
else if (event == "connecting" && connecting)
listener();
});
/***** Register and define API *****/
/**
* Finder implementation using collab
**/
plugin.freezePublicAPI({
_events: [
/**
* Fires when the collab VFS API is extended and available to be used by collab to
* connect a user to the collab server.
* @event available
*/
"available",
/**
* Fires when the collab is connected, handshaked and a stream is inited
* and pushing messages from the collab server.
* @event connect
*/
"connect",
/**
* Fires when the collab is connecting to the collab server
* @event connecting
*/
"connecting",
/**
* Fires when the collab is disconnected
* @event disconnect
*/
"disconnect",
/**
* Fires when a non-connect message is received on the collab stream
* when the collab is connected to the collab server
* @event message
*/
"message"
],
/**
* Specifies whether the collab debug is enabled or not
* @property {Boolean} debug
*/
get debug() { return debug; },
/**
* Specifies whether the collab is connected or not
* @property {Boolean} connected
*/
get connected() { return connected; },
/**
* Specifies whether the collab is connecting or not
* @property {Boolean} connecting
*/
get connecting() { return connecting; },
/**
* Specifies whether the collab is in master mode or not
* @property {Boolean} isMaster
*/
get isMaster() { return isMaster; },
/**
* Send a message to the collab server
* @param {String} type the type of the message
* @param {Object} message the message body to send
*/
send: send
});
register(null, {
"collab.connect": plugin
});
}
});