Commit f34a468a authored by Martin Sustrik's avatar Martin Sustrik

coding style fixed in zmqd

parent 7773fddd
...@@ -24,22 +24,22 @@ ...@@ -24,22 +24,22 @@
#include "../include/zmq.hpp" #include "../include/zmq.hpp"
#include "../foreign/xmlParser/xmlParser.cpp" #include "../foreign/xmlParser/xmlParser.cpp"
namespace { class device_cfg_t
{
class device_cfg_t
{
enum endpoint_direction {connect, bind}; enum endpoint_direction {connect, bind};
typedef std::pair<endpoint_direction, std::string> sock_details_t; typedef std::pair <endpoint_direction, std::string> sock_details_t;
typedef std::vector<sock_details_t> vsock_dets_t; typedef std::vector <sock_details_t> vsock_dets_t;
public: public:
explicit device_cfg_t(int type) explicit device_cfg_t (int type) :
: device_type(type) , context(0) device_type (type),
, in_socket(0), out_socket(0) context (0),
in_socket (0),
out_socket (0)
{ {
} }
...@@ -49,71 +49,69 @@ namespace { ...@@ -49,71 +49,69 @@ namespace {
delete in_socket; delete in_socket;
} }
bool init(XMLNode& device) bool init (XMLNode &device)
{ {
XMLNode in_node = device.getChildNode ("in"); XMLNode in_node = device.getChildNode ("in");
if (in_node.isEmpty ()) { if (in_node.isEmpty ()) {
fprintf (stderr, "'in' node is missing in the configuration file\n"); fprintf (stderr,
"'in' node is missing in the configuration file\n");
return false; return false;
} }
XMLNode out_node = device.getChildNode ("out"); XMLNode out_node = device.getChildNode ("out");
if (out_node.isEmpty ()) { if (out_node.isEmpty ()) {
fprintf (stderr, "'out' node is missing in the configuration file\n"); fprintf (stderr,
"'out' node is missing in the configuration file\n");
return false; return false;
} }
if (!process_node(in_node,true,device_cfg_t::bind)) if (!process_node (in_node, true, device_cfg_t::bind))
return false; return false;
if (!process_node(in_node,true,device_cfg_t::connect)) if (!process_node (in_node, true, device_cfg_t::connect))
return false; return false;
if (!process_node(out_node,false,device_cfg_t::bind)) if (!process_node (out_node, false, device_cfg_t::bind))
return false; return false;
if (!process_node(out_node,false,device_cfg_t::connect)) if (!process_node (out_node, false, device_cfg_t::connect))
return false; return false;
return true; return true;
} }
void set_context(zmq::context_t* context_) void set_context(zmq::context_t *context_)
{ {
context = context_; context = context_;
} }
zmq::context_t *get_context() const zmq::context_t *get_context () const
{ {
return context; return context;
} }
virtual bool make_sockets() = 0; virtual bool make_sockets () = 0;
bool set_up_connections() bool set_up_connections ()
{ {
for (vsock_dets_t::const_iterator i = in.begin() ; i != in.end(); for (vsock_dets_t::const_iterator i = in.begin (); i != in.end ();
++i) { ++i) {
switch (i->first) switch (i->first)
{ {
case device_cfg_t::connect : case device_cfg_t::connect:
in_socket->connect(i->second.c_str()); in_socket->connect (i->second.c_str ());
break; break;
case device_cfg_t::bind : case device_cfg_t::bind:
in_socket->bind(i->second.c_str()); in_socket->bind (i->second.c_str ());
} }
} }
for (vsock_dets_t::const_iterator i = out.begin() ; i != out.end(); for (vsock_dets_t::const_iterator i = out.begin (); i != out.end ();
++i) { ++i) {
switch (i->first) switch (i->first)
{ {
case device_cfg_t::connect : case device_cfg_t::connect:
out_socket->connect(i->second.c_str()); out_socket->connect (i->second.c_str ());
break; break;
case device_cfg_t::bind : case device_cfg_t::bind:
out_socket->bind(i->second.c_str()); out_socket->bind (i->second.c_str ());
} }
} }
...@@ -126,25 +124,24 @@ namespace { ...@@ -126,25 +124,24 @@ namespace {
} }
protected: protected:
bool make_sockets(int in_type, int out_type) bool make_sockets (int in_type, int out_type)
{ {
in_socket = new (std::nothrow) zmq::socket_t(*context, in_type); in_socket = new (std::nothrow) zmq::socket_t (*context, in_type);
if (!in_socket) if (!in_socket)
return false; return false;
out_socket = new (std::nothrow) zmq::socket_t(*context, out_type); out_socket = new (std::nothrow) zmq::socket_t (*context, out_type);
if (!out_socket) { if (!out_socket) {
return false; return false;
} }
return true; return true;
} }
int process_node(XMLNode& target_, bool in_, int process_node (XMLNode& target_, bool in_,
device_cfg_t::endpoint_direction ept_) device_cfg_t::endpoint_direction ept_)
{ {
const char *name =
const char * name =
(ept_ == device_cfg_t::connect) ? "connect" : "bind"; (ept_ == device_cfg_t::connect) ? "connect" : "bind";
int n = 0; int n = 0;
while (true) { while (true) {
...@@ -159,9 +156,9 @@ namespace { ...@@ -159,9 +156,9 @@ namespace {
} }
if (in_) if (in_)
in.push_back( sock_details_t(ept_, addr)); in.push_back (sock_details_t (ept_, addr));
else else
out.push_back( sock_details_t(ept_, addr)); out.push_back (sock_details_t (ept_, addr));
n++; n++;
} }
...@@ -170,63 +167,74 @@ namespace { ...@@ -170,63 +167,74 @@ namespace {
} }
protected: protected:
int device_type; int device_type;
zmq::context_t* context; zmq::context_t *context;
vsock_dets_t in; vsock_dets_t in;
vsock_dets_t out; vsock_dets_t out;
zmq::socket_t* in_socket; zmq::socket_t *in_socket;
zmq::socket_t* out_socket; zmq::socket_t *out_socket;
private:
device_cfg_t (device_cfg_t const&);
void operator = (device_cfg_t const&);
};
private: class queue_device_cfg_t : public device_cfg_t
void operator = (device_cfg_t const &); {
device_cfg_t(device_cfg_t const &); public:
};
queue_device_cfg_t () :
device_cfg_t (ZMQ_QUEUE)
{
}
virtual bool make_sockets ()
{
return device_cfg_t::make_sockets (ZMQ_XREP, ZMQ_XREQ);
}
};
class streamer_device_cfg_t : public device_cfg_t
{
public:
streamer_device_cfg_t () :
device_cfg_t (ZMQ_STREAMER)
{
}
class queue_device_cfg_t : public device_cfg_t virtual bool make_sockets ()
{ {
public: return device_cfg_t::make_sockets (ZMQ_UPSTREAM, ZMQ_DOWNSTREAM);
queue_device_cfg_t()
: device_cfg_t(ZMQ_QUEUE)
{}
virtual bool make_sockets(){
return device_cfg_t::make_sockets(ZMQ_XREP, ZMQ_XREQ);
} }
}; };
class forwarder_device_cfg_t : public device_cfg_t
{
public:
class streamer_device_cfg_t : public device_cfg_t forwarder_device_cfg_t() :
device_cfg_t (ZMQ_FORWARDER)
{ {
public:
streamer_device_cfg_t()
: device_cfg_t(ZMQ_STREAMER)
{}
virtual bool make_sockets () {
return device_cfg_t::make_sockets(ZMQ_UPSTREAM, ZMQ_DOWNSTREAM);
} }
};
class forwarder_device_cfg_t : public device_cfg_t virtual bool make_sockets()
{ {
public: if (!device_cfg_t::make_sockets (ZMQ_SUB, ZMQ_PUB) ) {
forwarder_device_cfg_t()
: device_cfg_t(ZMQ_FORWARDER)
{}
virtual bool make_sockets() {
if (!device_cfg_t::make_sockets(ZMQ_SUB, ZMQ_PUB) ) {
return false; return false;
} }
in_socket->setsockopt (ZMQ_SUBSCRIBE, "", 0); in_socket->setsockopt (ZMQ_SUBSCRIBE, "", 0);
return true; return true;
} }
}; };
device_cfg_t* make_device_config(XMLNode& device) device_cfg_t *make_device_config (XMLNode& device)
{ {
const char *dev_type = device.getAttribute ("type"); const char *dev_type = device.getAttribute ("type");
if (!dev_type) { if (!dev_type) {
...@@ -248,33 +256,32 @@ namespace { ...@@ -248,33 +256,32 @@ namespace {
"should be named 'forwarder', 'streamer' or 'queue'\n"); "should be named 'forwarder', 'streamer' or 'queue'\n");
return NULL; return NULL;
} }
extern "C" void* worker_function(void *arg)
{
extern "C" void* worker_function(void *arg)
{
if (!arg) { if (!arg) {
fprintf (stderr, "arg is null, returning \n"); fprintf (stderr, "arg is null, returning \n");
return 0; return 0;
} }
std::auto_ptr<device_cfg_t> cfg ( (device_cfg_t*) arg ); std::auto_ptr <device_cfg_t> cfg ((device_cfg_t*) arg);
zmq::context_t* ctx = cfg->get_context(); zmq::context_t *ctx = cfg->get_context ();
if (!ctx) { if (!ctx) {
fprintf (stderr, "no context, returning \n"); fprintf (stderr, "no context, returning \n");
return 0; return 0;
} }
if (! cfg->make_sockets()) { if (!cfg->make_sockets ()) {
fprintf (stderr, "failed to make sockets, returning \n"); fprintf (stderr, "failed to make sockets, returning \n");
return 0; return 0;
} }
if (! cfg->set_up_connections()) { if (!cfg->set_up_connections ()) {
fprintf (stderr, "failed to set up connections, returning \n"); fprintf (stderr, "failed to set up connections, returning \n");
return 0; return 0;
} }
...@@ -282,13 +289,8 @@ namespace { ...@@ -282,13 +289,8 @@ namespace {
cfg->run(); cfg->run();
return 0; return 0;
}
} }
int main (int argc, char *argv []) int main (int argc, char *argv [])
{ {
if (argc != 2) { if (argc != 2) {
...@@ -309,8 +311,7 @@ int main (int argc, char *argv []) ...@@ -309,8 +311,7 @@ int main (int argc, char *argv [])
return 1; return 1;
} }
std::vector <device_cfg_t*> vdev;
std::vector<device_cfg_t*> vdev;
while (true) { while (true) {
...@@ -319,49 +320,44 @@ int main (int argc, char *argv []) ...@@ -319,49 +320,44 @@ int main (int argc, char *argv [])
if (device.isEmpty()) if (device.isEmpty())
break; break;
device_cfg_t* dev = make_device_config(device); device_cfg_t* dev = make_device_config (device);
if (!dev) { if (!dev) {
fprintf(stderr, "failed to create device config\n"); fprintf(stderr, "failed to create device config\n");
return 1; return 1;
} }
if (! dev->init(device) ) { if (!dev->init (device)) {
fprintf(stderr,"error with initialising device configuration\n"); fprintf(stderr,"error with initialising device configuration\n");
delete dev; delete dev;
return 1; return 1;
} }
vdev.push_back(dev); vdev.push_back (dev);
} }
std::vector<device_cfg_t*>::size_type num_devices = vdev.size(); std::vector <device_cfg_t*>::size_type num_devices = vdev.size ();
if ( num_devices == 0 ) { if (num_devices == 0) {
fprintf(stderr,"no devices in the config file\n"); fprintf(stderr,"no devices in the config file\n");
return 1; return 1;
} }
zmq::context_t ctx (num_devices, 1);
zmq::context_t ctx (num_devices,1);
for (unsigned int i = 0 ; i < num_devices ; ++i) { for (unsigned int i = 0 ; i < num_devices ; ++i) {
vdev[i]->set_context(&ctx); vdev [i]->set_context (&ctx);
if (i) { if (i) {
pthread_t worker; pthread_t worker;
int rc = pthread_create (&worker, NULL, &worker_function, int rc = pthread_create (&worker, NULL, &worker_function,
(void*) vdev[i]); (void*) vdev [i]);
assert (rc == 0); assert (rc == 0);
} }
} }
worker_function ((void*) vdev [0]);
worker_function((void*)vdev[0]);
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment