diff --git a/client.cpp b/client.cpp index 7f92b3c3..0061eb0a 100644 --- a/client.cpp +++ b/client.cpp @@ -9,6 +9,15 @@ #include #include +const size_t size(1024); + +struct Chunk +{ + time_t timestamp; + char payload[size]; +}; + + int main (int argc, char *argv[]) { zmq::context_t context (1); @@ -24,9 +33,6 @@ int main (int argc, char *argv[]) // Process 100 updates int update_nbr; long total_temp = 0; - const size_t size(1024); - char msg[size]; -// for (update_nbr = 0; update_nbr < 100; update_nbr++) { while (1) { zmq::message_t update; @@ -34,10 +40,13 @@ int main (int argc, char *argv[]) // std::cerr << "received\n"; // std::istringstream iss(static_cast(update.data())); // iss >> zipcode >> relhumidity; - memcpy(&msg[0], update.data(), size); + Chunk* chunk = new Chunk(); + memcpy(chunk, update.data(), sizeof(Chunk)); + // std::cout << "update\n"; for (size_t n=0; npayload[n] << std::flush; + delete chunk; // std::cout << std::flush; // std::cerr << "flushed\n"; } diff --git a/server.cpp b/server.cpp index ba5bfa2d..88ec00b7 100644 --- a/server.cpp +++ b/server.cpp @@ -15,32 +15,36 @@ using namespace std; +const int size(1024); + +struct Chunk +{ + time_t timestamp; + char payload[size]; +}; + int main () { // Prepare our context and publisher zmq::context_t context (1); zmq::socket_t publisher (context, ZMQ_PUB); publisher.bind("tcp://0.0.0.0:123458"); -// publisher.bind("ipc://weather.ipc"); // Initialize random number generator - srandom ((unsigned) time (NULL)); size_t idx(0); - const int size(1024); - char msg[size]; char c;//[2]; -// msg[0] = '0'; - -// int fd; -// fd = open("stdin", O_RDONLY|O_BINARY, 0); + Chunk chunk; while (!cin.get(c).eof()) { + if (idx == 0) + time(&chunk.timestamp); + // read(fd, &msg[0], size); - msg[idx++] = c; + chunk.payload[idx++] = c; if (idx == size) { zmq::message_t message(size); - memcpy(message.data(), &msg[0], size); + memcpy(message.data(), &chunk, sizeof(Chunk)); // snprintf ((char *) message.data(), size, "%05d %d", zipcode, c); // message.data()[0] = c; publisher.send(message);