blob: f91c6e8b3d316d389ef1bf343e2a99084d535794 [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#ifndef _THRIFT_SERVER_TZMQSERVER_H_
21#define _THRIFT_SERVER_TZMQSERVER_H_ 1
22
23#include <zmq.hpp>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/server/TServer.h>
David Reiss9f3296b2010-08-31 16:58:41 +000025
26namespace apache { namespace thrift { namespace server {
27
28class TZmqServer : public TServer {
29 public:
30 TZmqServer(
31 boost::shared_ptr<TProcessor> processor,
32 zmq::context_t& ctx, const std::string& endpoint, int type)
33 : TServer(processor)
34 , zmq_type_(type)
35 , sock_(ctx, type)
36 {
37 if(zmq_type_ == ZMQ_SUB) {
38 sock_.setsockopt(ZMQ_SUBSCRIBE, "", 0) ; // listen to all messages
39 sock_.connect(endpoint.c_str()) ;
40 }
41 else {
42 sock_.bind(endpoint.c_str());
43 }
44 }
45
46 bool serveOne(int recv_flags = 0);
47 void serve() {
48 while (true) {
49 serveOne();
50 }
51 }
52
53 zmq::socket_t& getSocket() {
54 return sock_;
55 }
56
57 private:
58 int zmq_type_;
59 zmq::socket_t sock_;
60};
61
62
63class TZmqMultiServer {
64 public:
65 void serveOne(long timeout = -1);
66 void serveForever();
67
68 std::vector<TZmqServer*>& servers() {
69 return servers_;
70 }
71
72 private:
73 zmq::pollitem_t* setupPoll();
74 void serveActive(zmq::pollitem_t* items, long timeout);
75 std::vector<TZmqServer*> servers_;
76};
77
78
79}}} // apache::thrift::server
80
81#endif // #ifndef _THRIFT_SERVER_TZMQSERVER_H_