Exemplo n.º 1
0
int main(int argc, char **argv)
{
  struct katcp_dispatch *d;
  int status;

  if(argc <= 1){
    fprintf(stderr, "usage: %s [bind-ip:]listen-port\n", argv[0]);
    return 1;
  }

  d = startup_katcp();
  if(d == NULL){
    fprintf(stderr, "%s: unable to allocate state\n", argv[0]);
    return 1;
  }

  version_katcp(d, "echo-test", 0, 1);
  build_katcp(d, BUILD);

  if(register_katcp(d, "?echo", "echo returns its parameters", &echo_cmd)){
    fprintf(stderr, "server: unable to enroll command\n");
    return 1;
  }

  if(run_server_katcp(d, argv[1], 0) < 0){
    fprintf(stderr, "server: run failed\n");
    return 1;
  }

  status = exited_katcp(d);

  shutdown_katcp(d);

  return status;
}
Exemplo n.º 2
0
int run_client(struct list_loop *ls, struct task_loop *tl, int mask)
{
  struct mul_client *ci;
  int update, idle, result;
#if 0
  struct task_loop *tx;
#endif
  struct link_loop *lk;
  struct mul_msg *mm;
  
  ci = user_loop(ls, tl);
  if(ci == NULL){
    stop_loop(ls, tl);
    return 0;
  }

  ci->c_task = tl;

  idle = LO_STOP_MASK | LO_WAIT_MASK;

#ifdef DEBUG
  fprintf(stderr, "client[%p]: mask 0x%02x\n", tl, mask);
#endif

  update = idle;
  if(mask & LO_STOP_MASK){
    /* WARNING: may have to remove fd to prevent almost harmless but ugly double close */
    shutdown_client(ci);
    return 0;
  }

  /* handle read case */
  update |= LO_READ_MASK;
  if(mask & LO_READ_MASK){
    result = read_katcp(ci->c_dispatch);
#ifdef DEBUG
    fprintf(stderr, "client: read code is %d\n", result);
#endif
    if(result < 0){
      stop_loop(ls, tl);
      return 0;
    }
    if(result > 0){
      update &= ~(LO_READ_MASK); /* on EOF stop reading */
    }
  }

  /* handle work function */
  result = lookup_katcp(ci->c_dispatch);
  if(result < 0){
    stop_loop(ls, tl);
    return 0;
  }

  if(result > 0){
    /* WARNING: statement triggers dispatch if no request pending (new) or if something has arrived (with pending request) */
    if((ci->c_waiting == 0) || (mask & LO_WAIT_MASK)){
#ifdef DEBUG
      fprintf(stderr, "client[%p]: calling dispatch (queue=%d)\n", tl, ci->c_queue);
#endif
      result = call_katcp(ci->c_dispatch);
      if(result == KATCP_RESULT_RESUME){
        ci->c_waiting = (ci->c_queue > 0) ? 1 : 0;
      } else {
        ci->c_waiting = 0;
        if(ci->c_queue > 0){ /* WARNING: this should actually be an abort, as the dispatch routines have broken the queue */
          fprintf(stderr, "client[%p]: warning: finished with %d outstanding requests\n", tl, ci->c_queue);
          log_message_katcp(ci->c_dispatch, KATCP_LEVEL_ERROR, NULL, "client function has %d outstand requests", ci->c_queue);
        }
        ci->c_queue = 0;
      }
    }
  }

  if(mask & LO_WAIT_MASK){
    if(!(ci->c_waiting)){ /* no dispatch function busy, so we handle the informs and ditch everything else */
#ifdef DEBUG
      fprintf(stderr, "client[%p]: idle, handling queue\n", tl);
#endif
      while((lk = receive_link_loop(ls, tl)) != NULL){
#ifdef DEBUG
        fprintf(stderr, "client[%p]: received event type %d\n", tl, lk->k_type);
#endif
        if((lk->k_type == ci->c_overall->o_type_msg) && (lk->k_data != NULL)){
          mm = lk->k_data;
          if(arg_inform_msg(mm)){ /* inform messages get sent out */
            dispatch_from_msg(ci->c_dispatch, mm);
          }
        }
        discard_link_loop(ls, lk);
      } 
    }
  }

  /* handle write case */
  if(mask & LO_WRITE_MASK){
    if(write_katcp(ci->c_dispatch) < 0){
      stop_loop(ls, tl);
      return 0;
    }
  }

  if(flushing_katcp(ci->c_dispatch)){
    /* WARNING: could disable reads and running if flush buffer too large */
    update |= LO_WRITE_MASK;
  }

  if(update == idle){
    /* if we are only interested in stopping then stop */
    stop_loop(ls, tl);
  }

  set_mask_loop(ls, tl, update, 0);

  if(exited_katcp(ci->c_dispatch) != KATCP_EXIT_NOTYET){
    /* TODO: make it stop */
  }

  return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
  struct katcp_dispatch *d;
#if 0
  struct cached_sensor_state local_data;
  struct fifo_sensor_state *fss;
#endif
  int status, result;

  if(argc <= 1){
    fprintf(stderr, "usage: %s [bind-ip:]listen-port\n", argv[0]);
    return 1;
  }

  /* create a state handle */
  d = startup_katcp();
  if(d == NULL){
    fprintf(stderr, "%s: unable to allocate state\n", argv[0]);
    return 1;
  }

  /* load up build and version information */
  add_version_katcp(d, "mylabel", 0, "myversion", "mybuildtime");

  /* example sensor */
  if(register_integer_sensor_katcp(d, 0, "check.integer.simple", "unix time in decaseconds", "Ds", &simple_integer_check_sensor, NULL, NULL, 0, INT_MAX)){
    fprintf(stderr, "server: unable to register sensors\n");
    return 1;
  }

  /* register example commands */

  result = 0;

  result += register_katcp(d, "?check-own",   "return self generated code", &own_check_cmd);
  result += register_katcp(d, "?check-ok",    "return ok", &ok_check_cmd);
  result += register_katcp(d, "?check-fail",  "return fail", &fail_check_cmd);
  result += register_katcp(d, "?check-pause", "pauses", &pause_check_cmd);
  result += register_katcp(d, "?check-subprocess", "runs sleep 10 as a subprocess and waits for completion", &subprocess_check_cmd);

  if(result < 0){
    fprintf(stderr, "server: unable to register commands\n");
    return 1;
  }


#if 1
  /* alternative - run with more than one client */
  #define CLIENT_COUNT 3

  if(run_multi_server_katcp(d, CLIENT_COUNT, argv[1], 0) < 0){
    fprintf(stderr, "server: run failed\n");
  }
#else
  if(run_server_katcp(d, argv[1], 0) < 0){
    fprintf(stderr, "server: run failed\n");
  }
#endif

  status = exited_katcp(d);

  shutdown_katcp(d);
#if 0
  fifo_boolean_destroy_sensor(fss);
#endif

  return status;
}