Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.require("dojo.io.common");
12
dojo.provide("dojo.io.cometd");
13
dojo.require("dojo.AdapterRegistry");
14
dojo.require("dojo.json");
15
dojo.require("dojo.io.BrowserIO");
16
dojo.require("dojo.io.IframeIO");
17
dojo.require("dojo.io.ScriptSrcIO");
18
dojo.require("dojo.io.cookie");
19
dojo.require("dojo.event.*");
20
dojo.require("dojo.lang.common");
21
dojo.require("dojo.lang.func");
22
cometd = new function () {
23
	this.initialized = false;
24
	this.connected = false;
25
	this.connectionTypes = new dojo.AdapterRegistry(true);
26
	this.version = 0.1;
27
	this.minimumVersion = 0.1;
28
	this.clientId = null;
29
	this._isXD = false;
30
	this.handshakeReturn = null;
31
	this.currentTransport = null;
32
	this.url = null;
33
	this.lastMessage = null;
34
	this.globalTopicChannels = {};
35
	this.backlog = [];
36
	this.tunnelInit = function (childLocation, childDomain) {
37
	};
38
	this.tunnelCollapse = function () {
39
		dojo.debug("tunnel collapsed!");
40
	};
41
	this.init = function (props, root, bargs) {
42
		props = props || {};
43
		props.version = this.version;
44
		props.minimumVersion = this.minimumVersion;
45
		props.channel = "/meta/handshake";
46
		this.url = root || djConfig["cometdRoot"];
47
		if (!this.url) {
48
			dojo.debug("no cometd root specified in djConfig and no root passed");
49
			return;
50
		}
51
		var bindArgs = {url:this.url, method:"POST", mimetype:"text/json", load:dojo.lang.hitch(this, "finishInit"), content:{"message":dojo.json.serialize([props])}};
52
		var regexp = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$";
53
		var r = ("" + window.location).match(new RegExp(regexp));
54
		if (r[4]) {
55
			var tmp = r[4].split(":");
56
			var thisHost = tmp[0];
57
			var thisPort = tmp[1] || "80";
58
			r = this.url.match(new RegExp(regexp));
59
			if (r[4]) {
60
				tmp = r[4].split(":");
61
				var urlHost = tmp[0];
62
				var urlPort = tmp[1] || "80";
63
				if ((urlHost != thisHost) || (urlPort != thisPort)) {
64
					dojo.debug(thisHost, urlHost);
65
					dojo.debug(thisPort, urlPort);
66
					this._isXD = true;
67
					bindArgs.transport = "ScriptSrcTransport";
68
					bindArgs.jsonParamName = "jsonp";
69
					bindArgs.method = "GET";
70
				}
71
			}
72
		}
73
		if (bargs) {
74
			dojo.lang.mixin(bindArgs, bargs);
75
		}
76
		return dojo.io.bind(bindArgs);
77
	};
78
	this.finishInit = function (type, data, evt, request) {
79
		data = data[0];
80
		this.handshakeReturn = data;
81
		if (data["authSuccessful"] == false) {
82
			dojo.debug("cometd authentication failed");
83
			return;
84
		}
85
		if (data.version < this.minimumVersion) {
86
			dojo.debug("cometd protocol version mismatch. We wanted", this.minimumVersion, "but got", data.version);
87
			return;
88
		}
89
		this.currentTransport = this.connectionTypes.match(data.supportedConnectionTypes, data.version, this._isXD);
90
		this.currentTransport.version = data.version;
91
		this.clientId = data.clientId;
92
		this.tunnelInit = dojo.lang.hitch(this.currentTransport, "tunnelInit");
93
		this.tunnelCollapse = dojo.lang.hitch(this.currentTransport, "tunnelCollapse");
94
		this.initialized = true;
95
		this.currentTransport.startup(data);
96
		while (this.backlog.length != 0) {
97
			var cur = this.backlog.shift();
98
			var fn = cur.shift();
99
			this[fn].apply(this, cur);
100
		}
101
	};
102
	this._getRandStr = function () {
103
		return Math.random().toString().substring(2, 10);
104
	};
105
	this.deliver = function (messages) {
106
		dojo.lang.forEach(messages, this._deliver, this);
107
	};
108
	this._deliver = function (message) {
109
		if (!message["channel"]) {
110
			dojo.debug("cometd error: no channel for message!");
111
			return;
112
		}
113
		if (!this.currentTransport) {
114
			this.backlog.push(["deliver", message]);
115
			return;
116
		}
117
		this.lastMessage = message;
118
		if ((message.channel.length > 5) && (message.channel.substr(0, 5) == "/meta")) {
119
			switch (message.channel) {
120
			  case "/meta/subscribe":
121
				if (!message.successful) {
122
					dojo.debug("cometd subscription error for channel", message.channel, ":", message.error);
123
					return;
124
				}
125
				this.subscribed(message.subscription, message);
126
				break;
127
			  case "/meta/unsubscribe":
128
				if (!message.successful) {
129
					dojo.debug("cometd unsubscription error for channel", message.channel, ":", message.error);
130
					return;
131
				}
132
				this.unsubscribed(message.subscription, message);
133
				break;
134
			}
135
		}
136
		this.currentTransport.deliver(message);
137
		if (message.data) {
138
			var tname = (this.globalTopicChannels[message.channel]) ? message.channel : "/cometd" + message.channel;
139
			dojo.event.topic.publish(tname, message);
140
		}
141
	};
142
	this.disconnect = function () {
143
		if (!this.currentTransport) {
144
			dojo.debug("no current transport to disconnect from");
145
			return;
146
		}
147
		this.currentTransport.disconnect();
148
	};
149
	this.publish = function (channel, data, properties) {
150
		if (!this.currentTransport) {
151
			this.backlog.push(["publish", channel, data, properties]);
152
			return;
153
		}
154
		var message = {data:data, channel:channel};
155
		if (properties) {
156
			dojo.lang.mixin(message, properties);
157
		}
158
		return this.currentTransport.sendMessage(message);
159
	};
160
	this.subscribe = function (channel, useLocalTopics, objOrFunc, funcName) {
161
		if (!this.currentTransport) {
162
			this.backlog.push(["subscribe", channel, useLocalTopics, objOrFunc, funcName]);
163
			return;
164
		}
165
		if (objOrFunc) {
166
			var tname = (useLocalTopics) ? channel : "/cometd" + channel;
167
			if (useLocalTopics) {
168
				this.globalTopicChannels[channel] = true;
169
			}
170
			dojo.event.topic.subscribe(tname, objOrFunc, funcName);
171
		}
172
		return this.currentTransport.sendMessage({channel:"/meta/subscribe", subscription:channel});
173
	};
174
	this.subscribed = function (channel, message) {
175
		dojo.debug(channel);
176
		dojo.debugShallow(message);
177
	};
178
	this.unsubscribe = function (channel, useLocalTopics, objOrFunc, funcName) {
179
		if (!this.currentTransport) {
180
			this.backlog.push(["unsubscribe", channel, useLocalTopics, objOrFunc, funcName]);
181
			return;
182
		}
183
		if (objOrFunc) {
184
			var tname = (useLocalTopics) ? channel : "/cometd" + channel;
185
			dojo.event.topic.unsubscribe(tname, objOrFunc, funcName);
186
		}
187
		return this.currentTransport.sendMessage({channel:"/meta/unsubscribe", subscription:channel});
188
	};
189
	this.unsubscribed = function (channel, message) {
190
		dojo.debug(channel);
191
		dojo.debugShallow(message);
192
	};
193
};
194
cometd.iframeTransport = new function () {
195
	this.connected = false;
196
	this.connectionId = null;
197
	this.rcvNode = null;
198
	this.rcvNodeName = "";
199
	this.phonyForm = null;
200
	this.authToken = null;
201
	this.lastTimestamp = null;
202
	this.lastId = null;
203
	this.backlog = [];
204
	this.check = function (types, version, xdomain) {
205
		return ((!xdomain) && (!dojo.render.html.safari) && (dojo.lang.inArray(types, "iframe")));
206
	};
207
	this.tunnelInit = function () {
208
		this.postToIframe({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"iframe"}])});
