forked from hhuuggoo/ZmqWebBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.js
256 lines (231 loc) · 6.42 KB
/
bridge.js
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
zmq = {}
zmq.SUB = 2
zmq.REQ = 3
zmq.REP = 4
zmq.uuid = function () {
//from ipython project
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789ABCDEF";
for (var i = 0; i < 32; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
var uuid = s.join("");
return uuid;
};
zmq.Context = function(ws_conn_string){
//zmq context proxy. contains a websocketconnection
//routes messages to fake zmq sockets on the js side
this.sockets = {}
var that = this;
try {
this.s = new WebSocket(ws_conn_string);
}
catch (e) {
this.s = new MozWebSocket(ws_conn_string);
}
this.s.onmessage = function(msg){
var msgobj = JSON.parse(msg.data);
var socket = that.sockets[msgobj['identity']]
if (socket.connected){
socket._handle(msgobj['content']);
}else if (msgobj['msg_type'] === 'connection_reply'){
that.onconnect(socket, msgobj['content']);
}
}
this.s.onopen = function(){
that.connected = true;
$.map(that.send_buffer, function(x){
that.s.send(x);
});
that.send_buffer = [];
}
this.connected = false;
this.send_buffer = [];
}
zmq.Context.prototype.connect = function(socket, zmq_conn_string, auth){
auth['zmq_conn_string'] = zmq_conn_string;
auth['socket_type'] = socket.socket_type;
var msg = JSON.stringify(auth)
var msgobj = socket.construct_message(msg, 'connect')
msg = JSON.stringify(msgobj);
this.send(msg);
}
zmq.Context.prototype.onconnect = function(socket, msg){
var msgobj = JSON.parse(msg);
if (msgobj['status'] === 'success'){
socket.connected = true;
}else{
socket.connected = false;
}
}
zmq.Context.prototype.Socket = function(socket_type){
//contexts are also a factory for sockets, just like
//in normal zeromq
var fakesocket;
if (socket_type === zmq.SUB){
fakesocket = new zmq.SubSocket(this);
}else if (socket_type === zmq.REQ){
fakesocket = new zmq.ReqSocket(this);
}else if (socket_type === zmq.REP){
fakesocket = new zmq.RepSocket(this);
}
this.sockets[fakesocket.identity] = fakesocket;
return fakesocket;
}
zmq.Context.prototype.send = function(msg){
if (this.connected){
this.s.send(msg);
}else{
this.send_buffer.push(msg);
}
}
zmq.Socket = function(ctx){
this.ctx = ctx
this.identity = zmq.uuid();
}
zmq.Socket.prototype.construct_message = function(msg, msg_type){
//your message should be a string
//constructs a message object, as json
//this will be serialized before it goes to the wire
if(!msg_type){
msg_type = 'userlevel'
}
return {
'identity' : this.identity,
'content' : msg,
'msg_type' : msg_type
}
}
zmq.Socket.prototype.connect = function(zmq_conn_string, auth){
this.ctx.connect(this, zmq_conn_string, auth);
}
zmq.ReqSocket = function(ctx){
zmq.Socket.call(this, ctx);
this.reqrep_buffer = [];
this.busy = false;
this.socket_type = zmq.REQ;
}
zmq.ReqSocket.prototype = new zmq.Socket();
zmq.ReqSocket.prototype.send = function(msg, callback, msg_type){
var msgobj = this.construct_message(msg, msg_type)
this._send(JSON.stringify(msgobj), callback);
}
zmq.ReqSocket.prototype._send = function(msg, callback){
this.reqrep_buffer.push([msg, callback]);
if (this.busy){
return
}else{
this._send_buffer();
}
}
zmq.ReqSocket.prototype._send_buffer = function(){
if (this.busy || this.reqrep_buffer.length == 0){
return
}else{
this.busy = true;
this.ctx.send(this.reqrep_buffer[0][0]);
return
}
}
zmq.ReqSocket.prototype._handle = function(msg){
this.busy = false;
var callback = this.reqrep_buffer[0][1]
this.reqrep_buffer = this.reqrep_buffer.slice(1);
callback(msg);
this._send_buffer();
}
zmq.SubSocket = function(ctx){
zmq.ReqSocket.call(this, ctx);
this.socket_type = zmq.SUB;
}
//prototype from req socket, because we need the auth functionality
zmq.SubSocket.prototype = new zmq.Socket();
zmq.SubSocket.prototype._handle = function(msg){
this.onmessage(msg);
}
zmq.RepSocket = function(ctx){
zmq.ReqSocket.call(this, ctx);
this.socket_type = zmq.REP;
this.in_buffer = [];
},
zmq.RepSocket.prototype = new zmq.Socket();
zmq.RepSocket.prototype.send = function(msg){
//layer adressing information
var msg = [this.address, '', msg]
msg = JSON.stringify(msg);
var msgobj = this.construct_message(msg)
this.ctx.send(JSON.stringify(msgobj));
//this is a reply, so now we are no longer busy
this.busy = false;
//try to process in_buffer
this._recv_buffer();
}
zmq.RepSocket.prototype._recv_buffer = function(){
if (this.busy || this.in_buffer.length == 0){
return
}else{
var msg = this.in_buffer[0];
this.in_buffer = this.in_buffer.slice(1);
this.busy = true;
var msgobj = JSON.parse(msg);
var address = msgobj[0];
var content = msgobj[msgobj.length - 1];
this.address = address;
this.onmessage(content);
}
}
zmq.RepSocket.prototype._handle = function(msg){
this.in_buffer.push(msg);
this._recv_buffer();
}
zmq.RPCClient = function(socket){
this.socket = socket;
}
zmq.RPCClient.prototype.rpc = function(funcname, args, kwargs, callback){
msg = {'funcname' : funcname,
'args' : args,
'kwargs' : kwargs}
var wrapped_callback = function (msg){
var msgobj = JSON.parse(msg);
callback(msgobj['returnval']);
}
this.socket.send(JSON.stringify(msg), wrapped_callback);
}
zmq.PubRPCServer = function(socket){
this.socket = socket;
var that = this;
if (socket){
socket.onmessage = function(msg){
that.handle_pub(msg);
}
}
}
zmq.PubRPCServer.prototype.handle_pub = function(msg){
var msgobj = JSON.parse(msg)
var funcname = msgobj['funcname']
var args = msgobj['args'] || [];
this[funcname].apply(this, args);
}
zmq.RPCServer = function(socket){
this.socket = socket;
var that = this;
if (socket){
socket.onmessage = function(msg){
that.handle(msg);
}
}
}
zmq.RPCServer.prototype.handle = function(msg){
var msgobj = JSON.parse(msg)
var funcname = msgobj['funcname']
var args = msgobj['args'] || [];
try{
retval = this[funcname].apply(this, args);
}catch(err){
retval = err;
}
this.socket.send(JSON.stringify({'returnval' : retval}));
}