diff --git a/client.cpp b/client.cpp index 783b2b5b..8c2ec055 100644 --- a/client.cpp +++ b/client.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -15,8 +16,13 @@ #include #include #include +#include + #include "chunk.h" #include "stream.h" +#include "zhelpers.hpp" + +using namespace std; int bufferMs; @@ -43,6 +49,44 @@ void player() +std::string getMacAddress() +{ + std::ifstream t("/sys/class/net/eth0/address"); + std::string str((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun(std::isspace))).base(), str.end()); + return str; +} + + + +void control(Stream* stream) +{ + zmq::context_t context(1); + zmq::socket_t worker (context, ZMQ_REQ); + srand (time(NULL)); + // We use a string identity for ease here + s_set_id (worker); + worker.connect("tcp://192.168.0.2:123459"); + + // Tell the router we're ready for work + s_send (worker, "ready"); + while (1) { + std::string cmd = s_recv (worker); + istringstream iss(cmd); + vector splitCmd; + copy(istream_iterator(iss), istream_iterator(), back_inserter >(splitCmd)); + for (size_t n=0; n 1) + { + if (splitCmd[0] == "buffer") + stream->setBufferLen(atoi(cmd.c_str())); + } + s_send(worker, "ACK " + cmd); + } +} + /* This routine will be called by the PortAudio engine when audio is needed. ** It may called at interrupt level on some machines so don't do anything @@ -154,7 +198,7 @@ int main (int argc, char *argv[]) initAudio(); std::thread playerThread(player); - std::string cmd; +/* std::string cmd; while (true && (argc > 3)) { std::cout << "> "; @@ -171,7 +215,9 @@ int main (int argc, char *argv[]) stream->setBufferLen(atoi(cmd.c_str())); } } - +*/ + std::thread controlThread(control, stream); +// controlThread.join(); playerThread.join(); return 0; diff --git a/rtrClient.cpp b/rtrClient.cpp index a329f647..e6c90d14 100644 --- a/rtrClient.cpp +++ b/rtrClient.cpp @@ -3,39 +3,62 @@ // // Olivier Chamoux + +#include +#include +#include +#include +#include #include "zhelpers.hpp" +#include +#include +#include -#define NBR_WORKERS 10 +using namespace std; -int main () { +std::string getMacAddress() +{ + std::ifstream t("/sys/class/net/eth0/address"); + std::string str((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun(std::isspace))).base(), str.end()); + return str; +} + + + +void control() +{ zmq::context_t context(1); zmq::socket_t worker (context, ZMQ_REQ); srand (time(NULL)); // We use a string identity for ease here - s_set_id (worker); + string macAddress = getMacAddress(); + worker.setsockopt(ZMQ_IDENTITY, macAddress.c_str(), macAddress.length()); worker.connect("tcp://127.0.0.1:10000"); - int total = 0; + // Tell the router we're ready for work + s_send (worker, "ready"); while (1) { - // Tell the router we're ready for work - s_send (worker, "ready"); - - // Get workload from router, until finished - std::string workload = s_recv (worker); -std::cout << "workload: " << workload << "\n"; - int finished = (workload.compare("END") == 0); - - if (finished) { - std::cout << "Processed: " << total << " tasks" << std::endl; - break; - } - total++; - - // Do some random work - s_sleep(within (100) + 1); + std::string cmd = s_recv (worker); + istringstream iss(cmd); + vector splitCmd; + copy(istream_iterator(iss), istream_iterator(), back_inserter >(splitCmd)); + for (size_t n=0; n +#include +#include #include "zhelpers.hpp" -#define NBR_WORKERS 10 + + +void receiver(zmq::socket_t* client) +{ + while (true) + { + std::string address = s_recv (*client); + std::cout << "Address: " << address << "\n"; + // receiving and discarding'empty' message + s_recv (*client); + // receiving and discarding 'ready' message + std::string msg = s_recv (*client); + std::cout << "msg: " << msg << "\n"; + } +} + int main () { zmq::context_t context(2); zmq::socket_t client (context, ZMQ_ROUTER); - client.bind("tcp://127.0.0.1:10000"); + client.bind("tcp://0.0.0.0:123459"); + + std::thread receiveThread(receiver, &client); - int task_nbr; while (true) { - // LRU worker is next waiting in queue - std::string address = s_recv (client); - std::cout << "Address: " << address << "\n"; - - // receiving and discarding'empty' message - s_recv (client); - // receiving and discarding 'ready' message - std::string msg = s_recv (client); - std::cout << "msg: " << msg << "\n"; + std::string address; + std::string cmd; + std::getline(std::cin, address); + std::getline(std::cin, cmd); s_sendmore (client, address); s_sendmore (client, ""); - s_send (client, std::string("hello: ") + address); + s_send (client, cmd); } return 0; }