blob: 653caaa835abee9e78983d73b1c3a4cae386794a [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;
38using namespace apache::thrift::protocol;
39using namespace apache::thrift::transport;
40using namespace apache::thrift::server;
Mark Slee07a3aab2007-03-07 05:45:10 +000041
42using namespace tutorial;
43using namespace shared;
44
Mark Slee07a3aab2007-03-07 05:45:10 +000045class CalculatorHandler : public CalculatorIf {
46 public:
47 CalculatorHandler() {}
48
49 void ping() {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020050 cout << "ping()" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000051 }
52
53 int32_t add(const int32_t n1, const int32_t n2) {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020054 cout << "add(" << n1 << ", " << n2 << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000055 return n1 + n2;
56 }
57
Konrad Grochowskia8eec712014-09-04 00:56:27 +020058 int32_t calculate(const int32_t logid, const Work& work) {
59 cout << "calculate(" << logid << ", " << work << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000060 int32_t val;
61
62 switch (work.op) {
Bryan Duxbury833ae492010-09-27 17:26:02 +000063 case Operation::ADD:
Mark Slee07a3aab2007-03-07 05:45:10 +000064 val = work.num1 + work.num2;
65 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000066 case Operation::SUBTRACT:
Mark Slee07a3aab2007-03-07 05:45:10 +000067 val = work.num1 - work.num2;
68 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000069 case Operation::MULTIPLY:
Mark Slee07a3aab2007-03-07 05:45:10 +000070 val = work.num1 * work.num2;
71 break;
Bryan Duxbury833ae492010-09-27 17:26:02 +000072 case Operation::DIVIDE:
Mark Slee07a3aab2007-03-07 05:45:10 +000073 if (work.num2 == 0) {
74 InvalidOperation io;
75 io.what = work.op;
76 io.why = "Cannot divide by 0";
77 throw io;
78 }
79 val = work.num1 / work.num2;
80 break;
81 default:
82 InvalidOperation io;
83 io.what = work.op;
84 io.why = "Invalid Operation";
85 throw io;
86 }
87
88 SharedStruct ss;
89 ss.key = logid;
Konrad Grochowskia8eec712014-09-04 00:56:27 +020090 ss.value = to_string(val);
Mark Slee07a3aab2007-03-07 05:45:10 +000091
92 log[logid] = ss;
93
94 return val;
95 }
96
97 void getStruct(SharedStruct &ret, const int32_t logid) {
Konrad Grochowskia8eec712014-09-04 00:56:27 +020098 cout << "getStruct(" << logid << ")" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000099 ret = log[logid];
100 }
101
102 void zip() {
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200103 cout << "zip()" << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000104 }
105
106protected:
107 map<int32_t, SharedStruct> log;
108
109};
110
111int main(int argc, char **argv) {
112
Roger Meier5f2d34e2013-11-16 16:43:41 +0100113 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
114 boost::shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
115 boost::shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
116 boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
117 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Mark Slee07a3aab2007-03-07 05:45:10 +0000118
119 TSimpleServer server(processor,
120 serverTransport,
121 transportFactory,
122 protocolFactory);
123
124
125 /**
126 * Or you could do one of these
127
Roger Meier5f2d34e2013-11-16 16:43:41 +0100128 boost::shared_ptr<ThreadManager> threadManager =
Mark Slee07a3aab2007-03-07 05:45:10 +0000129 ThreadManager::newSimpleThreadManager(workerCount);
Roger Meier5f2d34e2013-11-16 16:43:41 +0100130 boost::shared_ptr<PosixThreadFactory> threadFactory =
131 boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
Mark Slee07a3aab2007-03-07 05:45:10 +0000132 threadManager->threadFactory(threadFactory);
133 threadManager->start();
134 TThreadPoolServer server(processor,
135 serverTransport,
136 transportFactory,
137 protocolFactory,
138 threadManager);
139
140 TThreadedServer server(processor,
141 serverTransport,
142 transportFactory,
143 protocolFactory);
144
145 */
146
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200147 cout << "Starting the server..." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000148 server.serve();
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200149 cout << "Done." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000150 return 0;
151}