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 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 20 | #include <concurrency/ThreadManager.h> |
| 21 | #include <concurrency/PosixThreadFactory.h> |
| 22 | #include <protocol/TBinaryProtocol.h> |
| 23 | #include <server/TSimpleServer.h> |
| 24 | #include <server/TThreadPoolServer.h> |
| 25 | #include <server/TThreadedServer.h> |
| 26 | #include <transport/TServerSocket.h> |
| 27 | #include <transport/TTransportUtils.h> |
| 28 | |
| 29 | #include <iostream> |
| 30 | #include <stdexcept> |
| 31 | #include <sstream> |
| 32 | |
| 33 | #include "../gen-cpp/Calculator.h" |
| 34 | |
| 35 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | using namespace apache::thrift; |
| 37 | using namespace apache::thrift::protocol; |
| 38 | using namespace apache::thrift::transport; |
| 39 | using namespace apache::thrift::server; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace tutorial; |
| 42 | using namespace shared; |
| 43 | |
| 44 | using namespace boost; |
| 45 | |
| 46 | class CalculatorHandler : public CalculatorIf { |
| 47 | public: |
| 48 | CalculatorHandler() {} |
| 49 | |
| 50 | void ping() { |
| 51 | printf("ping()\n"); |
| 52 | } |
| 53 | |
| 54 | int32_t add(const int32_t n1, const int32_t n2) { |
| 55 | printf("add(%d,%d)\n", n1, n2); |
| 56 | return n1 + n2; |
| 57 | } |
| 58 | |
| 59 | int32_t calculate(const int32_t logid, const Work &work) { |
| 60 | printf("calculate(%d,{%d,%d,%d})\n", logid, work.op, work.num1, work.num2); |
| 61 | int32_t val; |
| 62 | |
| 63 | switch (work.op) { |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 64 | case Operation::ADD: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 65 | val = work.num1 + work.num2; |
| 66 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 67 | case Operation::SUBTRACT: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 68 | val = work.num1 - work.num2; |
| 69 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 70 | case Operation::MULTIPLY: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 71 | val = work.num1 * work.num2; |
| 72 | break; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 73 | case Operation::DIVIDE: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 74 | 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; |
David Reiss | f33c7c6 | 2009-03-26 06:15:26 +0000 | [diff] [blame] | 91 | char buffer[12]; |
| 92 | snprintf(buffer, sizeof(buffer), "%d", val); |
Mark Slee | 0f2916c | 2007-04-11 09:16:23 +0000 | [diff] [blame] | 93 | ss.value = buffer; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 94 | |
| 95 | log[logid] = ss; |
| 96 | |
| 97 | return val; |
| 98 | } |
| 99 | |
| 100 | void getStruct(SharedStruct &ret, const int32_t logid) { |
| 101 | printf("getStruct(%d)\n", logid); |
| 102 | ret = log[logid]; |
| 103 | } |
| 104 | |
| 105 | void zip() { |
| 106 | printf("zip()\n"); |
| 107 | } |
| 108 | |
| 109 | protected: |
| 110 | map<int32_t, SharedStruct> log; |
| 111 | |
| 112 | }; |
| 113 | |
| 114 | int main(int argc, char **argv) { |
| 115 | |
| 116 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 117 | shared_ptr<CalculatorHandler> handler(new CalculatorHandler()); |
| 118 | shared_ptr<TProcessor> processor(new CalculatorProcessor(handler)); |
| 119 | shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090)); |
| 120 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 121 | |
| 122 | TSimpleServer server(processor, |
| 123 | serverTransport, |
| 124 | transportFactory, |
| 125 | protocolFactory); |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Or you could do one of these |
| 130 | |
| 131 | shared_ptr<ThreadManager> threadManager = |
| 132 | ThreadManager::newSimpleThreadManager(workerCount); |
| 133 | shared_ptr<PosixThreadFactory> threadFactory = |
| 134 | shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 135 | 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 | |
| 150 | printf("Starting the server...\n"); |
| 151 | server.serve(); |
| 152 | printf("done.\n"); |
| 153 | return 0; |
| 154 | } |