blob: f7a69e2063110efdb1cb7b55a6776d080aa84e54 [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>
Konrad Grochowskia8eec712014-09-04 00:56:27 +020028#include <thrift/TToString.h>
Mark Slee07a3aab2007-03-07 05:45:10 +000029
30#include <iostream>
31#include <stdexcept>
32#include <sstream>
33
34#include "../gen-cpp/Calculator.h"
35
36using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000037using namespace apache::thrift;
Jens Geyer04a4c152014-10-14 21:30:28 +020038using namespace apache::thrift::concurrency;
T Jake Lucianib5e62212009-01-31 22:36:20 +000039using namespace apache::thrift::protocol;
40using namespace apache::thrift::transport;
41using namespace apache::thrift::server;
Mark Slee07a3aab2007-03-07 05:45:10 +000042
43using namespace tutorial;
44using namespace shared;
45
Mark Slee07a3aab2007-03-07 05:45:10 +000046class CalculatorHandler : public CalculatorIf {
47 public:
48 CalculatorHandler() {}
49
50 void ping() {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020051 cout << "ping()" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000052 }
53
54 int32_t add(const int32_t n1, const int32_t n2) {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020055 cout << "add(" << n1 << ", " << n2 << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000056 return n1 + n2;
57 }
58
Konrad Grochowskia8eec712014-09-04 00:56:27 +020059 int32_t calculate(const int32_t logid, const Work& work) {
60 cout << "calculate(" << logid << ", " << work << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000061 int32_t val;
62
63 switch (work.op) {
Bryan Duxbury833ae492010-09-27 17:26:02 +000064 case Operation::ADD:
Mark Slee07a3aab2007-03-07 05:45:10 +000065 val = work.num1 + work.num2;
66 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000067 case Operation::SUBTRACT:
Mark Slee07a3aab2007-03-07 05:45:10 +000068 val = work.num1 - work.num2;
69 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000070 case Operation::MULTIPLY:
Mark Slee07a3aab2007-03-07 05:45:10 +000071 val = work.num1 * work.num2;
72 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000073 case Operation::DIVIDE:
Mark Slee07a3aab2007-03-07 05:45:10 +000074 if (work.num2 == 0) {
75 InvalidOperation io;
76 io.what = work.op;
77 io.why = "Cannot divide by 0";
78 throw io;
79 }
80 val = work.num1 / work.num2;
81 break;
82 default:
83 InvalidOperation io;
84 io.what = work.op;
85 io.why = "Invalid Operation";
86 throw io;
87 }
88
89 SharedStruct ss;
90 ss.key = logid;
Konrad Grochowskia8eec712014-09-04 00:56:27 +020091 ss.value = to_string(val);
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) {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020099 cout << "getStruct(" << logid << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000100 ret = log[logid];
101 }
102
103 void zip() {
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200104 cout << "zip()" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000105 }
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
Jens Geyer04a4c152014-10-14 21:30:28 +0200129 const int workerCount = 4;
130
Roger Meier5f2d34e2013-11-16 16:43:41 +0100131 boost::shared_ptr<ThreadManager> threadManager =
Mark Slee07a3aab2007-03-07 05:45:10 +0000132 ThreadManager::newSimpleThreadManager(workerCount);
Roger Meier5f2d34e2013-11-16 16:43:41 +0100133 boost::shared_ptr<PosixThreadFactory> threadFactory =
134 boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Mark Slee07a3aab2007-03-07 05:45:10 +0000135 threadManager->threadFactory(threadFactory);
136 threadManager->start();
137 TThreadPoolServer server(processor,
138 serverTransport,
139 transportFactory,
140 protocolFactory,
141 threadManager);
142
143 TThreadedServer server(processor,
144 serverTransport,
145 transportFactory,
146 protocolFactory);
147
148 */
149
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200150 cout << "Starting the server..." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000151 server.serve();
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200152 cout << "Done." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000153 return 0;
154}