Obsidian
Probabilistic Geophysical Joint Inversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
stateline::comms Namespace Reference

Classes

class  ClientHeartbeat
 Handles heartbeating on the client side. Client heartbeat assumes only one connection (to the server), so sends and receives to one source only. More...
 
class  Delegator
 Requester object that takes jobs and returns results. Communicates with a delegator living in a (possibly) different thread. More...
 
struct  JobData
 Abstraction of job specification. More...
 
class  Message
 Define valid messages to send on the GDFP-SW (GDF Server-Worker Protocol) More...
 
class  Minion
 Minion object that requests a job, computes the result, then submits that result to the connected worker. More...
 
class  Requester
 Requester object that takes jobs and returns results. Communicates with a delegator living in a (possibly) different thread. More...
 
struct  ResultData
 Abstraction of job results. More...
 
class  ServerHeartbeat
 Handles heartbeating on the server side. Server heartbeat assumes that it has multiple connections from different clients, so sends to all of them, and keeps track of who is connected. More...
 
struct  SocketHandler
 Represents the callback signals for a socket. More...
 
class  SocketRouter
 Implements polling and configurable routing between an arbitrary number of (pre-constructed) zeromq sockets. Functionality is attached through a signal interface. More...
 
class  Worker
 Worker object that takes jobs, forwards them to a minion then receives results from the minion and send them back to the delegator. More...
 

Typedefs

typedef boost::bimap< SocketID,
uint > 
IndexBiMap
 A type that maps between indices in an array, and SocketID objects.
 
typedef std::set< std::string > HBClients
 A set of clients being monitored for heartbeating.
 
typedef std::map< std::string,
hrc::time_point > 
HBMap
 A map of clients to when the last heartbeat arrived from that client.
 

Enumerations

enum  Subject : uint {
  HELLO = 0, HEARTBEAT = 1, PROBLEMSPEC = 2, JOBREQUEST = 3,
  JOB = 4, JOBSWAP = 5, ALLDONE = 6, GOODBYE = 7
}
 Defines the bytes actually sent in the subject frame of GDF messages. Allows us to use a switch statement on the subject.
 
enum  SocketID {
  REQUESTER, MINION, WORKER, NETWORK,
  HEARTBEAT, ALPHA, BETA
}
 Enums to give each type of socket an index.
 

Functions

void failedSend (const Message &m)
 Stop the heartbeat thread. This is called when the thread can't send to the local socket.
 
void monitorTimeout (hrc::time_point &lastReceivedTime, SocketRouter &router, uint msTimeout)
 Update book-keeping on server heartbeats. Called every time the socket is polled. More...
 
void heartbeatArrived (hrc::time_point &lastReceivedTime)
 Update book-keeping on server heartbeats. Called when the socket receives a heartbeat from the server. More...
 
void sendHeartbeat (hrc::time_point &lastSendTime, SocketRouter &router, uint msFrequency)
 Send a heartbeat to the server if necessary. More...
 
std::vector< uint > stringToIDs (const std::string &s)
 Convert list of uints in string form to vector of uint.
 
std::string idsToString (const std::vector< uint > &ids)
 Convert vector of uints to list of uints in string form seperated by space.
 
std::string subjectString (Subject s)
 Convert a subject into a string. More...
 
std::string addressAsString (const std::vector< std::string > &addr)
 Convert an address to a string. More...
 
std::ostream & operator<< (std::ostream &os, const Message &m)
 Print a message for logging and debugging purposes. More...
 
void print (const Message &m)
 Print a message for logging and debugging purposes. More...
 
void sendJob (zmq::socket_t &socket, const JobData &job)
 Send a job over a ZMQ socket. More...
 
void sendJob (zmq::socket_t &socket, const std::vector< uint > &ids, const JobData &job)
 Send a job over a ZMQ socket. More...
 
ResultData receiveResult (zmq::socket_t &socket)
 Read a job result from a socket. More...
 
std::pair< std::vector< uint >
, ResultData
receiveResultAndIDs (zmq::socket_t &socket)
 Read a job result from a socket. More...
 
std::ostream & operator<< (std::ostream &os, const SocketID &id)
 Print a socket ID for logging and debugging purposes. More...
 
void insertClient (const Message &m, HBClients &clients, HBMap &lastHeartbeats)
 Add a new client to be monitored for heartbeating. More...
 
void deleteClient (const Message &m, HBClients &clients, HBMap &lastHeartbeats)
 Remove a client from being monitored for heartbeating. More...
 
void failedSendServer (const Message &m)
 Stop the heartbeat thread. This is called when the thread can't send to the local socket.
 
