| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#include "server/TSimpleServer.h"
|
|
|
21 |
#include "transport/TTransportException.h"
|
|
|
22 |
#include <string>
|
|
|
23 |
#include <iostream>
|
|
|
24 |
|
|
|
25 |
namespace apache { namespace thrift { namespace server {
|
|
|
26 |
|
|
|
27 |
using namespace std;
|
|
|
28 |
using namespace apache::thrift;
|
|
|
29 |
using namespace apache::thrift::protocol;
|
|
|
30 |
using namespace apache::thrift::transport;
|
|
|
31 |
using boost::shared_ptr;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* A simple single-threaded application server. Perfect for unit tests!
|
|
|
35 |
*
|
|
|
36 |
*/
|
|
|
37 |
void TSimpleServer::serve() {
|
|
|
38 |
|
|
|
39 |
shared_ptr<TTransport> client;
|
|
|
40 |
shared_ptr<TTransport> inputTransport;
|
|
|
41 |
shared_ptr<TTransport> outputTransport;
|
|
|
42 |
shared_ptr<TProtocol> inputProtocol;
|
|
|
43 |
shared_ptr<TProtocol> outputProtocol;
|
|
|
44 |
|
|
|
45 |
try {
|
|
|
46 |
// Start the server listening
|
|
|
47 |
serverTransport_->listen();
|
|
|
48 |
} catch (TTransportException& ttx) {
|
|
|
49 |
cerr << "TSimpleServer::run() listen(): " << ttx.what() << endl;
|
|
|
50 |
return;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// Run the preServe event
|
|
|
54 |
if (eventHandler_ != NULL) {
|
|
|
55 |
eventHandler_->preServe();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Fetch client from server
|
|
|
59 |
while (!stop_) {
|
|
|
60 |
try {
|
|
|
61 |
client = serverTransport_->accept();
|
|
|
62 |
inputTransport = inputTransportFactory_->getTransport(client);
|
|
|
63 |
outputTransport = outputTransportFactory_->getTransport(client);
|
|
|
64 |
inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
|
|
|
65 |
outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
|
|
|
66 |
if (eventHandler_ != NULL) {
|
|
|
67 |
eventHandler_->clientBegin(inputProtocol, outputProtocol);
|
|
|
68 |
}
|
|
|
69 |
try {
|
|
|
70 |
while (processor_->process(inputProtocol, outputProtocol)) {
|
|
|
71 |
// Peek ahead, is the remote side closed?
|
|
|
72 |
if (!inputTransport->peek()) {
|
|
|
73 |
break;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
} catch (TTransportException& ttx) {
|
|
|
77 |
cerr << "TSimpleServer client died: " << ttx.what() << endl;
|
|
|
78 |
} catch (TException& tx) {
|
|
|
79 |
cerr << "TSimpleServer exception: " << tx.what() << endl;
|
|
|
80 |
}
|
|
|
81 |
if (eventHandler_ != NULL) {
|
|
|
82 |
eventHandler_->clientEnd(inputProtocol, outputProtocol);
|
|
|
83 |
}
|
|
|
84 |
inputTransport->close();
|
|
|
85 |
outputTransport->close();
|
|
|
86 |
client->close();
|
|
|
87 |
} catch (TTransportException& ttx) {
|
|
|
88 |
if (inputTransport != NULL) { inputTransport->close(); }
|
|
|
89 |
if (outputTransport != NULL) { outputTransport->close(); }
|
|
|
90 |
if (client != NULL) { client->close(); }
|
|
|
91 |
cerr << "TServerTransport died on accept: " << ttx.what() << endl;
|
|
|
92 |
continue;
|
|
|
93 |
} catch (TException& tx) {
|
|
|
94 |
if (inputTransport != NULL) { inputTransport->close(); }
|
|
|
95 |
if (outputTransport != NULL) { outputTransport->close(); }
|
|
|
96 |
if (client != NULL) { client->close(); }
|
|
|
97 |
cerr << "Some kind of accept exception: " << tx.what() << endl;
|
|
|
98 |
continue;
|
|
|
99 |
} catch (string s) {
|
|
|
100 |
if (inputTransport != NULL) { inputTransport->close(); }
|
|
|
101 |
if (outputTransport != NULL) { outputTransport->close(); }
|
|
|
102 |
if (client != NULL) { client->close(); }
|
|
|
103 |
cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
|
|
|
104 |
break;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if (stop_) {
|
|
|
109 |
try {
|
|
|
110 |
serverTransport_->close();
|
|
|
111 |
} catch (TTransportException &ttx) {
|
|
|
112 |
cerr << "TServerTransport failed on close: " << ttx.what() << endl;
|
|
|
113 |
}
|
|
|
114 |
stop_ = false;
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
}}} // apache::thrift::server
|