2020-09-09 17:56:23 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "../include/list.h"
|
|
|
|
|
#include "watch_util.h"
|
|
|
|
|
|
|
|
|
|
#define JSON_ARRAY_DELIM "\r\n"
|
|
|
|
|
|
|
|
|
|
static int wu_convert_to_json_array(list_t * json_array, const char *json_string)
|
|
|
|
|
{
|
|
|
|
|
if (!json_string || '\0' == json_string[0] || !json_array) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
|
char *json_string_dup = strdup(json_string);
|
|
|
|
|
|
|
|
|
|
char *token = NULL;
|
|
|
|
|
token = strtok(json_string_dup, JSON_ARRAY_DELIM);
|
|
|
|
|
while (token) {
|
|
|
|
|
cJSON *cjson = cJSON_Parse(token);
|
|
|
|
|
if (cjson == NULL) {
|
|
|
|
|
rc = -1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2020-11-02 21:38:52 +08:00
|
|
|
cJSON_Delete(cjson);
|
2020-09-09 17:56:23 +08:00
|
|
|
list_addElement(json_array, strdup(token));
|
|
|
|
|
token = strtok(NULL, JSON_ARRAY_DELIM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (json_string_dup) {
|
|
|
|
|
free(json_string_dup);
|
|
|
|
|
json_string_dup = NULL;
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void kubernets_watch_handler(void **pData, long *pDataLen, KUBE_WATCH_EVENT_HANDLER_FUNC event_hander)
|
|
|
|
|
{
|
|
|
|
|
char *data = *(char **) pData;
|
|
|
|
|
|
2022-03-09 14:09:10 +08:00
|
|
|
list_t *watch_event_list = list_createList();
|
2020-09-09 17:56:23 +08:00
|
|
|
if (!watch_event_list) {
|
|
|
|
|
fprintf(stderr, "Cannot create a list for watch events.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int rc = wu_convert_to_json_array(watch_event_list, data);
|
|
|
|
|
if (0 == rc) {
|
|
|
|
|
|
|
|
|
|
listEntry_t *listEntry = NULL;
|
|
|
|
|
list_ForEach(listEntry, watch_event_list) {
|
|
|
|
|
char *list_item = listEntry->data;
|
|
|
|
|
event_hander(list_item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
|
*pData = NULL;
|
|
|
|
|
*pDataLen = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear_and_free_string_list(watch_event_list);
|
|
|
|
|
watch_event_list = NULL;
|
|
|
|
|
}
|