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