Skip to content

Commit

Permalink
Start/stop HTTP servers when enabling/disabling REST module
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 1, 2022
1 parent 2c7e313 commit 8f3a4bc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
40 changes: 40 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35326,6 +35326,38 @@ void DequeueRest(ecs_iter_t *it) {
}
}

static
void DisableRest(ecs_iter_t *it) {
ecs_world_t *world = it->world;

ecs_iter_t rit = ecs_term_iter(world, &(ecs_term_t){
.id = ecs_id(EcsRest),
.src.flags = EcsSelf
});

if (it->event == EcsOnAdd) {
/* REST module was disabled */
while (ecs_term_next(&rit)) {
EcsRest *rest = ecs_field(&rit, EcsRest, 1);
int i;
for (i = 0; i < rit.count; i ++) {
ecs_rest_ctx_t *ctx = rest[i].impl;
ecs_http_server_stop(ctx->srv);
}
}
} else if (it->event == EcsOnRemove) {
/* REST module was enabled */
while (ecs_term_next(&rit)) {
EcsRest *rest = ecs_field(&rit, EcsRest, 1);
int i;
for (i = 0; i < rit.count; i ++) {
ecs_rest_ctx_t *ctx = rest[i].impl;
ecs_http_server_start(ctx->srv);
}
}
}
}

void FlecsRestImport(
ecs_world_t *world)
{
Expand All @@ -35344,6 +35376,14 @@ void FlecsRestImport(
});

ECS_SYSTEM(world, DequeueRest, EcsPostFrame, EcsRest);

ecs_observer(world, {
.filter = {
.terms = {{ .id = EcsDisabled, .src.id = ecs_id(FlecsRest) }}
},
.events = {EcsOnAdd, EcsOnRemove},
.callback = DisableRest
});
}

#endif
Expand Down
40 changes: 40 additions & 0 deletions src/addons/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,38 @@ void DequeueRest(ecs_iter_t *it) {
}
}

static
void DisableRest(ecs_iter_t *it) {
ecs_world_t *world = it->world;

ecs_iter_t rit = ecs_term_iter(world, &(ecs_term_t){
.id = ecs_id(EcsRest),
.src.flags = EcsSelf
});

if (it->event == EcsOnAdd) {
/* REST module was disabled */
while (ecs_term_next(&rit)) {
EcsRest *rest = ecs_field(&rit, EcsRest, 1);
int i;
for (i = 0; i < rit.count; i ++) {
ecs_rest_ctx_t *ctx = rest[i].impl;
ecs_http_server_stop(ctx->srv);
}
}
} else if (it->event == EcsOnRemove) {
/* REST module was enabled */
while (ecs_term_next(&rit)) {
EcsRest *rest = ecs_field(&rit, EcsRest, 1);
int i;
for (i = 0; i < rit.count; i ++) {
ecs_rest_ctx_t *ctx = rest[i].impl;
ecs_http_server_start(ctx->srv);
}
}
}
}

void FlecsRestImport(
ecs_world_t *world)
{
Expand All @@ -880,6 +912,14 @@ void FlecsRestImport(
});

ECS_SYSTEM(world, DequeueRest, EcsPostFrame, EcsRest);

ecs_observer(world, {
.filter = {
.terms = {{ .id = EcsDisabled, .src.id = ecs_id(FlecsRest) }}
},
.events = {EcsOnAdd, EcsOnRemove},
.callback = DisableRest
});
}

#endif
4 changes: 0 additions & 4 deletions test/api/src/World.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,16 +1111,12 @@ void World_delete_empty_tables_after_mini() {
void World_delete_empty_tables_after_init() {
ecs_world_t *world = ecs_init();

const ecs_world_info_t *info = ecs_get_world_info(world);
int32_t old_empty_table_count = info->empty_table_count;

int32_t deleted;
deleted = ecs_delete_empty_tables(world, 0, 0, 1, 0, 0); /* Increase to 1 */
test_int(deleted, 0);

deleted = ecs_delete_empty_tables(world, 0, 0, 1, 0, 0); /* Delete */
test_assert(deleted != 0);
test_int(info->empty_table_count + deleted, old_empty_table_count);

ecs_fini(world);
}
Expand Down

0 comments on commit 8f3a4bc

Please sign in to comment.