Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 62dea14

Browse files
committed
enable inserting new container
Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
1 parent dfa392e commit 62dea14

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/hyper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum {
2727
NEXT,
2828
WRITEFILE,
2929
READFILE,
30+
NEWCONTAINER,
3031
};
3132

3233
enum {

src/init.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,32 @@ static int hyper_start_pod(char *json, int length)
687687
return 0;
688688
}
689689

690+
static int hyper_new_container(char *json, int length)
691+
{
692+
int ret;
693+
struct hyper_pod *pod = &global_pod;
694+
695+
fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length);
696+
697+
if (!pod->init_pid)
698+
fprintf(stdout, "the pod is not created yet\n");
699+
700+
if (hyper_parse_new_container(pod, json, length) < 0) {
701+
fprintf(stderr, "parse container json failed\n");
702+
return -1;
703+
}
704+
705+
ret = hyper_start_container_stage0(&pod->c[pod->c_num - 1], pod);
706+
if (ret < 0) {
707+
//TODO full grace cleanup
708+
pod->remains -= 1;
709+
pod->c_num -= 1;
710+
return ret;
711+
}
712+
713+
return 0;
714+
}
715+
690716
static int hyper_cmd_write_file(char *json, int length)
691717
{
692718
struct hyper_writter writter;
@@ -1112,6 +1138,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
11121138
case WINSIZE:
11131139
ret = hyper_set_win_size((char *)buf->data + 8, len - 8);
11141140
break;
1141+
case NEWCONTAINER:
1142+
ret = hyper_new_container((char *)buf->data + 8, len - 8);
1143+
break;
11151144
default:
11161145
ret = -1;
11171146
break;

src/parse.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,51 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
530530
return next;
531531
}
532532

533+
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
534+
{
535+
int n;
536+
jsmn_parser p;
537+
int toks_num = 100;
538+
jsmntok_t *toks = NULL;
539+
struct hyper_container *c;
540+
541+
realloc:
542+
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
543+
544+
jsmn_init(&p);
545+
n = jsmn_parse(&p, json, length, toks, toks_num);
546+
if (n < 0) {
547+
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
548+
if (n == JSMN_ERROR_NOMEM) {
549+
toks_num *= 2;
550+
goto realloc;
551+
}
552+
553+
goto fail;
554+
}
555+
556+
c = realloc(pod->c, (pod->c_num + 1) * sizeof(*pod->c));
557+
if (c == NULL) {
558+
fprintf(stdout, "alloc memory for container failed\n");
559+
goto fail;
560+
}
561+
pod->c = c;
562+
memset(&pod->c[pod->c_num], 0, sizeof(pod->c[pod->c_num]));
563+
564+
// trick: toks-1, TODO: change all "i = 1" to "i = 0"
565+
if (hyper_parse_container(pod, &pod->c[pod->c_num], json, toks-1) < 0)
566+
goto fail;
567+
568+
pod->remains += 1;
569+
pod->c_num += 1;
570+
free(toks);
571+
return 0;
572+
573+
fail:
574+
free(toks);
575+
return -1;
576+
}
577+
533578
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
534579
{
535580
int i, n, ret = 0;

0 commit comments

Comments
 (0)