]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | var Thrift = require('./thrift'); | |
20 | ||
21 | exports.MultiplexedProcessor = MultiplexedProcessor; | |
22 | ||
23 | function MultiplexedProcessor(stream, options) { | |
24 | this.services = {}; | |
25 | }; | |
26 | ||
27 | MultiplexedProcessor.prototype.registerProcessor = function(name, handler) { | |
28 | this.services[name] = handler; | |
29 | }; | |
30 | ||
31 | MultiplexedProcessor.prototype.process = function(inp, out) { | |
32 | var begin = inp.readMessageBegin(); | |
33 | ||
34 | if (begin.mtype != Thrift.MessageType.CALL && begin.mtype != Thrift.MessageType.ONEWAY) { | |
35 | throw new Thrift.TException('TMultiplexedProcessor: Unexpected message type'); | |
36 | } | |
37 | ||
38 | var p = begin.fname.split(':'); | |
39 | var sname = p[0]; | |
40 | var fname = p[1]; | |
41 | ||
42 | if (! (sname in this.services)) { | |
43 | throw new Thrift.TException('TMultiplexedProcessor: Unknown service: ' + sname); | |
44 | } | |
45 | ||
46 | //construct a proxy object which stubs the readMessageBegin | |
47 | //for the service | |
48 | var inpProxy = {}; | |
49 | ||
50 | for (var attr in inp) { | |
51 | inpProxy[attr] = inp[attr]; | |
52 | } | |
53 | ||
54 | inpProxy.readMessageBegin = function() { | |
55 | return { | |
56 | fname: fname, | |
57 | mtype: begin.mtype, | |
58 | rseqid: begin.rseqid | |
59 | }; | |
60 | }; | |
61 | ||
62 | this.services[sname].process(inpProxy, out); | |
63 | }; |