Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
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
 
20
import org.apache.thrift.TException;
21
import org.apache.thrift.protocol.TBinaryProtocol;
22
import org.apache.thrift.protocol.TProtocol;
23
import org.apache.thrift.server.TServer;
24
import org.apache.thrift.server.TSimpleServer;
25
import org.apache.thrift.transport.TServerSocket;
26
import org.apache.thrift.transport.TServerTransport;
27
 
28
// Generated code
29
import tutorial.*;
30
import shared.*;
31
 
32
import java.util.HashMap;
33
 
34
public class JavaServer {
35
 
36
  public static class CalculatorHandler implements Calculator.Iface {
37
 
38
    private HashMap<Integer,SharedStruct> log;
39
 
40
    public CalculatorHandler() {
41
      log = new HashMap<Integer, SharedStruct>();
42
    }
43
 
44
    public void ping() {
45
      System.out.println("ping()");
46
    }
47
 
48
    public int add(int n1, int n2) {
49
      System.out.println("add(" + n1 + "," + n2 + ")");
50
      return n1 + n2;
51
    }
52
 
53
    public int calculate(int logid, Work work) throws InvalidOperation {
54
      System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})");
55
      int val = 0;
56
      switch (work.op) {
57
      case Operation.ADD:
58
        val = work.num1 + work.num2;
59
        break;
60
      case Operation.SUBTRACT:
61
        val = work.num1 - work.num2;
62
        break;
63
      case Operation.MULTIPLY:
64
        val = work.num1 * work.num2;
65
        break;
66
      case Operation.DIVIDE:
67
        if (work.num2 == 0) {
68
          InvalidOperation io = new InvalidOperation();
69
          io.what = work.op;
70
          io.why = "Cannot divide by 0";
71
          throw io;
72
        }
73
        val = work.num1 / work.num2;
74
        break;
75
      default:
76
        InvalidOperation io = new InvalidOperation();
77
        io.what = work.op;
78
        io.why = "Unknown operation";
79
        throw io;
80
      }
81
 
82
      SharedStruct entry = new SharedStruct();
83
      entry.key = logid;
84
      entry.value = Integer.toString(val);
85
      log.put(logid, entry);
86
 
87
      return val;
88
    }
89
 
90
    public SharedStruct getStruct(int key) {
91
      System.out.println("getStruct(" + key + ")");
92
      return log.get(key);
93
    }
94
 
95
    public void zip() {
96
      System.out.println("zip()");
97
    }
98
 
99
  }
100
 
101
  public static void main(String [] args) {
102
    try {
103
      CalculatorHandler handler = new CalculatorHandler();
104
      Calculator.Processor processor = new Calculator.Processor(handler);
105
      TServerTransport serverTransport = new TServerSocket(9090);
106
      TServer server = new TSimpleServer(processor, serverTransport);
107
 
108
      // Use this for a multithreaded server
109
      // server = new TThreadPoolServer(processor, serverTransport);
110
 
111
      System.out.println("Starting the server...");
112
      server.serve();
113
 
114
    } catch (Exception x) {
115
      x.printStackTrace();
116
    }
117
    System.out.println("done.");
118
  }
119
}