David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 1 | // Distributed under the Thrift Software License |
| 2 | // |
| 3 | // See accompanying file LICENSE or visit the Thrift site at: |
| 4 | // http://developers.facebook.com/thrift/ |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 7 | using Thrift.Collections; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 8 | using Thrift.Test; //generated code |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 9 | using Thrift.Transport; |
| 10 | using Thrift.Protocol; |
| 11 | using Thrift.Server; |
| 12 | |
| 13 | namespace Test |
| 14 | { |
| 15 | public class TestServer |
| 16 | { |
| 17 | public class TestHandler : ThriftTest.Iface |
| 18 | { |
| 19 | public TServer server; |
| 20 | |
| 21 | public TestHandler() { } |
| 22 | |
| 23 | public void testVoid() |
| 24 | { |
| 25 | Console.WriteLine("testVoid()"); |
| 26 | } |
| 27 | |
| 28 | public string testString(string thing) |
| 29 | { |
| 30 | Console.WriteLine("teststring(\"" + thing + "\")"); |
| 31 | return thing; |
| 32 | } |
| 33 | |
| 34 | public byte testByte(byte thing) |
| 35 | { |
| 36 | Console.WriteLine("testByte(" + thing + ")"); |
| 37 | return thing; |
| 38 | } |
| 39 | |
| 40 | public int testI32(int thing) |
| 41 | { |
| 42 | Console.WriteLine("testI32(" + thing + ")"); |
| 43 | return thing; |
| 44 | } |
| 45 | |
| 46 | public long testI64(long thing) |
| 47 | { |
| 48 | Console.WriteLine("testI64(" + thing + ")"); |
| 49 | return thing; |
| 50 | } |
| 51 | |
| 52 | public double testDouble(double thing) |
| 53 | { |
| 54 | Console.WriteLine("testDouble(" + thing + ")"); |
| 55 | return thing; |
| 56 | } |
| 57 | |
| 58 | public Xtruct testStruct(Xtruct thing) |
| 59 | { |
| 60 | Console.WriteLine("testStruct({" + |
| 61 | "\"" + thing.String_thing + "\", " + |
| 62 | thing.Byte_thing + ", " + |
| 63 | thing.I32_thing + ", " + |
| 64 | thing.I64_thing + "})"); |
| 65 | return thing; |
| 66 | } |
| 67 | |
| 68 | public Xtruct2 testNest(Xtruct2 nest) |
| 69 | { |
| 70 | Xtruct thing = nest.Struct_thing; |
| 71 | Console.WriteLine("testNest({" + |
| 72 | nest.Byte_thing + ", {" + |
| 73 | "\"" + thing.String_thing + "\", " + |
| 74 | thing.Byte_thing + ", " + |
| 75 | thing.I32_thing + ", " + |
| 76 | thing.I64_thing + "}, " + |
| 77 | nest.I32_thing + "})"); |
| 78 | return nest; |
| 79 | } |
| 80 | |
| 81 | public Dictionary<int, int> testMap(Dictionary<int, int> thing) |
| 82 | { |
| 83 | Console.WriteLine("testMap({"); |
| 84 | bool first = true; |
| 85 | foreach (int key in thing.Keys) |
| 86 | { |
| 87 | if (first) |
| 88 | { |
| 89 | first = false; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | Console.WriteLine(", "); |
| 94 | } |
| 95 | Console.WriteLine(key + " => " + thing[key]); |
| 96 | } |
| 97 | Console.WriteLine("})"); |
| 98 | return thing; |
| 99 | } |
| 100 | |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 101 | public THashSet<int> testSet(THashSet<int> thing) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 102 | { |
| 103 | Console.WriteLine("testSet({"); |
| 104 | bool first = true; |
| 105 | foreach (int elem in thing) |
| 106 | { |
| 107 | if (first) |
| 108 | { |
| 109 | first = false; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | Console.WriteLine(", "); |
| 114 | } |
| 115 | Console.WriteLine(elem); |
| 116 | } |
| 117 | Console.WriteLine("})"); |
| 118 | return thing; |
| 119 | } |
| 120 | |
| 121 | public List<int> testList(List<int> thing) |
| 122 | { |
| 123 | Console.WriteLine("testList({"); |
| 124 | bool first = true; |
| 125 | foreach (int elem in thing) |
| 126 | { |
| 127 | if (first) |
| 128 | { |
| 129 | first = false; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | Console.WriteLine(", "); |
| 134 | } |
| 135 | Console.WriteLine(elem); |
| 136 | } |
| 137 | Console.WriteLine("})"); |
| 138 | return thing; |
| 139 | } |
| 140 | |
| 141 | public Numberz testEnum(Numberz thing) |
| 142 | { |
| 143 | Console.WriteLine("testEnum(" + thing + ")"); |
| 144 | return thing; |
| 145 | } |
| 146 | |
| 147 | public long testTypedef(long thing) |
| 148 | { |
| 149 | Console.WriteLine("testTypedef(" + thing + ")"); |
| 150 | return thing; |
| 151 | } |
| 152 | |
| 153 | public Dictionary<int, Dictionary<int, int>> testMapMap(int hello) |
| 154 | { |
| 155 | Console.WriteLine("testMapMap(" + hello + ")"); |
| 156 | Dictionary<int, Dictionary<int, int>> mapmap = |
| 157 | new Dictionary<int, Dictionary<int, int>>(); |
| 158 | |
| 159 | Dictionary<int, int> pos = new Dictionary<int, int>(); |
| 160 | Dictionary<int, int> neg = new Dictionary<int, int>(); |
| 161 | for (int i = 1; i < 5; i++) |
| 162 | { |
| 163 | pos[i] = i; |
| 164 | neg[-i] = -i; |
| 165 | } |
| 166 | |
| 167 | mapmap[4] = pos; |
| 168 | mapmap[-4] = neg; |
| 169 | |
| 170 | return mapmap; |
| 171 | } |
| 172 | |
| 173 | public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument) |
| 174 | { |
| 175 | Console.WriteLine("testInsanity()"); |
| 176 | |
| 177 | Xtruct hello = new Xtruct(); |
| 178 | hello.String_thing = "Hello2"; |
| 179 | hello.Byte_thing = 2; |
| 180 | hello.I32_thing = 2; |
| 181 | hello.I64_thing = 2; |
| 182 | |
| 183 | Xtruct goodbye = new Xtruct(); |
| 184 | goodbye.String_thing = "Goodbye4"; |
| 185 | goodbye.Byte_thing = (byte)4; |
| 186 | goodbye.I32_thing = 4; |
| 187 | goodbye.I64_thing = (long)4; |
| 188 | |
| 189 | Insanity crazy = new Insanity(); |
| 190 | crazy.UserMap = new Dictionary<Numberz, long>(); |
| 191 | crazy.UserMap[Numberz.EIGHT] = (long)8; |
| 192 | crazy.Xtructs = new List<Xtruct>(); |
| 193 | crazy.Xtructs.Add(goodbye); |
| 194 | |
| 195 | Insanity looney = new Insanity(); |
| 196 | crazy.UserMap[Numberz.FIVE] = (long)5; |
| 197 | crazy.Xtructs.Add(hello); |
| 198 | |
| 199 | Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>(); |
| 200 | Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ; |
| 201 | |
| 202 | first_map[Numberz.TWO] = crazy; |
| 203 | first_map[Numberz.THREE] = crazy; |
| 204 | |
| 205 | second_map[Numberz.SIX] = looney; |
| 206 | |
| 207 | Dictionary<long, Dictionary<Numberz, Insanity>> insane = |
| 208 | new Dictionary<long, Dictionary<Numberz, Insanity>>(); |
| 209 | insane[(long)1] = first_map; |
| 210 | insane[(long)2] = second_map; |
| 211 | |
| 212 | return insane; |
| 213 | } |
| 214 | |
| 215 | public Xtruct testMulti(byte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5) |
| 216 | { |
| 217 | Console.WriteLine("testMulti()"); |
| 218 | |
| 219 | Xtruct hello = new Xtruct(); ; |
| 220 | hello.String_thing = "Hello2"; |
| 221 | hello.Byte_thing = arg0; |
| 222 | hello.I32_thing = arg1; |
| 223 | hello.I64_thing = arg2; |
| 224 | return hello; |
| 225 | } |
| 226 | |
| 227 | public void testException(string arg) |
| 228 | { |
| 229 | Console.WriteLine("testException(" + arg + ")"); |
| 230 | if (arg == "Xception") |
| 231 | { |
| 232 | Xception x = new Xception(); |
| 233 | x.ErrorCode = 1001; |
| 234 | x.Message = "This is an Xception"; |
| 235 | throw x; |
| 236 | } |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | public Xtruct testMultiException(string arg0, string arg1) |
| 241 | { |
| 242 | Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")"); |
| 243 | if (arg0 == "Xception") |
| 244 | { |
| 245 | Xception x = new Xception(); |
| 246 | x.ErrorCode = 1001; |
| 247 | x.Message = "This is an Xception"; |
| 248 | throw x; |
| 249 | } |
| 250 | else if (arg0 == "Xception2") |
| 251 | { |
| 252 | Xception2 x = new Xception2(); |
| 253 | x.ErrorCode = 2002; |
| 254 | x.Struct_thing = new Xtruct(); |
| 255 | x.Struct_thing.String_thing = "This is an Xception2"; |
| 256 | throw x; |
| 257 | } |
| 258 | |
| 259 | Xtruct result = new Xtruct(); |
| 260 | result.String_thing = arg1; |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | public void testStop() |
| 265 | { |
| 266 | if (server != null) |
| 267 | { |
| 268 | server.Stop(); |
| 269 | } |
| 270 | } |
| 271 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame^] | 272 | public void testOneway(int arg) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 273 | { |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame^] | 274 | Console.WriteLine("testOneway(" + arg + "), sleeping..."); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 275 | System.Threading.Thread.Sleep(arg * 1000); |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame^] | 276 | Console.WriteLine("testOneway finished"); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } // class TestHandler |
| 280 | |
| 281 | public static void Execute(string[] args) |
| 282 | { |
| 283 | try |
| 284 | { |
| 285 | bool useBufferedSockets = false; |
| 286 | int port = 9090; |
| 287 | if (args.Length > 0) |
| 288 | { |
| 289 | port = int.Parse(args[0]); |
| 290 | |
| 291 | if (args.Length > 1) |
| 292 | { |
| 293 | bool.TryParse(args[1], out useBufferedSockets); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Processor |
| 298 | TestHandler testHandler = new TestHandler(); |
| 299 | ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler); |
| 300 | |
| 301 | // Transport |
| 302 | TServerSocket tServerSocket = new TServerSocket(port, 0, useBufferedSockets); |
| 303 | |
| 304 | TServer serverEngine; |
| 305 | |
| 306 | // Simple Server |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 307 | serverEngine = new TSimpleServer(testProcessor, tServerSocket); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 308 | |
| 309 | // ThreadPool Server |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 310 | // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket); |
| 311 | |
| 312 | // Threaded Server |
| 313 | // serverEngine = new TThreadedServer(testProcessor, tServerSocket); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 314 | |
| 315 | testHandler.server = serverEngine; |
| 316 | |
| 317 | // Run it |
| 318 | Console.WriteLine("Starting the server on port " + port + (useBufferedSockets ? " with buffered socket" : "") + "..."); |
| 319 | serverEngine.Serve(); |
| 320 | |
| 321 | } |
| 322 | catch (Exception x) |
| 323 | { |
| 324 | Console.Error.Write(x); |
| 325 | } |
| 326 | Console.WriteLine("done."); |
| 327 | } |
| 328 | } |
| 329 | } |