Bryan Duxbury | 96b2ccc | 2010-09-24 16:47:59 +0000 | [diff] [blame] | 1 | /* |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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. |
Bryan Duxbury | 96b2ccc | 2010-09-24 16:47:59 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | using System;
|
| 21 | using System.Collections.Generic;
|
| 22 | using Thrift.Server;
|
| 23 | using Thrift.Transport;
|
| 24 |
|
| 25 | namespace CSharpTutorial
|
| 26 | {
|
| 27 | public class CalculatorHandler : Calculator.Iface
|
| 28 | {
|
| 29 | Dictionary<int, SharedStruct> log;
|
| 30 |
|
| 31 | public CalculatorHandler()
|
| 32 | {
|
| 33 | log = new Dictionary<int, SharedStruct>();
|
| 34 | }
|
| 35 |
|
| 36 | public void ping()
|
| 37 | {
|
| 38 | Console.WriteLine("ping()");
|
| 39 | }
|
| 40 |
|
| 41 | public int add(int n1, int n2)
|
| 42 | {
|
| 43 | Console.WriteLine("add({0},{1})", n1, n2);
|
| 44 | return n1 + n2;
|
| 45 | }
|
| 46 |
|
| 47 | public int calculate(int logid, Work work)
|
| 48 | {
|
| 49 | Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.Op, work.Num1, work.Num2);
|
| 50 | int val = 0;
|
| 51 | switch (work.Op)
|
| 52 | {
|
| 53 | case Operation.ADD:
|
| 54 | val = work.Num1 + work.Num2;
|
| 55 | break;
|
| 56 |
|
| 57 | case Operation.SUBTRACT:
|
| 58 | val = work.Num1 - work.Num2;
|
| 59 | break;
|
| 60 |
|
| 61 | case Operation.MULTIPLY:
|
| 62 | val = work.Num1 * work.Num2;
|
| 63 | break;
|
| 64 |
|
| 65 | case Operation.DIVIDE:
|
| 66 | if (work.Num2 == 0)
|
| 67 | {
|
| 68 | InvalidOperation io = new InvalidOperation();
|
| 69 | io.What = (int)work.Op;
|
| 70 | io.Why = "Cannot divide by 0";
|
| 71 | throw io;
|
| 72 | }
|
| 73 | val = work.Num1 / work.Num2;
|
| 74 | break;
|
| 75 |
|
| 76 | default:
|
| 77 | {
|
| 78 | InvalidOperation io = new InvalidOperation();
|
| 79 | io.What = (int)work.Op;
|
| 80 | io.Why = "Unknown operation";
|
| 81 | throw io;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | SharedStruct entry = new SharedStruct();
|
| 86 | entry.Key = logid;
|
| 87 | entry.Value = val.ToString();
|
| 88 | log[logid] = entry;
|
| 89 |
|
| 90 | return val;
|
| 91 | }
|
| 92 |
|
| 93 | public SharedStruct getStruct(int key)
|
| 94 | {
|
| 95 | Console.WriteLine("getStruct({0})", key);
|
| 96 | return log[key];
|
| 97 | }
|
| 98 |
|
| 99 | public void zip()
|
| 100 | {
|
| 101 | Console.WriteLine("zip()");
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public class CSharpServer
|
| 106 | {
|
| 107 | public static void Main()
|
| 108 | {
|
| 109 | try
|
| 110 | {
|
| 111 | CalculatorHandler handler = new CalculatorHandler();
|
| 112 | Calculator.Processor processor = new Calculator.Processor(handler);
|
| 113 | TServerTransport serverTransport = new TServerSocket(9090);
|
| 114 | TServer server = new TSimpleServer(processor, serverTransport);
|
| 115 |
|
| 116 | // Use this for a multithreaded server
|
| 117 | // server = new TThreadPoolServer(processor, serverTransport);
|
| 118 |
|
| 119 | Console.WriteLine("Starting the server...");
|
| 120 | server.Serve();
|
| 121 | }
|
| 122 | catch (Exception x)
|
| 123 | {
|
| 124 | Console.WriteLine(x.StackTrace);
|
| 125 | }
|
| 126 | Console.WriteLine("done.");
|
| 127 | }
|
| 128 | }
|
| 129 | }
|