209
	};
210
	this.tunnelCollapse = function () {
211
		if (this.connected) {
212
			this.connected = false;
213
			this.postToIframe({message:dojo.json.serialize([{channel:"/meta/reconnect", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
214
		}
215
	};
216
	this.deliver = function (message) {
217
		if (message["timestamp"]) {
218
			this.lastTimestamp = message.timestamp;
219
		}
220
		if (message["id"]) {
221
			this.lastId = message.id;
222
		}
223
		if ((message.channel.length > 5) && (message.channel.substr(0, 5) == "/meta")) {
224
			switch (message.channel) {
225
			  case "/meta/connect":
226
				if (!message.successful) {
227
					dojo.debug("cometd connection error:", message.error);
228
					return;
229
				}
230
				this.connectionId = message.connectionId;
231
				this.connected = true;
232
				this.processBacklog();
233
				break;
234
			  case "/meta/reconnect":
235
				if (!message.successful) {
236
					dojo.debug("cometd reconnection error:", message.error);
237
					return;
238
				}
239
				this.connected = true;
240
				break;
241
			  case "/meta/subscribe":
242
				if (!message.successful) {
243
					dojo.debug("cometd subscription error for channel", message.channel, ":", message.error);
244
					return;
245
				}
246
				dojo.debug(message.channel);
247
				break;
248
			}
249
		}
250
	};
251
	this.widenDomain = function (domainStr) {
252
		var cd = domainStr || document.domain;
253
		if (cd.indexOf(".") == -1) {
254
			return;
255
		}
256
		var dps = cd.split(".");
257
		if (dps.length <= 2) {
258
			return;
259
		}
260
		dps = dps.slice(dps.length - 2);
261
		document.domain = dps.join(".");
262
		return document.domain;
263
	};
264
	this.postToIframe = function (content, url) {
265
		if (!this.phonyForm) {
266
			if (dojo.render.html.ie) {
267
				this.phonyForm = document.createElement("<form enctype='application/x-www-form-urlencoded' method='POST' style='display: none;'>");
268
				dojo.body().appendChild(this.phonyForm);
269
			} else {
270
				this.phonyForm = document.createElement("form");
271
				this.phonyForm.style.display = "none";
272
				dojo.body().appendChild(this.phonyForm);
273
				this.phonyForm.enctype = "application/x-www-form-urlencoded";
274
				this.phonyForm.method = "POST";
275
			}
276
		}
277
		this.phonyForm.action = url || cometd.url;
278
		this.phonyForm.target = this.rcvNodeName;
279
		this.phonyForm.setAttribute("target", this.rcvNodeName);
280
		while (this.phonyForm.firstChild) {
281
			this.phonyForm.removeChild(this.phonyForm.firstChild);
282
		}
283
		for (var x in content) {
284
			var tn;
285
			if (dojo.render.html.ie) {
286
				tn = document.createElement("<input type='hidden' name='" + x + "' value='" + content[x] + "'>");
287
				this.phonyForm.appendChild(tn);
288
			} else {
289
				tn = document.createElement("input");
290
				this.phonyForm.appendChild(tn);
291
				tn.type = "hidden";
292
				tn.name = x;
293
				tn.value = content[x];
294
			}
295
		}
296
		this.phonyForm.submit();
297
	};
298
	this.processBacklog = function () {
299
		while (this.backlog.length > 0) {
300
			this.sendMessage(this.backlog.shift(), true);
301
		}
302
	};
303
	this.sendMessage = function (message, bypassBacklog) {
304
		if ((bypassBacklog) || (this.connected)) {
305
			message.connectionId = this.connectionId;
306
			message.clientId = cometd.clientId;
307
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"POST", mimetype:"text/json", content:{message:dojo.json.serialize([message])}};
308
			return dojo.io.bind(bindArgs);
309
		} else {
310
			this.backlog.push(message);
311
		}
312
	};
313
	this.startup = function (handshakeData) {
314
		dojo.debug("startup!");
315
		dojo.debug(dojo.json.serialize(handshakeData));
316
		if (this.connected) {
317
			return;
318
		}
319
		this.rcvNodeName = "cometdRcv_" + cometd._getRandStr();
320
		var initUrl = cometd.url + "/?tunnelInit=iframe";
321
		if (false && dojo.render.html.ie) {
322
			this.rcvNode = new ActiveXObject("htmlfile");
323
			this.rcvNode.open();
324
			this.rcvNode.write("<html>");
325
			this.rcvNode.write("<script>document.domain = '" + document.domain + "'");
326
			this.rcvNode.write("</html>");
327
			this.rcvNode.close();
328
			var ifrDiv = this.rcvNode.createElement("div");
329
			this.rcvNode.appendChild(ifrDiv);
330
			this.rcvNode.parentWindow.dojo = dojo;
331
			ifrDiv.innerHTML = "<iframe src='" + initUrl + "'></iframe>";
332
		} else {
333
			this.rcvNode = dojo.io.createIFrame(this.rcvNodeName, "", initUrl);
334
		}
335
	};
336
};
337
cometd.mimeReplaceTransport = new function () {
338
	this.connected = false;
339
	this.connectionId = null;
340
	this.xhr = null;
341
	this.authToken = null;
342
	this.lastTimestamp = null;
343
	this.lastId = null;
344
	this.backlog = [];
345
	this.check = function (types, version, xdomain) {
346
		return ((!xdomain) && (dojo.render.html.mozilla) && (dojo.lang.inArray(types, "mime-message-block")));
347
	};
348
	this.tunnelInit = function () {
349
		if (this.connected) {
350
			return;
351
		}
352
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"mime-message-block"}])});
353
		this.connected = true;
