blob: 88660a3305c373b0b65e446b07730b0b4fd12e2d [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"
Roger Meier49ff8b12012-04-13 09:12:31 +000021#include <thrift/transport/TBufferTransports.h>
David Reiss9f3296b2010-08-31 16:58:41 +000022#include <boost/scoped_ptr.hpp>
23
cyy316723a2019-01-05 16:35:14 +080024using apache::thrift::std::shared_ptr;
David Reiss9f3296b2010-08-31 16:58:41 +000025using apache::thrift::transport::TMemoryBuffer;
26using apache::thrift::protocol::TProtocol;
27
28namespace apache { namespace thrift { namespace server {
29
David Reiss9f3296b2010-08-31 16:58:41 +000030bool TZmqServer::serveOne(int recv_flags) {
31 zmq::message_t msg;
32 bool received = sock_.recv(&msg, recv_flags);
33 if (!received) {
34 return false;
35 }
36 shared_ptr<TMemoryBuffer> inputTransport(new TMemoryBuffer((uint8_t*)msg.data(), msg.size()));
37 shared_ptr<TMemoryBuffer> outputTransport(new TMemoryBuffer());
38 shared_ptr<TProtocol> inputProtocol(
39 inputProtocolFactory_->getProtocol(inputTransport));
40 shared_ptr<TProtocol> outputProtocol(
41 outputProtocolFactory_->getProtocol(outputTransport));
Jens Geyer3e9c3a22014-12-22 22:21:24 +010042 shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer);
David Reiss9f3296b2010-08-31 16:58:41 +000043
Jens Geyer3e9c3a22014-12-22 22:21:24 +010044 processor_->process(inputProtocol, outputProtocol, NULL);
David Reiss9f3296b2010-08-31 16:58:41 +000045
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