blob: 1e3494cc3670567757486f5f9079f8bc934d2f71 [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
26 void threadOne(const int32_t sleep) {
27 // Your implementation goes here
28 printf("threadOne\n");
29 go2sleep(1, sleep);
30 }
31
32 void threadTwo(const int32_t sleep) {
33 // Your implementation goes here
34 printf("threadTwo\n");
35 go2sleep(2, sleep);
36 }
37
38 void threadThree(const int32_t sleep) {
39 // Your implementation goes here
40 printf("threadThree\n");
41 go2sleep(3, sleep);
42 }
43
44 void threadFour(const int32_t sleep) {
45 // Your implementation goes here
46 printf("threadFour\n");
47 go2sleep(4, sleep);
48 }
49
50 void stop() {
51 printf("stop\n");
52 server_->stop();
53 }
54
55 void setServer(boost::shared_ptr<TServer> server) {
56 server_ = server;
57 }
58
59protected:
60 void go2sleep(int thread, int seconds) {
61 Monitor m;
62 for (int i = 0; i < seconds; ++i) {
63 fprintf(stderr, "Thread %d: sleep %d\n", thread, i);
64 m.wait(1000);
65 }
66 fprintf(stderr, "THREAD %d DONE\n", thread);
67 }
68
69private:
70 boost::shared_ptr<TServer> server_;
71
72};
73
74int main(int argc, char **argv) {
75 int port = 9090;
76 shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler());
77 shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler));
78 shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
79 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
80 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
81
82 shared_ptr<ThreadManager> threadManager =
83 ThreadManager::newSimpleThreadManager(10);
84 shared_ptr<PosixThreadFactory> threadFactory =
85 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
86 threadManager->threadFactory(threadFactory);
87 threadManager->start();
88
89 shared_ptr<TServer> threadPoolServer =
90 shared_ptr<TServer>(new TThreadPoolServer(processor,
91 serverTransport,
92 transportFactory,
93 protocolFactory,
94 threadManager));
95
96 handler->setServer(threadPoolServer);
97
98 threadPoolServer->serve();
99
100 fprintf(stderr, "done.\n");
101
102 return 0;
103}
104