From 064a168bb94ab076f581047583ab8c342aa308ec Mon Sep 17 00:00:00 2001
From: Paul Nierenz <2673-paun51@users.noreply.git.imp.fu-berlin.de>
Date: Tue, 27 Sep 2022 15:11:04 +0200
Subject: [PATCH] Use JSON format for communication MPI -> node-agent

including pre-sending the length of the json string which is required
for the TCP server to be able to process the message.
---
 ompi/communicator/comm_init.c | 41 ++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/ompi/communicator/comm_init.c b/ompi/communicator/comm_init.c
index 70e9f74895..d3d4e3b845 100644
--- a/ompi/communicator/comm_init.c
+++ b/ompi/communicator/comm_init.c
@@ -37,6 +37,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <string.h>
 
 #include "opal/util/bit_ops.h"
 #include "opal/util/info_subscriber.h"
@@ -54,7 +55,7 @@
 #include "ompi/memchecker.h"
 
 #define FD_STDIN 0
-#define BUFFLEN 64
+#define BUFFLEN 128
 
 /*
 ** Table for Fortran <-> C communicator handle conversion
@@ -138,21 +139,30 @@ static int getProcessAgentRank(uint32_t jobid, uint32_t vpid, size_t size) {
     pid_t pid = getpid();
 
     char info_to_send[BUFFLEN];
+    memset(info_to_send, 0, BUFFLEN);
     snprintf(info_to_send, BUFFLEN,
-             "%d,%u,%u,%zu",
-             pid, vpid, jobid, size);
-    if (send(socket_fd, info_to_send, BUFFLEN, 0) < 0) {
+            "{\"msg_type\": 128, \"msg_data\": \"%d,%u,%u,%zu\"}",
+            pid, vpid, jobid, size);
+
+    // TODO: endianness
+    uint32_t msg_length = strlen(info_to_send) + 1;
+    if (send(socket_fd, &msg_length, sizeof(msg_length), 0) < 0) {
+        errorExit("send");
+    }
+    if (send(socket_fd, info_to_send, msg_length, 0) < 0) {
         errorExit("send");
     }
 
     char rank_to_recv[BUFFLEN];
     recv(socket_fd, rank_to_recv, BUFFLEN, 0);
-    int received_rank = (int)strtol(rank_to_recv, NULL, 0);
-    printf("Received from server: %d\n", received_rank);
+    rank_to_recv[BUFFLEN-1] = '\0';
+    printf("Received from server: %s\n", rank_to_recv);
+    // int received_rank = (int)strtol(rank_to_recv, NULL, 0);
+    // printf("Received from server: %d\n", received_rank);
 
     close(socket_fd);
 
-    return received_rank;
+    return vpid; //received_rank;
 }
 
 /*
@@ -187,17 +197,18 @@ int ompi_comm_init(void)
     group->grp_proc_count = size;
 
     // transfer the infos about the current processes to the agent
-    for (size_t i = 0 ; i < size ; ++i) {
-        opal_process_name_t name = {.vpid = i, .jobid = OMPI_PROC_MY_NAME->jobid};
-        // call without return just to gain knowledge about the processes
-        getProcessAgentRank(name.jobid, name.vpid, size);
-    }
+    // for (size_t i = 0 ; i < size ; ++i) {
+    //     opal_process_name_t name = {.vpid = i, .jobid = OMPI_PROC_MY_NAME->jobid};
+    //     // call without return just to gain knowledge about the processes
+    //     getProcessAgentRank(name.jobid, name.vpid, size);
+    // }
+    getProcessAgentRank(OMPI_PROC_MY_NAME->jobid, OMPI_PROC_MY_NAME->vpid, size);
 
     for (size_t i = 0 ; i < size ; ++i) {
         opal_process_name_t name = {.vpid = i, .jobid = OMPI_PROC_MY_NAME->jobid};
-        /* get desired rank from agent (must be within size!)*/
-        name.vpid = getProcessAgentRank(name.jobid, name.vpid, size);
-        printf("INIT Loop Rank: %u\n", name.vpid);
+        // /* get desired rank from agent (must be within size!)*/
+        // name.vpid = getProcessAgentRank(name.jobid, name.vpid, size);
+        // printf("INIT Loop Rank: %u\n", name.vpid);
         /* look for existing ompi_proc_t that matches this name */
         group->grp_proc_pointers[i] = (ompi_proc_t *) ompi_proc_lookup (name);
         if (NULL == group->grp_proc_pointers[i]) {
-- 
GitLab