Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [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 | using System; |
| 21 | using System.Collections.Generic; |
| 22 | using Thrift.Collections; |
| 23 | using Thrift.Transport; |
| 24 | using Thrift.Protocol; |
| 25 | using Thrift.Server; |
| 26 | using Thrift; |
| 27 | using Test.Multiplex; |
| 28 | |
| 29 | namespace Test.Multiplex.Server |
| 30 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 31 | public class TestServer |
| 32 | { |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 33 | class BenchmarkServiceImpl : BenchmarkService.Iface |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 34 | { |
| 35 | public int fibonacci(sbyte n) |
| 36 | { |
| 37 | int prev, next, result; |
| 38 | prev = 0; |
| 39 | result = 1; |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 40 | while (n > 0) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 41 | { |
| 42 | next = result + prev; |
| 43 | prev = result; |
| 44 | result = next; |
| 45 | --n; |
| 46 | } |
| 47 | return result; |
| 48 | } |
| 49 | } |
| 50 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 51 | class AggrServiceImpl : Aggr.Iface |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 52 | { |
| 53 | List<int> values = new List<int>(); |
| 54 | |
| 55 | public void addValue(int value) |
| 56 | { |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 57 | values.Add(value); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public List<int> getValues() |
| 61 | { |
| 62 | return values; |
| 63 | } |
| 64 | } |
| 65 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 66 | static void Execute(int port) |
| 67 | { |
| 68 | try |
| 69 | { |
| 70 | // create protocol factory, default to BinaryProtocol |
| 71 | TProtocolFactory ProtocolFactory = new TBinaryProtocol.Factory(true,true); |
| 72 | TServerTransport servertrans = new TServerSocket(port, 0, false); |
| 73 | TTransportFactory TransportFactory = new TFramedTransport.Factory(); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 75 | BenchmarkService.Iface benchHandler = new BenchmarkServiceImpl(); |
| 76 | TProcessor benchProcessor = new BenchmarkService.Processor(benchHandler); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 78 | Aggr.Iface aggrHandler = new AggrServiceImpl(); |
| 79 | TProcessor aggrProcessor = new Aggr.Processor(aggrHandler); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 80 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 81 | TMultiplexedProcessor multiplex = new TMultiplexedProcessor(); |
| 82 | multiplex.RegisterProcessor(Constants.NAME_BENCHMARKSERVICE, benchProcessor); |
| 83 | multiplex.RegisterProcessor(Constants.NAME_AGGR, aggrProcessor); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 84 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 85 | TServer ServerEngine = new TSimpleServer(multiplex, servertrans, TransportFactory, ProtocolFactory); |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 86 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 87 | Console.WriteLine("Starting the server ..."); |
| 88 | ServerEngine.Serve(); |
| 89 | } |
| 90 | catch (Exception e) |
| 91 | { |
| 92 | Console.WriteLine(e.Message); |
| 93 | } |
| 94 | } |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 95 | |
Nobuaki Sukegawa | 88c5ee7 | 2016-09-04 18:49:18 +0900 | [diff] [blame] | 96 | static void Main(string[] args) |
| 97 | { |
| 98 | int port = 9090; |
| 99 | if (args.Length > 0) |
| 100 | { |
| 101 | port = ushort.Parse(args[0]); |
| 102 | } |
| 103 | Execute(port); |
| 104 | } |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 105 | } |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 106 | } |
| 107 | |