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