annotate src/implab/messaging/Session.js @ 8:f0035923ff3e

добавлена библиотека для работы с openlayers 3+
author cin
date Mon, 21 Aug 2017 17:47:00 +0300
parents 7d7059d2a810
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 "dojo/_base/declare",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 "dojo/_base/lang",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 "dojo/request",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 "./Destination",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 "dojo/Evented",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 "dojo/Deferred",
2
7d7059d2a810 Подправлены пути в пакетах
egor
parents: 0
diff changeset
9 "../log/_LogMixin" ],
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 function(declare, lang, request, Destination, Evented, Deferred, _LogMixin) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 var cls = declare(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 [ Evented, _LogMixin ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 _id : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 _baseUrl : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 _destinations : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 _timeout : 100000,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 _clients : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 _started : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 _starting : false,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 constructor : function(baseUrl, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 if (!baseUrl)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 throw new Error("baseUrl is required");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 options = options || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 this._baseUrl = baseUrl.replace(/\/*$/, "");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 this._destinations = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 this._pending = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 this._clients = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 if (options.timeout)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 this._timeout = options.timeout;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 this._started = new Deferred();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 start : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 if (this._starting)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 return this._started;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 this._starting = true;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 me.log("START");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 request(this._baseUrl, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 method : "POST",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 handleAs : "json"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 }).then(function(result) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 me._id = result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 me._emitConnected();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 me._poll();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 me._started.resolve(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 }, function(error) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 me._emitError(error);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 me._started.reject(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 return me._started.promise;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 createClient : function(options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 if (!options || !options.destination || !options.mode)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 throw new Error("Invalid argument");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 return me._started
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 .then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 var url = me._makeUrl(me._id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 me.log(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 "CREATE mode=${0}, destination=${1}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 options.mode,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 options.destination);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 return request(url, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 method : "POST",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 data : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 mode : options.mode,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 destination : options.destination
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 handleAs : 'json'
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 })
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 .then(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 me
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 .log(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 "CLIENT id=${0}, mode=${1}, destination=${2}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 id,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 options.mode,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 options.destination);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 me._clients[id] = options.client
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 ? options.client
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93 : function(msg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 me
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 .warn(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 "The client id=${0}, mode=${1}, destination=${2} isn't accepting mesages",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 id,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 options.mode,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 options.destination);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 return id;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 deleteClient : function(options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 if (!options || !options.clientId)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 throw new Error("Invalid argument");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 var me = this, id = options.clientId;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 return me._started.then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 var url = me._makeUrl(me._id, options.clientId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 me.log("DELETE CLIENT ${0}", options.clientId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 return request(url, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 method : "DELETE",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 handleAs : 'json'
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 }).then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 me.log("CLIENT DELETED ${0}", options.clientId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 me._clients[id] = undefined;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 _poll : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 var me = this, url = this._makeUrl(this._id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 me.log("POLL timeout=${0}", me._timeout);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 request(url, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 method : "GET",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 handleAs : "json",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 query : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 timeout : me._timeout
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 }).then(function(response) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 me._handlePoll(response);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 me._poll();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140 }, function(err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 me.error("POLL faield with ${0}", err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 me._emitError(err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 _handlePoll : function(response) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 if (!response) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 this.log("POLL response undefined, looks like a bug");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 return;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 if (!response.results || !response.results.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 this.log("POLL response is empty");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 return;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 var results = response.results;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 this.log("POLL got ${0} results", results.length);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 for (var i = 0; i < results.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 var result = results[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 var client = this._clients[result.clientId];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 if (!client) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 // TODO this could happen due to client isn't
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 // registered yet
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 this.error("Unknown client ${0}", result.clientId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 continue;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168 client.call(this, result);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172 _emitError : function(err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 this.emit("error", err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 _emitConnected : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 me.log("CONNECTED");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 me.emit("connected");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
180 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 _makeUrl : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 var parts = [ this._baseUrl ];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
184 for (var i = 0; i < arguments.length; i++)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
185 parts.push(arguments[i].replace(/\/*$/, ""));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186 return parts.join('/');
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
187 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 queue : function(name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 return this._getDestination("queue://" + name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 topic : function(name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 return this._getDestination("topic://" + name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 _getDestination : function(uri) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 if (uri in this._destinations)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 return this._destinations[uri];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 var dest = new Destination(this, uri);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 this._destinations[uri] = dest;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 return dest;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 toString : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207 return [ "[", "SESSION ", this._id, "]" ].join(" ");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
208 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
209 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 cls.connect = function(url, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 var session = new cls(url, options);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 return session.start();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
216 return cls;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
217 });