Skip to content

Commit

Permalink
Merge pull request #125 from Fraunhofer-IIS/124-refactor-japi_registe…
Browse files Browse the repository at this point in the history
…r_request-and-remove-ctx-init

Refactor `japi_register_request`
  • Loading branch information
vornkat-iis authored Jul 5, 2024
2 parents 5bf2e4b + c64c99f commit 890c7ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion include/japi.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ typedef struct __japi_context {
struct __japi_client *clients; /*!< Pointer to the JAPI client context */
bool include_args_in_response; /*!< Flag to include request args in response */
bool shutdown; /*!< Flag to shutdown the JAPI server */
bool init; /*!< Flag to mark finished initialization */
} japi_context;

/*!
Expand Down
36 changes: 22 additions & 14 deletions src/japi.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ int japi_destroy(japi_context *ctx)
return 0;
}

int japi_register_request(japi_context *ctx, const char *req_name,
japi_req_handler req_handler)
static int japi_register_request_internal(japi_context *ctx, const char *req_name,
japi_req_handler req_handler,
bool allow_internal_req_name)
{
japi_request *req;
char *bad_req_name = "japi_";
Expand Down Expand Up @@ -265,9 +266,11 @@ int japi_register_request(japi_context *ctx, const char *req_name,
return -4;
}

if (ctx->init && strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) {
fprintf(stderr, "ERROR: Request name is not allowed.\n");
return -6;
if (!allow_internal_req_name) {
if (strncmp(req_name, bad_req_name, strlen(bad_req_name)) == 0) {
fprintf(stderr, "ERROR: Request name is not allowed.\n");
return -6;
}
}

req = (japi_request *)malloc(sizeof(japi_request));
Expand All @@ -285,6 +288,12 @@ int japi_register_request(japi_context *ctx, const char *req_name,
return 0;
}

int japi_register_request(japi_context *ctx, const char *req_name,
japi_req_handler req_handler)
{
return japi_register_request_internal(ctx, req_name, req_handler, false);
}

japi_context *japi_init(void *userptr)
{
japi_context *ctx;
Expand All @@ -295,7 +304,6 @@ japi_context *japi_init(void *userptr)
return NULL;
}

ctx->init = false;
ctx->userptr = userptr;
ctx->requests = NULL;
ctx->push_services = NULL;
Expand All @@ -315,16 +323,16 @@ japi_context *japi_init(void *userptr)
signal(SIGPIPE, SIG_IGN);

/* Register the default fallback handler */
japi_register_request(ctx, "japi_request_not_found_handler",
&japi_request_not_found_handler);
japi_register_request_internal(ctx, "japi_request_not_found_handler",
&japi_request_not_found_handler, true);
/* Register subscribe/unsubscribe service function */
japi_register_request(ctx, "japi_pushsrv_subscribe", &japi_pushsrv_subscribe);
japi_register_request(ctx, "japi_pushsrv_unsubscribe", &japi_pushsrv_unsubscribe);
japi_register_request_internal(ctx, "japi_pushsrv_subscribe",
&japi_pushsrv_subscribe, true);
japi_register_request_internal(ctx, "japi_pushsrv_unsubscribe",
&japi_pushsrv_unsubscribe, true);
/* Register list_push_service function */
japi_register_request(ctx, "japi_pushsrv_list", &japi_pushsrv_list);
japi_register_request(ctx, "japi_cmd_list", &japi_cmd_list);

ctx->init = true;
japi_register_request_internal(ctx, "japi_pushsrv_list", &japi_pushsrv_list, true);
japi_register_request_internal(ctx, "japi_cmd_list", &japi_cmd_list, true);

return ctx;
}
Expand Down

0 comments on commit 890c7ea

Please sign in to comment.