David Reiss | 3095aa1 | 2008-04-02 22:10:17 +0000 | [diff] [blame] | 1 | using System;
|
| 2 | using System.Collections.Generic;
|
| 3 | using System.Linq;
|
| 4 | using System.Text;
|
| 5 | using Thrift.Server;
|
| 6 | using Thrift.Transport;
|
| 7 |
|
| 8 | namespace CSharpTutorial
|
| 9 | {
|
| 10 | public class CalculatorHandler : Calculator.Iface
|
| 11 | {
|
| 12 | Dictionary<int, SharedStruct> log;
|
| 13 |
|
| 14 | public CalculatorHandler()
|
| 15 | {
|
| 16 | log = new Dictionary<int, SharedStruct>();
|
| 17 | }
|
| 18 |
|
| 19 | public void ping()
|
| 20 | {
|
| 21 | Console.WriteLine("ping()");
|
| 22 | }
|
| 23 |
|
| 24 | public int add(int n1, int n2)
|
| 25 | {
|
| 26 | Console.WriteLine("add({0},{1})", n1, n2);
|
| 27 | return n1 + n2;
|
| 28 | }
|
| 29 |
|
| 30 | public int calculate(int logid, Work work)
|
| 31 | {
|
| 32 | Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.op, work.num1, work.num2);
|
| 33 | int val = 0;
|
| 34 | switch (work.op)
|
| 35 | {
|
| 36 | case Operation.ADD:
|
| 37 | val = work.num1 + work.num2;
|
| 38 | break;
|
| 39 |
|
| 40 | case Operation.SUBTRACT:
|
| 41 | val = work.num1 - work.num2;
|
| 42 | break;
|
| 43 |
|
| 44 | case Operation.MULTIPLY:
|
| 45 | val = work.num1 * work.num2;
|
| 46 | break;
|
| 47 |
|
| 48 | case Operation.DIVIDE:
|
| 49 | if (work.num2 == 0)
|
| 50 | {
|
| 51 | InvalidOperation io = new InvalidOperation();
|
| 52 | io.what = (int)work.op;
|
| 53 | io.why = "Cannot divide by 0";
|
| 54 | throw io;
|
| 55 | }
|
| 56 | val = work.num1 / work.num2;
|
| 57 | break;
|
| 58 |
|
| 59 | default:
|
| 60 | {
|
| 61 | InvalidOperation io = new InvalidOperation();
|
| 62 | io.what = (int)work.op;
|
| 63 | io.why = "Unknown operation";
|
| 64 | throw io;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | SharedStruct entry = new SharedStruct();
|
| 69 | entry.key = logid;
|
| 70 | entry.value = val.ToString();
|
| 71 | log[logid] = entry;
|
| 72 |
|
| 73 | return val;
|
| 74 | }
|
| 75 |
|
| 76 | public SharedStruct getStruct(int key)
|
| 77 | {
|
| 78 | Console.WriteLine("getStruct({0})", key);
|
| 79 | return log[key];
|
| 80 | }
|
| 81 |
|
| 82 | public void zip()
|
| 83 | {
|
| 84 | Console.WriteLine("zip()");
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | public class CSharpServer
|
| 89 | {
|
| 90 | public static void Main()
|
| 91 | {
|
| 92 | try
|
| 93 | {
|
| 94 | CalculatorHandler handler = new CalculatorHandler();
|
| 95 | Calculator.Processor processor = new Calculator.Processor(handler);
|
| 96 | TServerTransport serverTransport = new TServerSocket(9090);
|
| 97 | TServer server = new TSimpleServer(processor, serverTransport);
|
| 98 |
|
| 99 | // Use this for a multithreaded server
|
| 100 | // server = new TThreadPoolServer(processor, serverTransport);
|
| 101 |
|
| 102 | Console.WriteLine("Starting the server...");
|
| 103 | server.Serve();
|
| 104 | }
|
| 105 | catch (Exception x)
|
| 106 | {
|
| 107 | Console.WriteLine(x.StackTrace);
|
| 108 | }
|
| 109 | Console.WriteLine("done.");
|
| 110 | }
|
| 111 | }
|
| 112 | }
|