David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 20 | #include <thrift/concurrency/ThreadManager.h> |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 21 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 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> |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 27 | #include <thrift/transport/TSocket.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 28 | #include <thrift/transport/TTransportUtils.h> |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 29 | #include <thrift/TToString.h> |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 30 | |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 31 | #include <boost/make_shared.hpp> |
| 32 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 33 | #include <iostream> |
| 34 | #include <stdexcept> |
| 35 | #include <sstream> |
| 36 | |
| 37 | #include "../gen-cpp/Calculator.h" |
| 38 | |
| 39 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 40 | using namespace apache::thrift; |
Jens Geyer | 04a4c15 | 2014-10-14 21:30:28 +0200 | [diff] [blame] | 41 | using namespace apache::thrift::concurrency; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 42 | using namespace apache::thrift::protocol; |
| 43 | using namespace apache::thrift::transport; |
| 44 | using namespace apache::thrift::server; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 45 | |
| 46 | using namespace tutorial; |
| 47 | using namespace shared; |
| 48 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 49 | class CalculatorHandler : public CalculatorIf { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | public: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 51 | CalculatorHandler() {} |
| 52 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | void ping() { cout << "ping()" << endl; } |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 54 | |
| 55 | int32_t add(const int32_t n1, const int32_t n2) { |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 56 | cout << "add(" << n1 << ", " << n2 << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 57 | return n1 + n2; |
| 58 | } |
| 59 | |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 60 | int32_t calculate(const int32_t logid, const Work& work) { |
| 61 | cout << "calculate(" << logid << ", " << work << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 62 | int32_t val; |
| 63 | |
| 64 | switch (work.op) { |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 65 | case Operation::ADD: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 66 | val = work.num1 + work.num2; |
| 67 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 68 | case Operation::SUBTRACT: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 69 | val = work.num1 - work.num2; |
| 70 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 71 | case Operation::MULTIPLY: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 72 | val = work.num1 * work.num2; |
| 73 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 74 | case Operation::DIVIDE: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 75 | if (work.num2 == 0) { |
| 76 | InvalidOperation io; |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 77 | io.whatOp = work.op; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 78 | io.why = "Cannot divide by 0"; |
| 79 | throw io; |
| 80 | } |
| 81 | val = work.num1 / work.num2; |
| 82 | break; |
| 83 | default: |
| 84 | InvalidOperation io; |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 85 | io.whatOp = work.op; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 86 | io.why = "Invalid Operation"; |
| 87 | throw io; |
| 88 | } |
| 89 | |
| 90 | SharedStruct ss; |
| 91 | ss.key = logid; |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 92 | ss.value = to_string(val); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 93 | |
| 94 | log[logid] = ss; |
| 95 | |
| 96 | return val; |
| 97 | } |
| 98 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | void getStruct(SharedStruct& ret, const int32_t logid) { |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 100 | cout << "getStruct(" << logid << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 101 | ret = log[logid]; |
| 102 | } |
| 103 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 104 | void zip() { cout << "zip()" << endl; } |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 105 | |
| 106 | protected: |
| 107 | map<int32_t, SharedStruct> log; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 110 | /* |
| 111 | CalculatorIfFactory is code generated. |
| 112 | CalculatorCloneFactory is useful for getting access to the server side of the |
| 113 | transport. It is also useful for making per-connection state. Without this |
| 114 | CloneFactory, all connections will end up sharing the same handler instance. |
| 115 | */ |
| 116 | class CalculatorCloneFactory : virtual public CalculatorIfFactory { |
| 117 | public: |
| 118 | virtual ~CalculatorCloneFactory() {} |
| 119 | virtual CalculatorIf* getHandler(const ::apache::thrift::TConnectionInfo& connInfo) |
| 120 | { |
| 121 | boost::shared_ptr<TSocket> sock = boost::dynamic_pointer_cast<TSocket>(connInfo.transport); |
| 122 | cout << "Incoming connection\n"; |
| 123 | cout << "\tSocketInfo: " << sock->getSocketInfo() << "\n"; |
| 124 | cout << "\tPeerHost: " << sock->getPeerHost() << "\n"; |
| 125 | cout << "\tPeerAddress: " << sock->getPeerAddress() << "\n"; |
| 126 | cout << "\tPeerPort: " << sock->getPeerPort() << "\n"; |
| 127 | return new CalculatorHandler; |
| 128 | } |
| 129 | virtual void releaseHandler( ::shared::SharedServiceIf* handler) { |
| 130 | delete handler; |
| 131 | } |
| 132 | }; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 133 | |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 134 | int main() { |
| 135 | TThreadedServer server( |
| 136 | boost::make_shared<CalculatorProcessorFactory>(boost::make_shared<CalculatorCloneFactory>()), |
| 137 | boost::make_shared<TServerSocket>(9090), //port |
| 138 | boost::make_shared<TBufferedTransportFactory>(), |
| 139 | boost::make_shared<TBinaryProtocolFactory>()); |
| 140 | |
| 141 | /* |
| 142 | // if you don't need per-connection state, do the following instead |
| 143 | TThreadedServer server( |
| 144 | boost::make_shared<CalculatorProcessor>(boost::make_shared<CalculatorHandler>()), |
| 145 | boost::make_shared<TServerSocket>(9090), //port |
| 146 | boost::make_shared<TBufferedTransportFactory>(), |
| 147 | boost::make_shared<TBinaryProtocolFactory>()); |
| 148 | */ |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 151 | * Here are some alternate server types... |
| 152 | |
| 153 | // This server only allows one connection at a time, but spawns no threads |
| 154 | TSimpleServer server( |
| 155 | boost::make_shared<CalculatorProcessor>(boost::make_shared<CalculatorHandler>()), |
| 156 | boost::make_shared<TServerSocket>(9090), |
| 157 | boost::make_shared<TBufferedTransportFactory>(), |
| 158 | boost::make_shared<TBinaryProtocolFactory>()); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 159 | |
Jens Geyer | 04a4c15 | 2014-10-14 21:30:28 +0200 | [diff] [blame] | 160 | const int workerCount = 4; |
| 161 | |
Roger Meier | 5f2d34e | 2013-11-16 16:43:41 +0100 | [diff] [blame] | 162 | boost::shared_ptr<ThreadManager> threadManager = |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 163 | ThreadManager::newSimpleThreadManager(workerCount); |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 164 | threadManager->threadFactory( |
| 165 | boost::make_shared<PlatformThreadFactory>()); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 166 | threadManager->start(); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 167 | |
Ben Craig | 74086f1 | 2015-07-04 17:18:58 -0500 | [diff] [blame] | 168 | // This server allows "workerCount" connection at a time, and reuses threads |
| 169 | TThreadPoolServer server( |
| 170 | boost::make_shared<CalculatorProcessorFactory>(boost::make_shared<CalculatorCloneFactory>()), |
| 171 | boost::make_shared<TServerSocket>(9090), |
| 172 | boost::make_shared<TBufferedTransportFactory>(), |
| 173 | boost::make_shared<TBinaryProtocolFactory>(), |
| 174 | threadManager); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 175 | */ |
| 176 | |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 177 | cout << "Starting the server..." << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 178 | server.serve(); |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 179 | cout << "Done." << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |