blob: 61c2d7c803a447b897477d9f4924ed84ad2011de [file] [log] [blame]
Jens Geyer72034242013-05-08 18:46:57 +02001/*
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
20using System;
21using System.Collections.Generic;
22using Thrift.Collections;
23using Thrift.Transport;
24using Thrift.Protocol;
25using Thrift.Server;
26using Thrift;
27using Test.Multiplex;
28
29namespace Test.Multiplex.Server
30{
Jens Geyerd5436f52014-10-03 19:50:38 +020031 public class TestServer
32 {
Jens Geyer72034242013-05-08 18:46:57 +020033 public interface ITestHandler
34 {
35 void SetServer( TServer aServer);
36 }
37
38 protected class TestHandlerImpl : ITestHandler
39 {
40 private TServer Server;
Jens Geyerd5436f52014-10-03 19:50:38 +020041
Jens Geyer72034242013-05-08 18:46:57 +020042 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 Geyerd5436f52014-10-03 19:50:38 +020056 while( n > 0)
Jens Geyer72034242013-05-08 18:46:57 +020057 {
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 Geyerd5436f52014-10-03 19:50:38 +0200113 }
Jens Geyer72034242013-05-08 18:46:57 +0200114 catch( Exception e)
115 {
116 Console.WriteLine( e.Message);
117 }
118 Console.WriteLine( "done.");
119 }
120
Jens Geyerd5436f52014-10-03 19:50:38 +0200121
Jens Geyer72034242013-05-08 18:46:57 +0200122 static void Main(string[] args)
123 {
124 Execute();
125 }
126 }
127
128
129
130}
131