| 1 |
|
| 2 |
|
| 3 |
#ifndef _PROTO_ZMQ |
| 4 |
#define _PROTO_ZMQ |
| 5 |
|
| 6 |
#define ZMQ_BUILD_DRAFT_API |
| 7 |
|
| 8 |
#include "protoThread.h" |
| 9 |
#include "protoEvent.h" |
| 10 |
#include "zmq.h" |
| 11 |
|
| 12 |
class ProtoZmq |
| 13 |
{ |
| 14 |
protected: |
| 15 |
class PollerThread; // forward declaration |
| 16 |
|
| 17 |
public: |
| 18 |
// Use the ProtoEvent notification framework for a ZMQ Socket |
| 19 |
// A PollerThread is used to run a zmq_poll() loop to monitor active sockets |
| 20 |
class Socket : private ProtoEvent |
| 21 |
{ |
| 22 |
public: |
| 23 |
Socket(); |
| 24 |
virtual ~Socket(); |
| 25 |
|
| 26 |
enum State |
| 27 |
{ |
| 28 |
CLOSED = 0, |
| 29 |
IDLE, |
| 30 |
CONNECTED |
| 31 |
}; |
| 32 |
|
| 33 |
// "socketType" is ZMQ socket type. E.g., ZMQ_PUB, ZMQ_SUB, etc |
| 34 |
bool Open(int socketType, void* zmqSocket = NULL, void* zmqContext = NULL); |
| 35 |
void Close(); |
| 36 |
|
| 37 |
bool Bind(const char* endpoint); |
| 38 |
bool Connect(const char* endpoint); |
| 39 |
|
| 40 |
// for ZMQ_SUB sockets |
| 41 |
bool Subscribe(const char* prefix, unsigned int length = 0); // treats prefix as string if non-NULL and length is zero |
| 42 |
|
| 43 |
// for ZMQ_DISH sockets |
| 44 |
bool Join(const char* group); |
| 45 |
|
| 46 |
// TBD - support multi-part messages, etc |
| 47 |
bool Send(char* buffer, unsigned int& numBytes); |
| 48 |
bool Recv(char* buffer, unsigned int& numBytes); |
| 49 |
bool RecvMsg(zmq_msg_t* zmqMsg); |
| 50 |
bool StartInputNotification(); |
| 51 |
bool StopInputNotification(); |
| 52 |
bool StartOutputNotification(); |
| 53 |
bool StopOutputNotification(); |
| 54 |
|
| 55 |
int GetPollFlags() const |
| 56 |
{return poll_flags;} |
| 57 |
|
| 58 |
int GetPollStatus() const |
| 59 |
{return poll_status;} |
| 60 |
bool IsInputReady() const |
| 61 |
{return (0 != ((ZMQ_POLLIN & poll_status)));} |
| 62 |
bool IsOutputReady() const |
| 63 |
{return (0 != ((ZMQ_POLLOUT & poll_status)));} |
| 64 |
|
| 65 |
void* GetContext() |
| 66 |
{return zmq_ctx;} |
| 67 |
void* GetSocket() |
| 68 |
{return zmq_sock;} |
| 69 |
|
| 70 |
bool IsOpen() const |
| 71 |
{return (NULL != zmq_sock);} |
| 72 |
|
| 73 |
bool UsingExternalContext() const |
| 74 |
{return ext_ctx;} |
| 75 |
|
| 76 |
// If a non-NULL pollerThread is used, you should disassociate your sockets from the pollerThread |
| 77 |
// before deleting it! (The "default" NULL pollerThread takes care of itself) |
| 78 |
bool SetNotifier(ProtoEvent::Notifier* theNotifier, class PollerThread* pollerThread = NULL); |
| 79 |
|
| 80 |
template <class listenerType> |
| 81 |
bool SetListener(listenerType* theListener, void(listenerType::*eventHandler)(ProtoEvent&)) |
| 82 |
{return ProtoEvent::SetListener(theListener, eventHandler);} |
| 83 |
|
| 84 |
private: |
| 85 |
friend class PollerThread; |
| 86 |
void SetEvent() |
| 87 |
{ProtoEvent::Set();} |
| 88 |
void SetPollStatus(int status) |
| 89 |
{poll_status = status;} |
| 90 |
|
| 91 |
bool UpdateNotification(); |
| 92 |
|
| 93 |
static PollerThread* default_poller_thread; |
| 94 |
static ProtoMutex default_poller_mutex; // to guarantee thread-safe instantiation of default_poller_thread |
| 95 |
|
| 96 |
State state; |
| 97 |
void* zmq_ctx; // true if zmq_ctx was supplied externally |
| 98 |
bool ext_ctx; |
| 99 |
void* zmq_sock; |
| 100 |
bool ext_sock; // true if zmq_sock was supplied externally |
| 101 |
int poll_flags; |
| 102 |
int poll_status; |
| 103 |
|
| 104 |
PollerThread* poller_thread; |
| 105 |
bool poller_active; |
| 106 |
|
| 107 |
}; // end ProtoZmq::Socket |
| 108 |
|
| 109 |
protected: |
| 110 |
// This class is used to "wire up" thread-safe ZMQ sockets to ProtoDispatcher notifications |
| 111 |
class PollerThread : private ProtoThread |
| 112 |
{ |
| 113 |
public: |
| 114 |
PollerThread(); |
| 115 |
~PollerThread(); |
| 116 |
|
| 117 |
bool Open(void* zmqContext = NULL, bool retainLock = false); |
| 118 |
void Close(); |
| 119 |
|
| 120 |
bool AddSocket(Socket& zmqSock); |
| 121 |
bool ModSocket(Socket& zmqSock); |
| 122 |
bool RemoveSocket(Socket& zmqSock); |
| 123 |
|
| 124 |
private: |
| 125 |
int RunThread(); |
| 126 |
bool SetBreak() |
| 127 |
{ |
| 128 |
char dummy; |
| 129 |
unsigned oneByte = 1; |
| 130 |
return break_client.Send(&dummy, oneByte); |
| 131 |
} |
| 132 |
void ClosePrivate(); |
| 133 |
bool AddSocketPrivate(Socket& zmqSock); |
| 134 |
bool Signal(); |
| 135 |
void Unsignal(); |
| 136 |
|
| 137 |
void* zmq_ctx; |
| 138 |
bool ext_ctx; |
| 139 |
void* zmq_poller; |
| 140 |
volatile bool poller_running; |
| 141 |
zmq_poller_event_t* event_array; |
| 142 |
unsigned int event_array_length; |
| 143 |
unsigned int socket_count; |
| 144 |
Socket break_server; // listens for 'break' messages to interrupt poller |
| 145 |
Socket break_client; // used to send 'break' messages to the break_server |
| 146 |
ProtoMutex suspend_mutex; |
| 147 |
ProtoMutex signal_mutex; |
| 148 |
|
| 149 |
}; // end class ProtoZmq::PollerThread |
| 150 |
|
| 151 |
}; // end class ProtoZmq |
| 152 |
|
| 153 |
|
| 154 |
#endif // !_PROTO_ZMQ |