diff --git a/distrib/distrib.cc b/distrib/distrib.cc index 9040fab..fb21184 100644 --- a/distrib/distrib.cc +++ b/distrib/distrib.cc @@ -115,6 +115,7 @@ void connection_thread(distrib::connection_thread_arg_t * arg) int client_socket = arg->client_socket; delete arg; + bool done = false; /* loop listening for messages from the client */ for (;;) { @@ -122,7 +123,30 @@ void connection_thread(distrib::connection_thread_arg_t * arg) int nread = read(client_socket, &msg_type, sizeof(msg_type)); if (nread < 0) break; - cout << "Read message type " << msg_type << "!" << endl; + switch (msg_type) + { + case MSG_WANT_DATA: + { + int task = the_distrib->getTask(); + if (write(client_socket, &task, sizeof(task)) < 0) + done = true; + } + break; + case MSG_SEND_DATA: + { + unsigned char data[3 * UNIT_TASK_SIZE]; + int task; + if (read(client_socket, &task, sizeof(task)) < 0) + done = true; + else if (read(client_socket, &data[0], sizeof(data)) < 0) + done = true; + else + the_distrib->send_data(task, &data[0], sizeof(data)); + } + break; + default: + break; + } } close(client_socket); } @@ -269,6 +293,7 @@ int distrib::send_data(int task, unsigned char * data, int num_bytes) { int msg_header = MSG_SEND_DATA; /* send data */ if ( write(m_client_socket, &msg_header, sizeof(msg_header)) < 0 + || write(m_client_socket, &task, sizeof(task)) < 0 || write(m_client_socket, data, num_bytes) < 0) { return -1;