-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (165 loc) · 4.71 KB
/
server.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
// function echo(args, req) {
// req.err()
// req.response({}, true)
// req.response({})
// db.write().addErrBack(req.err).addCallback(req.response)
// req.setTimeout()
// }
//
// Later - 1 callback, 1 errback, 1 cancel
// - can fire multiple times
//
jsio('from net.protocols.rtjp import RTJPProtocol');
jsio('import net.interfaces');
jsio('import logging');
jsio('from net.later import Later');
jsio('from util.underscore import _');
var logger=logging.getLogger('pijoserver');
var Request = Class(function() {
this.init = function(conn, requestId) {
this._conn = conn;
this._requestId = requestId;
}
this.send = function(result, hasMore) {
var response = {
requestId: this._requestId,
isSuccess: true,
result: result
};
if(hasMore) { response.hasMore = true; }
this._conn.sendFrame('RESPONSE', response);
}
this.error = function() {
this._conn.sendFrame('RESPONSE', {
requestId: this._requestId,
isSuccess: false
});
}
})
exports.PijoConn = Class(RTJPProtocol, function(supr) {
this.init = function(protocol, Interface, impl, isClient) {
supr(this, 'init');
this._protocol = protocol;
this._impl = impl;
this._isClient = isClient;
this._requestDirection = isClient ? 'client' : 'server';
this._inFlight = {};
this.remote = new Interface(this);
}
this.request = function(name, args) {
if(!this._protocol.rpcs.hasOwnProperty(name)) { return; }
var id = this.sendFrame('REQUEST', {
name: name,
args: args
}),
req = this._inFlight[id] = {
id: id,
later: new Later()
};
if('timeout' in this._protocol.rpcs[name]) {
req.timeout = $setTimeout(bind(this, 'onTimeout', id), this._protocol.rpcs[name].timeout);
}
return req.later;
}
this.frameReceived = function(id, name, args) {
logger.info('in frameReceived', id, name, args);
switch (name) {
case 'REQUEST':
try {
this.handleRequest(id, args);
} catch(result) {
this.sendFrame('RESPONSE', {
requestId: id,
isSuccess: false,
result: result
});
}
break;
case 'RESPONSE':
try {
this.handleResponse(args);
} catch(e) {
throw e;
}
break;
case 'EVENT':
break;
}
}
this.onTimeout = function(id) {
this._inFlight[id].timeout = null;
this.handleResponse({
requestId: id,
isSuccess: false,
result: 'TIMEOUT'
});
}
this.handleResponse = function(response) {
var id = response.requestId,
req = this._inFlight[id];
if(!response.hasMore) {
delete this._inFlight[id];
}
if (req.timeout) { $clearTimeout(req.timeout); }
if (!response.isSuccess) {
logger.info('doing errback');
req.later.errback(response.result);
} else {
logger.info('doing callback');
req.later.callback(response.result);
}
}
this.handleRequest = function(id, args) {
var method_name = args['name'],
method_args = args['args'];
if (!method_name) { throw {msg: 'missing method name'}; }
if (!method_args) { throw {msg: 'missing args'}; }
var rpc = this._protocol.rpcs[method_name];
if (!rpc) { throw {msg: 'invalid method'}; } // TODO: error
if (rpc.direction && rpc.direction != this._requestDirection) { throw {msg: 'invalid method (wrong direction)'}; }
var constructed_args = {};
_.each(method_args, function(value, arg) {
if(rpc.request.args.args.hasOwnProperty(arg)) {
logger.log(' arg found');
constructed_args[arg] = method_args[arg];
}
});
if(typeof this._impl[method_name] != 'function') { throw {msg: 'method not implemented'}; }
try {
this._impl[method_name].call(this, new Request(this, id), constructed_args);
} catch(e) {
// if(!(e instanceof ExpectedError)) {
// log unexpected errors
// }
// send error back
throw e;
}
}
});
/**
* Adds a c
*/
exports.buildRequestInterface = function(protocol) {
var reqInterface = function(conn) { this._conn = conn; },
proto = reqInterface.prototype;
for (var rpc in protocol.rpcs) {
if (protocol.rpcs.hasOwnProperty(rpc)) {
proto[rpc] = (function(rpc) { return function(args) { return this._conn.request(rpc, args); }; })(rpc);
}
}
return reqInterface;
}
exports.PijoServer = Class(net.interfaces.Server, function(supr) {
this.init = function(protocol, impl) {
supr(this, 'init', [PijoConn]);
this._ProtocolInterface = exports.buildRequestInterface(protocol);
this._protocol = protocol;
this._impl = impl;
}
/* override -- the PijoProtocol works on the server and client, so it doesn't know about this.server._protocol -- we could, however, check for this.server instead... */
this.buildProtocol = function() {
return new this._protocolClass(this._protocol, this._ProtocolInterface, this._impl);
}
this.run = function() {
}
});