void receiveHeartbeat (const Message &m, HBMap &lastHeartbeats)
 Update book-keeping on client heartbeats. Called when a heartbeat message arrives from a client. More...
 
void monitorTimeouts (HBClients &clients, HBMap &lastHeartbeats, SocketRouter &router, uint msTimeout)
 Update book-keeping on client heartbeats. Called every time the socket is polled. More...
 
void sendHeartbeats (HBClients &clients, hrc::time_point &lastHbTime, SocketRouter &router, uint msFrequency)
 Send heartbeats to all the clients if necessary. More...
 
std::string receiveString (zmq::socket_t &socket)
 Read a string form a ZMQ socket. This is a blocking call. More...
 
bool sendString (zmq::socket_t &socket, const std::string &string)
 Send a string over a ZMQ socket. More...
 
bool sendStringPart (zmq::socket_t &socket, const std::string &string)
 Send a string over a ZMQ socket as a chunk of a multipart message. More...
 
Message receive (zmq::socket_t &socket)
 Receive a (possibly multi-part) message conforming to GDFP-SW comms protocal and provide it as a stateline::comms::Message class. More...
 
void send (zmq::socket_t &socket, const Message &message)
 Send a (possibly multi-part) message conforming to GDFP-SW comms protocol. More...
 
std::string randomSocketID ()
 Computes a random socket ID conforming to zeromq requirements for the string (not starting with a zero etc, see zeromq doco). More...
 
void setSocketID (const std::string &id, zmq::socket_t &socket)
 Sets the ID of a socket. The socket must be a DEALER or A REQ. More...
 
void forwardToNetwork (const Message &m, SocketRouter &router)
 Forward a message to the delegator. More...
 
void forwardToMinion (const Message &m, SocketRouter &router)
 Forward a message to a minion. More...
 
void disconnectFromServer (const Message &m)
 Disconnect from the server with a message. More...
 
std::string serialise (uint id)
 
void unserialise (const std::string &s, uint &id)
 
void unserialise (const std::string &s, std::vector< uint > &ids)
 
std::string serialise (const std::vector< uint > &ids)
 
std::string serialise (const mcmc::State &s)
 
void unserialise (const std::string &s, mcmc::State &g)
 

Detailed Description

Namespace for all communications functionality.

Function Documentation

std::string stateline::comms::addressAsString ( const std::vector< std::string > &  addr)

Convert an address to a string.

Parameters
addrThe address to convert.
Returns
The address as a single string.
void stateline::comms::deleteClient ( const Message &  m,
HBClients &  clients,
HBMap &  lastHeartbeats 
)

Remove a client from being monitored for heartbeating.

Parameters
mThe GOODBYE message from the client.
clientsList of clients being monitored.
lastHeartbeatsMap containing when the last heartbeat arrived from each client.
void stateline::comms::disconnectFromServer ( const Message &  m)

Disconnect from the server with a message.

Parameters
mThe message to send to the server.
void stateline::comms::forwardToMinion ( const Message &  m,
SocketRouter &  router 
)

Forward a message to a minion.

Parameters
mThe message to forward.
routerThe socket router.
void stateline::comms::forwardToNetwork ( const Message &  m,
SocketRouter &  router 
)

Forward a message to the delegator.

Parameters
mThe message to forward.
routerThe socket router.
void stateline::comms::heartbeatArrived ( hrc::time_point &  lastReceivedTime)

Update book-keeping on server heartbeats. Called when the socket receives a heartbeat from the server.

Parameters
lastReceivedTimeThe time that the client last received a heartbeat from the server.
void stateline::comms::insertClient ( const Message &  m,
HBClients &  clients,
HBMap &  lastHeartbeats 
)

Add a new client to be monitored for heartbeating.

Parameters
mThe HELLO message from the client.
clientsList of clients being monitored.
lastHeartbeatsMap containing when the last heartbeat arrived from each client.
void stateline::comms::monitorTimeout ( hrc::time_point &  lastReceivedTime,
SocketRouter &  router,
uint  msTimeout 
)

Update book-keeping on server heartbeats. Called every time the socket is polled.

Parameters
lastReceivedTimeThe time that the client last received a heartbeat from the server.
routerA reference to the socket router.
msTimeoutThe heartbeat timeout in milliseconds.
void stateline::comms::monitorTimeouts ( HBClients &  clients,
HBMap &  lastHeartbeats,
SocketRouter &  router,
uint  msTimeout 
)

Update book-keeping on client heartbeats. Called every time the socket is polled.

