blob: 7d17ecab926c22f66395bd57dd1dca44de4cf3a3 [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;
Mark Sleeb32f3c62007-03-05 04:48:48 +000015using namespace facebook::thrift;
16using namespace facebook::thrift::protocol;
17using namespace facebook::thrift::transport;
18using namespace facebook::thrift::server;
19using namespace facebook::thrift::concurrency;
20
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);
71 m.wait(1000);
72 }
73 fprintf(stderr, "THREAD %d DONE\n", thread);
74 }
75
76private:
77 boost::shared_ptr<TServer> server_;
78
79};
80
81int main(int argc, char **argv) {
82 int port = 9090;
83 shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler());
84 shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler));
85 shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
86 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
87 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
88
Mark Slee02d1edc2007-03-07 05:17:25 +000089 /*
Mark Sleeb32f3c62007-03-05 04:48:48 +000090 shared_ptr<ThreadManager> threadManager =
91 ThreadManager::newSimpleThreadManager(10);
92 shared_ptr<PosixThreadFactory> threadFactory =
93 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
94 threadManager->threadFactory(threadFactory);
95 threadManager->start();
Mark Slee02d1edc2007-03-07 05:17:25 +000096
97 shared_ptr<TServer> server =
Mark Sleeb32f3c62007-03-05 04:48:48 +000098 shared_ptr<TServer>(new TThreadPoolServer(processor,
99 serverTransport,
100 transportFactory,
101 protocolFactory,
102 threadManager));
Mark Slee02d1edc2007-03-07 05:17:25 +0000103 */
Mark Sleeb32f3c62007-03-05 04:48:48 +0000104
Mark Slee02d1edc2007-03-07 05:17:25 +0000105 shared_ptr<TServer> server =
106 shared_ptr<TServer>(new TThreadedServer(processor,
107 serverTransport,
108 transportFactory,
109 protocolFactory));
Mark Sleeb32f3c62007-03-05 04:48:48 +0000110
Mark Slee02d1edc2007-03-07 05:17:25 +0000111 handler->setServer(server);
112
113 server->serve();
Mark Sleeb32f3c62007-03-05 04:48:48 +0000114
115 fprintf(stderr, "done.\n");
116
117 return 0;
118}
119