Skip to content
Snippets Groups Projects
Commit 95e8c50f authored by FKHals's avatar FKHals
Browse files

Handle correct message length in MPI

and also convert the binary message length to small endian according to
the convention of the TCP server.
parent e93d1a09
No related branches found
No related tags found
1 merge request!6Handle correct message length in MPI
......@@ -32,6 +32,7 @@
#include "ompi_config.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
......@@ -39,6 +40,7 @@
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <endian.h>
#include "opal/util/bit_ops.h"
#include "opal/util/info_subscriber.h"
......@@ -159,21 +161,23 @@ static int get_modified_ranks(uint32_t jobid, uint32_t vpid, size_t size, opal_v
"{\"msg_type\": 128, \"msg_data\": \"%d,%u,%u,%zu%s\"}",
pid, vpid, jobid, size, vrm_jobid_with_leading_comma);
// TODO: endianness
uint32_t msg_length = strlen(info_to_send) + 1;
if (send(socket_fd, &msg_length, sizeof(msg_length), 0) < 0) {
// Ensure that little endian is used for communinication (by convention with server)
uint32_t le_msg_length = htole32(msg_length);
if (send(socket_fd, &le_msg_length, sizeof(le_msg_length), 0) < 0) {
errorExit("send");
}
if (send(socket_fd, info_to_send, msg_length, 0) < 0) {
errorExit("send");
}
char rank_to_recv[BUFFLEN];
memset(rank_to_recv, 0, BUFFLEN);
uint32_t le_answer_length = 0;
// drop (later overwrite) the length since it is not needed
recv(socket_fd, rank_to_recv, BUFFLEN-1, 0);
recv(socket_fd, &le_answer_length, sizeof(le_answer_length), 0);
uint32_t answer_length = le32toh(le_answer_length);
char * rank_to_recv = calloc (answer_length, sizeof(char));
// receive the actual message content
recv(socket_fd, rank_to_recv, BUFFLEN-1, 0);
recv(socket_fd, rank_to_recv, answer_length, 0);
printf("Received from server: %s\n", rank_to_recv);
// look for msg_data field in JSON string
......@@ -191,6 +195,8 @@ static int get_modified_ranks(uint32_t jobid, uint32_t vpid, size_t size, opal_v
modified_ranks[i] = strtoul(next_digit, &first_non_digit, 10);
}
free(rank_to_recv);
// // Print modified_ranks array for debugging purposes
// printf("modified_ranks: [");
// for (size_t i = 0; i < size; i++) {
......
......@@ -17,6 +17,7 @@
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <endian.h>
#define MAX_CONNECTIONS 10
#define BUFFLEN 256
......@@ -73,7 +74,7 @@ int main(void) {
// read the length and discard it (by overwriting it next)
recv(session_fd, client_message, sizeof(uint32_t), 0);
printf("Length-Msg: %s\n", client_message);
printf("Length-Msg: %u\n", le32toh(*(uint32_t *)client_message));
// since recv() seems to be atomar, no concurrent behaviour must be
// considered
recv(session_fd, client_message, BUFFLEN, 0);
......@@ -106,10 +107,11 @@ int main(void) {
snprintf(rankString, BUFFLEN, "{\"msg_type\": 0, \"msg_data\": \"%s\"}", rankList);
// the length must be send first to emulate the behaviour of the actual TCP server but it
// gets discarded on client side anyway so it does not need to make sense
uint32_t unneeded_length = 0;
send(session_fd, &unneeded_length, sizeof(uint32_t), 0);
uint32_t needed_length = strlen(rankString) + 1;
uint32_t le_needed_length = le32toh(needed_length);
send(session_fd, &le_needed_length, sizeof(uint32_t), 0);
// send the actual message
if (send(session_fd, rankString, BUFFLEN, 0) < 0) {
if (send(session_fd, rankString, needed_length, 0) < 0) {
errorExit("send");
}
printf("Send Rank: %s\n\n", rankString);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment