blob: f5bd665c62325ed8b30cc4ded7e25ef22a3333b8 [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>
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
13using namespace facebook::thrift;
14using namespace facebook::thrift::protocol;
15using namespace facebook::thrift::transport;
16using namespace facebook::thrift::server;
17using namespace facebook::thrift::concurrency;
18
19
20class ThreadsTestHandler : virtual public ThreadsTestIf {
21 public:
22 ThreadsTestHandler() {
23 // Your initialization goes here
24 }
25
Mark Sleec416a272007-03-05 05:40:37 +000026 int32_t threadOne(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000027 // Your implementation goes here
28 printf("threadOne\n");
29 go2sleep(1, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000030 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000031 }
32
Mark Sleec416a272007-03-05 05:40:37 +000033 int32_t threadTwo(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000034 // Your implementation goes here
35 printf("threadTwo\n");
36 go2sleep(2, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000037 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000038 }
39
Mark Sleec416a272007-03-05 05:40:37 +000040 int32_t threadThree(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000041 // Your implementation goes here
42 printf("threadThree\n");
43 go2sleep(3, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000044 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000045 }
46
Mark Sleec416a272007-03-05 05:40:37 +000047 int32_t threadFour(const int32_t sleep) {
Mark Sleeb32f3c62007-03-05 04:48:48 +000048 // Your implementation goes here
49 printf("threadFour\n");
50 go2sleep(4, sleep);
Mark Sleec416a272007-03-05 05:40:37 +000051 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000052 }
53
Mark Sleec416a272007-03-05 05:40:37 +000054 int32_t stop() {
Mark Sleeb32f3c62007-03-05 04:48:48 +000055 printf("stop\n");
56 server_->stop();
Mark Sleec416a272007-03-05 05:40:37 +000057 return 1;
Mark Sleeb32f3c62007-03-05 04:48:48 +000058 }
59
60 void setServer(boost::shared_ptr<TServer> server) {
61 server_ = server;
62 }
63
64protected:
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
74private:
75 boost::shared_ptr<TServer> server_;
76
77};
78
79int 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