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 | { |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 33 | public interface ITestHandler |
| 34 | { |
| 35 | void SetServer( TServer aServer); |
| 36 | } |
| 37 | |
| 38 | protected class TestHandlerImpl : ITestHandler |
| 39 | { |
| 40 | private TServer Server; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 41 | |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 42 | public void SetServer( TServer aServer) |
| 43 | { |
| 44 | Server = aServer; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | protected class BenchmarkServiceImpl : TestHandlerImpl, BenchmarkService.Iface |
| 50 | { |
| 51 | public int fibonacci(sbyte n) |
| 52 | { |
| 53 | int prev, next, result; |
| 54 | prev = 0; |
| 55 | result = 1; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 56 | while( n > 0) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 57 | { |
| 58 | next = result + prev; |
| 59 | prev = result; |
| 60 | result = next; |
| 61 | --n; |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | protected class AggrServiceImpl : TestHandlerImpl, Aggr.Iface |
| 69 | { |
| 70 | List<int> values = new List<int>(); |
| 71 | |
| 72 | public void addValue(int value) |
| 73 | { |
| 74 | values.Add( value); |
| 75 | } |
| 76 | |
| 77 | public List<int> getValues() |
| 78 | { |
| 79 | return values; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void Execute() |
| 84 | { |
| 85 | try |
| 86 | { |
| 87 | // create protocol factory, default to BinaryProtocol |
| 88 | TProtocolFactory ProtocolFactory = new TBinaryProtocol.Factory(true,true); |
| 89 | TServerTransport servertrans = new TServerSocket( 9090, 0, false); |
| 90 | TTransportFactory TransportFactory = new TFramedTransport.Factory(); |
| 91 | |
| 92 | BenchmarkService.Iface benchHandler = new BenchmarkServiceImpl(); |
| 93 | TProcessor benchProcessor = new BenchmarkService.Processor( benchHandler); |
| 94 | |
| 95 | Aggr.Iface aggrHandler = new AggrServiceImpl(); |
| 96 | TProcessor aggrProcessor = new Aggr.Processor( aggrHandler); |
| 97 | |
| 98 | TMultiplexedProcessor multiplex = new TMultiplexedProcessor(); |
| 99 | multiplex.RegisterProcessor(Constants.NAME_BENCHMARKSERVICE, benchProcessor); |
| 100 | multiplex.RegisterProcessor(Constants.NAME_AGGR, aggrProcessor); |
| 101 | |
| 102 | TServer ServerEngine = new TSimpleServer( multiplex, servertrans, TransportFactory, ProtocolFactory); |
| 103 | |
| 104 | (benchHandler as ITestHandler).SetServer( ServerEngine); |
| 105 | (aggrHandler as ITestHandler).SetServer( ServerEngine); |
| 106 | |
| 107 | Console.WriteLine("Starting the server ..."); |
| 108 | ServerEngine.Serve(); |
| 109 | |
| 110 | (benchHandler as ITestHandler).SetServer(null); |
| 111 | (aggrHandler as ITestHandler).SetServer(null); |
| 112 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 113 | } |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 114 | catch( Exception e) |
| 115 | { |
| 116 | Console.WriteLine( e.Message); |
| 117 | } |
| 118 | Console.WriteLine( "done."); |
| 119 | } |
| 120 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 121 | |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 122 | static void Main(string[] args) |
| 123 | { |
| 124 | Execute(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | } |
| 131 | |