David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 20 | // Distributed under the Thrift Software License |
| 21 | // |
| 22 | // See accompanying file LICENSE or visit the Thrift site at: |
| 23 | // http://developers.facebook.com/thrift/ |
| 24 | using System; |
| 25 | using System.Collections.Generic; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 26 | using System.Security.Cryptography.X509Certificates; |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 27 | using Thrift.Collections; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 28 | using Thrift.Test; //generated code |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 29 | using Thrift.Transport; |
| 30 | using Thrift.Protocol; |
| 31 | using Thrift.Server; |
| 32 | |
| 33 | namespace Test |
| 34 | { |
| 35 | public class TestServer |
| 36 | { |
| 37 | public class TestHandler : ThriftTest.Iface |
| 38 | { |
| 39 | public TServer server; |
| 40 | |
| 41 | public TestHandler() { } |
| 42 | |
| 43 | public void testVoid() |
| 44 | { |
| 45 | Console.WriteLine("testVoid()"); |
| 46 | } |
| 47 | |
| 48 | public string testString(string thing) |
| 49 | { |
| 50 | Console.WriteLine("teststring(\"" + thing + "\")"); |
| 51 | return thing; |
| 52 | } |
| 53 | |
Jens Geyer | 1b4c9b9 | 2013-04-26 23:38:58 +0200 | [diff] [blame] | 54 | public sbyte testByte(sbyte thing) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 55 | { |
| 56 | Console.WriteLine("testByte(" + thing + ")"); |
| 57 | return thing; |
| 58 | } |
| 59 | |
| 60 | public int testI32(int thing) |
| 61 | { |
| 62 | Console.WriteLine("testI32(" + thing + ")"); |
| 63 | return thing; |
| 64 | } |
| 65 | |
| 66 | public long testI64(long thing) |
| 67 | { |
| 68 | Console.WriteLine("testI64(" + thing + ")"); |
| 69 | return thing; |
| 70 | } |
| 71 | |
| 72 | public double testDouble(double thing) |
| 73 | { |
| 74 | Console.WriteLine("testDouble(" + thing + ")"); |
| 75 | return thing; |
| 76 | } |
| 77 | |
| 78 | public Xtruct testStruct(Xtruct thing) |
| 79 | { |
| 80 | Console.WriteLine("testStruct({" + |
| 81 | "\"" + thing.String_thing + "\", " + |
| 82 | thing.Byte_thing + ", " + |
| 83 | thing.I32_thing + ", " + |
| 84 | thing.I64_thing + "})"); |
| 85 | return thing; |
| 86 | } |
| 87 | |
| 88 | public Xtruct2 testNest(Xtruct2 nest) |
| 89 | { |
| 90 | Xtruct thing = nest.Struct_thing; |
| 91 | Console.WriteLine("testNest({" + |
| 92 | nest.Byte_thing + ", {" + |
| 93 | "\"" + thing.String_thing + "\", " + |
| 94 | thing.Byte_thing + ", " + |
| 95 | thing.I32_thing + ", " + |
| 96 | thing.I64_thing + "}, " + |
| 97 | nest.I32_thing + "})"); |
| 98 | return nest; |
| 99 | } |
| 100 | |
| 101 | public Dictionary<int, int> testMap(Dictionary<int, int> thing) |
| 102 | { |
| 103 | Console.WriteLine("testMap({"); |
| 104 | bool first = true; |
| 105 | foreach (int key in thing.Keys) |
| 106 | { |
| 107 | if (first) |
| 108 | { |
| 109 | first = false; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | Console.WriteLine(", "); |
| 114 | } |
| 115 | Console.WriteLine(key + " => " + thing[key]); |
| 116 | } |
| 117 | Console.WriteLine("})"); |
| 118 | return thing; |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 121 | public Dictionary<string, string> testStringMap(Dictionary<string, string> thing) |
| 122 | { |
| 123 | Console.WriteLine("testStringMap({"); |
| 124 | bool first = true; |
| 125 | foreach (string key in thing.Keys) |
| 126 | { |
| 127 | if (first) |
| 128 | { |
| 129 | first = false; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | Console.WriteLine(", "); |
| 134 | } |
| 135 | Console.WriteLine(key + " => " + thing[key]); |
| 136 | } |
| 137 | Console.WriteLine("})"); |
| 138 | return thing; |
| 139 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 140 | |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 141 | public THashSet<int> testSet(THashSet<int> thing) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 142 | { |
| 143 | Console.WriteLine("testSet({"); |
| 144 | bool first = true; |
| 145 | foreach (int elem in thing) |
| 146 | { |
| 147 | if (first) |
| 148 | { |
| 149 | first = false; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | Console.WriteLine(", "); |
| 154 | } |
| 155 | Console.WriteLine(elem); |
| 156 | } |
| 157 | Console.WriteLine("})"); |
| 158 | return thing; |
| 159 | } |
| 160 | |
| 161 | public List<int> testList(List<int> thing) |
| 162 | { |
| 163 | Console.WriteLine("testList({"); |
| 164 | bool first = true; |
| 165 | foreach (int elem in thing) |
| 166 | { |
| 167 | if (first) |
| 168 | { |
| 169 | first = false; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | Console.WriteLine(", "); |
| 174 | } |
| 175 | Console.WriteLine(elem); |
| 176 | } |
| 177 | Console.WriteLine("})"); |
| 178 | return thing; |
| 179 | } |
| 180 | |
| 181 | public Numberz testEnum(Numberz thing) |
| 182 | { |
| 183 | Console.WriteLine("testEnum(" + thing + ")"); |
| 184 | return thing; |
| 185 | } |
| 186 | |
| 187 | public long testTypedef(long thing) |
| 188 | { |
| 189 | Console.WriteLine("testTypedef(" + thing + ")"); |
| 190 | return thing; |
| 191 | } |
| 192 | |
| 193 | public Dictionary<int, Dictionary<int, int>> testMapMap(int hello) |
| 194 | { |
| 195 | Console.WriteLine("testMapMap(" + hello + ")"); |
| 196 | Dictionary<int, Dictionary<int, int>> mapmap = |
| 197 | new Dictionary<int, Dictionary<int, int>>(); |
| 198 | |
| 199 | Dictionary<int, int> pos = new Dictionary<int, int>(); |
| 200 | Dictionary<int, int> neg = new Dictionary<int, int>(); |
| 201 | for (int i = 1; i < 5; i++) |
| 202 | { |
| 203 | pos[i] = i; |
| 204 | neg[-i] = -i; |
| 205 | } |
| 206 | |
| 207 | mapmap[4] = pos; |
| 208 | mapmap[-4] = neg; |
| 209 | |
| 210 | return mapmap; |
| 211 | } |
| 212 | |
| 213 | public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument) |
| 214 | { |
| 215 | Console.WriteLine("testInsanity()"); |
| 216 | |
| 217 | Xtruct hello = new Xtruct(); |
| 218 | hello.String_thing = "Hello2"; |
| 219 | hello.Byte_thing = 2; |
| 220 | hello.I32_thing = 2; |
| 221 | hello.I64_thing = 2; |
| 222 | |
| 223 | Xtruct goodbye = new Xtruct(); |
| 224 | goodbye.String_thing = "Goodbye4"; |
Jens Geyer | 1b4c9b9 | 2013-04-26 23:38:58 +0200 | [diff] [blame] | 225 | goodbye.Byte_thing = (sbyte)4; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 226 | goodbye.I32_thing = 4; |
| 227 | goodbye.I64_thing = (long)4; |
| 228 | |
| 229 | Insanity crazy = new Insanity(); |
| 230 | crazy.UserMap = new Dictionary<Numberz, long>(); |
| 231 | crazy.UserMap[Numberz.EIGHT] = (long)8; |
| 232 | crazy.Xtructs = new List<Xtruct>(); |
| 233 | crazy.Xtructs.Add(goodbye); |
| 234 | |
| 235 | Insanity looney = new Insanity(); |
| 236 | crazy.UserMap[Numberz.FIVE] = (long)5; |
| 237 | crazy.Xtructs.Add(hello); |
| 238 | |
| 239 | Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>(); |
| 240 | Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ; |
| 241 | |
| 242 | first_map[Numberz.TWO] = crazy; |
| 243 | first_map[Numberz.THREE] = crazy; |
| 244 | |
| 245 | second_map[Numberz.SIX] = looney; |
| 246 | |
| 247 | Dictionary<long, Dictionary<Numberz, Insanity>> insane = |
| 248 | new Dictionary<long, Dictionary<Numberz, Insanity>>(); |
| 249 | insane[(long)1] = first_map; |
| 250 | insane[(long)2] = second_map; |
| 251 | |
| 252 | return insane; |
| 253 | } |
| 254 | |
Jens Geyer | 1b4c9b9 | 2013-04-26 23:38:58 +0200 | [diff] [blame] | 255 | public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 256 | { |
| 257 | Console.WriteLine("testMulti()"); |
| 258 | |
| 259 | Xtruct hello = new Xtruct(); ; |
| 260 | hello.String_thing = "Hello2"; |
| 261 | hello.Byte_thing = arg0; |
| 262 | hello.I32_thing = arg1; |
| 263 | hello.I64_thing = arg2; |
| 264 | return hello; |
| 265 | } |
| 266 | |
| 267 | public void testException(string arg) |
| 268 | { |
| 269 | Console.WriteLine("testException(" + arg + ")"); |
| 270 | if (arg == "Xception") |
| 271 | { |
| 272 | Xception x = new Xception(); |
| 273 | x.ErrorCode = 1001; |
| 274 | x.Message = "This is an Xception"; |
| 275 | throw x; |
| 276 | } |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | public Xtruct testMultiException(string arg0, string arg1) |
| 281 | { |
| 282 | Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")"); |
| 283 | if (arg0 == "Xception") |
| 284 | { |
| 285 | Xception x = new Xception(); |
| 286 | x.ErrorCode = 1001; |
| 287 | x.Message = "This is an Xception"; |
| 288 | throw x; |
| 289 | } |
| 290 | else if (arg0 == "Xception2") |
| 291 | { |
| 292 | Xception2 x = new Xception2(); |
| 293 | x.ErrorCode = 2002; |
| 294 | x.Struct_thing = new Xtruct(); |
| 295 | x.Struct_thing.String_thing = "This is an Xception2"; |
| 296 | throw x; |
| 297 | } |
| 298 | |
| 299 | Xtruct result = new Xtruct(); |
| 300 | result.String_thing = arg1; |
| 301 | return result; |
| 302 | } |
| 303 | |
| 304 | public void testStop() |
| 305 | { |
| 306 | if (server != null) |
| 307 | { |
| 308 | server.Stop(); |
| 309 | } |
| 310 | } |
| 311 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 312 | public void testOneway(int arg) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 313 | { |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 314 | Console.WriteLine("testOneway(" + arg + "), sleeping..."); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 315 | System.Threading.Thread.Sleep(arg * 1000); |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 316 | Console.WriteLine("testOneway finished"); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | } // class TestHandler |
| 320 | |
| 321 | public static void Execute(string[] args) |
| 322 | { |
| 323 | try |
| 324 | { |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 325 | bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false; |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 326 | int port = 9090; |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 327 | string pipe = null; |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 328 | for (int i = 0; i < args.Length; i++) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 329 | { |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 330 | if (args[i] == "-pipe") // -pipe name |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 331 | { |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 332 | pipe = args[++i]; |
| 333 | } |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 334 | else if (args[i].Contains("--port=")) |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 335 | { |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 336 | port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1)); |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 337 | } |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 338 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 339 | { |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 340 | useBufferedSockets = true; |
| 341 | } |
| 342 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 343 | { |
| 344 | useFramed = true; |
| 345 | } |
| 346 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 347 | { |
| 348 | compact = true; |
| 349 | } |
| 350 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 351 | { |
| 352 | json = true; |
| 353 | } |
| 354 | else if (args[i] == "--ssl") |
| 355 | { |
| 356 | useEncryption = true; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | // Processor |
| 361 | TestHandler testHandler = new TestHandler(); |
| 362 | ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler); |
| 363 | |
| 364 | // Transport |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 365 | TServerTransport trans; |
| 366 | if( pipe != null) |
| 367 | { |
| 368 | trans = new TNamedPipeServerTransport(pipe); |
| 369 | } |
| 370 | else |
| 371 | { |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 372 | if (useEncryption) |
| 373 | { |
| 374 | trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem")); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | trans = new TServerSocket(port, 0, useBufferedSockets); |
| 379 | } |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 380 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 381 | |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 382 | TProtocolFactory proto; |
| 383 | if ( compact ) |
| 384 | proto = new TCompactProtocol.Factory(); |
| 385 | else if ( json ) |
| 386 | proto = new TJSONProtocol.Factory(); |
| 387 | else |
| 388 | proto = new TBinaryProtocol.Factory(); |
| 389 | |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 390 | // Simple Server |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame] | 391 | TServer serverEngine; |
| 392 | if ( useFramed ) |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 393 | serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto); |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame] | 394 | else |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 395 | serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 396 | |
| 397 | // ThreadPool Server |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 398 | // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket); |
| 399 | |
| 400 | // Threaded Server |
| 401 | // serverEngine = new TThreadedServer(testProcessor, tServerSocket); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 402 | |
| 403 | testHandler.server = serverEngine; |
| 404 | |
| 405 | // Run it |
Jens Geyer | fd62df7 | 2014-03-20 00:52:18 +0200 | [diff] [blame] | 406 | string where = ( pipe != null ? "on pipe "+pipe : "on port " + port); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 407 | Console.WriteLine("Starting the server " + where + |
Jens Geyer | 2a9e6a4 | 2014-05-02 22:23:15 +0200 | [diff] [blame^] | 408 | (useBufferedSockets ? " with buffered socket" : "") + |
| 409 | (useFramed ? " with framed transport" : "") + |
| 410 | (useEncryption ? " with encryption" : "") + |
| 411 | (compact ? " with compact protocol" : "") + |
| 412 | (json ? " with json protocol" : "") + |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame] | 413 | "..."); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 414 | serverEngine.Serve(); |
| 415 | |
| 416 | } |
| 417 | catch (Exception x) |
| 418 | { |
| 419 | Console.Error.Write(x); |
| 420 | } |
| 421 | Console.WriteLine("done."); |
| 422 | } |
| 423 | } |
| 424 | } |