aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/imc.js
blob: 103e4cb8a80c5a3818cc6515d77b52601ed88e9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
'use strict'

var __extend__ = function(child,parent){
    child.prototype.__super__ = parent;
};

var imc = new function(){
    var that = this;

    that.Connection = function(link){
        var that = this;

        that.link_linkmap = {};
        that.close_callback = [];
        that.link = link;

        that.send_msg = function(data){};
        that.send_file = function(filekey,blob,callback){};
        that.start_recv = function(recv_callback){};

        that.close = function(){
            var i;

            for(i = 0;i < that.close_callback.length;i++){
                that.close_callback[i](that);
            }
        };
    };

    that.Proxy = function(self_link,auth,conn_link){
        var MSGTYPE_CALL = 'call';
        var MSGTYPE_RET = 'ret';
        var MSGTYPE_SENDFILE = 'sendfile';
        var MSGTYPE_ABORTFILE = 'abortfile';

        var that = this;
        var caller_retid_count = 0;
        var conn_linkmap = {};
        var conn_retidmap = {};
        var callpath_root = {'child':{},'name':{},'filt':[]};
        var info_filekeymap = {};
        var conn_filekeymap = {};

        function walk_path(path,create){
            var i;
            var parts;
            var part;
            var cnode;
            var nnode;
            
            parts = path.split('/');
            parts.pop();
            cnode = callpath_root;
            for(i = 0;i < parts.length;i++){
                part = parts[i];
                if(part in cnode.child){
                    cnode = cnode.child[part];
                }else{
                    break;
                }
            }

            if(create == true){
                for(;i < parts.length;i++){
                    part = parts[i];
                    nnode = {'child':{},'name':{},'filt':[]};
                    cnode.child[part] = nnode;
                    cnode = nnode;
                }
            }else if(i < parts.length){
                return undefined;
            }

            return cnode;
        }

        function route_call(caller_link,caller_retid,idendesc,dst,func_name,timeout,callback,param){
            var i;
            var j;
            var part;
            var dst_link;
            var dst_path;
            var caller_link;
            var cnode;
            var dpart;
            var func;

            function _add_wait_caller(conn_link){
                conn_retidmap[conn_link][caller_retid] = {
                    'timeout':timeout,
                    'callback':callback
                }   
            }

            part = dst.split('/');
            dst_link = part.slice(0,3).join('/') + '/'
            dst_path = part.slice(3,-1);

            if(dst_link == self_link){
                cnode = callpath_root;
                dpart = dst_path.slice(0);
                for(i = 0;i < cnode.filt.length;i++){
                    cnode.filt[i](dpart,func_name);
                }

                for(i = 0;i < dst_path.length;i++){
                    if((cnode = cnode.child[dst_path[i]]) == undefined){
                        cnode = null;
                        break;
                    }

                    dpart.shift();
                    for(j = 0;j < cnode.filt.length;j++){
                        cnode.filt[j](dpart,func_name);
                    }
                }

                if(cnode != null && (func = cnode.name[func_name]) != undefined){
                    _add_wait_caller(self_link);

                    func.apply(undefined,[function(data){
                        if(self_link in conn_retidmap && 
                            caller_retid in conn_retidmap[self_link]){

                            delete conn_retidmap[self_link][caller_retid];

                            if(callback != null && callback != undefined){
                                callback({'stat':true,'data':data}); 
                            }
                        }   
                    }].concat(param));
                }else{
                    if(callback != null && callback != undefined){
                        callback({'stat':false,'data':'Enoexist'}); 
                    }
                }   
            }else{
                that.request_conn(dst_link,function(conn){
                    if(caller_link == self_link){
                        _add_wait_caller(conn.link);
                    }

                    send_msg_call(conn,caller_link,caller_retid,idendesc,dst,
                                  func_name,timeout,param);
                });
            }
        }
        function ret_call(conn_link,caller_link,caller_retid,result){
            var wait;
            
            if(conn_link in conn_retidmap &&
               caller_retid in conn_retidmap[conn_link]){

                wait = conn_retidmap[conn_link][caller_retid];
                delete conn_retidmap[conn_link][caller_retid];
            }else{
                return;
            }

            if(caller_link == self_link){
                wait.callback(result);
            }else{
                request_conn(caller_link,function(conn){
                    send_msg_ret(conn,caller_link,caller_retid,result);
                });
            }
        }
        function route_sendfile(out_conn,src_link,filekey,filesize){
            var info;

            function _send_cb(err){
                if(del_wait_filekey(out_conn,filekey)){
                    return;
                }

                if(err != undefined){
                    out_conn.abort_file(filekey,err);
                    send_msg_abortfile(out_conn,filekey,err);
                }

                ret_sendfile(filekey,err);
            }

            if(src_link != self_link){
                //TODO
                return;
            }
            
            if((info = info_filekeymap[filekey]) == undefined){
                return;
            }

            info.callback = _send_cb;
            add_wait_filekey(out_conn.link,filekey,info.blob.size,_send_cb);
            out_conn.send_file(filekey,info.blob,_send_cb);
        }
        function ret_sendfile(filekey,err){
            var info;
            
            if((info = info_filekeymap[filekey]) == undefined){
                return false;
            }

            delete info_filekeymap[filekey];

            if(err == undefined){
                info.result_callback('Success');
            }else{
                info.result_callback(err);
            }

            return true;
        }
        function add_wait_filekey(conn_link,filekey,filesize,callback){
            conn_filekeymap[conn_link][filekey] = {
                'callback':callback
            };
        }
        function del_wait_filekey(conn_link,filekey){
            if(conn_link in conn_filekeymap &&
               filekey in conn_filekeymap[conn_link]){

                delete conn_filekeymap[conn_link][filekey];

                return true;
            }else{
                return false;
            }
        }

        function recv_dispatch(conn,data){
            var msgo = JSON.parse(data);

            if(msgo.type == MSGTYPE_CALL){
                recv_msg_call(conn,msgo);
            }else if(msgo.type == MSGTYPE_RET){
                recv_msg_ret(conn,msgo);
            }else if(msgo.type == MSGTYPE_SENDFILE){
                recv_msg_sendfile(conn,msgo);
            }else if(msgo.type == MSGTYPE_ABORTFILE){
                recv_msg_abortfile(conn,msgo);
            }
        }

        function send_msg_call(conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param){
            var msg = {
                'type':MSGTYPE_CALL,
                'caller_link':caller_link,
                'caller_retid':caller_retid,
                'idendesc':idendesc,
                'dst':dst,
                'func_name':func_name,
                'timeout':timeout,
                'param':param
            };

            conn.send_msg(JSON.stringify(msg));
        }
        function recv_msg_call(conn,msg){
            var caller_link = msg.caller_link
            var caller_retid = msg.caller_retid;
            var timeout = msg.timeout;
            var idendesc = msg.idendesc;
            var dst = msg.dst;
            var func_name = msg.func_name;
            var param = msg.param;

            route_call(caller_link,caller_retid,idendesc,dst,func_name,timeout,function(result){
                that.request_conn(caller_retid,function(conn){
                    send_msg_ret(conn,caller_link,caller_retid,result);
                });
            },param);
        }

        function send_msg_ret(conn,caller_link,caller_retid,result){
            var msg = {
                'type':MSGTYPE_RET,
                'caller_link':caller_link,
                'caller_retid':caller_retid,
                'result':result
            };

            conn.send_msg(JSON.stringify(msg));
        }
        function recv_msg_ret(conn,msg){
            var caller_link = msg['caller_link'];
            var caller_retid = msg['caller_retid'];
            var result = msg['result'];

            ret_call(conn.link,caller_link,caller_retid,result);
        }

        function recv_msg_sendfile(conn,msg){
            route_sendfile(conn,msg.src_link,msg.filekey,msg.filesize);
        }

        function send_msg_abortfile(conn,filekey,err){
            var msg = {
                'type':MSGTYPE_ABORTFILE,
                'filekey':filekey,
                'error':err
            };

            conn.send_msg(JSON.stringify(msg));
        }
        function recv_msg_abortfile(conn,msg){
            var filekey = msg.filekey;    
            var err = msg.error;
            
            if(conn.link in conn_filekeymap &&
               filekey in conn_filekeymap[conn.link]){

                conn_filekeymap[conn.link][filekey].callback(err);
            }
        }

        that.add_conn = function(conn){
            conn_linkmap[conn.link] = conn;
            conn_retidmap[conn.link] = {};
            conn_filekeymap[conn.link] = {};
            conn.start_recv(recv_dispatch);
        };
        that.link_conn = function(link,conn){
            conn.link_linkmap[link] = true;
            conn_linkmap[link] = conn;    
        };
        that.unlink_conn = function(link){
            conn = conn_linkmap[link];
            delete conn_linkmap[link];
            delete conn.link_linkmap[link];
        };
        that.del_conn = function(conn){
            retids = conn_retidmap[conn.link];
            for(retid in retids){
                ret_call(conn.link,caller_link,caller_retid,result);
            }

            filekeys = conn_filekeymap[conn.link];
            for(filekey in filekeys){
                filekeys[filekey].callback('Eclose');
            }

            delete conn_retidmap[conn.link];
            delete conn_linkmap[conn.link];
            delete conn_filekeymap[conn.link];
        };
        that.request_conn = function(link,callback){
            var conn = conn_linkmap[link];
            var _conn_cb = function(conn){
                if(conn != null && conn.link != link){
                    that.link_conn(link,conn);
                }

                callback(conn);
            };

            if(conn != undefined){
                _conn_cb(conn); 
            }else{
                conn_link(link,_conn_cb);
            }
        };

        that.call = function(dst,func_name,timeout,callback){
            var i;
            var params = new Array()
            var caller_retid;

            for(i = 4;i < arguments.length;i++){
                params.push(arguments[i]); 
            }

            caller_retid = self_link + '/' + caller_retid_count;
            caller_retid_count += 1;

            route_call(self_link,caller_retid,imc.Auth.get_current_idendesc(),dst,func_name,timeout,callback,params);
        };
        that.sendfile = function(dst_link,
                                 blob,
                                 filekey_callback,
                                 result_callback,
                                 prog_callback){

            var filekey = self_link + '_' + Math.random();

            info_filekeymap[filekey] = {
                'blob':blob,
                'result_callback':result_callback,
                'prog_callback':prog_callback,
                'callback':function(err){
                    if(ret_sendfile(filekey,err) && err != undefined){
                        that.call(dst_link + 'imc/','abort_sendfile',65536,null,filekey,err);
                    }
                }
            };

            that.call(dst_link + 'imc/','pend_recvfile',65536,function(result){
                filekey_callback(filekey);
            },self_link,filekey,blob.size);
        };
        that.abortfile = function(filekey){
            if((info = info_filekeymap[filekey]) != undefined){
                info.callback('Eabort');
            }  
        };

        that.register_call = function(path,func_name,func){
            var cnode;

            cnode = walk_path(path,true);
            cnode.name[func_name] = func;
        };
        that.unregister_call = function(path,func_name){
            var cnode;

            cnode = walk_path(path,true);
            delete cnode.name[func_name];
        }
        that.register_filter = function(path,func){
            var cnode;

            cnode = walk_path(path,true);
            cnode.filt.push(func);
        };
        that.unregister_filter = function(path,func){
            var i;
            var cnode;
            var new_filt = new Array();

            cnode = walk_path(path,true);
            for(i = 0;i < cnode.filt.length;i++){
                if(cnode.filt[i] != func){
                    new_filt.push(cnode.filt[i]); 
                }
            }
            cnode.filt.remove(func);
        };

        that.register_call('imc/','abort_sendfile',
                           function(callback,filekey,err){

            callback('Success');
            ret_sendfile(filekey,'Eabort');
        });

        conn_retidmap[self_link] = {};
        conn_filekeymap[self_link] = {};

        imc.Proxy.instance = that;
    };

    this.Auth = function(){
        var that = this;

        that.get_iden = function(idendesc){
            return JSON.parse(JSON.parse(idendesc)[0]);
        };
        
        imc.Auth.change_current_iden = function(idendesc){
            var iden = imc.Auth.instance.get_iden(idendesc);

            imc.Auth.current_idendata = [iden,idendesc];
        };
        imc.Auth.get_current_iden = function(){
            return imc.Auth.current_idendata[0];
        };
        imc.Auth.get_current_idendesc = function(){
            return imc.Auth.current_idendata[1];
        };

        imc.Auth.current_idendata = null;
        imc.Auth.instance = that;
    };
};