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> |
| 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 Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 28 | #include <thrift/TToString.h> |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 29 | |
| 30 | #include <iostream> |
| 31 | #include <stdexcept> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "../gen-cpp/Calculator.h" |
| 35 | |
| 36 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 37 | using namespace apache::thrift; |
Jens Geyer | 04a4c15 | 2014-10-14 21:30:28 +0200 | [diff] [blame] | 38 | using namespace apache::thrift::concurrency; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 39 | using namespace apache::thrift::protocol; |
| 40 | using namespace apache::thrift::transport; |
| 41 | using namespace apache::thrift::server; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace tutorial; |
| 44 | using namespace shared; |
| 45 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 46 | class CalculatorHandler : public CalculatorIf { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 47 | public: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 48 | CalculatorHandler() {} |
| 49 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 50 | void ping() { cout << "ping()" << endl; } |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 51 | |
| 52 | int32_t add(const int32_t n1, const int32_t n2) { |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 53 | cout << "add(" << n1 << ", " << n2 << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 54 | return n1 + n2; |
| 55 | } |
| 56 | |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 57 | int32_t calculate(const int32_t logid, const Work& work) { |
| 58 | cout << "calculate(" << logid << ", " << work << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 59 | int32_t val; |
| 60 | |
| 61 | switch (work.op) { |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 62 | case Operation::ADD: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 63 | val = work.num1 + work.num2; |
| 64 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 65 | case Operation::SUBTRACT: |
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::MULTIPLY: |
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::DIVIDE: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 72 | 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; |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 89 | ss.value = to_string(val); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 90 | |
| 91 | log[logid] = ss; |
| 92 | |
| 93 | return val; |
| 94 | } |
| 95 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 96 | void getStruct(SharedStruct& ret, const int32_t logid) { |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 97 | cout << "getStruct(" << logid << ")" << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 98 | ret = log[logid]; |
| 99 | } |
| 100 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 101 | void zip() { cout << "zip()" << endl; } |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 102 | |
| 103 | protected: |
| 104 | map<int32_t, SharedStruct> log; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Konrad Grochowski | 157872d | 2014-11-06 19:55:28 +0100 | [diff] [blame] | 107 | int main() { |
Roger Meier | 5f2d34e | 2013-11-16 16:43:41 +0100 | [diff] [blame] | 108 | boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 109 | boost::shared_ptr<CalculatorHandler> handler(new CalculatorHandler()); |
| 110 | boost::shared_ptr<TProcessor> processor(new CalculatorProcessor(handler)); |
| 111 | boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090)); |
| 112 | boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 113 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame^] | 114 | TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Or you could do one of these |
| 118 | |
Jens Geyer | 04a4c15 | 2014-10-14 21:30:28 +0200 | [diff] [blame] | 119 | const int workerCount = 4; |
| 120 | |
Roger Meier | 5f2d34e | 2013-11-16 16:43:41 +0100 | [diff] [blame] | 121 | boost::shared_ptr<ThreadManager> threadManager = |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 122 | ThreadManager::newSimpleThreadManager(workerCount); |
Roger Meier | 5f2d34e | 2013-11-16 16:43:41 +0100 | [diff] [blame] | 123 | boost::shared_ptr<PosixThreadFactory> threadFactory = |
| 124 | boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 125 | threadManager->threadFactory(threadFactory); |
| 126 | threadManager->start(); |
| 127 | TThreadPoolServer server(processor, |
| 128 | serverTransport, |
| 129 | transportFactory, |
| 130 | protocolFactory, |
| 131 | threadManager); |
| 132 | |
| 133 | TThreadedServer server(processor, |
| 134 | serverTransport, |
| 135 | transportFactory, |
| 136 | protocolFactory); |
| 137 | |
| 138 | */ |
| 139 | |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 140 | cout << "Starting the server..." << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 141 | server.serve(); |
Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 142 | cout << "Done." << endl; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | } |