blob: d53d174c79ff46abaaa86925e9b742a8ebb87edc [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
Marc Slemko16698852006-08-04 03:16:10 +000015class TThreadPoolServer::Task: public Runnable {
Marc Slemko35452342006-08-03 19:01:37 +000016
Marc Slemko16698852006-08-04 03:16:10 +000017 shared_ptr<TProcessor> _processor;
18 shared_ptr<TTransport> _transport;
19 shared_ptr<TBufferedTransport> _bufferedTransport;
Marc Slemko35452342006-08-03 19:01:37 +000020
Marc Slemko16698852006-08-04 03:16:10 +000021public:
Marc Slemko35452342006-08-03 19:01:37 +000022
Marc Slemko16698852006-08-04 03:16:10 +000023 Task(shared_ptr<TProcessor> processor,
24 shared_ptr<TTransport> transport) :
25 _processor(processor),
26 _transport(transport),
27 _bufferedTransport(new TBufferedTransport(transport)) {
Marc Slemko35452342006-08-03 19:01:37 +000028 }
Marc Slemko16698852006-08-04 03:16:10 +000029
30 ~Task() {}
Marc Slemko35452342006-08-03 19:01:37 +000031
Marc Slemko35452342006-08-03 19:01:37 +000032 void run() {
Marc Slemko16698852006-08-04 03:16:10 +000033
34 while(true) {
35
Marc Slemko35452342006-08-03 19:01:37 +000036 try {
Marc Slemko16698852006-08-04 03:16:10 +000037 _processor->process(_bufferedTransport);
38
Marc Slemko35452342006-08-03 19:01:37 +000039 } catch (TTransportException& ttx) {
Marc Slemko16698852006-08-04 03:16:10 +000040
41 break;
42
43 } catch(...) {
44
Marc Slemko35452342006-08-03 19:01:37 +000045 break;
46 }
47 }
Marc Slemko16698852006-08-04 03:16:10 +000048
49 _bufferedTransport->close();
Marc Slemko35452342006-08-03 19:01:37 +000050 }
51};
Marc Slemko16698852006-08-04 03:16:10 +000052
53TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
54 shared_ptr<TServerOptions> options,
55 shared_ptr<TServerTransport> serverTransport,
56 shared_ptr<ThreadManager> threadManager) :
57 TServer(processor, options),
58 serverTransport_(serverTransport),
59 threadManager_(threadManager) {
60}
61
62TThreadPoolServer::~TThreadPoolServer() {}
63
64void TThreadPoolServer::run() {
65
66 try {
67 // Start the server listening
68 serverTransport_->listen();
69 } catch (TTransportException& ttx) {
70 cerr << "TThreadPoolServer::run() listen(): " << ttx.getMessage() << endl;
71 return;
72 }
73
74 // Fetch client from server
75
76 while (true) {
77
78 try {
79
80 threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_,
81 shared_ptr<TTransport>(serverTransport_->accept()))));
82
83 } catch (TTransportException& ttx) {
84 break;
85 }
86 }
87}
Marc Slemko35452342006-08-03 19:01:37 +000088
89}}} // facebook::thrift::server