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 | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 20 | // This autogenerated skeleton file illustrates how to build a server. |
| 21 | // You should copy it to another filename to avoid overwriting it. |
| 22 | |
| 23 | #include "ThreadsTest.h" |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 24 | #include <thrift/protocol/TBinaryProtocol.h> |
| 25 | #include <thrift/server/TThreadPoolServer.h> |
| 26 | #include <thrift/server/TThreadedServer.h> |
| 27 | #include <thrift/transport/TServerSocket.h> |
| 28 | #include <thrift/transport/TTransportUtils.h> |
| 29 | #include <thrift/concurrency/Monitor.h> |
| 30 | #include <thrift/concurrency/ThreadManager.h> |
| 31 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 32 | #if _WIN32 |
| 33 | #include <thrift/windows/TWinsockSingleton.h> |
| 34 | #endif |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 35 | |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 36 | using boost::shared_ptr; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 37 | using namespace apache::thrift; |
| 38 | using namespace apache::thrift::protocol; |
| 39 | using namespace apache::thrift::transport; |
| 40 | using namespace apache::thrift::server; |
| 41 | using namespace apache::thrift::concurrency; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | class ThreadsTestHandler : virtual public ThreadsTestIf { |
| 45 | public: |
| 46 | ThreadsTestHandler() { |
| 47 | // Your initialization goes here |
| 48 | } |
| 49 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 50 | int32_t threadOne(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 51 | // Your implementation goes here |
| 52 | printf("threadOne\n"); |
| 53 | go2sleep(1, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 54 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 57 | int32_t threadTwo(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 58 | // Your implementation goes here |
| 59 | printf("threadTwo\n"); |
| 60 | go2sleep(2, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 61 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 64 | int32_t threadThree(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 65 | // Your implementation goes here |
| 66 | printf("threadThree\n"); |
| 67 | go2sleep(3, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 68 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 71 | int32_t threadFour(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 72 | // Your implementation goes here |
| 73 | printf("threadFour\n"); |
| 74 | go2sleep(4, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 75 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 78 | int32_t stop() { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 79 | printf("stop\n"); |
| 80 | server_->stop(); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 81 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void setServer(boost::shared_ptr<TServer> server) { |
| 85 | server_ = server; |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | void go2sleep(int thread, int seconds) { |
| 90 | Monitor m; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 91 | Synchronized s(m); |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 92 | for (int i = 0; i < seconds; ++i) { |
| 93 | fprintf(stderr, "Thread %d: sleep %d\n", thread, i); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 94 | try { |
| 95 | m.wait(1000); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 96 | } catch(const TimedOutException&) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 97 | } |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 98 | } |
| 99 | fprintf(stderr, "THREAD %d DONE\n", thread); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | boost::shared_ptr<TServer> server_; |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | int main(int argc, char **argv) { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 108 | #if _WIN32 |
| 109 | transport::TWinsockSingleton::create(); |
| 110 | #endif |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 111 | int port = 9090; |
| 112 | shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler()); |
| 113 | shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler)); |
| 114 | shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); |
| 115 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 116 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 117 | |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 118 | /* |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 119 | shared_ptr<ThreadManager> threadManager = |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 120 | ThreadManager::newSimpleThreadManager(10); |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 121 | shared_ptr<PlatformThreadFactory> threadFactory = |
| 122 | shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()); |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 123 | threadManager->threadFactory(threadFactory); |
| 124 | threadManager->start(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 125 | |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 126 | shared_ptr<TServer> server = |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 127 | shared_ptr<TServer>(new TThreadPoolServer(processor, |
| 128 | serverTransport, |
| 129 | transportFactory, |
| 130 | protocolFactory, |
| 131 | threadManager)); |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 132 | */ |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 133 | |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 134 | shared_ptr<TServer> server = |
| 135 | shared_ptr<TServer>(new TThreadedServer(processor, |
| 136 | serverTransport, |
| 137 | transportFactory, |
| 138 | protocolFactory)); |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 139 | |
Mark Slee | 02d1edc | 2007-03-07 05:17:25 +0000 | [diff] [blame] | 140 | handler->setServer(server); |
| 141 | |
| 142 | server->serve(); |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 143 | |
| 144 | fprintf(stderr, "done.\n"); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |