Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / xmpp / TransportSession.js @ 10

History | View | Annotate | Download (8.46 KB)

1
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6

    
7

    
8
if(!dojo._hasResource["dojox.xmpp.TransportSession"]){
9
dojo._hasResource["dojox.xmpp.TransportSession"]=true;
10
dojo.provide("dojox.xmpp.TransportSession");
11
dojo.require("dojox.xmpp.bosh");
12
dojo.require("dojox.xmpp.util");
13
dojo.require("dojox.data.dom");
14
dojox.xmpp.TransportSession=function(_1){
15
this.sendTimeout=(this.wait+20)*1000;
16
if(_1&&dojo.isObject(_1)){
17
dojo.mixin(this,_1);
18
if(this.useScriptSrcTransport){
19
this.transportIframes=[];
20
}
21
}
22
};
23
dojo.extend(dojox.xmpp.TransportSession,{rid:0,hold:1,polling:1000,secure:false,wait:60,lang:"en",submitContentType:"text/xml; charset=utf=8",serviceUrl:"/httpbind",defaultResource:"dojoIm",domain:"imserver.com",sendTimeout:0,useScriptSrcTransport:false,keepAliveTimer:null,state:"NotReady",transmitState:"Idle",protocolPacketQueue:[],outboundQueue:[],outboundRequests:{},inboundQueue:[],deferredRequests:{},matchTypeIdAttribute:{},open:function(){
24
this.status="notReady";
25
this.rid=Math.round(Math.random()*1000000000);
26
this.protocolPacketQueue=[];
27
this.outboundQueue=[];
28
this.outboundRequests={};
29
this.inboundQueue=[];
30
this.deferredRequests={};
31
this.matchTypeIdAttribute={};
32
this.keepAliveTimer=setTimeout(dojo.hitch(this,"_keepAlive"),10000);
33
if(this.useScriptSrcTransport){
34
dojox.xmpp.bosh.initialize({iframes:this.hold+1,load:dojo.hitch(this,function(){
35
this._sendLogin();
36
})});
37
}else{
38
this._sendLogin();
39
}
40
},_sendLogin:function(){
41
var _2=this.rid++;
42
var _3={content:this.submitContentType,hold:this.hold,rid:_2,to:this.domain,secure:this.secure,wait:this.wait,"xml:lang":this.lang,"xmpp:version":"1.0",xmlns:dojox.xmpp.xmpp.BODY_NS,"xmlns:xmpp":"urn:xmpp:xbosh"};
43
var _4=dojox.xmpp.util.createElement("body",_3,true);
44
this.addToOutboundQueue(_4,_2);
45
},_sendRestart:function(){
46
var _5=this.rid++;
47
var _6={rid:_5,sid:this.sid,to:this.domain,"xmpp:restart":"true","xml:lang":this.lang,xmlns:dojox.xmpp.xmpp.BODY_NS,"xmlns:xmpp":"urn:xmpp:xbosh"};
48
var _7=dojox.xmpp.util.createElement("body",_6,true);
49
this.addToOutboundQueue(_7,_5);
50
},processScriptSrc:function(_8,_9){
51
var _a=dojox.xml.parser.parse(_8,"text/xml");
52
if(_a){
53
this.processDocument(_a,_9);
54
}else{
55
}
56
},_keepAlive:function(){
57
if(this.state=="wait"||this.isTerminated()){
58
return;
59
}
60
this._dispatchPacket();
61
this.keepAliveTimer=setTimeout(dojo.hitch(this,"_keepAlive"),10000);
62
},close:function(_b){
63
var _c=this.rid++;
64
var _d={sid:this.sid,rid:_c,type:"terminate"};
65
var _e=null;
66
if(_b){
67
_e=new dojox.string.Builder(dojox.xmpp.util.createElement("body",_d,false));
68
_e.append(_b);
69
_e.append("</body>");
70
}else{
71
_e=new dojox.string.Builder(dojox.xmpp.util.createElement("body",_d,false));
72
}
73
this.addToOutboundQueue(_e.toString(),_c);
74
this.state=="Terminate";
75
},dispatchPacket:function(_f,_10,_11,_12){
76
if(_f){
77
this.protocolPacketQueue.push(_f);
78
}
79
var def=new dojo.Deferred();
80
if(_10&&_11){
81
def.protocolMatchType=_10;
82
def.matchId=_11;
83
def.matchProperty=_12||"id";
84
if(def.matchProperty!="id"){
85
this.matchTypeIdAttribute[_10]=def.matchProperty;
86
}
87
}
88
this.deferredRequests[def.protocolMatchType+"-"+def.matchId]=def;
89
if(!this.dispatchTimer){
90
this.dispatchTimer=setTimeout(dojo.hitch(this,"_dispatchPacket"),600);
91
}
92
return def;
93
},_dispatchPacket:function(){
94
clearTimeout(this.dispatchTimer);
95
delete this.dispatchTimer;
96
if(!this.sid){
97
return;
98
}
99
if(!this.authId){
100
return;
101
}
102
if(this.transmitState!="error"&&(this.protocolPacketQueue.length==0)&&(this.outboundQueue.length>0)){
103
return;
104
}
105
if(this.state=="wait"||this.isTerminated()){
106
return;
107
}
108
var req={sid:this.sid,xmlns:dojox.xmpp.xmpp.BODY_NS};
109
var _13;
110
if(this.protocolPacketQueue.length>0){
111
req.rid=this.rid++;
112
_13=new dojox.string.Builder(dojox.xmpp.util.createElement("body",req,false));
113
_13.append(this.processProtocolPacketQueue());
114
_13.append("</body>");
115
delete this.lastPollTime;
116
}else{
117
if(this.lastPollTime){
118
var now=new Date().getTime();
119
if(now-this.lastPollTime<this.polling){
120
this.dispatchTimer=setTimeout(dojo.hitch(this,"_dispatchPacket"),this.polling-(now-this.lastPollTime)+10);
121
return;
122
}
123
}
124
req.rid=this.rid++;
125
this.lastPollTime=new Date().getTime();
126
_13=new dojox.string.Builder(dojox.xmpp.util.createElement("body",req,true));
127
}
128
this.addToOutboundQueue(_13.toString(),req.rid);
129
},redispatchPacket:function(rid){
130
var env=this.outboundRequests[rid];
131
this.sendXml(env,rid);
132
},addToOutboundQueue:function(msg,rid){
133
this.outboundQueue.push({msg:msg,rid:rid});
134
this.outboundRequests[rid]=msg;
135
this.sendXml(msg,rid);
136
},removeFromOutboundQueue:function(rid){
137
for(var i=0;i<this.outboundQueue.length;i++){
138
if(rid==this.outboundQueue[i]["rid"]){
139
this.outboundQueue.splice(i,1);
140
break;
141
}
142
}
143
delete this.outboundRequests[rid];
144
},processProtocolPacketQueue:function(){
145
var _14=new dojox.string.Builder();
146
for(var i=0;i<this.protocolPacketQueue.length;i++){
147
_14.append(this.protocolPacketQueue[i]);
148
}
149
this.protocolPacketQueue=[];
150
return _14.toString();
151
},sendXml:function(_15,rid){
152
if(this.isTerminated()){
153
return false;
154
}
155
this.transmitState="transmitting";
156
var def=null;
157
if(this.useScriptSrcTransport){
158
def=dojox.xmpp.bosh.get({rid:rid,url:this.serviceUrl+"?"+encodeURIComponent(_15),error:dojo.hitch(this,function(res,io){
159
this.setState("Terminate","error");
160
return false;
161
}),timeout:this.sendTimeout});
162
}else{
163
def=dojo.rawXhrPost({contentType:"text/xml",url:this.serviceUrl,postData:_15,handleAs:"xml",error:dojo.hitch(this,function(res,io){
164
return this.processError(io.xhr.responseXML,io.xhr.status,rid);
165
}),timeout:this.sendTimeout});
166
}
167
def.addCallback(this,function(res){
168
return this.processDocument(res,rid);
169
});
170
return def;
171
},processDocument:function(doc,rid){
172
if(this.isTerminated()||!doc.firstChild){
173
return false;
174
}
175
this.transmitState="idle";
176
var _16=doc.firstChild;
177
if(_16.nodeName!="body"){
178
}
179
if(this.outboundQueue.length<1){
180
return false;
181
}
182
var _17=this.outboundQueue[0]["rid"];
183
if(rid==_17){
184
this.removeFromOutboundQueue(rid);
185
this.processResponse(_16,rid);
186
this.processInboundQueue();
187
}else{
188
var gap=rid-_17;
189
if(gap<this.hold+2){
190
this.addToInboundQueue(doc,rid);
191
}else{
192
}
193
}
194
return doc;
195
},processInboundQueue:function(){
196
while(this.inboundQueue.length>0){
197
var _18=this.inboundQueue.shift();
198
this.processDocument(_18["doc"],_18["rid"]);
199
}
200
},addToInboundQueue:function(doc,rid){
201
for(var i=0;i<this.inboundQueue.length;i++){
202
if(rid<this.inboundQueue[i]["rid"]){
203
continue;
204
}
205
this.inboundQueue.splice(i,0,{doc:doc,rid:rid});
206
}
207
},processResponse:function(_19,rid){
208
if(_19.getAttribute("type")=="terminate"){
209
var _1a=_19.firstChild.firstChild;
210
var _1b="";
211
if(_1a.nodeName=="conflict"){
212
_1b="conflict";
213
}
214
this.setState("Terminate",_1b);
215
return;
216
}
217
if((this.state!="Ready")&&(this.state!="Terminate")){
218
var sid=_19.getAttribute("sid");
219
if(sid){
220
this.sid=sid;
221
}else{
222
throw new Error("No sid returned during xmpp session startup");
223
}
224
this.authId=_19.getAttribute("authid");
225
if(this.authId==""){
226
if(this.authRetries--<1){
227
console.error("Unable to obtain Authorization ID");
228
this.terminateSession();
229
}
230
}
231
this.wait=_19.getAttribute("wait");
232
if(_19.getAttribute("polling")){
233
this.polling=parseInt(_19.getAttribute("polling"))*1000;
234
}
235
this.inactivity=_19.getAttribute("inactivity");
236
this.setState("Ready");
237
}
238
dojo.forEach(_19.childNodes,function(_1c){
239
this.processProtocolResponse(_1c,rid);
240
},this);
241
if(this.transmitState=="idle"){
242
this.dispatchPacket();
243
}
244
},processProtocolResponse:function(msg,rid){
245
this.onProcessProtocolResponse(msg);
246
var key=msg.nodeName+"-"+msg.getAttribute("id");
247
var def=this.deferredRequests[key];
248
if(def){
249
def.callback(msg);
250
delete this.deferredRequests[key];
251
}
252
},setState:function(_1d,_1e){
253
if(this.state!=_1d){
254
if(this["on"+_1d]){
255
this["on"+_1d](_1d,this.state,_1e);
256
}
257
this.state=_1d;
258
}
259
},isTerminated:function(){
260
return this.state=="Terminate";
261
},processError:function(err,_1f,rid){
262
if(this.isTerminated()){
263
return false;
264
}
265
if(_1f!=200){
266
if(_1f>=400&&_1f<500){
267
this.setState("Terminate",_20);
268
return false;
269
}else{
270
this.removeFromOutboundQueue(rid);
271
setTimeout(dojo.hitch(this,function(){
272
this.dispatchPacket();
273
}),200);
274
return true;
275
}
276
return false;
277
}
278
if(err&&err.dojoType&&err.dojoType=="timeout"){
279
}
280
this.removeFromOutboundQueue(rid);
281
if(err&&err.firstChild){
282
if(err.firstChild.getAttribute("type")=="terminate"){
283
var _21=err.firstChild.firstChild;
284
var _20="";
285
if(_21&&_21.nodeName=="conflict"){
286
_20="conflict";
287
}
288
this.setState("Terminate",_20);
289
return false;
290
}
291
}
292
this.transmitState="error";
293
setTimeout(dojo.hitch(this,function(){
294
this.dispatchPacket();
295
}),200);
296
return true;
297
},onTerminate:function(_22,_23,_24){
298
},onProcessProtocolResponse:function(msg){
299
},onReady:function(_25,_26){
300
}});
301
}