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