blob: 631fb9a3d7ede8e09b761165e64d9fd00e52c57c [file] [log] [blame]
Bryan Duxburyafa80ea2009-01-15 23:47:51 +00001import org.apache.thrift.TException;
2import org.apache.thrift.protocol.TBinaryProtocol;
3import org.apache.thrift.protocol.TProtocol;
4import org.apache.thrift.server.TServer;
5import org.apache.thrift.server.TSimpleServer;
6import org.apache.thrift.transport.TServerSocket;
7import org.apache.thrift.transport.TServerTransport;
Mark Sleecb39f082007-04-10 02:30:30 +00008
9// Generated code
10import tutorial.*;
11import shared.*;
12
13import java.util.HashMap;
14
15public class JavaServer {
16
17 public static class CalculatorHandler implements Calculator.Iface {
18
19 private HashMap<Integer,SharedStruct> log;
20
21 public CalculatorHandler() {
22 log = new HashMap<Integer, SharedStruct>();
23 }
24
25 public void ping() {
26 System.out.println("ping()");
27 }
28
29 public int add(int n1, int n2) {
30 System.out.println("add(" + n1 + "," + n2 + ")");
31 return n1 + n2;
32 }
33
34 public int calculate(int logid, Work work) throws InvalidOperation {
35 System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})");
36 int val = 0;
37 switch (work.op) {
38 case Operation.ADD:
39 val = work.num1 + work.num2;
40 break;
41 case Operation.SUBTRACT:
42 val = work.num1 - work.num2;
43 break;
44 case Operation.MULTIPLY:
45 val = work.num1 * work.num2;
46 break;
47 case Operation.DIVIDE:
48 if (work.num2 == 0) {
49 InvalidOperation io = new InvalidOperation();
50 io.what = work.op;
51 io.why = "Cannot divide by 0";
52 throw io;
53 }
54 val = work.num1 / work.num2;
55 break;
56 default:
57 InvalidOperation io = new InvalidOperation();
58 io.what = work.op;
59 io.why = "Unknown operation";
60 throw io;
61 }
62
63 SharedStruct entry = new SharedStruct();
64 entry.key = logid;
65 entry.value = Integer.toString(val);
66 log.put(logid, entry);
67
68 return val;
69 }
70
71 public SharedStruct getStruct(int key) {
72 System.out.println("getStruct(" + key + ")");
73 return log.get(key);
74 }
75
76 public void zip() {
77 System.out.println("zip()");
78 }
79
80 }
81
82 public static void main(String [] args) {
83 try {
84 CalculatorHandler handler = new CalculatorHandler();
85 Calculator.Processor processor = new Calculator.Processor(handler);
86 TServerTransport serverTransport = new TServerSocket(9090);
87 TServer server = new TSimpleServer(processor, serverTransport);
88
89 // Use this for a multithreaded server
90 // server = new TThreadPoolServer(processor, serverTransport);
91
92 System.out.println("Starting the server...");
93 server.serve();
94
95 } catch (Exception x) {
96 x.printStackTrace();
97 }
98 System.out.println("done.");
99 }
100}