blob: c4ce2c99df2f0da3db9bbbfe6bbde4e5a991319d [file] [log] [blame]
Mark Sleeb32f3c62007-03-05 04:48:48 +00001// 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 Slee02d1edc2007-03-07 05:17:25 +00007#include <server/TThreadedServer.h>
Mark Sleeb32f3c62007-03-05 04:48:48 +00008#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 Slee02d1edc2007-03-07 05:17:25 +000014using boost::shared_ptr;
T Jake Lucianib5e62212009-01-31 22:36:20 +000015using namespace apache::thrift;
16using namespace apache::thrift::protocol;
17using namespace apache::thrift::transport;
18using namespace apache::thrift::server;
19using namespace apache::thrift::concurrency;
Mark Sleeb32f3c62007-03-05 04:48:48 +000020
21
22class ThreadsTestHandler : virtual public ThreadsTestIf {
23 public:
24 ThreadsTestHandler() {
25 // Your initialization goes here
26 }
27
Mark Sleec416a272007-03-05 05:40:37 +000028 int32_t threadOne(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000029 // Your implementation goes here
30 printf("threadOne\n");
31 go2sleep(1, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000032 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000033 }
34
Mark Sleec416a272007-03-05 05:40:37 +000035 int32_t threadTwo(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000036 // Your implementation goes here
37 printf("threadTwo\n");
38 go2sleep(2, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000039 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000040 }
41
Mark Sleec416a272007-03-05 05:40:37 +000042 int32_t threadThree(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000043 // Your implementation goes here
44 printf("threadThree\n");
45 go2sleep(3, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000046 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000047 }
48
Mark Sleec416a272007-03-05 05:40:37 +000049 int32_t threadFour(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000050 // Your implementation goes here
51 printf("threadFour\n");
52 go2sleep(4, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000053 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000054 }
55
Mark Sleec416a272007-03-05 05:40:37 +000056 int32_t stop() {
Mark Sleeb32f3c62007-03-05 04:48:48 +000057 printf("stop\n");
58 server_->stop();
Mark Sleec416a272007-03-05 05:40:37 +000059 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000060 }
61
62 void setServer(boost::shared_ptr<TServer> server) {
63 server_ = server;
64 }
65
66protected:
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 Slemko3a3b53b2007-05-22 23:59:54 +000071 try {
72 m.wait(1000);
73 } catch(TimedOutException& e) {
74 }
Mark Sleeb32f3c62007-03-05 04:48:48 +000075 }
76 fprintf(stderr, "THREAD %d DONE\n", thread);
77 }
78
79private:
80 boost::shared_ptr<TServer> server_;
81
82};
83
84int 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 Slee02d1edc2007-03-07 05:17:25 +000092 /*
David Reiss0c90f6f2008-02-06 22:18:40 +000093 shared_ptr<ThreadManager> threadManager =
Mark Sleeb32f3c62007-03-05 04:48:48 +000094 ThreadManager::newSimpleThreadManager(10);
95 shared_ptr<PosixThreadFactory> threadFactory =
96 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
97 threadManager->threadFactory(threadFactory);
98 threadManager->start();
David Reiss0c90f6f2008-02-06 22:18:40 +000099
Mark Slee02d1edc2007-03-07 05:17:25 +0000100 shared_ptr<TServer> server =
Mark Sleeb32f3c62007-03-05 04:48:48 +0000101 shared_ptr<TServer>(new TThreadPoolServer(processor,
102 serverTransport,
103 transportFactory,
104 protocolFactory,
105 threadManager));
Mark Slee02d1edc2007-03-07 05:17:25 +0000106 */
Mark Sleeb32f3c62007-03-05 04:48:48 +0000107
Mark Slee02d1edc2007-03-07 05:17:25 +0000108 shared_ptr<TServer> server =
109 shared_ptr<TServer>(new TThreadedServer(processor,
110 serverTransport,
111 transportFactory,
112 protocolFactory));
Mark Sleeb32f3c62007-03-05 04:48:48 +0000113
Mark Slee02d1edc2007-03-07 05:17:25 +0000114 handler->setServer(server);
115
116 server->serve();
Mark Sleeb32f3c62007-03-05 04:48:48 +0000117
118 fprintf(stderr, "done.\n");
119
120 return 0;
121}
122