From b03f6d50ecd138acde1d7938fd1782f84910a9f2 Mon Sep 17 00:00:00 2001 From: Mudit Date: Tue, 29 Sep 2020 17:03:06 +0100 Subject: [PATCH] Exit if connecting to the receptionist fails --- workers/c_client_direct/client.c | 17 ++++++++++++++--- workers/c_client_vtable/client.c | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/workers/c_client_direct/client.c b/workers/c_client_direct/client.c index 5d77072..57d573d 100644 --- a/workers/c_client_direct/client.c +++ b/workers/c_client_direct/client.c @@ -56,11 +56,11 @@ void OnEntityQueryResponse(const Worker_EntityQueryResponseOp* op) { } void OnAddEntity(const Worker_AddEntityOp* op) { - printf("received add entity op (entity: %" PRId64 ")\n", op->entity_id); + printf("received add entity op (entity: %" PRId64 ")\n", op->entity_id); } void OnRemoveEntity(const Worker_RemoveEntityOp* op) { - printf("received remove entity op (entity: %" PRId64 ")\n", op->entity_id); + printf("received remove entity op (entity: %" PRId64 ")\n", op->entity_id); } void OnAddComponent(const Worker_AddComponentOp* op) { @@ -125,7 +125,9 @@ int main(int argc, char** argv) { printf("Connects to SpatialOS\n"); printf(" - hostname of the receptionist to connect to.\n"); printf(" - port to use\n"); - printf(" - name of the worker assigned by SpatialOS. A random prefix will be added to it to ensure uniqueness.\n"); + printf( + " - name of the worker assigned by SpatialOS. A random prefix will be " + "added to it to ensure uniqueness.\n"); return EXIT_FAILURE; } @@ -148,6 +150,14 @@ int main(int argc, char** argv) { Worker_ConnectionFuture_Destroy(connection_future); free(worker_id); + if (Worker_Connection_GetConnectionStatusCode(connection) != + WORKER_CONNECTION_STATUS_CODE_SUCCESS) { + printf("failed to connect to receptionist. reason: %s\n", + Worker_Connection_GetConnectionStatusDetailString(connection)); + Worker_Connection_Destroy(connection); + return EXIT_FAILURE; + } + /* Send a test message. */ Worker_LogMessage message = {WORKER_LOG_LEVEL_WARN, "Client", "Connected successfully", NULL}; Worker_Connection_SendLogMessage(connection, &message); @@ -210,4 +220,5 @@ int main(int argc, char** argv) { } Worker_Connection_Destroy(connection); + return EXIT_SUCCESS; } diff --git a/workers/c_client_vtable/client.c b/workers/c_client_vtable/client.c index 4e9a519..7612f9f 100644 --- a/workers/c_client_vtable/client.c +++ b/workers/c_client_vtable/client.c @@ -15,7 +15,7 @@ char* GenerateWorkerId(char* worker_id_prefix) { /* Format string. */ char* worker_id = malloc(sizeof(char) * (size + 1)); - sprintf(worker_id,fmt, worker_id_prefix, id); + sprintf(worker_id, fmt, worker_id_prefix, id); return worker_id; } @@ -170,6 +170,14 @@ int main(int argc, char** argv) { Worker_ConnectionFuture_Destroy(connection_future); free(worker_id); + if (Worker_Connection_GetConnectionStatusCode(connection) != + WORKER_CONNECTION_STATUS_CODE_SUCCESS) { + printf("failed to connect to receptionist. reason: %s\n", + Worker_Connection_GetConnectionStatusDetailString(connection)); + Worker_Connection_Destroy(connection); + return EXIT_FAILURE; + } + /* Send a test message. */ Worker_LogMessage message = {WORKER_LOG_LEVEL_WARN, "Client", "Connected successfully", NULL}; Worker_Connection_SendLogMessage(connection, &message); @@ -228,4 +236,5 @@ int main(int argc, char** argv) { } Worker_Connection_Destroy(connection); + return EXIT_SUCCESS; }