diff options
| author | chenzizhan <[email protected]> | 2024-02-27 16:23:00 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-02-27 16:23:00 +0800 |
| commit | dfca1d62c8e834ddbdc51a633b65a123cd468016 (patch) | |
| tree | 585c9f450fa13b7b8aa86033c9f39c86cdb5c417 | |
| parent | a4250aeb2453e15dd686b2ccd839d0dfecfbcb1a (diff) | |
adapt burst scope api changes. c2s and s2c handleczz-burstscope
3 files changed, 65 insertions, 62 deletions
diff --git a/src/burst_scope_plugin/burst_scope_plugin.c b/src/burst_scope_plugin/burst_scope_plugin.c index a854a3e..c5bb8fe 100644 --- a/src/burst_scope_plugin/burst_scope_plugin.c +++ b/src/burst_scope_plugin/burst_scope_plugin.c @@ -7,6 +7,7 @@ #include <assert.h> #include <sys/stat.h> #include <sys/types.h> +#include <cJSON.h> #include "stellar/session.h" #include "stellar/session_exdata.h" @@ -23,10 +24,10 @@ struct burst_scope_plugin_context }; struct burst_scope_plugin_exdata { - struct burst_scope *handle; + struct burst_scope *c2s; + struct burst_scope *s2c; }; - int feed_packet_to_bs(const struct packet *pkt, struct burst_scope_plugin_exdata *ex_data) { int dir = packet_get_direction(pkt); @@ -49,51 +50,11 @@ int feed_packet_to_bs(const struct packet *pkt, struct burst_scope_plugin_exdata return -1; } - long long signed_len; - if (dir == PACKET_DIRECTION_C2S) { - signed_len = raw_pkt_len; - } else { - signed_len = -raw_pkt_len; - } - - burst_scope_feed(ex_data->handle, ts, 1, signed_len); + struct burst_scope *handle = (dir == PACKET_DIRECTION_C2S) ? ex_data->c2s : ex_data->s2c; + burst_scope_record(handle, ts, raw_pkt_len); return 0; } -bool check_bsp_ret_burst_number(const char *json) -{ - char *ptr = (char *)json; - while (*ptr != '[') { - ptr++; - } - - ptr++; // Skip the opening bracket - - // Read number1 - int number1 = 0; - while (*ptr >= '0' && *ptr <= '9') { - number1 = number1 * 10 + (*ptr - '0'); - ptr++; - } - - // Skip the comma - while (*ptr < '0' || *ptr > '9') { - ptr++; - } - - // Read number2 - int number2 = 0; - while (*ptr >= '0' && *ptr <= '9') { - number2 = number2 * 10 + (*ptr - '0'); - ptr++; - } - - if (number1 <= 2 && number2 <= 2) { - return false; - } - return true; -} - void get_unique_file_path(char *path, size_t path_len) { uuid_t uuid; @@ -103,6 +64,44 @@ void get_unique_file_path(char *path, size_t path_len) snprintf(path, path_len, "%s/%s.json", BURST_JSON_SAVED_DIR, uuid_str); } +cJSON *bursts_to_json(struct burst *bursts, int n_bursts) +{ + cJSON *root = cJSON_CreateArray(); + for (int i = 0; i < n_bursts; i++) { + struct burst *burst = &bursts[i]; + cJSON *item = cJSON_CreateArray(); + cJSON_AddItemToArray(item, cJSON_CreateNumber(burst->start)); + cJSON_AddItemToArray(item, cJSON_CreateNumber(burst->end)); + cJSON_AddItemToArray(item, cJSON_CreateNumber(burst->size)); + cJSON_AddItemToArray(item, cJSON_CreateNumber(burst->state_level)); + cJSON_AddItemToArray(root, item); + } + + return root; +} + +char *get_two_dir_json(struct burst *c2s, size_t n_c2s, struct burst *s2c,size_t n_s2c) +{ + cJSON *root = cJSON_CreateObject(); + cJSON *c2s_json = bursts_to_json(c2s, n_c2s); + cJSON *s2c_json = bursts_to_json(s2c, n_s2c); + cJSON_AddItemToObject(root, "c2s", c2s_json); + cJSON_AddItemToObject(root, "s2c", s2c_json); + char *out = cJSON_Print(root); + cJSON_Delete(root); + return out; +} + +void exdata_free(struct burst_scope_plugin_exdata *ex_data) +{ + if (ex_data == NULL) { + return; + } + burst_scope_free(ex_data->c2s); + burst_scope_free(ex_data->s2c); + free(ex_data); +} + int burst_scope_plugin_entry(struct session *sess, int events, const struct packet *pkt, void *cb_arg) { struct burst_scope_plugin_context *ctx = (struct burst_scope_plugin_context *)cb_arg; @@ -117,14 +116,17 @@ int burst_scope_plugin_entry(struct session *sess, int events, const struct pack return 0; } - char *json; - size_t json_len; - burst_scope_get_fp(ex_data->handle, &json, &json_len); - - if (check_bsp_ret_burst_number(json) == false) { + struct burst *bursts_c2s; + size_t n_bursts_c2s; + burst_scope_query(ex_data->c2s, &bursts_c2s, &n_bursts_c2s); + struct burst *bursts_s2c; + size_t n_bursts_s2c; + burst_scope_query(ex_data->s2c, &bursts_s2c, &n_bursts_s2c); + if (n_bursts_c2s <=2 && n_bursts_s2c <=2) { msg->ret_code = BSP_RET_TOO_FEW_BURSTS; session_mq_publish_message(sess, ctx->topic_id, msg); - free(json); + free(bursts_c2s); + free(bursts_s2c); return 0; } @@ -133,21 +135,25 @@ int burst_scope_plugin_entry(struct session *sess, int events, const struct pack if (fp == NULL) { printf("open file failed\n"); msg->ret_code = BSP_FILE_SAVE_ERR; - free(json); + free(bursts_c2s); + free(bursts_s2c); session_mq_publish_message(sess, ctx->topic_id, msg); return -1; } + + char *json = get_two_dir_json(bursts_c2s, n_bursts_c2s, bursts_s2c, n_bursts_s2c); fprintf(fp, "%s", json); fflush(fp); fclose(fp); - free(json); + + free(bursts_c2s); + free(bursts_s2c); msg->ret_code = BSP_RET_OK; session_mq_publish_message(sess, ctx->topic_id, msg); // free ex_data - burst_scope_free(ex_data->handle); - free(ex_data); + exdata_free(ex_data); session_set_ex_data(sess, ctx->ex_data_idx, NULL); return 0; } @@ -155,7 +161,8 @@ int burst_scope_plugin_entry(struct session *sess, int events, const struct pack if ((events & SESS_EV_OPENING)) { assert(ex_data == NULL); ex_data = (struct burst_scope_plugin_exdata *)calloc(1, sizeof(struct burst_scope_plugin_exdata)); - ex_data->handle = burst_scope_new(); + ex_data->s2c = burst_scope_new(); + ex_data->c2s = burst_scope_new(); session_set_ex_data(sess, ctx->ex_data_idx, ex_data); } @@ -185,8 +192,7 @@ static void burst_scope_plugin_ex_data_free(struct session *s, int idx, void *ex } struct burst_scope_plugin_exdata *data = (struct burst_scope_plugin_exdata *)ex_data; - burst_scope_free(data->handle); - free(data); + exdata_free(data); } void *burst_scope_plugin_init(struct stellar *st) diff --git a/test/burst_scope_plugin/test_burst_scope_plugin.c b/test/burst_scope_plugin/test_burst_scope_plugin.c index 251b5e6..6206b32 100644 --- a/test/burst_scope_plugin/test_burst_scope_plugin.c +++ b/test/burst_scope_plugin/test_burst_scope_plugin.c @@ -33,8 +33,6 @@ static int msg_receive_entry(struct session *sess, int topic_id, const void *dat char *json_str = malloc(fsize + 1); fread(json_str, fsize, 1, fp); fclose(fp); - // delete the file to restore the environment - // remove(msg->saved_json_path); json_str[fsize] = 0; cJSON *root = cJSON_Parse(json_str); diff --git a/test/burst_scope_plugin/test_result_json/burst_scope_plugin_result_video.json b/test/burst_scope_plugin/test_result_json/burst_scope_plugin_result_video.json index 33ce8a4..cee5567 100644 --- a/test/burst_scope_plugin/test_result_json/burst_scope_plugin_result_video.json +++ b/test/burst_scope_plugin/test_result_json/burst_scope_plugin_result_video.json @@ -1,7 +1,6 @@ [{ - "n_bursts": [32, 33], - "c2s": [[1708669343597, 1708669346190, 40958, 5], [1708669349855, 1708669350245, 3753, 4], [1708669354039, 1708669354220, 3571, 4], [1708669356261, 1708669356535, 3177, 5], [1708669358326, 1708669358650, 7165, 5], [1708669363458, 1708669363596, 1997, 5], [1708669363604, 1708669363770, 2191, 5], [1708669368442, 1708669368912, 10147, 5], [1708669374350, 1708669375225, 14205, 5], [1708669379808, 1708669379902, 3043, 5], [1708669381826, 1708669382052, 6209, 3], [1708669385051, 1708669385299, 5858, 5], [1708669388265, 1708669388514, 6683, 5], [1708669392538, 1708669393117, 14759, 5], [1708669398814, 1708669400278, 32428, 5], [1708669403599, 1708669403917, 5036, 5], [1708669407621, 1708669408163, 6841, 5], [1708669410730, 1708669411308, 7598, 5], [1708669417040, 1708669417760, 13534, 5], [1708669422104, 1708669422590, 9346, 5], [1708669424663, 1708669424946, 5296, 5], [1708669426800, 1708669427038, 6132, 5], [1708669430014, 1708669430512, 11635, 5], [1708669436276, 1708669436509, 5111, 5], [1708669437489, 1708669437791, 6728, 5], [1708669440701, 1708669441107, 10753, 5], [1708669443913, 1708669444050, 3592, 2], [1708669447140, 1708669447362, 5441, 5], [1708669450351, 1708669450710, 14880, 5], [1708669452570, 1708669452689, 3236, 1], [1708669457539, 1708669457835, 5866, 3], [1708669463222, 1708669463538, 7312, 5]], - "s2c": [[1708669343821, 1708669346190, 1410591, 5], [1708669349853, 1708669350224, 440531, 5], [1708669354118, 1708669354199, 117278, 5], [1708669356261, 1708669356520, 371753, 5], [1708669358405, 1708669358630, 200183, 5], [1708669363456, 1708669363596, 258668, 5], [1708669363599, 1708669363834, 292395, 5], [1708669368520, 1708669368989, 606830, 5], [1708669374429, 1708669374995, 779606, 5], [1708669379808, 1708669379956, 388822, 5], [1708669381904, 1708669382031, 122197, 4], [1708669385130, 1708669385318, 427852, 5], [1708669388344, 1708669388592, 191007, 5], [1708669392617, 1708669393063, 595966, 5], [1708669398892, 1708669400284, 626385, 5], [1708669403331, 1708669403550, 121383, 5], [1708669403678, 1708669403896, 135018, 5], [1708669407619, 1708669408148, 520728, 5], [1708669410809, 1708669411288, 616507, 5], [1708669417118, 1708669417739, 698526, 5], [1708669422182, 1708669422590, 458080, 5], [1708669424742, 1708669424974, 357637, 5], [1708669426878, 1708669427017, 105828, 5], [1708669430093, 1708669430490, 509011, 5], [1708669436354, 1708669436557, 326489, 5], [1708669437568, 1708669437770, 145408, 5], [1708669440780, 1708669441085, 333454, 5], [1708669443992, 1708669444092, 119053, 5], [1708669447218, 1708669447390, 332672, 5], [1708669450430, 1708669450710, 231212, 5], [1708669452567, 1708669452669, 55901, 4], [1708669457618, 1708669457732, 53755, 5], [1708669463303, 1708669463538, 280269, 5]], + "c2s": [[1708669343597, 1708669344791, 25004, 5], [1708669344914, 1708669346210, 15483, 4], [1708669349766, 1708669350187, 5891, 4], [1708669354039, 1708669354220, 3571, 5], [1708669356175, 1708669356535, 5700, 5], [1708669358326, 1708669358650, 7165, 5], [1708669363377, 1708669363770, 6720, 5], [1708669368442, 1708669368912, 10147, 5], [1708669374350, 1708669375108, 13894, 5], [1708669379691, 1708669379902, 5573, 5], [1708669381826, 1708669382052, 6209, 5], [1708669385051, 1708669385299, 5858, 5], [1708669388265, 1708669388514, 6683, 5], [1708669392538, 1708669393218, 14952, 5], [1708669398814, 1708669400186, 31858, 5], [1708669403252, 1708669403917, 11152, 5], [1708669407541, 1708669408163, 9375, 5], [1708669410730, 1708669411308, 7598, 5], [1708669417040, 1708669417760, 13534, 5], [1708669422104, 1708669422611, 9441, 5], [1708669424663, 1708669424946, 5296, 5], [1708669426800, 1708669427038, 6132, 5], [1708669430014, 1708669430512, 11635, 5], [1708669436276, 1708669436509, 5111, 5], [1708669437489, 1708669437791, 6728, 5], [1708669440701, 1708669441107, 10753, 5], [1708669443913, 1708669444050, 3592, 5], [1708669447140, 1708669447362, 5441, 5], [1708669450351, 1708669450630, 14781, 5], [1708669452489, 1708669452689, 5785, 5], [1708669457539, 1708669457835, 5866, 5], [1708669463222, 1708669463538, 7312, 5]], + "s2c": [[1708669343678, 1708669346096, 1418479, 5], [1708669349853, 1708669350224, 440531, 5], [1708669354118, 1708669354199, 117278, 5], [1708669356261, 1708669356520, 371753, 5], [1708669358405, 1708669358630, 200183, 5], [1708669363456, 1708669363834, 551063, 5], [1708669368520, 1708669368891, 606742, 5], [1708669374429, 1708669374968, 757642, 5], [1708669379808, 1708669379956, 388822, 5], [1708669381904, 1708669382031, 122197, 4], [1708669385130, 1708669385318, 427852, 5], [1708669388344, 1708669388494, 190919, 5], [1708669392617, 1708669393063, 595966, 5], [1708669398892, 1708669400284, 626385, 5], [1708669403331, 1708669403550, 121383, 5], [1708669403678, 1708669403812, 134807, 5], [1708669407619, 1708669408148, 520728, 5], [1708669410809, 1708669411288, 616507, 5], [1708669417118, 1708669417739, 698526, 5], [1708669422182, 1708669422529, 457610, 5], [1708669424742, 1708669424974, 357637, 5], [1708669426878, 1708669427017, 105828, 5], [1708669430093, 1708669430490, 509011, 5], [1708669436354, 1708669436557, 326489, 5], [1708669437568, 1708669437770, 145408, 5], [1708669440780, 1708669441085, 333454, 5], [1708669443992, 1708669444092, 119053, 5], [1708669447218, 1708669447390, 332672, 5], [1708669450430, 1708669450710, 231212, 5], [1708669452567, 1708669452767, 55988, 4], [1708669457618, 1708669457732, 53755, 5], [1708669463303, 1708669463538, 280269, 5]], "ret_code": "BSP_RET_OK", "name": "burst_scope_plugin_normal_video" } |
