Пример #1
0
int sharelog_list(shpeer_t *peer, time_t stime, time_t etime)
{
  shbuf_t *buff;
  fd_set read_fd;
  shjson_t *json;
  char tbuf[256];
  char *data;
  time_t now;
  char *str;
  int err;
  int fd;

  fd = shconnect_host("127.0.0.1", PROCESS_PORT, SHNET_ASYNC);
  if (fd < 0)
    return (fd);

  json = shjson_init(NULL);
  shjson_num_add(json, "id", 1);
  shjson_str_add(json, "method", "log.list");
  shjson_str_add(json, "key", (char *)shkey_print(shpeer_kpub(peer)));
  shjson_null_add(json, "params");

  str = shjson_print(json);
  shjson_free(&json);

  err = shnet_write(fd, str, strlen(str));
  free(str);
  if (err < 0) {
    shclose(fd);
    return (err);
  }

  err = shnet_write(fd, "\n", 1);
  if (err < 0) {
    shclose(fd);
    return (err);
  }

  while (1) {
    FD_ZERO(&read_fd);
    FD_SET(fd, &read_fd);
    err = shnet_verify(&read_fd, NULL, NULL);
    if (err < 0) {
      continue;
    }

    buff = shnet_read_buf(fd);
    if (!buff)
      break;

    data = shbuf_data(buff);
    if (!strchr(data, '\n'))
      continue;

    json = shjson_init(data);
    if (json) {
      char *text = shjson_astr(json, "result", NULL);
      if (text) {
        printf("%s", text);
      }
      shjson_free(&json);
    }

    break;
  }

  shclose(fd);

  return (0);
}
Пример #2
0
void sharelog_server(int parent_pid)
{
  unsigned int port = (unsigned int)process_socket_port;
  char buff[TEST_BUFFER_SIZE];
  ssize_t b_read, b_write;
  int cli_fd;
  int err;
  int fd;

  err = shfs_proc_lock(process_path, "");
  if (err) {
    printf ("Terminating.. '%s' server '%s' is already running.\n", "shlogd", process_path);
    return;
  }

  fd = shnet_sk();
  if (fd == -1) {
    perror("shsk");
    return;
  }
  
  err = shnet_bindsk(fd, NULL, port);
  if (err) {
    perror("shbindport");
    shclose(fd);
    return;
  }

  process_socket_fd = fd;
  daemon_server(parent_pid);

#if 0
  cli_fd = shnet_accept(fd);
  if (cli_fd == -1) {
    perror("shnet_accept");
    shclose(fd);
    return;
  }

  printf ("Received new connection on port %d.\n", port);

  memset(buff, 0, sizeof(buff));
  memset(buff, 'a', sizeof(buff) - 1);
  b_write = shnet_write(cli_fd, buff, sizeof(buff));
  if (b_write <= 0) {
    shclose(cli_fd);
    shnet_close(fd);
    perror("shnet_write");
return;
  }
  printf ("%d of %d bytes written to port %d on fd %d..\n", b_write, sizeof(buff), port, cli_fd); 

  memset(buff, 0, sizeof(buff));
  b_read = shnet_read(cli_fd, buff, sizeof(buff));
  if (b_read <= 0) {
    perror("shread");
    shnet_close(cli_fd);
    shnet_close(fd);
    return;
  }

  printf ("MESSAGE: %-*.*s\n", b_read, b_read, buff);
  printf ("%d of %d bytes read from port %d on fd %d..\n", b_read, sizeof(buff), port, cli_fd); 
  
  err = shnet_close(fd);
  if (err) {
    perror("shnet_close");
    shnet_close(cli_fd);
    shnet_close(fd);
    return;
  }

  shnet_close(cli_fd);
#endif

  shclose(fd);

}
Пример #3
0
int sharelog_tail(shpeer_t *peer)
{
  shbuf_t *buff;
  fd_set read_fd;
  shjson_t *json;
  char tbuf[256];
  time_t stime, etime;
  time_t now;
  char *str;
  int err;
  int fd;

  fd = shconnect_host("127.0.0.1", PROCESS_PORT, SHNET_ASYNC);
  if (fd < 0)
    return (fd);

  json = shjson_init(NULL);
  shjson_num_add(json, "id", 1);
  shjson_str_add(json, "method", "log.subscribe");
  shjson_str_add(json, "key", (char *)shkey_print(shpeer_kpub(peer)));
  shjson_null_add(json, "params");

  str = shjson_print(json);
  shjson_free(&json);

  err = shnet_write(fd, str, strlen(str));
  free(str);
  if (err < 0) {
    shclose(fd);
    return (err);
  }

  err = shnet_write(fd, "\n", 1);
  if (err < 0) {
    shclose(fd);
    return (err);
  }

  while (proc_mode == RUN_TAIL) {
    FD_ZERO(&read_fd);
    FD_SET(fd, &read_fd);
    err = shnet_verify(&read_fd, NULL, NULL);
    if (err < 0) {
      continue;
    }

    buff = shnet_read_buf(fd);
    if (!buff || shbuf_size(buff) == 0)
      continue;

    json = shjson_init(shbuf_data(buff));
    if (json) {
      char *text = shjson_astr(json, "result", NULL);
      if (text) {
        now = time(NULL);
        strftime(tbuf, sizeof(tbuf) - 1, "%D %T", localtime(&now));
        printf("[%s] %s", tbuf, text);
      }
    }

    shbuf_clear(buff);
  }

  shclose(fd);

  return (0);
}