blob: d75b5532f020e0aa2215a2f54c43b53d2035ab64 [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
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/concurrency/ThreadManager.h>
21#include <thrift/concurrency/PosixThreadFactory.h>
22#include <thrift/protocol/TBinaryProtocol.h>
23#include <thrift/server/TSimpleServer.h>
24#include <thrift/server/TThreadPoolServer.h>
25#include <thrift/server/TThreadedServer.h>
26#include <thrift/transport/TServerSocket.h>
27#include <thrift/transport/TTransportUtils.h>
Mark Slee07a3aab2007-03-07 05:45:10 +000028
29#include <iostream>
30#include <stdexcept>
31#include <sstream>
32
33#include "../gen-cpp/Calculator.h"
34
35using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000036using namespace apache::thrift;
37using namespace apache::thrift::protocol;
38using namespace apache::thrift::transport;
39using namespace apache::thrift::server;
Mark Slee07a3aab2007-03-07 05:45:10 +000040
41using namespace tutorial;
42using namespace shared;
43
Mark Slee07a3aab2007-03-07 05:45:10 +000044class CalculatorHandler : public CalculatorIf {
45 public:
46 CalculatorHandler() {}
47
48 void ping() {
49 printf("ping()\n");
50 }
51
52 int32_t add(const int32_t n1, const int32_t n2) {
53 printf("add(%d,%d)\n", n1, n2);
54 return n1 + n2;
55 }
56
57 int32_t calculate(const int32_t logid, const Work &work) {
58 printf("calculate(%d,{%d,%d,%d})\n", logid, work.op, work.num1, work.num2);
59 int32_t val;
60
61 switch (work.op) {
Bryan Duxbury833ae492010-09-27 17:26:02 +000062 case Operation::ADD:
Mark Slee07a3aab2007-03-07 05:45:10 +000063 val = work.num1 + work.num2;
64 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000065 case Operation::SUBTRACT:
Mark Slee07a3aab2007-03-07 05:45:10 +000066 val = work.num1 - work.num2;
67 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000068 case Operation::MULTIPLY:
Mark Slee07a3aab2007-03-07 05:45:10 +000069 val = work.num1 * work.num2;
70 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000071 case Operation::DIVIDE:
Mark Slee07a3aab2007-03-07 05:45:10 +000072 if (work.num2 == 0) {
73 InvalidOperation io;
74 io.what = work.op;
75 io.why = "Cannot divide by 0";
76 throw io;
77 }
78 val = work.num1 / work.num2;
79 break;
80 default:
81 InvalidOperation io;
82 io.what = work.op;
83 io.why = "Invalid Operation";
84 throw io;
85 }
86
87 SharedStruct ss;
88 ss.key = logid;
David Reissf33c7c62009-03-26 06:15:26 +000089 char buffer[12];
90 snprintf(buffer, sizeof(buffer), "%d", val);
Mark Slee0f2916c2007-04-11 09:16:23 +000091 ss.value = buffer;
Mark Slee07a3aab2007-03-07 05:45:10 +000092
93 log[logid] = ss;
94
95 return val;
96 }
97
98 void getStruct(SharedStruct &ret, const int32_t logid) {
99 printf("getStruct(%d)\n", logid);
100 ret = log[logid];
101 }
102
103 void zip() {
104 printf("zip()\n");
105 }
106
107protected:
108 map<int32_t, SharedStruct> log;
109
110};
111
112int main(int argc, char **argv) {
113
Roger Meier5f2d34e2013-11-16 16:43:41 +0100114 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
115 boost::shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
116 boost::shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
117 boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
118 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Mark Slee07a3aab2007-03-07 05:45:10 +0000119
120 TSimpleServer server(processor,
121 serverTransport,
122 transportFactory,
123 protocolFactory);
124
125
126 /**
127 * Or you could do one of these
128
Roger Meier5f2d34e2013-11-16 16:43:41 +0100129 boost::shared_ptr<ThreadManager> threadManager =
Mark Slee07a3aab2007-03-07 05:45:10 +0000130 ThreadManager::newSimpleThreadManager(workerCount);
Roger Meier5f2d34e2013-11-16 16:43:41 +0100131 boost::shared_ptr<PosixThreadFactory> threadFactory =
132 boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Mark Slee07a3aab2007-03-07 05:45:10 +0000133 threadManager->threadFactory(threadFactory);
134 threadManager->start();
135 TThreadPoolServer server(processor,
136 serverTransport,
137 transportFactory,
138 protocolFactory,
139 threadManager);
140
141 TThreadedServer server(processor,
142 serverTransport,
143 transportFactory,
144 protocolFactory);
145
146 */
147
148 printf("Starting the server...\n");
149 server.serve();
150 printf("done.\n");
151 return 0;
152}