354
	};
355
	this.tunnelCollapse = function () {
356
		if (this.connected) {
357
			this.connected = false;
358
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
359
		}
360
	};
361
	this.deliver = cometd.iframeTransport.deliver;
362
	this.handleOnLoad = function (resp) {
363
		cometd.deliver(dojo.json.evalJson(this.xhr.responseText));
364
	};
365
	this.openTunnelWith = function (content, url) {
366
		this.xhr = dojo.hostenv.getXmlhttpObject();
367
		this.xhr.multipart = true;
368
		if (dojo.render.html.mozilla) {
369
			this.xhr.addEventListener("load", dojo.lang.hitch(this, "handleOnLoad"), false);
370
		} else {
371
			if (dojo.render.html.safari) {
372
				dojo.debug("Webkit is broken with multipart responses over XHR = (");
373
				this.xhr.onreadystatechange = dojo.lang.hitch(this, "handleOnLoad");
374
			} else {
375
				this.xhr.onload = dojo.lang.hitch(this, "handleOnLoad");
376
			}
377
		}
378
		this.xhr.open("POST", (url || cometd.url), true);
379
		this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
380
		dojo.debug(dojo.json.serialize(content));
381
		this.xhr.send(dojo.io.argsFromMap(content, "utf8"));
382
	};
