Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 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 | |
Jens Geyer | 18564d2 | 2022-06-05 11:36:40 +0200 | [diff] [blame] | 22 | #if ! (flash || html5 || js) |
| 23 | |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 24 | import haxe.Int64; |
| 25 | import haxe.Int32; |
| 26 | |
| 27 | import org.apache.thrift.*; |
| 28 | import org.apache.thrift.protocol.*; |
| 29 | import org.apache.thrift.transport.*; |
| 30 | import org.apache.thrift.server.*; |
| 31 | import org.apache.thrift.meta_data.*; |
| 32 | |
| 33 | // debug only |
| 34 | import org.apache.thrift.protocol.TProtocolDecorator; |
| 35 | import org.apache.thrift.protocol.TMultiplexedProtocol; |
| 36 | import org.apache.thrift.protocol.TMultiplexedProcessor; |
| 37 | |
| 38 | // generated code imports |
| 39 | import Aggr; |
| 40 | import AggrImpl; |
| 41 | import AggrProcessor; |
| 42 | import BenchmarkService; |
| 43 | import BenchmarkServiceImpl; |
| 44 | import BenchmarkServiceProcessor; |
| 45 | import Error; |
| 46 | |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 47 | class BenchmarkServiceHandler implements BenchmarkService_service |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 48 | { |
| 49 | public function new() { |
| 50 | } |
| 51 | |
Jens Geyer | 7628392 | 2022-06-11 14:24:33 +0200 | [diff] [blame] | 52 | public function fibonacci(n : haxe.Int32) : haxe.Int32 { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 53 | 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 Geyer | 7628392 | 2022-06-11 14:24:33 +0200 | [diff] [blame] | 64 | return result; |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 69 | class AggrServiceHandler implements Aggr_service |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 70 | { |
| 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 | |
| 89 | class 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 Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 95 | public static function Run(server : Bool) : Void { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 96 | if ( server) { |
| 97 | RunMultiplexServer(); |
| 98 | } else { |
| 99 | RunMultiplexClient(); |
| 100 | RunDefaultClient(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // run the multiplex server |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 106 | public static function RunMultiplexServer() : Void { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 107 | try |
| 108 | { |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 109 | var benchHandler : BenchmarkService_service = new BenchmarkServiceHandler(); |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 110 | var benchProcessor : TProcessor = new BenchmarkServiceProcessor( benchHandler); |
| 111 | |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 112 | var aggrHandler : Aggr_service = new AggrServiceHandler(); |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 113 | 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 Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 141 | public static function RunMultiplexClient() : Void { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 142 | 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 Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 191 | public static function RunDefaultClient() : Void { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 192 | 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 Geyer | 18564d2 | 2022-06-05 11:36:40 +0200 | [diff] [blame] | 225 | #end |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 226 | |