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> |
| 7 | #include <transport/TServerSocket.h> |
| 8 | #include <transport/TTransportUtils.h> |
| 9 | #include <thrift/concurrency/Monitor.h> |
| 10 | #include <thrift/concurrency/ThreadManager.h> |
| 11 | #include <thrift/concurrency/PosixThreadFactory.h> |
| 12 | |
| 13 | using namespace facebook::thrift; |
| 14 | using namespace facebook::thrift::protocol; |
| 15 | using namespace facebook::thrift::transport; |
| 16 | using namespace facebook::thrift::server; |
| 17 | using namespace facebook::thrift::concurrency; |
| 18 | |
| 19 | |
| 20 | class ThreadsTestHandler : virtual public ThreadsTestIf { |
| 21 | public: |
| 22 | ThreadsTestHandler() { |
| 23 | // Your initialization goes here |
| 24 | } |
| 25 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 26 | int32_t threadOne(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 27 | // Your implementation goes here |
| 28 | printf("threadOne\n"); |
| 29 | go2sleep(1, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 30 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 33 | int32_t threadTwo(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 34 | // Your implementation goes here |
| 35 | printf("threadTwo\n"); |
| 36 | go2sleep(2, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 37 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 40 | int32_t threadThree(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 41 | // Your implementation goes here |
| 42 | printf("threadThree\n"); |
| 43 | go2sleep(3, sleep); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 44 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 47 | int32_t threadFour(const int32_t sleep) { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 48 | // Your implementation goes here |
| 49 | printf("threadFour\n"); |
| 50 | go2sleep(4, 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 stop() { |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 55 | printf("stop\n"); |
| 56 | server_->stop(); |
Mark Slee | c416a27 | 2007-03-05 05:40:37 +0000 | [diff] [blame] | 57 | return 1; |
Mark Slee | b32f3c6 | 2007-03-05 04:48:48 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void setServer(boost::shared_ptr<TServer> server) { |
| 61 | server_ = server; |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | void go2sleep(int thread, int seconds) { |
| 66 | Monitor m; |
| 67 | for (int i = 0; i < seconds; ++i) { |
| 68 | fprintf(stderr, "Thread %d: sleep %d\n", thread, i); |
| 69 | m.wait(1000); |
| 70 | } |
| 71 | fprintf(stderr, "THREAD %d DONE\n", thread); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | boost::shared_ptr<TServer> server_; |
| 76 | |
| 77 | }; |
| 78 | |
| 79 | int main(int argc, char **argv) { |
| 80 | int port = 9090; |
| 81 | shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler()); |
| 82 | shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler)); |
| 83 | shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); |
| 84 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 85 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 86 | |
| 87 | shared_ptr<ThreadManager> threadManager = |
| 88 | ThreadManager::newSimpleThreadManager(10); |
| 89 | shared_ptr<PosixThreadFactory> threadFactory = |
| 90 | shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 91 | threadManager->threadFactory(threadFactory); |
| 92 | threadManager->start(); |
| 93 | |
| 94 | shared_ptr<TServer> threadPoolServer = |
| 95 | shared_ptr<TServer>(new TThreadPoolServer(processor, |
| 96 | serverTransport, |
| 97 | transportFactory, |
| 98 | protocolFactory, |
| 99 | threadManager)); |
| 100 | |
| 101 | handler->setServer(threadPoolServer); |
| 102 | |
| 103 | threadPoolServer->serve(); |
| 104 | |
| 105 | fprintf(stderr, "done.\n"); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |