blob: f9ab8fd286e8f93cefa2980dd8a4dcc590d4ae80 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Reiss06a93122009-03-30 21:22:52 +000020using System;
21using System.Collections.Generic;
22using Thrift.Server;
23using Thrift.Transport;
24
25namespace 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}