blob: 2d6290c73fc3d9fc9be4a8bb566bf12464eec184 [file] [log] [blame]
Marc Slemko35452342006-08-03 19:01:37 +00001#include "server/TThreadPoolServer.h"
2#include "transport/TBufferedTransport.h"
3#include "transport/TTransportException.h"
4#include "concurrency/Thread.h"
5#include "concurrency/ThreadManager.h"
6#include <string>
7#include <iostream>
8
9namespace facebook { namespace thrift { namespace server {
10
11using namespace std;
12using namespace facebook::thrift::concurrency;
13using namespace facebook::thrift::transport;
14
15class TThreadPoolServer : public TServer {
16
17 class Task: public Runnable {
18
19 TProcessor* _processor;
20 TTransport* _transport;
21 TBufferedTransport _bufferedTransport;
22
23 public:
24
25 Task(TProcessor* processor,
26 TTransport* transport) :
27 _processor(processor),
28 _transport(transport),
29 _bufferedTransport(_transport) {
30 }
31
32 ~Task() {
33 delete _transport;
34 }
35
36 void run() {
37
38 while(true) {
39
40 try {
41 _processor->process(&_bufferedTransport);
42
43 } catch (TTransportException& ttx) {
44
45 break;
46
47 } catch(...) {
48
49 break;
50 }
51 }
52
53 _bufferedTransport.close();
54 }
55 };
56
57 TThreadPoolServer(TProcessor* processor,
58 TServerOptions* options,
59 TServerTransport* serverTransport,
60 ThreadManager* threadManager) :
61 TServer(processor, options),
62 serverTransport_(serverTransport),
63 threadManager_(threadManager) {
64 }
65
66 ~TThreadPoolServer() {}
67
68 protected:
69
70 TServerTransport* serverTransport_;
71 ThreadManager* threadManager_;
72
73 void run() {
74
75 try {
76 // Start the server listening
77 serverTransport_->listen();
78 } catch (TTransportException& ttx) {
79 cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl;
80 return;
81 }
82
83 // Fetch client from server
84
85 while (true) {
86
87 try {
88
89 threadManager_->add(shared_ptr<Task>(new TThreadPoolServer::Task(processor_, serverTransport_->accept())));;
90
91 } catch (TTransportException& ttx) {
92 break;
93 }
94 }
95 }
96};
97
98}}} // facebook::thrift::server