Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed data to protocol #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/main.c
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char** argv)
}

// Connect to the socket.
fprintf(stdout, "Connecting to server on %s:%u...", ipAddress, port);
fprintf(stdout, "Connecting to server on %s:%u...\n", ipAddress, port);
int sock = connectToRemoteServer(ipAddress, port);
fprintf(stdout, "connected\n");

Expand All @@ -66,11 +66,11 @@ int main(int argc, char** argv)
replacement_free(change);

// Let's read some outputs.
char* table = "Report";
char* table = "HarvestReport";
uint32_t nparams = 1;
char* params[nparams];
params[0] = "Yield";
// params[1] = "Sorghum.AboveGround.Wt";
//params[0] = "Yield";
params[0] = "Sorghum.AboveGround.Wt";
// params[2] = "Sorghum.Leaf.LAI";
double t = get_wall_time();

Expand All @@ -79,8 +79,8 @@ int main(int argc, char** argv)
printf("Read %d outputs in %.2fms\n", nparams, t * 1000);
for (uint32_t i = 0; i < nparams; i++) {
uint32_t n = outputs[i]->len / sizeof(double); // all of our outputs are double for now
printf("Received output %s with %u elements: [", params[i], n);
for (size_t j = 0; j < n; j++) {
printf("Received output %s of type %s with %u elements: [", params[i], outputs[i]->type, n);
for (size_t j = 0; j < n && strcmp(outputs[i]->type, "System.Double") == 0; j++) {
double val;
memcpy(&val, &outputs[i]->data[j * sizeof(double)], sizeof(double));
printf("%.2f%s", val, j < n - 1 ? ", " : "");
Expand Down
1 change: 1 addition & 0 deletions src/apsimclient.h
Expand Up @@ -6,6 +6,7 @@

typedef struct output
{
char* type;
char* data;
uint32_t len;
} output_t;
Expand Down
1 change: 1 addition & 0 deletions src/client.c
Expand Up @@ -178,6 +178,7 @@ output_t** readOutput(int sock, char* table, char** param_names, uint32_t nparam
// Now we should receive one result per parameter name.
for (uint32_t i = 0; i < nparams; i++) {
outputs[i] = malloc(sizeof(output_t));
outputs[i]->type = readString(sock, &msg_len);
outputs[i]->data = readFromSocket(sock, &outputs[i]->len);
sendString(sock, ACK);
}
Expand Down