Subversion Repositories Applications.papyrus

Rev

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