2 // --- Scope ------------------
\r
3 // T : Reflect.Service instance
\r
6 * Constructs a new runtime Service.
\r
7 * @name ProtoBuf.Builder.Service
\r
8 * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
\r
9 * @class Barebone of all runtime services.
\r
11 * @throws {Error} If the service cannot be created
\r
13 var Service = function(rpcImpl) {
\r
14 ProtoBuf.Builder.Service.call(this);
\r
17 * Service implementation.
\r
18 * @name ProtoBuf.Builder.Service#rpcImpl
\r
19 * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
\r
22 this.rpcImpl = rpcImpl || function(name, msg, callback) {
\r
23 // This is what a user has to implement: A function receiving the method name, the actual message to
\r
24 // send (type checked) and the callback that's either provided with the error as its first
\r
25 // argument or null and the actual response message.
\r
26 setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
\r
31 * @alias ProtoBuf.Builder.Service.prototype
\r
34 var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
\r
37 * Asynchronously performs an RPC call using the given RPC implementation.
\r
38 * @name ProtoBuf.Builder.Service.[Method]
\r
40 * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
\r
41 * @param {ProtoBuf.Builder.Message} req Request
\r
42 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
\r
43 * the error if any and the response either as a pre-parsed message or as its raw bytes
\r
48 * Asynchronously performs an RPC call using the instance's RPC implementation.
\r
49 * @name ProtoBuf.Builder.Service#[Method]
\r
51 * @param {ProtoBuf.Builder.Message} req Request
\r
52 * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
\r
53 * the error if any and the response either as a pre-parsed message or as its raw bytes
\r
57 var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
\r
58 for (var i=0; i<rpc.length; i++) {
\r
61 // service#Method(message, callback)
\r
62 ServicePrototype[method.name] = function(req, callback) {
\r
65 // If given as a buffer, decode the request. Will throw a TypeError if not a valid buffer.
\r
66 req = method.resolvedRequestType.clazz.decode(ByteBuffer.wrap(req));
\r
68 if (!(err instanceof TypeError))
\r
71 if (req === null || typeof req !== 'object')
\r
72 throw Error("Illegal arguments");
\r
73 if (!(req instanceof method.resolvedRequestType.clazz))
\r
74 req = new method.resolvedRequestType.clazz(req);
\r
75 this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
\r
80 // Coalesce to empty string when service response has empty content
\r
83 try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
\r
84 if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
\r
85 callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
\r
88 callback(null, res);
\r
91 setTimeout(callback.bind(this, err), 0);
\r
95 // Service.Method(rpcImpl, message, callback)
\r
96 Service[method.name] = function(rpcImpl, req, callback) {
\r
97 new Service(rpcImpl)[method.name](req, callback);
\r
100 if (Object.defineProperty)
\r
101 Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
\r
102 Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
\r
110 * @name ProtoBuf.Builder.Service.$options
\r
111 * @type {Object.<string,*>}
\r
114 var $optionsS; // cc needs this
\r
118 * @name ProtoBuf.Builder.Service#$options
\r
119 * @type {Object.<string,*>}
\r
126 * @name ProtoBuf.Builder.Service.$type
\r
127 * @type {!ProtoBuf.Reflect.Service}
\r
134 * @name ProtoBuf.Builder.Service#$type
\r
135 * @type {!ProtoBuf.Reflect.Service}
\r
140 if (Object.defineProperty)
\r
141 Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
\r
142 Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }),
\r
143 Object.defineProperty(Service, "$type", { "value": T }),
\r
144 Object.defineProperty(ServicePrototype, "$type", { "value": T });
\r