383
	this.processBacklog = function () {
384
		while (this.backlog.length > 0) {
385
			this.sendMessage(this.backlog.shift(), true);
386
		}
387
	};
388
	this.sendMessage = function (message, bypassBacklog) {
389
		if ((bypassBacklog) || (this.connected)) {
390
			message.connectionId = this.connectionId;
391
			message.clientId = cometd.clientId;
392
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"POST", mimetype:"text/json", content:{message:dojo.json.serialize([message])}};
393
			return dojo.io.bind(bindArgs);
394
		} else {
395
			this.backlog.push(message);
396
		}
397
	};
398
	this.startup = function (handshakeData) {
399
		dojo.debugShallow(handshakeData);
400
		if (this.connected) {
401
			return;
402
		}
403
		this.tunnelInit();
404
	};
405
};
406
cometd.longPollTransport = new function () {
407
	this.connected = false;
408
	this.connectionId = null;
409
	this.authToken = null;
410
	this.lastTimestamp = null;
411
	this.lastId = null;
412
	this.backlog = [];
413
	this.check = function (types, version, xdomain) {
414
		return ((!xdomain) && (dojo.lang.inArray(types, "long-polling")));
415
	};
416
	this.tunnelInit = function () {
417
		if (this.connected) {
418
			return;
419
		}
420
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"long-polling"}])});
421
		this.connected = true;
422
	};
423
	this.tunnelCollapse = function () {
424
		if (!this.connected) {
425
			this.connected = false;
426
			dojo.debug("clientId:", cometd.clientId);
427
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", connectionType:"long-polling", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
428
		}
429
	};
