Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame^] | 1 | import org.apache.thrift.TException; |
| 2 | import org.apache.thrift.protocol.TBinaryProtocol; |
| 3 | import org.apache.thrift.protocol.TProtocol; |
| 4 | import org.apache.thrift.server.TServer; |
| 5 | import org.apache.thrift.server.TSimpleServer; |
| 6 | import org.apache.thrift.transport.TServerSocket; |
| 7 | import org.apache.thrift.transport.TServerTransport; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 8 | |
| 9 | // Generated code |
| 10 | import tutorial.*; |
| 11 | import shared.*; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | |
| 15 | public 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 | } |