diff --git a/src/hyper.h b/src/hyper.h index 081c9b0..b3ed4c9 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -27,6 +27,7 @@ enum { NEXT, WRITEFILE, READFILE, + NEWCONTAINER, }; enum { diff --git a/src/init.c b/src/init.c index 2a886bc..b1ed9a4 100644 --- a/src/init.c +++ b/src/init.c @@ -687,6 +687,32 @@ static int hyper_start_pod(char *json, int length) return 0; } +static int hyper_new_container(char *json, int length) +{ + int ret; + struct hyper_pod *pod = &global_pod; + + fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length); + + if (!pod->init_pid) + fprintf(stdout, "the pod is not created yet\n"); + + if (hyper_parse_new_container(pod, json, length) < 0) { + fprintf(stderr, "parse container json failed\n"); + return -1; + } + + ret = hyper_start_container_stage0(&pod->c[pod->c_num - 1], pod); + if (ret < 0) { + //TODO full grace cleanup + pod->remains -= 1; + pod->c_num -= 1; + return ret; + } + + return 0; +} + static int hyper_cmd_write_file(char *json, int length) { struct hyper_writter writter; @@ -1112,6 +1138,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) case WINSIZE: ret = hyper_set_win_size((char *)buf->data + 8, len - 8); break; + case NEWCONTAINER: + ret = hyper_new_container((char *)buf->data + 8, len - 8); + break; default: ret = -1; break; diff --git a/src/parse.c b/src/parse.c index 104493d..113852b 100644 --- a/src/parse.c +++ b/src/parse.c @@ -530,6 +530,51 @@ out: return next; } +int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length) +{ + int n; + jsmn_parser p; + int toks_num = 100; + jsmntok_t *toks = NULL; + struct hyper_container *c; + +realloc: + toks = realloc(toks, toks_num * sizeof(jsmntok_t)); + + jsmn_init(&p); + n = jsmn_parse(&p, json, length, toks, toks_num); + if (n < 0) { + fprintf(stdout, "jsmn parse failed, n is %d\n", n); + if (n == JSMN_ERROR_NOMEM) { + toks_num *= 2; + goto realloc; + } + + goto fail; + } + + c = realloc(pod->c, (pod->c_num + 1) * sizeof(*pod->c)); + if (c == NULL) { + fprintf(stdout, "alloc memory for container failed\n"); + goto fail; + } + pod->c = c; + memset(&pod->c[pod->c_num], 0, sizeof(pod->c[pod->c_num])); + + // trick: toks-1, TODO: change all "i = 1" to "i = 0" + if (hyper_parse_container(pod, &pod->c[pod->c_num], json, toks-1) < 0) + goto fail; + + pod->remains += 1; + pod->c_num += 1; + free(toks); + return 0; + +fail: + free(toks); + return -1; +} + int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length) { int i, n, ret = 0;