430
	this.deliver = cometd.iframeTransport.deliver;
431
	this.openTunnelWith = function (content, url) {
432
		dojo.io.bind({url:(url || cometd.url), method:"post", content:content, mimetype:"text/json", load:dojo.lang.hitch(this, function (type, data, evt, args) {
433
			cometd.deliver(data);
434
			this.connected = false;
435
			this.tunnelCollapse();
436
		}), error:function () {
437
			dojo.debug("tunnel opening failed");
438
		}});
439
		this.connected = true;
440
	};
441
	this.processBacklog = function () {
442
		while (this.backlog.length > 0) {
443
			this.sendMessage(this.backlog.shift(), true);
444
		}
445
	};
446
	this.sendMessage = function (message, bypassBacklog) {
447
		if ((bypassBacklog) || (this.connected)) {
448
			message.connectionId = this.connectionId;
449
			message.clientId = cometd.clientId;
450
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"post", mimetype:"text/json", content:{message:dojo.json.serialize([message])}, load:dojo.lang.hitch(this, function (type, data, evt, args) {
451
				cometd.deliver(data);
452
			})};
453
			return dojo.io.bind(bindArgs);
454
		} else {
455
			this.backlog.push(message);
456
		}
457
	};
458
	this.startup = function (handshakeData) {
459
		if (this.connected) {
460
			return;
461
		}
462
		this.tunnelInit();
463
	};
464
};
465
cometd.callbackPollTransport = new function () {
466
	this.connected = false;
467
	this.connectionId = null;
468
	this.authToken = null;
469
	this.lastTimestamp = null;
470
	this.lastId = null;
471
	this.backlog = [];
472
	this.check = function (types, version, xdomain) {
473
		return dojo.lang.inArray(types, "callback-polling");
474
	};
475
	this.tunnelInit = function () {
476
		if (this.connected) {
477
			return;
478
		}
479
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"callback-polling"}])});
480
		this.connected = true;
481
	};
482
	this.tunnelCollapse = function () {
483
		if (!this.connected) {
484
			this.connected = false;
485
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", connectionType:"long-polling", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
486
		}
487
	};
488
	this.deliver = cometd.iframeTransport.deliver;
489
	this.openTunnelWith = function (content, url) {
490
		var req = dojo.io.bind({url:(url || cometd.url), content:content, mimetype:"text/json", transport:"ScriptSrcTransport", jsonParamName:"jsonp", load:dojo.lang.hitch(this, function (type, data, evt, args) {
491
			cometd.deliver(data);
492
			this.connected = false;
493
			this.tunnelCollapse();
494
		}), error:function () {
495
			dojo.debug("tunnel opening failed");
496
		}});
497
		this.connected = true;
498
	};
499
	this.processBacklog = function () {
500
		while (this.backlog.length > 0) {
501
			this.sendMessage(this.backlog.shift(), true);
502
		}
503
	};
504
	this.sendMessage = function (message, bypassBacklog) {
505
		if ((bypassBacklog) || (this.connected)) {
506
			message.connectionId = this.connectionId;
507
			message.clientId = cometd.clientId;
508
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], mimetype:"text/json", transport:"ScriptSrcTransport", jsonParamName:"jsonp", content:{message:dojo.json.serialize([message])}, load:dojo.lang.hitch(this, function (type, data, evt, args) {
509
				cometd.deliver(data);
510
			}), };
511
			return dojo.io.bind(bindArgs);
512
		} else {
513
			this.backlog.push(message);
514
		}
515
	};
516
	this.startup = function (handshakeData) {
517
		if (this.connected) {
518
			return;
519
		}
520
		this.tunnelInit();
521
	};
522
};
523
cometd.connectionTypes.register("mime-message-block", cometd.mimeReplaceTransport.check, cometd.mimeReplaceTransport);
524
cometd.connectionTypes.register("long-polling", cometd.longPollTransport.check, cometd.longPollTransport);
525
cometd.connectionTypes.register("callback-polling", cometd.callbackPollTransport.check, cometd.callbackPollTransport);
526
cometd.connectionTypes.register("iframe", cometd.iframeTransport.check, cometd.iframeTransport);
527
dojo.io.cometd = cometd;
528