Created
October 19, 2010 20:19
-
-
Save drbobbeaty/635015 to your computer and use it in GitHub Desktop.
Simple ZeroMQ Transmitter of Messages
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
// System Headers | |
#include <iostream> | |
#include <stdio.h> | |
#include <string> | |
#include <stdint.h> | |
#include <sys/time.h> | |
// Third-Party Headers | |
#include <zmq.hpp> | |
// Other Headers | |
// Forward Declarations | |
// Public Constants | |
// Public DataTypes | |
// Public Data Constants | |
/** | |
* This is the main test frame -- open up a ZMQ socket to one URL and | |
* send a simple message every second. It's meant to be a measurement | |
* test frame. | |
*/ | |
int main(int argc, char *argv[]) { | |
bool error = false; | |
// this is the URL to publish on | |
std::string url("epgm://eth0;239.22.3.13:11111"); | |
// make the ZMQ Context and Socket for what we need to transmit on | |
zmq::context_t *mContext = NULL; | |
if (!error) { | |
mContext = new zmq::context_t(1); | |
if (mContext == NULL) { | |
error = true; | |
std::cout << "could not create the ZMQ context" << std::endl; | |
} | |
} | |
zmq::socket_t *mSocket = NULL; | |
if (!error) { | |
mSocket = new zmq::socket_t(*mContext, ZMQ_PUB); | |
if (mSocket == NULL) { | |
error = true; | |
std::cout << "could not create the ZMQ socket" << std::endl; | |
} else { | |
// we need to set the maximum rate on this guy to 200Mbps | |
int64_t rate = 200000; | |
mSocket->setsockopt(ZMQ_RATE, &rate, sizeof(rate)); | |
// connect to the right multicast group (with the URL) | |
mSocket->connect(url.c_str()); | |
} | |
} | |
// verify it's all initialized OK | |
if (!error) { | |
std::cout << "Initialization complete." << std::endl; | |
} | |
// now let's listen for a while and just record the stats | |
if (!error) { | |
// let's make a "payload" that we'll put into all the messages | |
std::string payload("ABCEDGHIJKLMNOPQRSTUVWXYZ0123456789abcedfghijklmnopqrstuvwxyz"); | |
// now let's get into the sending loop | |
while (true) { | |
// create a message, fill it, and send it out the socket | |
zmq::message_t msg(payload.size()); | |
memcpy(msg.data(), payload.data(), payload.size()); | |
std::cout << "sending..." << std::endl; | |
mSocket->send(msg); | |
// wait a second and do it again | |
sleep(1); | |
} | |
} | |
// clean it all up | |
if (mSocket != NULL) { | |
delete mSocket; | |
mSocket = NULL; | |
} | |
if (mContext != NULL) { | |
delete mContext; | |
mContext = NULL; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment