-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stefan Eilemann
authored and
Stefan Eilemann
committed
Jul 7, 2017
1 parent
6dbb2fb
commit ac15a38
Showing
12 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
/* Copyright (c) 2014-2015, Human Brain Project | ||
/* Copyright (c) 2014-2017, Human Brain Project | ||
* [email protected] | ||
*/ | ||
|
||
|
@@ -130,5 +130,10 @@ std::string Broker::getAddress() const | |
{ | ||
return _impl->getAddress(); | ||
} | ||
|
||
void* Broker::getSocket() | ||
{ | ||
return _impl->socket; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
/* Copyright (c) 2014-2015, Human Brain Project | ||
/* Copyright (c) 2014-2017, Human Brain Project | ||
* [email protected] | ||
*/ | ||
|
||
|
@@ -8,6 +8,7 @@ | |
|
||
#include <zeroeq/log.h> | ||
#include <zeroeq/receiver.h> // base class | ||
#include <zeroeq/sender.h> // base class | ||
|
||
namespace zeroeq | ||
{ | ||
|
@@ -35,7 +36,7 @@ class Broker; | |
* | ||
* Example: @include tests/connection/broker.cpp | ||
*/ | ||
class Broker : public Receiver | ||
class Broker : public Receiver, public Sender | ||
{ | ||
public: | ||
enum PortSelection | ||
|
@@ -94,6 +95,9 @@ class Broker : public Receiver | |
// Receiver API | ||
void addSockets(std::vector<zeroeq::detail::Socket>& entries) final; | ||
void process(zeroeq::detail::Socket& socket, uint32_t timeout) final; | ||
|
||
// Sender API | ||
void* getSocket() final; | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
/* Copyright (c) 2017, Human Brain Project | ||
* [email protected] | ||
*/ | ||
|
||
#include "monitor.h" | ||
|
||
#include "detail/socket.h" | ||
#include "log.h" | ||
#include "sender.h" | ||
|
||
#include <zmq.h> | ||
|
||
namespace zeroeq | ||
{ | ||
class Monitor::Impl | ||
{ | ||
public: | ||
Impl(Sender& sender, void* context) | ||
{ | ||
const auto inproc = std::string("inproc://zeroeq.monitor.") + | ||
servus::make_UUID().getString(); | ||
|
||
if (::zmq_socket_monitor(sender.getSocket(), inproc.c_str(), | ||
ZMQ_EVENT_ALL) != 0) | ||
{ | ||
ZEROEQTHROW( | ||
std::runtime_error(std::string("Cannot monitor socket: ") + | ||
zmq_strerror(zmq_errno()))); | ||
} | ||
|
||
_socket = ::zmq_socket(context, ZMQ_PAIR); | ||
if (!_socket) | ||
ZEROEQTHROW(std::runtime_error( | ||
std::string("Cannot create inproc socket: ") + | ||
zmq_strerror(zmq_errno()))); | ||
|
||
if (::zmq_connect(_socket, inproc.c_str()) != 0) | ||
{ | ||
ZEROEQTHROW(std::runtime_error( | ||
std::string("Cannot connect inproc socket: ") + | ||
zmq_strerror(zmq_errno()))); | ||
} | ||
} | ||
|
||
~Impl() | ||
{ | ||
if (_socket) | ||
::zmq_close(_socket); | ||
} | ||
|
||
void addSockets(std::vector<zeroeq::detail::Socket>& entries) | ||
{ | ||
zeroeq::detail::Socket entry; | ||
entry.socket = _socket; | ||
entry.events = ZMQ_POLLIN; | ||
entries.push_back(entry); | ||
} | ||
|
||
private: | ||
void* _socket; | ||
}; | ||
|
||
Monitor::Monitor(Sender& sender) | ||
: Receiver() | ||
, _impl(new Impl(sender, getZMQContext())) | ||
{ | ||
} | ||
|
||
Monitor::Monitor(Sender& sender, Receiver& shared) | ||
: Receiver(shared) | ||
, _impl(new Impl(sender, getZMQContext())) | ||
{ | ||
} | ||
|
||
Monitor::~Monitor() | ||
{ | ||
} | ||
|
||
void Monitor::addSockets(std::vector<zeroeq::detail::Socket>& entries) | ||
{ | ||
_impl->addSockets(entries); | ||
} | ||
|
||
void Monitor::process(zeroeq::detail::Socket& socket, uint32_t) | ||
{ | ||
// Messages consist of 2 Frames, the first containing the event-id and the | ||
// associated value. The second frame holds the affected endpoint as string. | ||
zmq_msg_t msg; | ||
zmq_msg_init(&msg); | ||
|
||
// The layout of the first Frame is: 16 bit event id 32 bit event value | ||
if (zmq_msg_recv(&msg, socket.socket, 0) == -1) | ||
return; | ||
|
||
const uint16_t event = *(uint16_t*)zmq_msg_data(&msg); | ||
if (!zmq_msg_more(&msg)) | ||
return; | ||
zmq_msg_close(&msg); | ||
|
||
// Second frame in message contains event address, skip | ||
zmq_msg_init(&msg); | ||
if (zmq_msg_recv(&msg, socket.socket, 0) == -1) | ||
return; | ||
zmq_msg_close(&msg); | ||
|
||
switch (event) | ||
{ | ||
case ZMQ_EVENT_CONNECTED: | ||
case ZMQ_EVENT_ACCEPTED: | ||
notifyNewConnection(); | ||
break; | ||
|
||
default: | ||
ZEROEQWARN << "Unhandled monitor event " << event << std::endl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
/* Copyright (c) 2017, Human Brain Project | ||
* [email protected] | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <zeroeq/receiver.h> // base class | ||
|
||
namespace zeroeq | ||
{ | ||
/** Monitors a Sender and notifies on events on its socket. */ | ||
class Monitor : public Receiver | ||
{ | ||
public: | ||
/** Monitor the given sender. */ | ||
ZEROEQ_API explicit Monitor(Sender& sender); | ||
|
||
/** Monitor the given sender and notify with the given shared group. */ | ||
ZEROEQ_API Monitor(Sender& sender, Receiver& shared); | ||
|
||
/** Destroy this monitor*/ | ||
ZEROEQ_API ~Monitor(); | ||
|
||
/** Notify of a new connection to the sender. */ | ||
virtual void notifyNewConnection() {} | ||
private: | ||
class Impl; | ||
std::unique_ptr<Impl> _impl; | ||
|
||
Monitor(const Monitor&) = delete; | ||
Monitor& operator=(const Monitor&) = delete; | ||
|
||
// Receiver API | ||
void addSockets(std::vector<zeroeq::detail::Socket>& entries) final; | ||
void process(zeroeq::detail::Socket& socket, uint32_t timeout) final; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
/* Copyright (c) 2014-2016, Human Brain Project | ||
/* Copyright (c) 2014-2017, Human Brain Project | ||
* Daniel Nachbaur <[email protected]> | ||
* [email protected] | ||
*/ | ||
|
@@ -8,6 +8,7 @@ | |
#define ZEROEQ_PUBLISHER_H | ||
|
||
#include <zeroeq/api.h> | ||
#include <zeroeq/sender.h> // base class | ||
#include <zeroeq/types.h> | ||
|
||
#include <memory> | ||
|
@@ -22,7 +23,7 @@ namespace zeroeq | |
* | ||
* Example: @include tests/publisher.cpp | ||
*/ | ||
class Publisher | ||
class Publisher : public Sender | ||
{ | ||
public: | ||
/** | ||
|
@@ -136,6 +137,8 @@ class Publisher | |
|
||
Publisher(const Publisher&) = delete; | ||
Publisher& operator=(const Publisher&) = delete; | ||
|
||
ZEROEQ_API void* getSocket() final; | ||
}; | ||
} | ||
|
||
|
Oops, something went wrong.