]>
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 | ||
20 | package; | |
21 | ||
22 | import haxe.Int64; | |
23 | import haxe.Int32; | |
24 | ||
25 | import org.apache.thrift.*; | |
26 | import org.apache.thrift.protocol.*; | |
27 | import org.apache.thrift.transport.*; | |
28 | import org.apache.thrift.server.*; | |
29 | import org.apache.thrift.meta_data.*; | |
30 | ||
31 | // debug only | |
32 | import org.apache.thrift.protocol.TProtocolDecorator; | |
33 | import org.apache.thrift.protocol.TMultiplexedProtocol; | |
34 | import org.apache.thrift.protocol.TMultiplexedProcessor; | |
35 | ||
36 | // generated code imports | |
37 | import Aggr; | |
38 | import AggrImpl; | |
39 | import AggrProcessor; | |
40 | import BenchmarkService; | |
41 | import BenchmarkServiceImpl; | |
42 | import BenchmarkServiceProcessor; | |
43 | import Error; | |
44 | ||
45 | ||
46 | class BenchmarkServiceHandler implements BenchmarkService | |
47 | { | |
48 | public function new() { | |
49 | } | |
50 | ||
51 | public function fibonacci(n : haxe.Int32) : haxe.Int32 { | |
52 | trace('Benchmark.fibonacci($n)'); | |
53 | var next : Int; | |
54 | var prev = 0; | |
55 | var result = 1; | |
56 | while( n > 0) | |
57 | { | |
58 | next = result + prev; | |
59 | prev = result; | |
60 | result = next; | |
61 | --n; | |
62 | } | |
63 | return result; | |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | class AggrServiceHandler implements Aggr | |
69 | { | |
70 | private var values : List<haxe.Int32> = new List<haxe.Int32>(); | |
71 | ||
72 | public function new() { | |
73 | } | |
74 | ||
75 | public function addValue(value : haxe.Int32) : Void { | |
76 | trace('Aggr.addValue($value)'); | |
77 | values.add( value); | |
78 | } | |
79 | ||
80 | public function getValues() : List< haxe.Int32> { | |
81 | trace('Aggr.getValues()'); | |
82 | return values; | |
83 | } | |
84 | } | |
85 | ||
86 | ||
87 | ||
88 | class MultiplexTest extends TestBase { | |
89 | ||
90 | private inline static var NAME_BENCHMARKSERVICE : String = "BenchmarkService"; | |
91 | private inline static var NAME_AGGR : String = "Aggr"; | |
92 | ||
93 | ||
94 | public static override function Run(server : Bool) : Void { | |
95 | if ( server) { | |
96 | RunMultiplexServer(); | |
97 | } else { | |
98 | RunMultiplexClient(); | |
99 | RunDefaultClient(); | |
100 | } | |
101 | } | |
102 | ||
103 | ||
104 | // run the multiplex server | |
105 | public static override function RunMultiplexServer() : Void { | |
106 | try | |
107 | { | |
108 | var benchHandler : BenchmarkService = new BenchmarkServiceHandler(); | |
109 | var benchProcessor : TProcessor = new BenchmarkServiceProcessor( benchHandler); | |
110 | ||
111 | var aggrHandler : Aggr = new AggrServiceHandler(); | |
112 | var aggrProcessor : TProcessor = new AggrProcessor( aggrHandler); | |
113 | ||
114 | var multiplex : TMultiplexedProcessor = new TMultiplexedProcessor(); | |
115 | multiplex.RegisterProcessor( NAME_BENCHMARKSERVICE, benchProcessor, true); // default | |
116 | multiplex.RegisterProcessor( NAME_AGGR, aggrProcessor); | |
117 | ||
118 | // protocol+transport stack | |
119 | var protfact : TProtocolFactory = new TBinaryProtocolFactory(true,true); | |
120 | var servertrans : TServerTransport = new TServerSocket( 9090, 5, false); | |
121 | var transfact : TTransportFactory = new TFramedTransportFactory(); | |
122 | ||
123 | var server : TServer = new TSimpleServer( multiplex, servertrans, transfact, protfact); | |
124 | ||
125 | trace("Starting the server ..."); | |
126 | server.Serve(); | |
127 | } | |
128 | catch( e : TApplicationException) | |
129 | { | |
130 | TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); | |
131 | } | |
132 | catch( e : TException) | |
133 | { | |
134 | TestBase.Expect(false,'$e'); | |
135 | } | |
136 | } | |
137 | ||
138 | ||
139 | // run multiplex client against multiplex server | |
140 | public static override function RunMultiplexClient() : Void { | |
141 | try | |
142 | { | |
143 | var trans : TTransport; | |
144 | trans = new TSocket("localhost", 9090); | |
145 | trans = new TFramedTransport(trans); | |
146 | trans.open(); | |
147 | ||
148 | var protocol : TProtocol = new TBinaryProtocol(trans,true,true); | |
149 | var multiplex : TMultiplexedProtocol; | |
150 | ||
151 | multiplex = new TMultiplexedProtocol( protocol, NAME_BENCHMARKSERVICE); | |
152 | var bench = new BenchmarkServiceImpl( multiplex); | |
153 | ||
154 | multiplex = new TMultiplexedProtocol( protocol, NAME_AGGR); | |
155 | var aggr = new AggrImpl( multiplex); | |
156 | ||
157 | trace('calling aggr.add( bench.fibo())...'); | |
158 | for( i in 1 ... 10) | |
159 | { | |
160 | trace('$i'); | |
161 | aggr.addValue( bench.fibonacci(i)); | |
162 | } | |
163 | ||
164 | trace('calling aggr ...'); | |
165 | var i = 1; | |
166 | var values = aggr.getValues(); | |
167 | TestBase.Expect(values != null,'aggr.getValues() == null'); | |
168 | for( k in values) | |
169 | { | |
170 | trace('fib($i) = $k'); | |
171 | ++i; | |
172 | } | |
173 | ||
174 | trans.close(); | |
175 | trace('done.'); | |
176 | ||
177 | } | |
178 | catch( e : TApplicationException) | |
179 | { | |
180 | TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); | |
181 | } | |
182 | catch( e : TException) | |
183 | { | |
184 | TestBase.Expect(false,'$e'); | |
185 | } | |
186 | } | |
187 | ||
188 | ||
189 | // run non-multiplex client against multiplex server to test default fallback | |
190 | public static override function RunDefaultClient() : Void { | |
191 | try | |
192 | { | |
193 | var trans : TTransport; | |
194 | trans = new TSocket("localhost", 9090); | |
195 | trans = new TFramedTransport(trans); | |
196 | trans.open(); | |
197 | ||
198 | var protocol : TProtocol = new TBinaryProtocol(trans,true,true); | |
199 | ||
200 | var bench = new BenchmarkServiceImpl( protocol); | |
201 | ||
202 | trace('calling bench (via default) ...'); | |
203 | for( i in 1 ... 10) | |
204 | { | |
205 | var k = bench.fibonacci(i); | |
206 | trace('fib($i) = $k'); | |
207 | } | |
208 | ||
209 | trans.close(); | |
210 | trace('done.'); | |
211 | } | |
212 | catch( e : TApplicationException) | |
213 | { | |
214 | TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); | |
215 | } | |
216 | catch( e : TException) | |
217 | { | |
218 | TestBase.Expect(false,'$e'); | |
219 | } | |
220 | } | |
221 | ||
222 | } | |
223 | ||
224 |