예제 #1
0
void shutdown_client(struct mul_client *ci)
{
  if(ci == NULL){
    return;
  }

#ifdef DEBUG
  if(ci->c_magic != MUL_MAGIC){
    fprintf(stderr, "shutdown client: bad client magic\n");
    abort();
  }
#endif

  if(ci->c_xlookup){
    destroy_xlookup(ci->c_xlookup);
    ci->c_xlookup = NULL;
  }

  if(ci->c_dispatch){
    shutdown_katcp(ci->c_dispatch);
    ci->c_dispatch = NULL;
  }

  free(ci);
}
예제 #2
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;
}
예제 #3
0
파일: ktype.c 프로젝트: gitj/katcp_devel
int main(int argc, char *argv[])
{
  struct katcp_dispatch *d;
  int rtn;

  d = startup_katcp();
  if (d == NULL){
    fprintf(stderr, "unable to create dispatch\n");
    return 1;
  }
  
  rtn = 0;

  rtn += register_name_type_katcp(d, "test", NULL, NULL, NULL, NULL, NULL);
  rtn += register_name_type_katcp(d, "test", NULL, NULL, NULL, NULL, NULL);
  
  rtn += store_data_type_katcp(d, "names", "john", NULL, NULL, NULL, NULL, NULL, NULL);
  
  rtn += store_data_type_katcp(d, "string", "test1", NULL, NULL, NULL, NULL, NULL, NULL);
  rtn += store_data_type_katcp(d, "string", "test2", NULL, NULL, NULL, NULL, NULL, NULL);

  rtn += store_data_type_katcp(d, "names", "adam", NULL, NULL, NULL, NULL, NULL, NULL);
  rtn += store_data_type_katcp(d, "names", "perry", NULL, NULL, NULL, NULL, NULL, NULL);
 
  rtn += store_data_type_katcp(d, "string", "thisisalongstring", NULL, NULL, NULL, NULL, NULL, NULL);
  
  fprintf(stderr, "katcp_type: cumulative rtn in main: %d\n", rtn);
  
  fprintf(stderr,"\n");
  print_types_katcp(d);
  fprintf(stderr,"\n");
  
  destroy_type_list_katcp(d);

  shutdown_katcp(d);
  return 0;
} 
예제 #4
0
파일: dmon.c 프로젝트: project8/katcp_devel
int main(int argc, char **argv)
{
  struct udp_state *ud;
  struct katcp_dispatch *d;
  unsigned int result;
  struct timeval now;
  fd_set fsr;
  char *ip_addr = NULL;
  uint32_t address, length;
  int i, j, c, pos;
  int wait, nooftries;
  int port = 0;
int rw_flag = 0;

  i = j = 1;
  pos = 0;
  wait = 0;
  nooftries = 10;

  while (i < argc) {
    if (argv[i][0] == '-') {
      c = argv[i][j];
      switch (c) {
        case '\0':
          j = 1;
          i++;
          break;
        case '-' :
          j++;
          break;
        case 'h' :
          fprintf(stderr, "usage: %s -R [-i ipaddress] [-p port] address length\n", argv[0]);
          return 0;
          break;
        case 'i' : 
          j++;
          if(argv[i][j] == '\0'){
            j = 0;
            i++;
          }
          if(i >= argc){
            fprintf(stderr, "%s: option -%c requires a parameter\n", argv[0], c);
          }
          ip_addr = argv[i] + j;	
          i++;
          j = 1;
          break;
        case 'p' : 
          j++;
          if(argv[i][j] == '\0'){
            j = 0;
            i++;
          }
          if(i >= argc){
            fprintf(stderr, "%s: option -%c requires a parameter\n", argv[0], c);
          }
          port = atoi(argv[i] + j);	
#if DEBUG
          fprintf(stderr, "port number is %d\n", port);
#endif
          i++;
          j = 1;
          break;
        case 'R' : 
          rw_flag = 1;	
          i++;
          break;
        default:
          fprintf(stderr, "%s: unknown option -%c\n", argv[0], argv[i][j]);
          return 2;
      }
    } else {
      pos = i;
      i = argc;
    }
  }
  d = setup_katcp(STDOUT_FILENO);
  if(d == NULL){
    fprintf(stderr, "setup katcp failed\n");

    return EX_OSERR;
  }
  ud = create_udp(d);
  if(ud == NULL){
    fprintf(stderr, "create udp failed\n");
    log_message_katcp(d, KATCP_LEVEL_ERROR, DMON_MODULE_NAME, "unable to allocate local udp state");
    write_katcp(d);
    return EX_OSERR;
  }
#if 0
  if(connect_udp(d, ud, port) < 0){
    fprintf(stderr, "connect udp failed\n");
    log_message_katcp(d, KATCP_LEVEL_ERROR, DMON_MODULE_NAME, "unable to bind udp");
    return EX_OSERR;
  }
#endif
  ud->u_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(ud->u_fd < 0){
    fprintf(stderr, "unable to create udp socket:\n ");
    log_message_katcp(d, KATCP_LEVEL_ERROR, DMON_MODULE_NAME, "unable to create udp socket: %s", strerror(errno));
    return -1;
  }
  ud->u_rw = rw_flag;

  for(;;){

    FD_ZERO(&fsr);

    FD_SET(ud->u_fd, &fsr);
    address = strtol(argv[pos], NULL, 16); 
    length  = strtol(argv[pos + 1], NULL, 16);

#if DEBUG
    printf("pos:%d, address[%x] and length[%x]\n", pos, address, length);
#endif

    send_udp(d, ud, ip_addr, port, address, length);

    now.tv_sec =  10;
    now.tv_usec = 0;
    result = select(ud->u_fd + 1, &fsr, NULL, NULL, &now);
    if(result == 0){
      /* Resend again after timeout */
      printf("Resending udp again\n");
      send_udp(d, ud, ip_addr, port, address, length);
    }

    for(wait = 0; wait < nooftries; wait++){

      if(FD_ISSET(ud->u_fd, &fsr)){
        result = rcv_udp(d, ud);
        if(!result){
          return EX_OK;
        }
      }
    }
    return EX_OSERR;

  }

  destroy_udp(d, ud);
  shutdown_katcp(d);

  return EX_OK;
}
예제 #5
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;
}