コード例 #1
0
ファイル: stream.c プロジェクト: kunth/libphenom
int main(int argc, char **argv)
{
  ph_stream_t *stm;
  char namebuf[128];
  int fd;
  char buf[BUFSIZ];
  uint64_t amount;
  int len;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(18);

  strcpy(namebuf, "/tmp/phenomXXXXXX");
  fd = ph_mkostemp(namebuf, 0);
  diag("opened %s -> %d", namebuf, fd);
  unlink(namebuf);

  stm = ph_stm_fd_open(fd, 0, PH_STM_BUFSIZE);
  ph_stm_write(stm, "lemon\n", 6, &amount);
  is(amount, 6);

  // Shouldn't see it yet
  is(0, pread(fd, buf, sizeof(buf), 0));

  // Should see it now
  ph_stm_flush(stm);
  is(6, pread(fd, buf, sizeof(buf), 0));

  ok(!memcmp("lemon\n", buf, 6), "right content");
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("lem", buf, 3), "got prefix");

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("on\n", buf, 3), "got remainder");

  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  len = ph_stm_printf(stm, "testing %d %s!", 1, "two");
  ok(14 == len, "printed len %d", len);
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));
  ok(ph_stm_read(stm, buf, 14, &amount), "read ok");
  ok(amount == 14, "len was %" PRIu64, amount);
  ok(!memcmp("testing 1 two!", buf, 14), "got formatted");

  ok(ph_stm_close(stm), "closed");

  return exit_status();
}
コード例 #2
0
ファイル: buf.c プロジェクト: GamingAtheist/libphenom
int main(int argc, char **argv)
{
  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(25);

  test_straddle_edges();

  return exit_status();
}
コード例 #3
0
ファイル: counter.c プロジェクト: kunth/libphenom
int main(int argc, char** argv)
{
  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(39);
  basicCounterFunctionality();
  concurrentCounters();

  return exit_status();
}
コード例 #4
0
ファイル: timer.c プロジェクト: kunth/libphenom
int main(int argc, char **argv)
{
  ph_job_t timer;
  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(8);

  is(PH_OK, ph_nbio_init(0));
  is(PH_OK, ph_job_init(&timer));

  timer.callback = record_tick;
  last_tick = ph_time_now();
  is(PH_OK, ph_job_set_timer_at(&timer, last_tick));

  is(PH_OK, ph_sched_run());

  return exit_status();
}
コード例 #5
0
ファイル: sclient.c プロジェクト: DreamDuo/libphenom
int main(int argc, char **argv)
{
  int c;

  // Must be called prior to calling any other phenom functions
  ph_library_init();
  // Initialize OpenSSL and make it safe for multi-threaded use
  ph_library_init_openssl();

  while ((c = getopt(argc, argv, "p:h:")) != -1) {
    switch (c) {
      case 'h':
        addrstring = optarg;
        break;
      case 'p':
        portno = atoi(optarg);
        break;
      default:
        usage();
    }
  }

  if (!addrstring) {
    usage();
  }

  ph_nbio_init(0);
  ph_sock_resolve_and_connect(addrstring, portno,
      NULL, PH_SOCK_CONNECT_RESOLVE_SYSTEM,
      connected, NULL);

  ph_debug_console_start("/tmp/phenom-debug-console");

  // Run
  ph_sched_run();

  return 0;
}
コード例 #6
0
ファイル: echo.c プロジェクト: bigwhite/libphenom
int main(int argc, char **argv)
{
  int c;
  uint16_t portno = 8080;
  char *addrstring = NULL;
  ph_sockaddr_t addr;
  ph_listener_t *lstn;
  bool use_v4 = false;

  // Must be called prior to calling any other phenom functions
  ph_library_init();

  while ((c = getopt(argc, argv, "p:l:4")) != -1) {
    switch (c) {
      case '4':
        use_v4 = true;
        break;
      case 'l':
        addrstring = optarg;
        break;
      case 'p':
        portno = atoi(optarg);
        break;
      default:
        ph_fdprintf(STDERR_FILENO,
            "Invalid parameters\n"
            " -4          - interpret address as an IPv4 address\n"
            " -l ADDRESS  - which address to listen on\n"
            " -p PORTNO   - which port to listen on\n"
        );
        exit(EX_USAGE);
    }
  }

  // Set up the address that we're going to listen on
  if ((use_v4 && ph_sockaddr_set_v4(&addr, addrstring, portno) != PH_OK) ||
      (!use_v4 && ph_sockaddr_set_v6(&addr, addrstring, portno) != PH_OK)) {
    ph_fdprintf(STDERR_FILENO,
        "Invalid address [%s]:%d",
        addrstring ? addrstring : "*",
        portno
    );
    exit(EX_USAGE);
  }

  // Register our memtype
  mt_state = ph_memtype_register(&mt_state_def);

  // Optional config file for tuning internals
  ph_config_load_config_file("/path/to/my/config.json");

  // Enable the non-blocking IO manager
  ph_nbio_init(0);

  ph_log(PH_LOG_ERR, "will listen on `P{sockaddr:%p}", (void*)&addr);

  // This enables a very simple request/response console
  // that allows you to run diagnostic commands:
  // `echo memory | nc -UC /tmp/phenom-debug-console`
  // (on BSD systems, use `nc -Uc`!)
  // The code behind this is in
  // https://github.com/facebook/libphenom/blob/master/corelib/debug_console.c
  ph_debug_console_start("/tmp/phenom-debug-console");

  lstn = ph_listener_new("echo-server", acceptor);
  ph_listener_bind(lstn, &addr);
  ph_listener_enable(lstn, true);

  // Run
  ph_sched_run();

  return 0;
}
コード例 #7
0
ファイル: string.c プロジェクト: DreamDuo/libphenom
int main(int argc, char **argv)
{
  ph_string_t *str, *str2;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(102);

  mt_misc = ph_memtype_register(&mt_def);

  stack_tests();

  // Tests reallocation
  str = ph_string_make_empty(mt_misc, 16);
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(10, ph_string_len(str));
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(20, ph_string_len(str));
  is(memcmp(str->buf, "12345678901234567890", 20), 0);
  ph_string_delref(str);

  // Tests reallocation and string formatting
  str = ph_string_make_empty(mt_misc, 4);
  is(ph_string_printf(str, "Hello %s", "world"), 11);
  is(ph_string_len(str), 11);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%p", (void*)str), 16);
  diag(":%.*s:", str2->len, str2->buf);
  is(ph_string_len(str2), 16);
  is(memcmp(str2->buf, "copy Hello world", 16), 0);
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%d%p", 5, (void*)str), 10);
  is(memcmp(str2->buf, "copy Hello", 10), 0);
  ok(!ph_string_equal(str, str2), "not same");
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  ph_string_append_buf(str2, str->buf, str->len);
  ok(ph_string_equal(str, str2), "same");
  is(ph_string_compare(str, str2), 0);

  ph_string_delref(str);
  ph_string_delref(str2);

  str = ph_string_make_cstr(mt_misc, "abc");
  str2 = ph_string_make_cstr(mt_misc, "bbc");
  ok(ph_string_compare(str, str2) < 0, "abc < bbc");
  ok(ph_string_compare(str2, str) > 0, "abc < bbc");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "abca");
  ok(ph_string_compare(str, str2) < 0, "abc < abca");
  ok(ph_string_compare(str2, str) > 0, "abc < abca");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "ab");
  ok(ph_string_compare(str, str2) > 0, "abc > ab");
  ok(ph_string_compare(str2, str) < 0, "abc > ab");

  ph_string_delref(str2);
  ph_string_delref(str);

  str = ph_string_make_printf(mt_misc, 16, "Hello %d", 42);
  ok(ph_string_equal_cstr(str, "Hello 42"), "same");
  ph_string_delref(str);

  utf16_tests();

  string_stream_tests();

  return exit_status();
}
コード例 #8
0
ファイル: uei_of.c プロジェクト: BlastTNG/flight
void *uei_508_loop(void *m_arg)
{
    struct timespec next;
    uint64_t periodns = ((double)(NSEC_PER_SEC) / 10);
    int ret;
    int channel_list_508[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    uint32_t channel_cfg_508[8] = {
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600),
        CFG_485(DQ_SL501_BAUD_9600)
    };
    int channel_flags_508[8] = { 0 };

    ph_library_init();
    ph_thread_set_name("UEI508");
    blast_startup("Starting UEI 508 loop");

    // set channel configuration
    for (int i = 0; i < 8; i++) {
        if ((ret = DqAdv501SetChannelCfg(hd_508, 4, i, channel_cfg_508[i])) < 0) {
            blast_err("Error in DqAdv501SetChannelCfg()");
        }
    }
    ret = DqRtVmapAddChannel(hd_508, vmapid_508, 4, DQ_SS0IN, channel_list_508, channel_flags_508, 8);
    ret = DqRtVmapAddChannel(hd_508, vmapid_508, 4, DQ_SS0OUT, channel_list_508, channel_flags_508, 8);
    ret = DqRtVmapStart(hd_508, vmapid_508);

    clock_gettime(CLOCK_MONOTONIC, &next);

    while (!shutdown_mcp) {
        for (int i = 0; i < 8; i++) {
            size_t num_bytes;
            ph_buf_t *outbuf = NULL;
            if ((num_bytes = ph_bufq_len(SL508_write_buffer[i]))) {
                outbuf = ph_bufq_consume_bytes(SL508_write_buffer[i], num_bytes);
                DqRtVmapWriteOutput(hd_508, vmapid_508, 4, i, num_bytes, ph_buf_mem(outbuf));
                ph_buf_delref(outbuf);
            }
            DqRtVmapRequestInput(hd_508, vmapid_508, 4, i, 128);
        }
        // Write output data to each TX port FIFO and Read each RX port FIFO
        ret = DqRtVmapRefresh(hd_508, vmapid_508, 0);

        // Read data received during the last refresh
        for (int i = 0; i < 8; i++) {
            uint8_t read_buffer[128];
            int read_len;
            DqRtVmapReadInput(hd_508, vmapid_508, 4, i, sizeof(read_buffer), &read_len, read_buffer);
            if (read_len > 0) {
                ph_bufq_append(SL508_read_buffer[i], read_buffer, read_len, NULL);
            }
        }

        timespec_add_ns(&next, periodns);
        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);
    }

    DqRtVmapStop(hd_508, vmapid_508);
    DqRtVmapClose(hd_508, vmapid_508);
    return NULL;
}