git-svn-id: svn://elaine/murooma/trunk@127 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-07-31 20:16:07 +00:00
parent 32acdc83f2
commit d94f6c99da
2 changed files with 27 additions and 16 deletions

View file

@ -19,31 +19,42 @@ using namespace std;
void control() void alive(zmq::socket_t* worker)
{ {
zmq::context_t context(1);
zmq::socket_t worker (context, ZMQ_REQ);
srand (time(NULL));
// We use a string identity for ease here
string macAddress = getMacAddress();
worker.setsockopt(ZMQ_IDENTITY, macAddress.c_str(), macAddress.length());
worker.connect("tcp://127.0.0.1:123459");
// Tell the router we're ready for work // Tell the router we're ready for work
s_send (worker, "ready");
while (1) { while (1) {
std::string cmd = s_recv (worker); s_send (*worker, "ping");
sleep(1);
}
}
void control(zmq::socket_t* worker)
{
// Tell the router we're ready for work
// s_send (*worker, "ready");
while (1) {
std::string cmd = s_recv (*worker);
vector<std::string> splitCmd = split(cmd); vector<std::string> splitCmd = split(cmd);
for (size_t n=0; n<splitCmd.size(); ++n) for (size_t n=0; n<splitCmd.size(); ++n)
std::cout << "cmd: " << splitCmd[n] << "\n"; std::cout << "cmd: " << splitCmd[n] << "\n";
s_send(worker, "ACK " + cmd); s_send(*worker, "ACK " + cmd);
} }
} }
int main () { int main () {
cout << getMacAddress() << "\n"; cout << getMacAddress() << "\n";
std::thread controlThread(control); zmq::context_t context(1);
zmq::socket_t worker (context, ZMQ_DEALER);
srand (time(NULL));
// We use a string identity for ease here
string macAddress = getMacAddress();
worker.setsockopt(ZMQ_IDENTITY, macAddress.c_str(), macAddress.length());
worker.connect("tcp://127.0.0.1:123459");
std::thread controlThread(control, &worker);
std::thread aliveThread(alive, &worker);
controlThread.join(); controlThread.join();
return 0; return 0;
} }

View file

@ -16,7 +16,7 @@ void receiver(zmq::socket_t* client)
{ {
std::string address = s_recv (*client); std::string address = s_recv (*client);
// receiving and discarding'empty' message // receiving and discarding'empty' message
s_recv (*client); // s_recv (*client);
// receiving and discarding 'ready' message // receiving and discarding 'ready' message
std::string msg = s_recv (*client); std::string msg = s_recv (*client);
std::cout << "msg from " << address << ": " << msg << "\n"; std::cout << "msg from " << address << ": " << msg << "\n";
@ -27,7 +27,7 @@ void receiver(zmq::socket_t* client)
void send(const std::string& address, const std::string& cmd) void send(const std::string& address, const std::string& cmd)
{ {
s_sendmore (*client, address); s_sendmore (*client, address);
s_sendmore (*client, ""); // s_sendmore (*client, "");
s_send (*client, cmd); s_send (*client, cmd);
} }
@ -48,7 +48,7 @@ int main () {
std::getline(std::cin, address); std::getline(std::cin, address);
std::cout << "command: "; std::cout << "command: ";
std::getline(std::cin, cmd); std::getline(std::cin, cmd);
std::newl; std::cout << std::endl;
send(trim(address), trim(cmd)); send(trim(address), trim(cmd));
} }
return 0; return 0;