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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 using System.Collections.Generic;
23 using Thrift.Transport;
25 namespace CSharpTutorial
27 public class CalculatorHandler : Calculator.Iface
29 Dictionary<int, SharedStruct> log;
31 public CalculatorHandler()
33 log = new Dictionary<int, SharedStruct>();
38 Console.WriteLine("ping()");
41 public int add(int n1, int n2)
43 Console.WriteLine("add({0},{1})", n1, n2);
47 public int calculate(int logid, Work work)
49 Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.Op, work.Num1, work.Num2);
54 val = work.Num1 + work.Num2;
57 case Operation.SUBTRACT:
58 val = work.Num1 - work.Num2;
61 case Operation.MULTIPLY:
62 val = work.Num1 * work.Num2;
65 case Operation.DIVIDE:
68 InvalidOperation io = new InvalidOperation();
69 io.WhatOp = (int)work.Op;
70 io.Why = "Cannot divide by 0";
73 val = work.Num1 / work.Num2;
78 InvalidOperation io = new InvalidOperation();
79 io.WhatOp = (int)work.Op;
80 io.Why = "Unknown operation";
85 SharedStruct entry = new SharedStruct();
87 entry.Value = val.ToString();
93 public SharedStruct getStruct(int key)
95 Console.WriteLine("getStruct({0})", key);
101 Console.WriteLine("zip()");
105 public class CSharpServer
107 public static void Main()
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);
116 // Use this for a multithreaded server
117 // server = new TThreadPoolServer(processor, serverTransport);
119 Console.WriteLine("Starting the server...");
124 Console.WriteLine(x.StackTrace);
126 Console.WriteLine("done.");