Changeset abfe5861994b2f5d62f789bf196ef636b0ef04fe

Show
Ignore:
Timestamp:
02/10/08 20:06:22 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202673982 +0000
git-parent:

[1fe89bbf2c59cbd0895409389eb9df047e96d692]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1202673982 +0000
Message:

make the delegate state a first-class citizen and preserve closure for future use. show checks? seriously?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_check.c

    r1d69a01 rabfe586  
    1919#include "noit_check.h" 
    2020#include "noit_module.h" 
     21#include "noit_console.h" 
    2122#include "eventer/eventer.h" 
    2223 
     
    3031  uuid_t foo; 
    3132}; 
     33 
     34static void register_console_check_commands(); 
    3235 
    3336#define UUID_SIZE sizeof(struct uuid_dummy) 
     
    219222  noit_poller_load_checks(); 
    220223  noit_poller_make_causal_map(); 
     224  register_console_check_commands(); 
    221225  noit_poller_initiate(); 
    222226} 
     
    427431  } 
    428432} 
     433 
     434static void 
     435nc_printf_check_brief(noit_console_closure_t ncct, 
     436                      noit_check_t *check) { 
     437  char out[512]; 
     438  snprintf(out, sizeof(out), "%s/%s", check->target, check->name); 
     439  nc_printf(ncct, "%30s [%c%c%c]: %s\n", 
     440            out,  
     441            check->flags & NP_RUNNING  ? 'R' : ' ', 
     442            check->flags & NP_KILLED   ? 'K' : ' ', 
     443            check->flags & NP_DISABLED ? 'D' : ' ', 
     444            check->stats.current.status ? 
     445              check->stats.current.status : "unknown"); 
     446} 
     447 
     448static int 
     449noit_console_show_checks(noit_console_closure_t ncct, 
     450                         int argc, char **argv, 
     451                         noit_console_state_t *dstate, 
     452                         void *closure) { 
     453  struct timeval _now; 
     454  noit_hash_iter iter = NOIT_HASH_ITER_ZERO; 
     455  uuid_t key_id; 
     456  int klen; 
     457  noit_check_t *check; 
     458 
     459  gettimeofday(&_now, NULL); 
     460  while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen, 
     461                       (void **)&check)) { 
     462    nc_printf_check_brief(ncct, check); 
     463  } 
     464  return 0; 
     465} 
     466static void 
     467register_console_check_commands() { 
     468  noit_console_state_t *tl; 
     469  cmd_info_t *showcmd; 
     470 
     471  tl = noit_console_state_initial(); 
     472  showcmd = noit_console_state_get_cmd(tl, "show"); 
     473  assert(showcmd && showcmd->dstate); 
     474 
     475  noit_console_state_add_cmd(showcmd->dstate, 
     476    NCSCMD("checks", noit_console_show_checks, NULL, NULL)); 
     477} 
     478 
  • src/noit_conf.c

    r1fe89bb rabfe586  
    3939}; 
    4040 
    41 void register_console_config_commands(); 
     41static void register_console_config_commands(); 
    4242 
    4343void noit_conf_init() { 
     
    284284static int 
    285285noit_console_state_conf_terminal(noit_console_closure_t ncct, 
    286                                  int argc, char **argv, void *state) { 
     286                                 int argc, char **argv, 
     287                                 noit_console_state_t *state, void *closure) { 
    287288  noit_conf_t_userdata_t *info; 
    288289  if(argc) { 
     
    322323} 
    323324 
     325static 
    324326void register_console_config_commands() { 
    325327  noit_console_state_t *tl, *_conf_state, *_conf_t_state; 
     
    333335  _conf_state = calloc(1, sizeof(*_conf_state)); 
    334336  noit_console_state_add_cmd(_conf_state, 
    335     NCSCMD("terminal", noit_console_state_conf_terminal, _conf_t_state)); 
     337    NCSCMD("terminal", noit_console_state_conf_terminal, _conf_t_state, NULL)); 
    336338 
    337339  noit_console_state_add_cmd(tl, 
    338     NCSCMD("configure", noit_console_state_delegate, _conf_state)); 
    339 } 
     340    NCSCMD("configure", noit_console_state_delegate, _conf_state, NULL)); 
     341} 
  • src/noit_console.h

    r1fe89bb rabfe586  
    1818 
    1919typedef int (*console_cmd_func_t)(struct __noit_console_closure *, 
    20                                   int, char **, void *); 
     20                                  int, char **, 
     21                                  struct _console_state *, void *); 
    2122typedef char *(*console_prompt_func_t)(EditLine *); 
    2223typedef void (*state_free_func_t)(struct _console_state *); 
     
    2425 
    2526typedef struct { 
    26   const char          *name; 
    27   console_cmd_func_t   func; 
    28   void                *closure; 
     27  const char            *name; 
     28  console_cmd_func_t     func; 
     29  struct _console_state *dstate; 
     30  void                  *closure; 
    2931} cmd_info_t; 
    3032 
     
    110112API_EXPORT(int) 
    111113  noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv, 
    112                          void *); 
     114                         noit_console_state_t *, void *); 
    113115 
    114116API_EXPORT(int) 
    115117  noit_console_state_add_cmd(noit_console_state_t *state, 
    116118                             cmd_info_t *cmd); 
     119 
     120API_EXPORT(cmd_info_t *) 
     121  noit_console_state_get_cmd(noit_console_state_t *state, 
     122                             const char *name); 
    117123 
    118124API_EXPORT(noit_console_state_t *) 
     
    140146API_EXPORT(int) 
    141147  noit_console_state_delegate(noit_console_closure_t ncct, 
    142                               int argc, char **argv, void *closure); 
     148                              int argc, char **argv, 
     149                              noit_console_state_t *dstate, 
     150                              void *closure); 
    143151  
    144152API_EXPORT(cmd_info_t *) 
    145   NCSCMD(const char *name, console_cmd_func_t func, void *closure); 
     153  NCSCMD(const char *name, console_cmd_func_t func, 
     154         noit_console_state_t *dstate, void *closure); 
    146155 
    147156#endif 
  • src/noit_console_state.c

    r7192210 rabfe586  
    1313#include "noit_tokenizer.h" 
    1414 
    15 cmd_info_t console_command_exit = { "exit", noit_console_state_pop, NULL }; 
     15cmd_info_t console_command_exit = { 
     16  "exit", noit_console_state_pop, NULL, NULL 
     17}; 
    1618 
    1719static char * 
     
    2325int 
    2426noit_console_state_delegate(noit_console_closure_t ncct, 
    25                             int argc, char **argv, void *closure) { 
     27                            int argc, char **argv, 
     28                            noit_console_state_t *dstate, 
     29                            void *closure) { 
    2630  noit_console_state_stack_t tmps = { 0 }; 
    2731 
    2832  if(argc == 0) { 
    2933    nc_printf(ncct, "arguments expected\n"); 
     34    /* XXX: noit_console_render_help(dstate); */ 
    3035    return -1; 
    3136  } 
    32   tmps.state = closure; 
     37  if(!dstate) { 
     38    nc_printf(ncct, "internal error: no delegate state\n"); 
     39    return -1; 
     40  } 
     41  tmps.state = dstate; 
    3342  return _noit_console_state_do(ncct, &tmps, argc, argv); 
    3443} 
     
    4958    return -1; 
    5059  } 
    51   return cmd->func(ncct, argc-1, argv+1, cmd->closure); 
     60  return cmd->func(ncct, argc-1, argv+1, cmd->dstate, cmd->closure); 
    5261} 
    5362int 
     
    7382} 
    7483 
     84cmd_info_t * 
     85noit_console_state_get_cmd(noit_console_state_t *state, 
     86                           const char *name) { 
     87  cmd_info_t *cmd = NULL; 
     88  if(noit_hash_retrieve(&state->cmds, name, strlen(name), (void **)&cmd)) 
     89    return cmd; 
     90  return NULL; 
     91} 
     92 
    7593noit_console_state_t * 
    7694noit_console_state_build(console_prompt_func_t promptf, cmd_info_t **clist, 
     
    90108 
    91109cmd_info_t *NCSCMD(const char *name, console_cmd_func_t func, 
    92                    void *closure) { 
     110                   noit_console_state_t *dstate, void *closure) { 
    93111  cmd_info_t *cmd; 
    94112  cmd = calloc(1, sizeof(*cmd)); 
    95113  cmd->name = strdup(name); 
    96114  cmd->func = func; 
     115  cmd->dstate = dstate; 
    97116  cmd->closure = closure; 
    98117  return cmd; 
     
    103122  static noit_console_state_t *_top_level_state = NULL; 
    104123  if(!_top_level_state) { 
     124    static noit_console_state_t *show_state; 
    105125    _top_level_state = calloc(1, sizeof(*_top_level_state)); 
    106126    noit_console_state_add_cmd(_top_level_state, &console_command_exit); 
     127    show_state = calloc(1, sizeof(*show_state)); 
     128    noit_console_state_add_cmd(_top_level_state, 
     129      NCSCMD("show", noit_console_state_delegate, show_state, NULL)); 
    107130  } 
    108131  return _top_level_state; 
     
    121144int 
    122145noit_console_state_pop(noit_console_closure_t ncct, int argc, char **argv, 
    123                        void *unused) { 
     146                       noit_console_state_t *dstate, void *unused) { 
    124147  noit_console_state_stack_t *current; 
    125148