blob: c6142d7c31039d7ce2fe8cee214a77a8fcc7e838 [file] [log] [blame]
David Reiss9f3296b2010-08-31 16:58:41 +00001/*
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 "TZmqServer.h"
21#include <transport/TBufferTransports.h>
22#include <boost/scoped_ptr.hpp>
23
24using boost::shared_ptr;
25using apache::thrift::transport::TMemoryBuffer;
26using apache::thrift::protocol::TProtocol;
27
28namespace apache { namespace thrift { namespace server {
29
30
31bool TZmqServer::serveOne(int recv_flags) {
32 zmq::message_t msg;
33 bool received = sock_.recv(&msg, recv_flags);
34 if (!received) {
35 return false;
36 }
37 shared_ptr<TMemoryBuffer> inputTransport(new TMemoryBuffer((uint8_t*)msg.data(), msg.size()));
38 shared_ptr<TMemoryBuffer> outputTransport(new TMemoryBuffer());
39 shared_ptr<TProtocol> inputProtocol(
40 inputProtocolFactory_->getProtocol(inputTransport));
41 shared_ptr<TProtocol> outputProtocol(
42 outputProtocolFactory_->getProtocol(outputTransport));
43
44 processor_->process(inputProtocol, outputProtocol);
45
46 if (zmq_type_ == ZMQ_REP) {
47 uint8_t* buf;
48 uint32_t size;
49 outputTransport->getBuffer(&buf, &size);
50 msg.rebuild(size);
51 std::memcpy(msg.data(), buf, size);
52 (void)sock_.send(msg);
53 }
54
55 return true;
56}
57
58
59void TZmqMultiServer::serveOne(long timeout) {
60 boost::scoped_ptr<zmq::pollitem_t> items(setupPoll());
61 serveActive(items.get(), timeout);
62}
63
64
65void TZmqMultiServer::serveForever() {
66 boost::scoped_ptr<zmq::pollitem_t> items(setupPoll());
67 while (true) {
68 serveActive(items.get(), -1);
69 }
70}
71
72
73zmq::pollitem_t* TZmqMultiServer::setupPoll() {
74 zmq::pollitem_t* items = new zmq::pollitem_t[servers_.size()];
75 for (int i = 0; i < servers_.size(); ++i) {
76 items[i].socket = servers_[i]->getSocket();
77 items[i].events = ZMQ_POLLIN;
78 }
79 return items;
80}
81
82void TZmqMultiServer::serveActive(zmq::pollitem_t* items, long timeout) {
83 int rc = zmq::poll(items, servers_.size(), timeout);
84 if (rc == 0) {
85 return;
86 }
87 for (int i = 0; i < servers_.size(); ++i) {
88 if ((items[i].revents & ZMQ_POLLIN) != 0) {
89 // Should we pass ZMQ_NOBLOCK here to be safe?
90 servers_[i]->serveOne();
91 }
92 }
93}
94
95
96}}} // apache::thrift::server