initial commit
[namibia] / public / ape-source / Pipe / PipeProxy.js
1 APE.PipeProxy = new Class({
2
3         Extends: APE.Pipe,
4
5         initialize: function(core, options){
6                 this.core = core || window.Ape;
7                 this.ape = this.core;
8
9                 this.initRequestMethod();
10                 this.type = 'proxy';
11
12                 if (options) {
13                         this.init(options);
14                 }
15         },
16
17         init: function(options){
18                 this.pipe = options.pipe;
19
20                 this.core.addPipe(this.getPubid(), this);
21
22                 this.onRaw('proxy_event', this.rawProxyEvent);
23                 this.ape.fireEvent('proxyPipeCreate', [this, options]);
24         },
25
26         reset: function() {
27                 //close connection
28         },
29
30         close: function() {
31                 //close connection
32         },
33
34         open: function(hostname, port){
35                 if (this.core.status == 0) this.core.start(null, false);
36                 //Adding a callback to request response to create a new pipe if this.pipe haven't been init
37                 this.request.stack.add('PROXY_CONNECT', {'host':hostname, 'port':port}, this.pipe ? {} : {'callback':this.callback.bind(this)});
38                 this.request.stack.send();
39         },
40
41         send: function(data){
42                 this.request.send('SEND', {'msg':B64.encode(data)});
43         },
44
45         rawProxyEvent: function(resp){
46                 switch (resp.data.event) {
47                         case 'read':
48                                 var data = B64.decode(resp.data.data);
49                                 this.fireGlobalEvent('proxyRead', data)
50                                 if (this.onread) this.onread(data);
51                                 break;
52                         case 'connect':
53                                 this.fireGlobalEvent('proxyConnect');
54                                 if (this.onopen) this.onopen();
55                                 break;
56                         case 'close':
57                                 this.fireGlobalEvent('proxyClose');
58                                 if (this.onclose) this.onclose();
59                                 break;
60                 }
61         },
62
63         callback: function(raw){
64                 this.init(raw.data);
65                 this.rawProxyEvent(raw);
66         }
67 });
68
69 APE.Core = new Class({
70
71         Extends: APE.Core,
72
73         /***
74          * This allow ape to be compatible with TCPSocket
75          */
76         TCPSocket: APE.PipeProxy
77 });