Skip to content
Snippets Groups Projects
Commit 2cea4b46 authored by burneykb's avatar burneykb
Browse files

Testing and fully working getnodes command

parent 472e18c2
No related branches found
No related tags found
No related merge requests found
Cli
\ No newline at end of file
Cli
\ No newline at end of file
Cli
\ No newline at end of file
Cli
\ No newline at end of file
Cli
\ No newline at end of file
Cli
\ No newline at end of file
Cli
\ No newline at end of file
......@@ -738,7 +738,6 @@ static void client_recv(int fd) {
/* Only add the client to the pending responses if it was a getparam command */
if (m.msg_type == GETPARAM_ID || m.msg_type == GETOUTPUT_ID ||
m.msg_type == GETSOURCE_ID || m.msg_type == GETNODES_ID || m.msg_type == ADDNODE_ID) {
printf("adding fd %d with id %d\n", fd, BytesTo16(packet[ID_L], packet[ID_H]));
if (clientAddPendResponses(fd, BytesTo16(packet[ID_L], packet[ID_H])) == -1) {
warnx("Ran out of room! Consider increasing CLIENT_MAX_PENDING_RESPONSES!\n");
}
......@@ -889,7 +888,7 @@ static void handleResponse(struct metadata *m, uint8_t * data)
return;
}
printf("msg to client = '%s'\n", buffer);
// printf("msg to client = '%s'\n", buffer);
for(int fd = 0; fd <= max_fd; ++fd) {
if (get_client_index(fd) > -1) {
......@@ -898,7 +897,6 @@ static void handleResponse(struct metadata *m, uint8_t * data)
}
}
printf("leaving handleResponse\n");
free(buffer);
}
......
......@@ -69,6 +69,18 @@ enum ResponseGetnodesData {
RESP_GN_MIN_DATA_SIZE
};
static int resizeMsg(char **msg_ptr, int *curr_max, int check_val) {
/* resize the msg if necessary */
if (*curr_max < check_val) {
*curr_max = *curr_max * 4;
*msg_ptr = realloc(*msg_ptr, sizeof(**msg_ptr) * *curr_max);
if (!msg_ptr) {
return -1;
}
}
return 0;
}
/* Decode a metadata and data to populate a.
* Returns bytes written to msg, -1 on failure.
*/
......@@ -87,42 +99,38 @@ int DecodeResponseGetNodes(
}
uint16_t num_nodes = BytesTo16(data[RESP_GN_NUM_NODES_L], data[RESP_GN_NUM_NODES_H]);
/* resize the msg if necessary */
if (max_len < (m->data_len * 2)) {
max_len = max_len * 2;
msg = realloc(msg, sizeof(*msg) * max_len);
if (!msg) {
return -1;
}
if (resizeMsg(&msg, (int *)&max_len, (m->data_len *2)) == -1) {
return -1;
}
int16_t val;
char name[512]; /* Corresponding to the maximum name len that the frontend will accept */
char name[512] = "";/* Corresponding to the maximum name len that the frontend will accept */
size_t i;
int msg_offset = 0;
int data_offset = RESP_GN_MIN_DATA_SIZE;
int msg_len = 0, msg_offset = 0, data_offset = RESP_GN_MIN_DATA_SIZE;
sprintf(msg, "getnodes %hu %n", num_nodes, &msg_offset);
msg += msg_offset;
msg_len += msg_offset;
for(i = 0; i < num_nodes * 3; ++i) {
if (i < num_nodes * 2) {
val = BytesTo16(data[data_offset], data[data_offset+1]);
data_offset += 2;
sprintf(msg, "%" PRId16 " %n", val, &msg_offset);
sprintf(&msg[msg_len], "%" PRId16 " %n", val, &msg_offset);
} else {
strncpy(name, (char *) &data[data_offset], 512);
strncpy(name, (char *) (data + data_offset), 512);
data_offset += strlen(name) + 1;
sprintf(msg, "%s %n", name, &msg_offset);
sprintf(&msg[msg_len], "'%s' %n", name, &msg_offset);
}
msg_len += msg_offset;
msg += msg_offset;
}
if (resizeMsg(&msg, (int *)&max_len, msg_len + (msg_offset *2)) == -1) {
return -1;
}
}
strcat(&msg[msg_len], "\n");
return strlen(msg);
}
......@@ -148,6 +156,6 @@ int DecodeResponseAddNode(
return -1;
}
return sprintf(msg, "addnode %d",
return sprintf(msg, "addnode %d\n",
BytesTo16(data[RESP_AN_BLOCK_ID_L], data[RESP_AN_BLOCK_ID_H]));
}
\ No newline at end of file
......@@ -70,7 +70,7 @@ int DecodeResponseOutput(
return -1;
}
return snprintf(msg, max_len, "getoutput %" PRId16 " %" PRId16 " %f",
return snprintf(msg, max_len, "getoutput %" PRId16 " %" PRId16 " %f\n",
BytesTo16(data[RESP_BLOCK_ID_L], data[RESP_BLOCK_ID_H]),
BytesTo16(data[RESP_OUTPUT_ID_L], data[RESP_OUTPUT_ID_H]),
BytesToFloat(data[RESP_VAL_1], data[RESP_VAL_2],
......
......@@ -116,7 +116,7 @@ int DecodeResponseSource(
return -1;
}
return snprintf(msg, max_len, "getsource %" PRId16 " %" PRId16 " %" PRId16" %" PRId16,
return snprintf(msg, max_len, "getsource %" PRId16 " %" PRId16 " %" PRId16" %" PRId16 "\n",
BytesTo16(data[RESP_DST_BLOCK_ID_L], data[RESP_DST_BLOCK_ID_H]),
BytesTo16(data[RESP_DST_INPUT_ID_L], data[RESP_DST_INPUT_ID_H]),
BytesTo16(data[RESP_SRC_BLOCK_ID_L], data[RESP_SRC_BLOCK_ID_H]),
......
......@@ -25,14 +25,21 @@ int cli_getnodes(struct backend_conn * conn, int argc, char ** argv) {
return 1;
}
node_data = malloc(sizeof(*node_data) * num_nodes);
node_data = malloc(sizeof(*node_data));
if (frontend_getnodes(conn, node_data, &num_nodes)) {
if (frontend_getnodes(conn, &node_data, &num_nodes)) {
free(node_data);
return 1;
}
printf("unimplemented\n");
printf("------------------------------------------------\n");
printf("The following %lu Nodes have been found:\n", num_nodes);
printf("------------------------------------------------\n");
printf("\tBLOCK\t\tTYPE\t\tNAME\n");
printf("------------------------------------------------\n");
for(size_t i = 0; i < num_nodes; ++i) {
printf("\t%3d\t\t%3d\t\t%s\n", node_data[i].block, node_data[i].type, node_data[i].name);
}
free(node_data);
return 0;
}
......
......@@ -13,7 +13,7 @@
*/
int frontend_getnodes(
struct backend_conn * conn,
struct frontend_node_data * node_data,
struct frontend_node_data ** node_data,
size_t * num_nodes) {
char msg[64] = "";
......@@ -48,16 +48,28 @@ int frontend_getnodes(
/* Resize if necessary */
if (num_nodes_ret != *num_nodes) {
*num_nodes = num_nodes_ret;
node_data = realloc(node_data, sizeof(*node_data) * (*num_nodes));
if (!node_data) {
*node_data = realloc(*node_data, sizeof(**node_data) * (*num_nodes));
if (!*node_data) {
return 1;
}
}
struct frontend_node_data * arr = *node_data;
for (size_t i = 0; i < *num_nodes; ++i) {
sscanf(response, "%" SCNd16 " %" SCNd16 " %s %n",
&node_data[i].block, &node_data[i].type, (char *) &node_data[i].name,
(int *)&offset);
sscanf(response, "%" SCNd16 " %n", &arr[i].block, (int*)&offset);
// printf("found block %d\n", arr[i].block);
response += offset;
}
for (size_t i = 0; i < *num_nodes; ++i) {
sscanf(response, "%" SCNd16 " %n", &arr[i].type, (int*)&offset);
// printf("found type %d\n", arr[i].type);
response += offset;
}
for (size_t i = 0; i < *num_nodes; ++i) {
sscanf(response, " '%[^\t\n']' %n", (char *)&arr[i].name, (int *)&offset);
// printf("found name '%s'\n", arr[i].name);
response += offset;
}
......
......@@ -10,7 +10,7 @@
*/
int frontend_getnodes(
struct backend_conn * conn,
struct frontend_node_data * node_data,
struct frontend_node_data ** node_data,
size_t * num_nodes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment