Skip to content

Commit

Permalink
jrpc_cmd support parse float and string
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Jul 15, 2024
1 parent 1c925f4 commit 60500ff
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 45 deletions.
101 changes: 87 additions & 14 deletions package/jrpc/jrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void JRPC_server_handle_string(JRPC* self, char* json_str) {
jrpc_free(ack_str);
}

cJSON** param_array = (cJSON**)jrpc_malloc(param_count * sizeof(cJSON*));
cJSON** param_array =
(cJSON**)jrpc_malloc(param_count * sizeof(cJSON*) + 1);
if (param_array == NULL) {
jrpc_debug("Memory allocation failed for param_array\n");
JRPC_send_acknowledgement(self, id->valueint, ACK_MEMORY_ERROR,
Expand Down Expand Up @@ -726,17 +727,23 @@ int jrpc_test_server() {

char* jrpc_strtok(char* str, const char* delimiters, char** context) {
char* start = str ? str : *context;
if (!start)
if (!start) {
return NULL;
}

while (*start && strchr(delimiters, *start))
// Skip initial delimiters
while (*start && strchr(delimiters, *start)) {
++start;
if (!*start)
}
if (!*start) {
*context = NULL;
return NULL;
}

char* end = start;
while (*end && !strchr(delimiters, *end))
while (*end && !strchr(delimiters, *end)) {
++end;
}

if (*end) {
*end = '\0';
Expand All @@ -748,6 +755,36 @@ char* jrpc_strtok(char* str, const char* delimiters, char** context) {
return start;
}

static char* extract_quoted_string(const char** input) {
const char* start = *input;
if (*start != '"') {
return NULL;
}
start++;
const char* end = strchr(start, '"');
if (!end) {
return NULL;
}
size_t len = end - start;
char* result = (char*)malloc(len + 1);
if (!result) {
return NULL;
}
strncpy(result, start, len);
result[len] = '\0';
*input = end + 1;
return result;
}

static void skip_whitespace(const char** input) {
if (input == NULL || *input == NULL) {
return;
}
while (**input == ' ' || **input == '\t' || **input == '\n') {
(*input)++;
}
}

char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
char* cmd_copy = NULL;
char* method = NULL;
Expand All @@ -762,8 +799,10 @@ char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
goto __exit;
}

char* context = NULL;
char* token = jrpc_strtok(cmd_copy, " ", &context);
const char* cursor = cmd_copy;
skip_whitespace(&cursor);

char* token = jrpc_strtok(cmd_copy, " ", (char**)&cursor);
if (token == NULL) {
jrpc_debug("Invalid command\n");
goto __exit;
Expand All @@ -775,18 +814,52 @@ char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
goto __exit;
}

while ((token = jrpc_strtok(NULL, " ", &context)) != NULL) {
int param_value = atoi(token);
params_array[param_count] = cJSON_CreateNumber(param_value);
if (!params_array[param_count]) {
jrpc_debug("Failed to create JSON number\n");
skip_whitespace(&cursor);

while (cursor && *cursor != '\0') {
cJSON* param = NULL;
if (*cursor == '"') {
char* str_param = extract_quoted_string(&cursor);
if (!str_param) {
jrpc_debug("Failed to extract quoted string\n");
goto __exit;
}
param = cJSON_CreateString(str_param);
free(str_param);
} else {
const char* start = cursor;
while (*cursor != ' ' && *cursor != '\0') {
cursor++;
}
size_t len = cursor - start;
char* token_param = (char*)malloc(len + 1);
if (!token_param) {
jrpc_debug("Failed to allocate memory for token_param\n");
goto __exit;
}
strncpy(token_param, start, len);
token_param[len] = '\0';

param = cJSON_Parse(token_param);
if (!param) {
param = cJSON_CreateString(token_param);
}
free(token_param);
}
if (!param) {
jrpc_debug("Failed to create JSON parameter\n");
goto __exit;
}
params_array[param_count] = param;
param_count++;

skip_whitespace(&cursor);
}

result =
JRPC_send_request_blocking(jrpc, method, params_array, param_count);
// Ensure params_array and param_count are handled correctly when
// param_count is 0
result = JRPC_send_request_blocking(
jrpc, method, param_count > 0 ? params_array : NULL, param_count);
if (result == NULL) {
jrpc_debug("No result\n");
goto __exit;
Expand Down
2 changes: 1 addition & 1 deletion package/jrpc/jrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
#define PARAM_COUNT_NO_CHECK -1

// Timeout definitions
#define ACK_TIMEOUT 50
#define ACK_TIMEOUT 200
#define BLOCKING_TIMEOUT 1000
#define RETRY_COUNT 5

Expand Down
3 changes: 2 additions & 1 deletion port/linux/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
// "--gtest_filter=time*"
// "--gtest_filter=flashdb.tsdb1"
// "--gtest_filter=flashdb.base"
"--gtest_filter=jrpc.server"
// "--gtest_filter=jrpc.server"
// "--gtest_filter=jrpc.client"
// "--gtest_filter=jrpc.BlockingRequestBetweenTwoJRPC"
// "--gtest_filter=jrpc.cmd"
"--gtest_filter=jrpc.exec_get_val"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
101 changes: 87 additions & 14 deletions port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void JRPC_server_handle_string(JRPC* self, char* json_str) {
jrpc_free(ack_str);
}

cJSON** param_array = (cJSON**)jrpc_malloc(param_count * sizeof(cJSON*));
cJSON** param_array =
(cJSON**)jrpc_malloc(param_count * sizeof(cJSON*) + 1);
if (param_array == NULL) {
jrpc_debug("Memory allocation failed for param_array\n");
JRPC_send_acknowledgement(self, id->valueint, ACK_MEMORY_ERROR,
Expand Down Expand Up @@ -726,17 +727,23 @@ int jrpc_test_server() {

char* jrpc_strtok(char* str, const char* delimiters, char** context) {
char* start = str ? str : *context;
if (!start)
if (!start) {
return NULL;
}

while (*start && strchr(delimiters, *start))
// Skip initial delimiters
while (*start && strchr(delimiters, *start)) {
++start;
if (!*start)
}
if (!*start) {
*context = NULL;
return NULL;
}

char* end = start;
while (*end && !strchr(delimiters, *end))
while (*end && !strchr(delimiters, *end)) {
++end;
}

if (*end) {
*end = '\0';
Expand All @@ -748,6 +755,36 @@ char* jrpc_strtok(char* str, const char* delimiters, char** context) {
return start;
}

static char* extract_quoted_string(const char** input) {
const char* start = *input;
if (*start != '"') {
return NULL;
}
start++;
const char* end = strchr(start, '"');
if (!end) {
return NULL;
}
size_t len = end - start;
char* result = (char*)malloc(len + 1);
if (!result) {
return NULL;
}
strncpy(result, start, len);
result[len] = '\0';
*input = end + 1;
return result;
}

static void skip_whitespace(const char** input) {
if (input == NULL || *input == NULL) {
return;
}
while (**input == ' ' || **input == '\t' || **input == '\n') {
(*input)++;
}
}

char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
char* cmd_copy = NULL;
char* method = NULL;
Expand All @@ -762,8 +799,10 @@ char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
goto __exit;
}

char* context = NULL;
char* token = jrpc_strtok(cmd_copy, " ", &context);
const char* cursor = cmd_copy;
skip_whitespace(&cursor);

char* token = jrpc_strtok(cmd_copy, " ", (char**)&cursor);
if (token == NULL) {
jrpc_debug("Invalid command\n");
goto __exit;
Expand All @@ -775,18 +814,52 @@ char* JRPC_cmd(JRPC* jrpc, const char* cmd) {
goto __exit;
}

while ((token = jrpc_strtok(NULL, " ", &context)) != NULL) {
int param_value = atoi(token);
params_array[param_count] = cJSON_CreateNumber(param_value);
if (!params_array[param_count]) {
jrpc_debug("Failed to create JSON number\n");
skip_whitespace(&cursor);

while (cursor && *cursor != '\0') {
cJSON* param = NULL;
if (*cursor == '"') {
char* str_param = extract_quoted_string(&cursor);
if (!str_param) {
jrpc_debug("Failed to extract quoted string\n");
goto __exit;
}
param = cJSON_CreateString(str_param);
free(str_param);
} else {
const char* start = cursor;
while (*cursor != ' ' && *cursor != '\0') {
cursor++;
}
size_t len = cursor - start;
char* token_param = (char*)malloc(len + 1);
if (!token_param) {
jrpc_debug("Failed to allocate memory for token_param\n");
goto __exit;
}
strncpy(token_param, start, len);
token_param[len] = '\0';

param = cJSON_Parse(token_param);
if (!param) {
param = cJSON_CreateString(token_param);
}
free(token_param);
}
if (!param) {
jrpc_debug("Failed to create JSON parameter\n");
goto __exit;
}
params_array[param_count] = param;
param_count++;

skip_whitespace(&cursor);
}

result =
JRPC_send_request_blocking(jrpc, method, params_array, param_count);
// Ensure params_array and param_count are handled correctly when
// param_count is 0
result = JRPC_send_request_blocking(
jrpc, method, param_count > 0 ? params_array : NULL, param_count);
if (result == NULL) {
jrpc_debug("No result\n");
goto __exit;
Expand Down
2 changes: 1 addition & 1 deletion port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
#define PARAM_COUNT_NO_CHECK -1

// Timeout definitions
#define ACK_TIMEOUT 50
#define ACK_TIMEOUT 200
#define BLOCKING_TIMEOUT 1000
#define RETRY_COUNT 5

Expand Down
Loading

0 comments on commit 60500ff

Please sign in to comment.