blob: 80b100e59a9bfa78b9061122335497d431030487 [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>
Ben Craig74086f12015-07-04 17:18:58 -050021#include <thrift/concurrency/PlatformThreadFactory.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000022#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>
Ben Craig74086f12015-07-04 17:18:58 -050027#include <thrift/transport/TSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000028#include <thrift/transport/TTransportUtils.h>
Konrad Grochowskia8eec712014-09-04 00:56:27 +020029#include <thrift/TToString.h>
James E. King, III82ae9572017-08-05 12:23:54 -040030#include <thrift/stdcxx.h>
Ben Craig74086f12015-07-04 17:18:58 -050031
Mark Slee07a3aab2007-03-07 05:45:10 +000032#include <iostream>
33#include <stdexcept>
34#include <sstream>
35
36#include "../gen-cpp/Calculator.h"
37
38using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000039using namespace apache::thrift;
Jens Geyer04a4c152014-10-14 21:30:28 +020040using namespace apache::thrift::concurrency;
T Jake Lucianib5e62212009-01-31 22:36:20 +000041using namespace apache::thrift::protocol;
42using namespace apache::thrift::transport;
43using namespace apache::thrift::server;
Mark Slee07a3aab2007-03-07 05:45:10 +000044
45using namespace tutorial;
46using namespace shared;
47
Mark Slee07a3aab2007-03-07 05:45:10 +000048class CalculatorHandler : public CalculatorIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010049public:
Mark Slee07a3aab2007-03-07 05:45:10 +000050 CalculatorHandler() {}
51
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052 void ping() { cout << "ping()" << endl; }
Mark Slee07a3aab2007-03-07 05:45:10 +000053
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;
Konrad Grochowski3b115df2015-05-18 17:58:36 +020076 io.whatOp = work.op;
Mark Slee07a3aab2007-03-07 05:45:10 +000077 io.why = "Cannot divide by 0";
78 throw io;
79 }
80 val = work.num1 / work.num2;
81 break;
82 default:
83 InvalidOperation io;
Konrad Grochowski3b115df2015-05-18 17:58:36 +020084 io.whatOp = work.op;
Mark Slee07a3aab2007-03-07 05:45:10 +000085 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100103 void zip() { cout << "zip()" << endl; }
Mark Slee07a3aab2007-03-07 05:45:10 +0000104
105protected:
106 map<int32_t, SharedStruct> log;
Mark Slee07a3aab2007-03-07 05:45:10 +0000107};
108
Ben Craig74086f12015-07-04 17:18:58 -0500109/*
110 CalculatorIfFactory is code generated.
111 CalculatorCloneFactory is useful for getting access to the server side of the
112 transport. It is also useful for making per-connection state. Without this
113 CloneFactory, all connections will end up sharing the same handler instance.
114*/
115class CalculatorCloneFactory : virtual public CalculatorIfFactory {
116 public:
117 virtual ~CalculatorCloneFactory() {}
118 virtual CalculatorIf* getHandler(const ::apache::thrift::TConnectionInfo& connInfo)
119 {
James E. King, III82ae9572017-08-05 12:23:54 -0400120 stdcxx::shared_ptr<TSocket> sock = stdcxx::dynamic_pointer_cast<TSocket>(connInfo.transport);
Ben Craig74086f12015-07-04 17:18:58 -0500121 cout << "Incoming connection\n";
122 cout << "\tSocketInfo: " << sock->getSocketInfo() << "\n";
123 cout << "\tPeerHost: " << sock->getPeerHost() << "\n";
124 cout << "\tPeerAddress: " << sock->getPeerAddress() << "\n";
125 cout << "\tPeerPort: " << sock->getPeerPort() << "\n";
126 return new CalculatorHandler;
127 }
128 virtual void releaseHandler( ::shared::SharedServiceIf* handler) {
129 delete handler;
130 }
131};
Mark Slee07a3aab2007-03-07 05:45:10 +0000132
Ben Craig74086f12015-07-04 17:18:58 -0500133int main() {
134 TThreadedServer server(
James E. King, III82ae9572017-08-05 12:23:54 -0400135 stdcxx::make_shared<CalculatorProcessorFactory>(stdcxx::make_shared<CalculatorCloneFactory>()),
136 stdcxx::make_shared<TServerSocket>(9090), //port
137 stdcxx::make_shared<TBufferedTransportFactory>(),
138 stdcxx::make_shared<TBinaryProtocolFactory>());
Ben Craig74086f12015-07-04 17:18:58 -0500139
140 /*
141 // if you don't need per-connection state, do the following instead
142 TThreadedServer server(
James E. King, III82ae9572017-08-05 12:23:54 -0400143 stdcxx::make_shared<CalculatorProcessor>(stdcxx::make_shared<CalculatorHandler>()),
144 stdcxx::make_shared<TServerSocket>(9090), //port
145 stdcxx::make_shared<TBufferedTransportFactory>(),
146 stdcxx::make_shared<TBinaryProtocolFactory>());
Ben Craig74086f12015-07-04 17:18:58 -0500147 */
Mark Slee07a3aab2007-03-07 05:45:10 +0000148
149 /**
Ben Craig74086f12015-07-04 17:18:58 -0500150 * Here are some alternate server types...
151
152 // This server only allows one connection at a time, but spawns no threads
153 TSimpleServer server(
James E. King, III82ae9572017-08-05 12:23:54 -0400154 stdcxx::make_shared<CalculatorProcessor>(stdcxx::make_shared<CalculatorHandler>()),
155 stdcxx::make_shared<TServerSocket>(9090),
156 stdcxx::make_shared<TBufferedTransportFactory>(),
157 stdcxx::make_shared<TBinaryProtocolFactory>());
Mark Slee07a3aab2007-03-07 05:45:10 +0000158
Jens Geyer04a4c152014-10-14 21:30:28 +0200159 const int workerCount = 4;
160
James E. King, III82ae9572017-08-05 12:23:54 -0400161 stdcxx::shared_ptr<ThreadManager> threadManager =
Mark Slee07a3aab2007-03-07 05:45:10 +0000162 ThreadManager::newSimpleThreadManager(workerCount);
Ben Craig74086f12015-07-04 17:18:58 -0500163 threadManager->threadFactory(
James E. King, III82ae9572017-08-05 12:23:54 -0400164 stdcxx::make_shared<PlatformThreadFactory>());
Mark Slee07a3aab2007-03-07 05:45:10 +0000165 threadManager->start();
Mark Slee07a3aab2007-03-07 05:45:10 +0000166
Ben Craig74086f12015-07-04 17:18:58 -0500167 // This server allows "workerCount" connection at a time, and reuses threads
168 TThreadPoolServer server(
James E. King, III82ae9572017-08-05 12:23:54 -0400169 stdcxx::make_shared<CalculatorProcessorFactory>(stdcxx::make_shared<CalculatorCloneFactory>()),
170 stdcxx::make_shared<TServerSocket>(9090),
171 stdcxx::make_shared<TBufferedTransportFactory>(),
172 stdcxx::make_shared<TBinaryProtocolFactory>(),
Ben Craig74086f12015-07-04 17:18:58 -0500173 threadManager);
Mark Slee07a3aab2007-03-07 05:45:10 +0000174 */
175
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200176 cout << "Starting the server..." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000177 server.serve();
Konrad Grochowskia8eec712014-09-04 00:56:27 +0200178 cout << "Done." << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +0000179 return 0;
180}