Parameters
clientsList of clients being monitored.
lastHeartbeatsMap containing when the last heartbeat arrived from each client.
routerA reference to the socket router.
msTimeoutThe heartbeat timeout in milliseconds.
std::ostream & stateline::comms::operator<< ( std::ostream &  os,
const SocketID &  id 
)

Print a socket ID for logging and debugging purposes.

Parameters
osThe output stream.
idThe socket ID to print.
std::ostream & stateline::comms::operator<< ( std::ostream &  os,
const Message &  m 
)

Print a message for logging and debugging purposes.

Allows a message to be printed with std::cout.

Parameters
osThe output stream.
mThe message to print.
void stateline::comms::print ( const Message &  m)

Print a message for logging and debugging purposes.

Parameters
mThe message to print.
std::string stateline::comms::randomSocketID ( )

Computes a random socket ID conforming to zeromq requirements for the string (not starting with a zero etc, see zeromq doco).

Returns
A socket id string.
Message stateline::comms::receive ( zmq::socket_t &  socket)

Receive a (possibly multi-part) message conforming to GDFP-SW comms protocal and provide it as a stateline::comms::Message class.

Parameters
socketThe socket the message is coming into.
Returns
The Message object received.
void stateline::comms::receiveHeartbeat ( const Message &  m,
HBMap &  lastHeartbeats 
)

Update book-keeping on client heartbeats. Called when a heartbeat message arrives from a client.

Parameters
mThe HEARTBEAT message from the client.
lastHeartbeatsMap containing when the last heartbeat arrived from each client.
ResultData stateline::comms::receiveResult ( zmq::socket_t &  socket)

Read a job result from a socket.

Parameters
socketThe socket to read from.
Returns
The job result that was read from the socket.
std::pair<std::vector<uint>, ResultData> stateline::comms::receiveResultAndIDs ( zmq::socket_t &  socket)

Read a job result from a socket.

Parameters
socketThe socket to read from.
Returns
The job result that was read from the socket.
std::string stateline::comms::receiveString ( zmq::socket_t &  socket)

Read a string form a ZMQ socket. This is a blocking call.

Parameters
socketThe socket to read from.
Returns
A string containing the data read from the socket.
void stateline::comms::send ( zmq::socket_t &  socket,
const Message &  message 
)

Send a (possibly multi-part) message conforming to GDFP-SW comms protocol.

Parameters
socketThe socket the message is coming into.
messageThe stateline::comms::Message to send.
void stateline::comms::sendHeartbeat ( hrc::time_point &  lastSendTime,
SocketRouter &  router,
uint  msFrequency 
)

Send a heartbeat to the server if necessary.

Parameters
lastSendTimeThe last time this client sent a heartbeat.
routerA reference to the socket router.
msFrequencyThe number of milliseconds between each heartbeat.
void stateline::comms::sendHeartbeats ( HBClients &  clients,
hrc::time_point &  lastHbTime,
SocketRouter &  router,
uint  msFrequency 
)

Send heartbeats to all the clients if necessary.

Parameters
clientsList of clients being monitored.
lastHbTimeThe last time this thread sent out heartbeats.
routerA reference to the socket router.
msFrequencyThe number of milliseconds between each heartbeat.
void stateline::comms::sendJob ( zmq::socket_t &  socket,
const JobData &  job 
)

Send a job over a ZMQ socket.

Parameters
socketThe socket to send the job over.
jobThe job to send.
void stateline::comms::sendJob ( zmq::socket_t &  socket,
const std::vector< uint > &  ids,
const JobData &  job 
)

Send a job over a ZMQ socket.

Parameters
socketThe socket to send the job over.
jobThe job to send.
bool stateline::comms::sendString ( zmq::socket_t &  socket,
const std::string &  string 
)

Send a string over a ZMQ socket.

Parameters
socketThe socket to send the string over.
stringThe string data to send.
Returns
Whether the send was successful.
bool stateline::comms::sendStringPart ( zmq::socket_t &  socket,
const std::string &  string 
)

Send a string over a ZMQ socket as a chunk of a multipart message.

Parameters
socketThe socket to send the string over.
stringThe string data to send.
Returns
Whether the send was successful.
void stateline::comms::setSocketID ( const std::string &  id,
zmq::socket_t &  socket 
)

Sets the ID of a socket. The socket must be a DEALER or A REQ.

Parameters
idThe socket ID.
socketThe socket that is being assigned the ID.
std::string stateline::comms::subjectString ( Subject  s)

Convert a subject into a string.

Identifies a particular message code, defined as a Subject enum.

Parameters
sThe subject to convert.
Returns
The string representation of the subject.
Parameters
sThe subject enum being queried (a uint really).
Returns
The name of the corresponding message.