12 * Internal storage for established contracts and execution responses.
22 initialize : function()
24 // Nothing to do here.
31 /* ------------------------------------- TASK HANDLING ------------------------------------- */
33 * Establish a Task Contract with server.
40 * @param errorCallback
42 getTask : function( id, workspace, task, jobId, data, callback, errorCallback )
44 this.taskContracts[id] = {
45 "Callback" : callback,
46 "ErrorCallback" : errorCallback
56 "url" : 'workspace/contract-task',
57 "data" : [{"Workspace": workspace, "Task": task, "JobId": jobId, "Packet": data}]
59 $.proxy(this._getTaskSuccess, this),
60 $.proxy(this._getTaskError, this)
65 * Retrieve a Task Contract established with server.
67 * @returns object|null
69 taskContract : function( id )
71 return this.taskContracts[id]
72 ? this.taskContracts[id]
76 _getTaskSuccess : function( id, response )
78 response = response[0];
79 if (!this.taskContracts[id])
83 this.taskContracts[id]["Response"] = response;
84 if (response.Status && 'Success' != response.Status)
86 if (this.taskContracts[id]['ErrorCallback'])
88 this.taskContracts[id]['ErrorCallback'](response);
93 this.taskContracts[id]["Hash"] = response.Hash;
94 this.taskContracts[id]["LifeTime"] = response.LifeTime;
95 if (this.taskContracts[id]['Callback'])
97 this.taskContracts[id]['Callback'](response);
102 _getTaskError : function( id, error )
104 this.taskContracts[id]["Response"] = error;
105 if (this.taskContracts[id]['ErrorCallback'])
107 this.taskContracts[id]['ErrorCallback'](error);
112 * Establish multiple Task Contracts with server.
115 getTasks : function( contracts, callback )
117 var id = this.requestId;
120 this.requests.push({});
121 this.requestCallback.push({
124 for (var i in contracts)
126 this.requests[id][i] = contracts[i].id;
127 this.taskContracts[contracts[i].id] = {
128 "Callback" : contracts[i].callback,
129 "ErrorCallback" : contracts[i].errorCallback
131 jobId = (contracts[i].jobId)
134 data = (contracts[i].data)
138 "Workspace": contracts[i].workspace,
139 "Task": contracts[i].task,
146 "url" : 'workspace/contract-task',
149 $.proxy(this._getTasksSuccess, this),
150 $.proxy(this._getTaskError, this)
154 _getTasksSuccess : function( gid, responses )
156 for (var i in responses)
158 response = responses[i];
159 var id = this.requests[gid][i];
160 if (!this.taskContracts[id])
164 this.taskContracts[id]["Response"] = response;
165 if (undefined != response.Status && 'Success' != response.Status)
167 if (this.taskContracts[id]['ErrorCallback'])
169 this.taskContracts[id]['ErrorCallback'](response);
174 this.taskContracts[id]["Hash"] = response.Hash;
175 this.taskContracts[id]["LifeTime"] = response.LifeTime;
176 if (this.taskContracts[id]['Callback'])
178 this.taskContracts[id]['Callback'](response);
182 if (undefined != this.requestCallback[gid])
184 this.requestCallback[gid].callback();
186 delete this.requests[gid];
187 delete this.requestCallback[gid];
191 * Execute a Task Contract against server.
196 * @param errorCallback
197 * @returns boolean|void
199 execTask: function( id, data, options, callback, errorCallback, method, direct )
201 if (!this.taskContracts[id])
203 console.log('! No contract for: ' + id);
206 this.taskContracts[id]["Callback"] = callback;
207 this.taskContracts[id]["ErrorCallback"] = errorCallback;
220 "url" : 'workspace/execute-task',
221 "data" : [{"Contract" : this.taskContracts[id]["Hash"], "Packet": data, "Options": options}]
225 packet.direct = true;
229 $.proxy(this._execTaskSuccess, this),
230 $.proxy(this._execTaskError, this)
234 _execTaskSuccess : function( id, response )
236 response = response[0];
237 this.taskExecutions[id] = response;
238 if (!this.taskContracts[id])
240 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
242 console.log(this.taskContracts);
244 if (response.Status && 'Success' != response.Status)
246 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
248 this.taskContracts[id]['ErrorCallback'](response);
253 var callback = this.taskContracts[id]['Callback']
254 ? this.taskContracts[id]['Callback']
256 if (this.taskContracts[id] && this.taskContracts[id]['LifeTime'] != 'Recurring')
258 delete this.taskContracts[id];
267 _execTaskError : function( id, error )
269 this.taskExecutions[id] = error;
270 if (this.taskContracts[id]['ErrorCallback'])
272 this.taskContracts[id]['ErrorCallback'](error);
279 * Execute a Task Contract against server.
284 * @param errorCallback
285 * @returns boolean|void
287 execTasks: function( contracts, callback )
288 //id, data, options, callback, errorCallback, method, direct )
290 console.log('executetasks',data);
291 var gid = this.requestId;
293 this.requests.push({});
294 this.requestCallback.push({
298 for (var i in contracts)
300 this.requests[gid][i] = contracts[i].id;
301 this.taskContracts[contracts[i].id]["Callback"] = contracts[i].callback;
302 this.taskContracts[contracts[i].id]["ErrorCallback"] = contracts[i].errorCallback;
304 "Contract" : this.taskContracts[contracts[i].id]["Hash"],
305 "Packet": contracts[i].data,
306 "Options": contracts[i].options
312 "url" : 'workspace/execute-task',
315 $.proxy(this._execTasksSuccess, this),
316 $.proxy(this._execTaskError, this)
320 _execTasksSuccess : function( gid, responses )
322 for (var i in responses)
324 response = responses[i];
325 var id = this.requests[gid][i];
326 this.taskExecutions[id] = response;
327 if (!this.taskContracts[id])
329 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
331 console.log(this.taskContracts);
333 if (response.Status && 'Success' != response.Status)
335 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
337 this.taskContracts[id]['ErrorCallback'](response);
342 if (this.taskContracts[id]['Callback'])
344 this.taskContracts[id]['Callback'](response);
346 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
348 delete this.taskContracts[id];
352 if (undefined != this.requestCallback[gid])
354 this.requestCallback[gid].callback(responses);
356 delete this.requests[gid];
357 delete this.requestCallback[gid];
361 * Execute a Task Contract against server.
366 * @param errorCallback
367 * @returns boolean|void
369 execTaskMulti: function( id, data, options, callback, errorCallback, method, direct )
371 if (!this.taskContracts[id])
373 console.log('! No contract for: ' + id);
376 this.taskContracts[id]["Callback"] = callback;
377 this.taskContracts[id]["ErrorCallback"] = errorCallback;
390 for (var i = 0; i < data.length; i++)
393 "Contract" : this.taskContracts[id]["Hash"],
395 "Options": options[i] ? options[i] : {}
400 "url" : 'workspace/execute-task',
405 packet.direct = true;
409 $.proxy(this._execTaskSuccessMulti, this),
410 $.proxy(this._execTaskError, this)
414 _execTaskSuccessMulti : function( id, responses )
417 this.taskExecutions[id] = [];
418 for (var i = 0; i < responses.length; i++)
420 response = responses[i];
421 this.taskExecutions[id].push(response);
422 if (!this.taskContracts[id])
424 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
426 console.log(this.taskContracts);
428 if (response.Status && 'Success' != response.Status)
435 if (this.taskContracts[id]['ErrorCallback'])
437 this.taskContracts[id]['ErrorCallback'](response);
442 if (this.taskContracts[id]['Callback'])
444 this.taskContracts[id]['Callback'](this.taskExecutions[id]);
446 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
448 delete this.taskContracts[id];
453 _execTaskError : function( id, error )
455 this.taskExecutions[id] = error;
456 if (this.taskContracts[id]['ErrorCallback'])
458 this.taskContracts[id]['ErrorCallback'](error);
471 /* ------------------------------------- ROUTE HANDLING ------------------------------------- */
473 * Directly route an item to a new sate.
480 * @param errorCallback
482 directRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
484 this.routeContracts[id] = {
485 "Callback" : callback,
486 "ErrorCallback" : errorCallback
496 "url" : 'workspace/direct-route',
497 "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
499 $.proxy(this._directRouteSuccess, this),
500 $.proxy(this._directRouteError, this)
504 _directRouteSuccess : function( id, response )
506 response = response[0];
507 this.routeExecutions[id] = response;
508 if (response.Status && 'Success' != response.Status)
510 if (this.routeContracts[id]['ErrorCallback'])
512 this.routeContracts[id]['ErrorCallback'](response);
517 if (this.routeContracts[id]['Callback'])
519 this.routeContracts[id]['Callback'](response);
521 delete this.routeContracts[id];
525 _directRouteError : function( id, error )
527 this.routeExecutions[id] = error;
528 if (this.routeContracts[id]['ErrorCallback'])
530 this.routeContracts[id]['ErrorCallback'](error);
535 * Establish a Route Contract with server.
542 * @param errorCallback
544 getRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
546 this.routeContracts[id] = {
547 "Callback" : callback,
548 "ErrorCallback" : errorCallback
558 "url" : 'workspace/contract-route',
559 "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
561 $.proxy(this._getRouteSuccess, this),
562 $.proxy(this._getRouteError, this)
567 * Retrieve a Route Contract established with server.
569 * @returns object|null
571 routeContract : function( id )
573 return this.routeContracts[id]
574 ? this.routeContracts[id]
578 _getRouteSuccess : function( id, response )
580 response = response[0];
581 this.routeContracts[id]["Response"] = response;
582 if (response.Status && 'Success' != response.Status)
584 if (this.routeContracts[id]['ErrorCallback'])
586 this.routeContracts[id]['ErrorCallback'](response);
591 this.routeContracts[id]["Hash"] = response.Hash;
592 this.routeContracts[id]["LifeTime"] = response.LifeTime;
593 if (this.routeContracts[id]['Callback'])
595 this.routeContracts[id]['Callback'](response);
600 _getRouteError : function( id, error )
602 this.routeContracts[id]["Response"] = error;
603 if (this.routeContracts[id]['ErrorCallback'])
605 this.routeContracts[id]['ErrorCallback'](error);
610 * Execute a Route Contract against server.
615 * @param errorCallback
616 * @returns boolean|void
618 execRoute: function( id, data, options, callback, errorCallback )
620 if (!this.routeContracts[id])
624 this.routeContracts[id]["Callback"] = callback;
625 this.routeContracts[id]["ErrorCallback"] = errorCallback;
634 "url" : 'workspace/execute-route',
635 "data" : [{"Contract" : this.routeContracts[id]["Hash"], "Packet": data, "Options": options}]
637 $.proxy(this._execRouteSuccess, this),
638 $.proxy(this._execRouteError, this)
642 _execRouteSuccess : function( id, response )
644 response = response[0];
645 this.routeExecutions[id] = response;
646 if (response.Status && 'Success' != response.Status)
648 if (this.routeContracts[id]['ErrorCallback'])
650 this.routeContracts[id]['ErrorCallback'](response);
655 if (this.routeContracts[id]['Callback'])
657 this.routeContracts[id]['Callback'](response);
659 if (this.routeContracts[id]['LifeTime'] != 'Recurring')
661 delete this.routeContracts[id];
666 _execRouteError : function( id, error )
668 this.routeExecutions[id] = error;
669 if (this.routeContracts[id]['ErrorCallback'])
671 this.routeContracts[id]['ErrorCallback'](error);