moonbridge

changeset 274:42b85f65327c

Allow "main threads" to be named; main threads also use MOONBR_PROTO_INTERVAL as listener type now
author jbe
date Tue Jun 06 11:25:07 2017 +0200 (2017-06-06)
parents 928473f7df96
children d4fdc4ed4d81
files moonbridge.c
line diff
     1.1 --- a/moonbridge.c	Mon Jun 05 23:19:08 2017 +0200
     1.2 +++ b/moonbridge.c	Tue Jun 06 11:25:07 2017 +0200
     1.3 @@ -119,10 +119,9 @@
     1.4  #define MOONBR_PSTATE_FORKED  2
     1.5  
     1.6  /* Enum for 'proto' field of struct moonbr_listener */
     1.7 -#define MOONBR_PROTO_MAIN 1
     1.8 -#define MOONBR_PROTO_INTERVAL 2
     1.9 -#define MOONBR_PROTO_LOCAL 3
    1.10 -#define MOONBR_PROTO_TCP 4
    1.11 +#define MOONBR_PROTO_INTERVAL 1
    1.12 +#define MOONBR_PROTO_LOCAL 2
    1.13 +#define MOONBR_PROTO_TCP 3
    1.14  
    1.15  /* Data structure for a pool's listener that can accept incoming connections */
    1.16  struct moonbr_listener {
    1.17 @@ -133,6 +132,7 @@
    1.18    union {
    1.19      struct {
    1.20        char *name;  /* name of interval passed to 'connect' function as 'interval' field in table */
    1.21 +      int main;    /* nonzero = termination of 'connect' function causes shutdown */
    1.22        int strict;  /* nonzero = runtime of 'connect' function does not delay interval */
    1.23        struct timeval delay;   /* interval between invocations of 'connect' function */
    1.24        struct timeval wakeup;  /* point in time of next invocation */
    1.25 @@ -512,16 +512,21 @@
    1.26      for (i=0; i<pool->listener_count; i++) {
    1.27        struct moonbr_listener *listener = &pool->listener[i];
    1.28        switch (listener->proto) {
    1.29 -      case MOONBR_PROTO_MAIN:
    1.30 -        /* nothing to do here: starting main thread is performed in moonbr_run() function */
    1.31 -        moonbr_log(LOG_INFO, "Adding main thread");
    1.32 -        break;
    1.33        case MOONBR_PROTO_INTERVAL:
    1.34 -        /* nothing to do here: starting intervals is performed in moonbr_run() function */
    1.35 -        if (!listener->type_specific.interval.name) {
    1.36 -          moonbr_log(LOG_INFO, "Adding unnamed interval listener");
    1.37 +        if (listener->type_specific.interval.main) {
    1.38 +          /* nothing to do here: starting main thread is performed in moonbr_run() function */
    1.39 +          if (!listener->type_specific.interval.name) {
    1.40 +            moonbr_log(LOG_INFO, "Adding unnamed main thread");
    1.41 +          } else {
    1.42 +            moonbr_log(LOG_INFO, "Adding main thread \"%s\"", listener->type_specific.interval.name);
    1.43 +          }
    1.44          } else {
    1.45 -          moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name);
    1.46 +          /* nothing to do here: starting intervals is performed in moonbr_run() function */
    1.47 +          if (!listener->type_specific.interval.name) {
    1.48 +            moonbr_log(LOG_INFO, "Adding unnamed interval listener");
    1.49 +          } else {
    1.50 +            moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name);
    1.51 +          }
    1.52          }
    1.53          break;
    1.54        case MOONBR_PROTO_LOCAL:
    1.55 @@ -781,9 +786,7 @@
    1.56      ) {
    1.57        moonbr_child_log_fatal("Received unexpected file descriptor from parent process");
    1.58      } else if (
    1.59 -      listener->proto != MOONBR_PROTO_MAIN &&
    1.60 -      listener->proto != MOONBR_PROTO_INTERVAL &&
    1.61 -      fd < 0
    1.62 +      listener->proto != MOONBR_PROTO_INTERVAL && fd < 0
    1.63      ) {
    1.64        moonbr_child_log_fatal("Missing file descriptor from parent process");
    1.65      }
    1.66 @@ -791,15 +794,16 @@
    1.67      lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
    1.68      if (fd < 0) {
    1.69        lua_newtable(L);
    1.70 -      if (listener->proto == MOONBR_PROTO_MAIN) {
    1.71 -        lua_pushboolean(L, 1);
    1.72 -        lua_setfield(L, -2, "main");
    1.73 -      } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
    1.74 +      if (listener->proto == MOONBR_PROTO_INTERVAL) {
    1.75          lua_pushstring(L,
    1.76            listener->type_specific.interval.name ?
    1.77            listener->type_specific.interval.name : ""
    1.78          );
    1.79 -        lua_setfield(L, -2, "interval");
    1.80 +        if (listener->type_specific.interval.main) {
    1.81 +          lua_setfield(L, -2, "main");
    1.82 +        } else {
    1.83 +          lua_setfield(L, -2, "interval");
    1.84 +        }
    1.85        }
    1.86      } else {
    1.87        lua_pushvalue(L, -2);
    1.88 @@ -1416,22 +1420,21 @@
    1.89  static void moonbr_connect(struct moonbr_pool *pool) {
    1.90    struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
    1.91    struct moonbr_worker *worker;
    1.92 -  if (listener->proto == MOONBR_PROTO_MAIN) {
    1.93 +  if (listener->proto == MOONBR_PROTO_INTERVAL) {
    1.94      worker = moonbr_pop_idle_worker(pool);
    1.95 -    if (moonbr_stat) {
    1.96 -      moonbr_log(LOG_INFO, "Dispatching main thread of pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
    1.97 +    if (listener->type_specific.interval.main) {
    1.98 +      if (moonbr_stat) {
    1.99 +        moonbr_log(LOG_INFO, "Dispatching main thread \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
   1.100 +      }
   1.101 +      worker->main = 1;
   1.102 +    } else {
   1.103 +      if (moonbr_stat) {
   1.104 +        moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
   1.105 +      }
   1.106 +      worker->restart_interval_listener = listener;
   1.107      }
   1.108 -    worker->main = 1;
   1.109      moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
   1.110 -    /* do not push listener to queue of idle listeners */
   1.111 -  } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
   1.112 -    worker = moonbr_pop_idle_worker(pool);
   1.113 -    if (moonbr_stat) {
   1.114 -      moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
   1.115 -    }
   1.116 -    worker->restart_interval_listener = listener;
   1.117 -    moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
   1.118 -    /* do not push listener to queue of idle listeners yet */
   1.119 +    /* do not push listener to queue of idle listeners (yet) */
   1.120    } else {
   1.121      int peerfd;
   1.122      do {
   1.123 @@ -1895,6 +1898,7 @@
   1.124  static int moonbr_listen_init_pool(lua_State *L) {
   1.125    struct moonbr_pool *pool;
   1.126    const char *proto;
   1.127 +  int is_main;
   1.128    int i;
   1.129    int dynamic = 0;  /* nonzero = listeners exist which require dynamic worker creation */
   1.130    pool = lua_touserdata(L, 1);
   1.131 @@ -1909,10 +1913,8 @@
   1.132  #endif
   1.133      lua_getfield(L, 3, "proto");
   1.134      proto = lua_tostring(L, -1);
   1.135 -    if (proto && !strcmp(proto, "main")) {
   1.136 -      listener->proto = MOONBR_PROTO_MAIN;
   1.137 -    } else if (proto && !strcmp(proto, "interval")) {
   1.138 -      dynamic = 1;
   1.139 +    is_main = !strcmp(proto, "main");
   1.140 +    if (proto && (is_main || !strcmp(proto, "interval"))) {
   1.141        listener->proto = MOONBR_PROTO_INTERVAL;
   1.142        lua_getfield(L, 3, "name");
   1.143        {
   1.144 @@ -1926,19 +1928,25 @@
   1.145            listener->type_specific.interval.name = name_dup;
   1.146          }
   1.147        }
   1.148 -      lua_getfield(L, 3, "delay");
   1.149 -      if (
   1.150 -        !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) ||
   1.151 -        !timerisset(&listener->type_specific.interval.delay)
   1.152 -      ) {
   1.153 -        luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
   1.154 -      }
   1.155 -      lua_getfield(L, 3, "strict");
   1.156 -      if (!lua_isnil(L, -1)) {
   1.157 -        if (lua_isboolean(L, -1)) {
   1.158 -          if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1;
   1.159 -        } else {
   1.160 -          luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
   1.161 +      if (is_main) {
   1.162 +        listener->type_specific.interval.main = 1;
   1.163 +      } else {
   1.164 +        listener->type_specific.interval.main = 0;
   1.165 +        dynamic = 1;
   1.166 +        lua_getfield(L, 3, "delay");
   1.167 +        if (
   1.168 +          !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) ||
   1.169 +          !timerisset(&listener->type_specific.interval.delay)
   1.170 +        ) {
   1.171 +          luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
   1.172 +        }
   1.173 +        lua_getfield(L, 3, "strict");
   1.174 +        if (!lua_isnil(L, -1)) {
   1.175 +          if (lua_isboolean(L, -1)) {
   1.176 +            if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1;
   1.177 +          } else {
   1.178 +            luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
   1.179 +          }
   1.180          }
   1.181        }
   1.182      } else if (proto && !strcmp(proto, "local")) {

Impressum / About Us