Example #1
0
static int
set_timer(duk_context *duk, int repeat)
{
  es_context_t *ec = es_get(duk);

  es_timer_t *et = es_resource_create(ec, &es_resource_timer, 1);
  int val = duk_require_int(duk, 1);

  es_root_register(duk, 0, et);

  et->et_interval = val * repeat;

  int64_t now = arch_get_ts();
  et->et_expire = now + val * 1000LL;

  hts_mutex_lock(&timer_mutex);

  if(thread_running == 0) {
    thread_running = 1;
    hts_thread_create_detached("estimer", timer_thread, NULL,
                               THREAD_PRIO_MODEL);
  } else {
    hts_cond_signal(&timer_cond);
  }

  LIST_INSERT_SORTED(&timers, et, et_link, estimercmp, es_timer_t);

  hts_mutex_unlock(&timer_mutex);

  es_resource_push(duk, &et->super);
  return 1;
}
Example #2
0
static int
es_sqlite_create(duk_context *ctx)
{
    char path[PATH_MAX];
    char errbuf[512];
    es_context_t *ec = es_get(ctx);
    const char *name = duk_safe_to_string(ctx, 0);

    // Create the db-dir for this plugin

    snprintf(path, sizeof(path), "%s/databases", ec->ec_storage);

    if(fa_makedirs(path, errbuf, sizeof(errbuf)))
        duk_error(ctx, DUK_ERR_ERROR, "Unable to create directory %s -- %s",
                  path, errbuf);

    snprintf(path, sizeof(path), "%s/databases/%s", ec->ec_storage, name);

    sqlite3 *db = db_open(path, 0);
    if(db == NULL)
        duk_error(ctx, DUK_ERR_ERROR, "Unable to open database -- check logs");


    es_sqlite_t *es = es_resource_create(ec, &es_resource_sqlite, 0);

    es->es_db = db;
    es->es_name = strdup(name);

    es_resource_push(ctx, &es->super);
    return 1;
}
Example #3
0
static int
es_file_open(duk_context *ctx)
{
  char errbuf[512];

  es_context_t *ec = es_get(ctx);

  const char *flagsstr = duk_to_string(ctx, 1);
  //  int mode = duk_to_int(ctx, 2);

  int flags;


  if(!strcmp(flagsstr, "r")) {
    flags = 0;
  } else if(!strcmp(flagsstr, "w")) {
    flags = FA_WRITE;
  } else if(!strcmp(flagsstr, "a")) {
    flags = FA_WRITE | FA_APPEND;
  } else {
    duk_error(ctx, DUK_ERR_ERROR, "Invalid flags '%s' to open", flagsstr);
  }

  const char *filename = get_filename(ctx, 0, ec, !!flags);

  fa_handle_t *fh = fa_open_ex(filename, errbuf, sizeof(errbuf), flags, NULL);
  if(fh == NULL)
    duk_error(ctx, DUK_ERR_ERROR, "Unable to open file '%s' -- %s",
              filename, errbuf);

  es_fd_t *efd = es_resource_create(ec, &es_resource_fd, 0);
  efd->efd_path = strdup(filename);
  efd->efd_fh = fh;

  es_resource_push(ctx, &efd->super);
  return 1;
}