/
jrw.js
201 lines (188 loc) · 5.69 KB
/
jrw.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
/* Json-Rpc over Websockets
*/
// helpers to generate 'uuid'
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
// encapsulates a json-rpc message
function JRMessage(msg){
this.json = msg;
this.parsed = $.evalJSON(msg);
this.id = this.parsed['id'];
this.method = this.parsed['method'];
if(this.parsed['params']){
this.params = this.parsed['params'];
for(p=0;p<this.params.length;++p){
if(JRObject.is_jrobject(this.params[p]))
this.params[p] = JRObject.from_json(this.params[p]);
else if(JRObject.is_jrobject_array(this.params[p]))
this.params[p] = JRObject.from_json_array(this.params[p]);
}
}
this.error = this.parsed['error'];
this.result = this.parsed['result'];
if(this.result && JRObject.is_jrobject(this.result))
this.result = JRObject.from_json(this.result);
else if(JRObject.is_jrobject_array(this.result))
this.result = JRObject.from_json_array(this.result);
}
// encapsulates an object w/ type
// - adaptor for the ruby 'json' library
function JRObject (type, value, ignore_properties){
this.type = type;
this.value = value;
this.ignore_properties = (typeof(ignore_properties) != 'undefined') ?
ignore_properties : ["toJSON"];
this.toJSON = function(){
var data = {};
for(p in this.value)
if($.inArray(p, this.ignore_properties) == -1)
data[p] = value[p];
return {json_class: this.type, data: data };
};
};
JRObject.is_jrobject = function(json){
return json && json['json_class'] && json['data'];
};
JRObject.is_jrobject_array = function(json){
return json && typeof(json) == "object" && json.length > 0 && JRObject.is_jrobject(json[0]);
};
JRObject.from_json = function(json){
// TODO lookup class corresponding in global registry and instantiate
//var cl = JRObject.class_registry[json['json_class']];
var obj = json['data'];
obj.json_class = json['json_class'];
for(var p in obj){
if(JRObject.is_jrobject(obj[p]))
obj[p] = JRObject.from_json(obj[p]);
else if(JRObject.is_jrobject_array(obj[p])){
obj[p] = JRObject.from_json_array(obj[p]);
}
}
return obj;
};
JRObject.from_json_array = function(json){
var objs = [];
for(var i in json)
if(JRObject.is_jrobject(json[i]))
objs[i] = JRObject.from_json(json[i]);
return objs;
};
// global / shared class registry
//JRObject.class_registry = {};
// main json-rpc websocket interface
function WSNode (host, port){
var node = this;
this.node_id = null;
this.headers = {};
this.open = function(){
node.socket = new WebSocket("ws://" + host + ":" + port);
node.socket.onopen = function (){
// XXX hack, give other handlers time to register
setTimeout(function(){
if(node.onopen)
node.onopen();
}, 250);
};
node.socket.onclose = function (){
if(node.onclose)
node.onclose();
};
node.socket.onmessage = function (e){
msg = new JRMessage(e.data);
if(node.onmessage)
node.onmessage(msg);
};
node.socket.onerror = function(e){
if(node.onerror)
node.onerror(e);
}
};
this.close = function(){
node.socket.close();
};
this.invoke_request = function(){
id = guid();
rpc_method = arguments[0];
args = [];
for(a = 1; a < arguments.length; a++){
args.push(arguments[a]);
}
request = {jsonrpc: '2.0',
method: rpc_method,
params: args,
id: id};
if(node.node_id){
request['node_id'] = node.node_id;
}
for(var header in node.headers){
request[header] = node.headers[header];
}
node.onmessage = function(msg){
if(node.message_received)
node.message_received(msg);
if(msg['id'] == id){
success = !msg['error'];
if(success && node.onsuccess){
result = msg['result'];
node.onsuccess(result);
}
else if(!success && node.onfailed)
node.onfailed(msg['error']['code'], msg['error']['message']);
}else{
if(msg['method'] && node.invoke_method){
params = msg['params'];
node.invoke_method(msg['method'], params);
}
}
};
node.socket.send($.toJSON(request));
};
};
// main json-rpc www interface
function WebNode (uri){
var node = this;
this.node_id = null;
this.headers = {};
this.invoke_request = function(){
id = guid();
rpc_method = arguments[0];
args = [];
for(a = 1; a < arguments.length; a++){
args.push(arguments[a]);
}
request = {jsonrpc: '2.0',
method: rpc_method,
params: args,
id: id};
if(node.node_id){
request['node_id'] = node.node_id;
}
for(var header in node.headers){
request[header] = node.headers[header];
}
$.ajax({type: 'POST',
url: uri,
data: $.toJSON(request),
dataType: 'text', // using text so we can parse json ourselves
success: function(data){
data = new JRMessage(data);
if(node.message_received)
node.message_received(data);
success = !data['error'];
if(success && node.onsuccess){
result = data['result'];
node.onsuccess(result);
}
else if(!success && node.onfailed)
node.onfailed(data['error']['code'], data['error']['message']);
},
error: function(jqXHR, textStatus, errorThrown){
if(node.onfailed)
node.onfailed(jqXHR.status, textStatus);
}});
};
};