Esempio n. 1
0
static int callback(void *notUsed, int argc, char **argv, char **azColName) 
{
  ETERM **record_list;
  int i;

  if (result == 0) {
    result = erl_mk_empty_list();
  }

  record_list = malloc(argc * sizeof(ETERM *));
  
  fprintf(log, "runs %d\n", argc);
  for (i = 0; i < argc; i++) {
    fprintf(log, "%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    if (argv[i]) {
      record_list[i] = erl_mk_string(argv[i]);
    }
    else {
      record_list[i] = erl_mk_empty_list();
    }
  }
  fprintf(log, "\n");
  fflush(log);

  result = erl_cons(erl_mk_tuple(record_list, argc), result);

  free(record_list);
  return 0;
}
Esempio n. 2
0
void handle_subscriber(erlmunk_subscriber *subscriber, element *bodies) {

    element *el;

    int count = 0;
    LL_COUNT(bodies, el, count);
    // DEBUGF(("# bodies in subscriber bounding box: %d", count));

    ETERM **l_array = (ETERM **) malloc(sizeof(ETERM) * count);

    int nth_body = 0;
    LL_FOREACH(bodies, el) {
        cpVect vect = cpBodyGetPosition(el->body);
        float angle = cpBodyGetAngle(el->body);
        // cpVect vel = cpBodyGetVelocity(el->body);
        erlmunk_body_data *data = (erlmunk_body_data *) cpBodyGetUserData(el->body);
        // DEBUGF(("id: %d, x: %f, y: %f, angle: %f, vel.x: %f, vel.y: %f, data: %p",
        //     data->id, vect.x, vect.y, angle, vel.x, vel.y, data));

        ETERM **t_array = (ETERM **) malloc(sizeof(ETERM) * 4);
        t_array[0] = erl_mk_float(vect.x);
        t_array[1] = erl_mk_float(vect.y);
        t_array[2] = erl_mk_float(angle);
        if (data->term == NULL)
            t_array[3] = erl_mk_undefined();
        else
            t_array[3] = erl_copy_term(data->term);
        ETERM *tuple = erl_mk_tuple(t_array, 4);
        free(t_array);

        ETERM *prop_value = erl_mk_int_prop_value(data->id, tuple);
        l_array[nth_body++] = prop_value;
    }
Esempio n. 3
0
ETERM *
make_row()
{
  ETERM **rowtup, *rc;
  unsigned int i;

  rowtup = (ETERM **)safe_malloc(numfields * sizeof(ETERM *));
  for (i = 0; i < numfields; i++) {
    if (*r_bind[i].is_null)
      rowtup[i] = erl_mk_atom("null");
    else
      rowtup[i] = erl_mk_estring(r_bind[i].buffer, *r_bind[i].length);
  }

  rc = erl_mk_tuple(rowtup, numfields);
  if (rc == NULL) {
    ETERM *resp;

    resp = erl_format("{error, {erl_mk_tuple, ~i}}", numfields);
    write_msg(resp);
    erl_free_term(resp);
    exit(3);
  }

  for (i = 0; i < numfields; i++)
    erl_free_term(rowtup[i]);
  free(rowtup);

  return rc;
}
Esempio n. 4
0
ETERM *body_get_user_data(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    erlmunk_body_data *data = cpBodyGetUserData(b->body);
    ETERM *datap = erl_copy_term(data->term);

    ETERM *atom_ok = erl_mk_atom("ok");
    ETERM **body_get_data_array = (ETERM **) malloc(sizeof(ETERM*) * 2);
    body_get_data_array[0] = atom_ok;
    body_get_data_array[1] = datap;
    ETERM *body_get_data_tuple = erl_mk_tuple(body_get_data_array, 2);
    free(body_get_data_array);

    ETERM *reply_tuple = erl_mk_reply(fromp, body_get_data_tuple);
    ETERM *gen_cast_tuple = erl_mk_gen_cast(reply_tuple);

    return gen_cast_tuple;
}
Esempio n. 5
0
ETERM *body_get_position(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s = NULL;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpVect position = cpBodyGetPosition(b->body);
    float angle = cpBodyGetAngle(b->body);

    ETERM **position_tuple_array = (ETERM **) malloc(sizeof(ETERM*) * 2);
    position_tuple_array[0] = erl_mk_float(position.x);
    position_tuple_array[1] = erl_mk_float(position.y);
    ETERM *position_tuple = erl_mk_tuple(position_tuple_array, 2);
    free(position_tuple_array);

    ETERM *atom_ok = erl_mk_atom("ok");
    ETERM **get_position_array = (ETERM **) malloc(sizeof(ETERM*) * 3);
    get_position_array[0] = atom_ok;
    get_position_array[1] = position_tuple;
    get_position_array[2] = erl_mk_float(angle);
    ETERM *get_position_tuple = erl_mk_tuple(get_position_array, 3);
    free(get_position_array);

    ETERM *reply_tuple = erl_mk_reply(fromp, get_position_tuple);
    ETERM *gen_cast_tuple = erl_mk_gen_cast(reply_tuple);

    // DEBUGF(("body_get_position(body: %d, x: %f, y: %f, angle: %f) has succeeded",
    //     body_id, position.x, position.y, angle));

    return gen_cast_tuple;
}
Esempio n. 6
0
static cpBool
handle_collision(cpArbiter *arbiter, cpSpace *space, cpDataPointer user_data)
{
    CP_ARBITER_GET_BODIES(arbiter, b1, b2);
    erlmunk_subscriber *subscriber = (erlmunk_subscriber *) user_data;

    erlmunk_body_data *data1 = cpBodyGetUserData(b1);
    erlmunk_body_data *data2 = cpBodyGetUserData(b2);

    // DEBUGF(("collision detected between %d and %d\n", data1->id, data2->id));

    ETERM **data1_array = (ETERM **) malloc(sizeof(ETERM) * 2);
    data1_array[0] = erl_mk_int(data1->id);
    data1_array[1] = data1->term;
    ETERM *data1_term = erl_mk_tuple(data1_array, 2);
    free(data1_array);

    ETERM **data2_array = (ETERM **) malloc(sizeof(ETERM) * 2);
    data2_array[0] = erl_mk_int(data2->id);
    data2_array[1] = data2->term;
    ETERM *data2_term = erl_mk_tuple(data2_array, 2);
    free(data2_array);

    ETERM *a = erl_mk_atom("erlmunk_collision");
    ETERM **data_array = (ETERM **) malloc(sizeof(ETERM) * 3);
    data_array[0] = a;
    data_array[1] = data1_term;
    data_array[2] = data2_term;
    ETERM *data = erl_mk_tuple(data_array, 3);
    free(data_array);

    ETERM *gen_cast = erl_mk_gen_cast(data);

    if (erl_send(subscriber->client->fd, subscriber->from, gen_cast) != 1) {
        DEBUGF(("failed to send data to subscriber"));
    }

    return cpFalse;
}
Esempio n. 7
0
void send_error(char *err_msg) 
{
  ETERM *tup_list[2];
  ETERM *to_send;
	
  tup_list[0] = erl_mk_atom("sql_error");
  tup_list[1] = erl_mk_string(err_msg);
  to_send = erl_mk_tuple(tup_list, 2);

  fprintf(log, "SQL Error: %s\n", err_msg);
  respond(to_send);

  erl_free_term(tup_list[0]);
  erl_free_term(tup_list[1]);
  erl_free_compound(to_send);
}
Esempio n. 8
0
void local_add_watch(ETERM* args)
{
  ETERM *pathp = erl_element(1, args);
  int pathLength = ERL_BIN_SIZE(pathp);
  char *path = ERL_BIN_PTR(pathp);

  ETERM *notifyFilterp = erl_element(2, args);
  unsigned int notifyFilter_ui = ERL_INT_UVALUE(notifyFilterp);
  long notifyFilter = (long)notifyFilter_ui;

  ETERM *watchSubdirsp = erl_element(3, args);
  int watchSubdirs = ERL_INT_VALUE(watchSubdirsp);

  int watchID = eNotify_addWatch(path, pathLength, notifyFilter, watchSubdirs);

  // Prepare response
  ETERM *tuplep;
  ETERM *tupleArray[2];

  if (watchID < 0)
  {
	  long errorCode = (long)(-watchID);
	  char errorDesc[1024];
	  eNotify_getErrorDesc(errorCode, errorDesc, 1024);
	  tupleArray[0] = erl_mk_atom("error");
	  tupleArray[1] = erl_mk_string(errorDesc);
  }
  else if (0 == watchID)
  {
	  // this may happen if there's a problem converting
	  // a path from utf8 to UTF16
	  tupleArray[0] = erl_mk_atom("error");
	  tupleArray[1] = erl_mk_string("internal_error");
  }
  else
  {
	  tupleArray[0] = erl_mk_atom("ok");
	  tupleArray[1] = erl_mk_int(watchID);
  }

  char buf[1024];
  tuplep = erl_mk_tuple(tupleArray, 2);
  erl_encode(tuplep, buf);
  write_cmd(buf, erl_term_len(tuplep));

  erl_free_array(tupleArray, 2); // free contents from tupleArray
}
Esempio n. 9
0
static ETERM*
all_types(void)
{
    ETERM* t;
    ETERM* terms[3];
    int i;
    static char a_binary[] = "A binary";

#define CONS_AND_FREE(expr, tail) \
  do { \
    ETERM* term = expr; \
    ETERM* nl = erl_cons(term, tail); \
    erl_free_term(term); \
    erl_free_term(tail); \
    tail = nl; \
  } while (0)

    t = erl_mk_empty_list();

    CONS_AND_FREE(erl_mk_atom("I am an atom"), t);
    CONS_AND_FREE(erl_mk_binary("A binary", sizeof(a_binary)-1), t);
    CONS_AND_FREE(erl_mk_float(3.0), t);
    CONS_AND_FREE(erl_mk_int(0), t);
    CONS_AND_FREE(erl_mk_int(-1), t);
    CONS_AND_FREE(erl_mk_int(1), t);

    CONS_AND_FREE(erl_mk_string("A string"), t);

    terms[0] = erl_mk_atom("element1");
    terms[1] = erl_mk_int(42);
    terms[2] = erl_mk_int(767);
    CONS_AND_FREE(erl_mk_tuple(terms, ASIZE(terms)), t);
    for (i = 0; i < ASIZE(terms); i++) {
	erl_free_term(terms[i]);
    }

    CONS_AND_FREE(erl_mk_pid("kalle@localhost", 3, 2, 1), t);
    CONS_AND_FREE(erl_mk_pid("abcdefghijabcdefghij@localhost", 3, 2, 1), t);
    CONS_AND_FREE(erl_mk_port("kalle@localhost", 4, 1), t);
    CONS_AND_FREE(erl_mk_port("abcdefghijabcdefghij@localhost", 4, 1), t);
    CONS_AND_FREE(erl_mk_ref("kalle@localhost", 6, 1), t);
    CONS_AND_FREE(erl_mk_ref("abcdefghijabcdefghij@localhost", 6, 1), t);
    return t;

#undef CONS_AND_FREE
}
Esempio n. 10
0
void eNotifyCallback(int watchID, int action, const void* rootPath, int rootPathLength, const void* filePath, int filePathLength)
{
 // MAX_FILE_PATHNAME_LENGTH * 2 because we are passing 2 paths
  byte buf[MAX_FILE_PATHNAME_LENGTH*2];
  ETERM *tuplep;

  // Build response
  ETERM *tupleArray[4];
  tupleArray[0] = erl_mk_int(watchID);
  tupleArray[1] = erl_mk_int(action);
  tupleArray[2] = erl_mk_binary(rootPath, rootPathLength);
  tupleArray[3] = erl_mk_binary(filePath, filePathLength);
  tuplep = erl_mk_tuple(tupleArray, 4);

  erl_encode(tuplep, buf);
  write_cmd(buf, erl_term_len(tuplep));

  // free contents from tupleArray
  erl_free_array(tupleArray, 4);
}
Esempio n. 11
0
ETERM *body_update_user_data(ETERM *fromp, ETERM *argp) {

    // DEBUGF(("body_update_user_data\n"));

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *keyp = erl_element(3, argp);
    ETERM *valuep = erl_element(4, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    erlmunk_body_data *data = cpBodyGetUserData(b->body);

    ETERM *new_data_term = erl_lists_keyreplace(data->term, keyp, valuep);
    erl_free_compound(data->term);
    data->term = new_data_term;

    ETERM *new_valuep = erl_proplists_get_value(keyp, new_data_term);

    // obtain updated key value and return that
    ETERM *atom_ok = erl_mk_atom("ok");
    ETERM **body_update_data_array = (ETERM **) malloc(sizeof(ETERM*) * 2);
    body_update_data_array[0] = atom_ok;
    body_update_data_array[1] = new_valuep;
    ETERM *body_update_data_tuple = erl_mk_tuple(body_update_data_array, 2);
    free(body_update_data_array);

    ETERM *reply_tuple = erl_mk_reply(fromp, body_update_data_tuple);
    ETERM *gen_cast_tuple = erl_mk_gen_cast(reply_tuple);

    return gen_cast_tuple;
}
Esempio n. 12
0
ETERM *space_new(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *iterationsp = erl_element(1, argp);
    ETERM *gravityp = erl_element(2, argp);
    ETERM *gravityxp = erl_element(1, gravityp);
    ETERM *gravityyp = erl_element(2, gravityp);

    // create the new space
    cpSpace *space = cpSpaceNew();
    cpSpaceSetIterations(space, ERL_INT_VALUE(iterationsp));
    cpSpaceSetGravity(space, cpv(ERL_FLOAT_VALUE(gravityxp),
                                 ERL_FLOAT_VALUE(gravityyp)));
    cpSpaceSetSleepTimeThreshold(space, 5.0);

    // add it to the hash table
    ETERM *ref = erl_mk_node_ref();
    erlmunk_space *s = (erlmunk_space *) malloc(sizeof(erlmunk_space));
    s->id = ERL_REF_NUMBER(ref);
    s->space = space;
    s->subscriber_count = 0;
    s->subscribers = NULL;
    s->bodies = NULL;
    HASH_ADD_INT(erlmunk_spaces, id, s);

    ETERM *atom_ok = erl_mk_atom("ok");
    ETERM **space_new_array = (ETERM **) malloc(sizeof(ETERM*) * 2);
    space_new_array[0] = atom_ok;
    space_new_array[1] = ref;
    ETERM *space_new_tuple = erl_mk_tuple(space_new_array, 2);
    free(space_new_array);

    ETERM *reply_tuple = erl_mk_reply(fromp, space_new_tuple);
    ETERM *gen_cast_tuple = erl_mk_gen_cast(reply_tuple);

    return gen_cast_tuple;
}
Esempio n. 13
0
        else
            t_array[3] = erl_copy_term(data->term);
        ETERM *tuple = erl_mk_tuple(t_array, 4);
        free(t_array);

        ETERM *prop_value = erl_mk_int_prop_value(data->id, tuple);
        l_array[nth_body++] = prop_value;
    }

    ETERM *a = erl_mk_atom("erlmunk_update");
    ETERM *l = erl_mk_list(l_array, count);
    free(l_array);
    ETERM **data_array = (ETERM **) malloc(sizeof(ETERM) * 2);
    data_array[0] = a;
    data_array[1] = l;
    ETERM *data = erl_mk_tuple(data_array, 2);
    free(data_array);
    ETERM *gen_cast = erl_mk_gen_cast(data);

    if (erl_send(subscriber->client->fd, subscriber->from, gen_cast) != 1) {
        DEBUGF(("failed to send data to subscriber"));
    }

    erl_free_compound(gen_cast);
}

void handle_space_subscribers(erlmunk_space *s) {

    for(erlmunk_subscriber *subscriber = s->subscribers;
        subscriber != NULL;
        subscriber = subscriber->hh.next) {
Esempio n. 14
0
int main(void)
#endif
{
  ei_x_buff eix;
  int index = 0;
  ETERM **etermpp = NULL, *etermp = NULL;
  char *charp = NULL;
  unsigned char uchar, **ucharpp = NULL, *ucharp = NULL;
  void *voidp = NULL;
  Erl_Heap *erl_heapp = NULL;
  int intx = 0;
  int *intp = NULL;
  unsigned int uintx, *uintp;
  unsigned long *ulongp = NULL;
  long longx = 0;
  double doublex = 0.0;
  short shortx = 42;
  FILE *filep = NULL;
  Erl_IpAddr erl_ipaddr = NULL;
  ErlMessage *erlmessagep = NULL;
  ErlConnect *erlconnectp = NULL;
  struct hostent *hostp = NULL;
  struct in_addr *inaddrp = NULL;

  /* Converion to erl_interface format is in liberl_interface */

  intx = erl_errno;

  ei_encode_term(charp, &index, voidp);
  ei_x_encode_term(&eix, voidp);
  ei_decode_term(charp, &index, voidp);

  erl_init(voidp, longx);
  erl_connect_init(intx, charp,shortx);
  erl_connect_xinit(charp,charp,charp,erl_ipaddr,charp,shortx);
  erl_connect(charp); 
  erl_xconnect(erl_ipaddr,charp);
  erl_close_connection(intx);
  erl_receive(intx, ucharp, intx);
  erl_receive_msg(intx, ucharp, intx, erlmessagep);
  erl_xreceive_msg(intx, ucharpp, intp, erlmessagep);
  erl_send(intx, etermp, etermp);
  erl_reg_send(intx, charp, etermp);
  erl_rpc(intx,charp,charp,etermp);
  erl_rpc_to(intx,charp,charp,etermp);
  erl_rpc_from(intx,intx,erlmessagep);

  erl_publish(intx);
  erl_accept(intx,erlconnectp);

  erl_thiscookie();
  erl_thisnodename();
  erl_thishostname();
  erl_thisalivename();
  erl_thiscreation();
  erl_unpublish(charp);
  erl_err_msg(charp);
  erl_err_quit(charp);
  erl_err_ret(charp);
  erl_err_sys(charp);

  erl_cons(etermp,etermp);
  erl_copy_term(etermp);
  erl_element(intx,etermp);

  erl_hd(etermp);
  erl_iolist_to_binary(etermp);
  erl_iolist_to_string(etermp);
  erl_iolist_length(etermp);
  erl_length(etermp);
  erl_mk_atom(charp);
  erl_mk_binary(charp,intx);
  erl_mk_empty_list();
  erl_mk_estring(charp, intx);
  erl_mk_float(doublex);
  erl_mk_int(intx);
  erl_mk_list(etermpp,intx);
  erl_mk_pid(charp,uintx,uintx,uchar);
  erl_mk_port(charp,uintx,uchar);
  erl_mk_ref(charp,uintx,uchar);
  erl_mk_long_ref(charp,uintx,uintx,uintx,uchar);
  erl_mk_string(charp);
  erl_mk_tuple(etermpp,intx);
  erl_mk_uint(uintx);
  erl_mk_var(charp);
  erl_print_term(filep,etermp);
  /*  erl_sprint_term(charp,etermp); */
  erl_size(etermp);
  erl_tl(etermp);
  erl_var_content(etermp, charp);

  erl_format(charp);
  erl_match(etermp, etermp);

  erl_global_names(intx, intp);
  erl_global_register(intx, charp, etermp);
  erl_global_unregister(intx, charp);
  erl_global_whereis(intx, charp, charp);

  erl_init_malloc(erl_heapp,longx);
  erl_alloc_eterm(uchar);
  erl_eterm_release();
  erl_eterm_statistics(ulongp,ulongp);
  erl_free_array(etermpp,intx);
  erl_free_term(etermp);
  erl_free_compound(etermp);
  erl_malloc(longx);
  erl_free(voidp);

  erl_compare_ext(ucharp, ucharp);
  erl_decode(ucharp);
  erl_decode_buf(ucharpp);
  erl_encode(etermp,ucharp);
  erl_encode_buf(etermp,ucharpp);
  erl_ext_size(ucharp);
  erl_ext_type(ucharp);
  erl_peek_ext(ucharp,intx);
  erl_term_len(etermp);

  erl_gethostbyname(charp);
  erl_gethostbyaddr(charp, intx, intx);
  erl_gethostbyname_r(charp, hostp, charp, intx, intp);
  erl_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp);

  erl_init_resolve();
  erl_distversion(intx);

  erl_epmd_connect(inaddrp);
  erl_epmd_port(inaddrp, charp, intp);

  charp  = ERL_ATOM_PTR(etermp);
  intx   = ERL_ATOM_SIZE(etermp);
  ucharp = ERL_BIN_PTR(etermp);
  intx   = ERL_BIN_SIZE(etermp);
  etermp = ERL_CONS_HEAD(etermp);
  etermp = ERL_CONS_TAIL(etermp);
  intx   = ERL_COUNT(etermp);
  doublex= ERL_FLOAT_VALUE(etermp);
  uintx  = ERL_INT_UVALUE(etermp);
  intx   = ERL_INT_VALUE(etermp);
  intx   = ERL_IS_ATOM(etermp);
  intx   = ERL_IS_BINARY(etermp);
  intx   = ERL_IS_CONS(etermp);
  intx   = ERL_IS_EMPTY_LIST(etermp);
  intx   = ERL_IS_FLOAT(etermp);
  intx   = ERL_IS_INTEGER(etermp);
  intx   = ERL_IS_LIST(etermp);
  intx   = ERL_IS_PID(etermp);
  intx   = ERL_IS_PORT(etermp);
  intx   = ERL_IS_REF(etermp);
  intx   = ERL_IS_TUPLE(etermp);
  intx   = ERL_IS_UNSIGNED_INTEGER(etermp);
  uchar  = ERL_PID_CREATION(etermp);
  charp  = ERL_PID_NODE(etermp);
  uintx  = ERL_PID_NUMBER(etermp);
  uintx  = ERL_PID_SERIAL(etermp);
  uchar  = ERL_PORT_CREATION(etermp);
  charp  = ERL_PORT_NODE(etermp);
  uintx  = ERL_PORT_NUMBER(etermp);
  uchar  = ERL_REF_CREATION(etermp);
  intx   = ERL_REF_LEN(etermp);
  charp  = ERL_REF_NODE(etermp);
  uintx  = ERL_REF_NUMBER(etermp);
  uintp  = ERL_REF_NUMBERS(etermp);
  etermp = ERL_TUPLE_ELEMENT(etermp,intx);
  intx   = ERL_TUPLE_SIZE(etermp);

  return 
      BUFSIZ +
      EAGAIN +
      EHOSTUNREACH +
      EINVAL +
      EIO +
      EMSGSIZE +
      ENOMEM +
      ERL_ATOM +
      ERL_BINARY +
      ERL_ERROR +
      ERL_EXIT +
      ERL_FLOAT +
      ERL_INTEGER +
      ERL_LINK +
      ERL_LIST +
      ERL_MSG +
      ERL_NO_TIMEOUT +
      ERL_PID +
      ERL_PORT +
      ERL_REF +
      ERL_REG_SEND +
      ERL_SEND +
      ERL_SMALL_BIG +
      ERL_TICK +
      ERL_TIMEOUT +
      ERL_TUPLE +
      ERL_UNLINK +
      ERL_U_INTEGER +
      ERL_U_SMALL_BIG +
      ERL_VARIABLE +
      ETIMEDOUT +
      MAXNODELEN +
      MAXREGLEN;
}
Esempio n. 15
0
int main(int argc, char* argv[]) {
  ETERM *tuplep, *fnp, *pathp, *binp, *widthp, *heightp;
  ETERM *res_tuplep;
  ETERM *res_arrp[2];
  ETERM *res_ok_atomp, *res_error_atomp, *res_error_cause_atomp;

  int idx = 1;
  byte *buf = malloc(INIT_BUF_SIZE);
  int buf_len = INIT_BUF_SIZE;
  int res_len;

  erl_init(NULL, 0);

  //Create common atoms(ok, error)
  res_ok_atomp = erl_mk_atom("ok");
  res_error_atomp = erl_mk_atom("error");
  res_error_cause_atomp = erl_mk_atom("resize failed");

  while (read_cmd(&buf, &buf_len) > 0) {
    tuplep = erl_decode(buf);
    fnp     = erl_element(idx++, tuplep);
    pathp   = erl_element(idx++, tuplep);
    binp    = erl_element(idx++, tuplep);
    widthp  = erl_element(idx++, tuplep);
    heightp = erl_element(idx++, tuplep);
    
    if (strncmp(ERL_ATOM_PTR(fnp), "resize", 6) == 0) {
      char* algop = ERL_ATOM_PTR(fnp) + 6;
      resizerl_handler* rhp = (strncmp(algop, "_opencv", 7) == 0) ? resizerl_impls[0] : NULL;
      if (rhp == NULL) {
        exit(1);
      }
      // ERL_INT_VALUE, ERL_BIN_PTR, ERL_BIN_SIZE
      void* dp = NULL;
      rhp->exec(ERL_BIN_PTR(binp),
                ERL_BIN_SIZE(binp),
                ERL_BIN_PTR(pathp),
                ERL_BIN_SIZE(pathp),
                ERL_INT_VALUE(widthp),
                ERL_INT_VALUE(heightp), &dp);
      if (dp == NULL) {
        exit(2);
      }
      unsigned char* dst;
      int dst_size;
      rhp->get_result(dp, &dst, &dst_size);
      res_arrp[0] = res_ok_atomp;
      res_arrp[1] = erl_mk_binary((const char*)dst, dst_size);
      res_tuplep = erl_mk_tuple(res_arrp, 2);
      res_len = erl_term_len(res_tuplep);
      if (res_len > buf_len) {
        byte* new_buf = (byte*)realloc((void*)buf, res_len);
        if (new_buf == NULL) {
          exit(3);
        }
        buf = new_buf;
        buf_len = res_len;
      }
      erl_encode(res_tuplep, buf);
      write_cmd(buf, erl_term_len(res_tuplep));
      erl_free_term(res_arrp[1]);
      erl_free_term(res_tuplep);
      rhp->release(&dp);
    } else {
      res_arrp[0] = res_error_atomp;
      res_arrp[1] = res_error_cause_atomp;
      res_tuplep = erl_mk_tuple(res_arrp, 2);
      if (res_len > buf_len) {
        byte* new_buf = (byte*)realloc((void*)buf, res_len);
        if (new_buf == NULL) {
          exit(4);
        }
        buf = new_buf;
        buf_len = res_len;
      }
      erl_encode(res_tuplep, buf);
      write_cmd(buf, erl_term_len(res_tuplep));
      erl_free_term(res_tuplep);
    }

    erl_free_compound(tuplep);
    erl_free_term(fnp);
    erl_free_term(pathp);
    erl_free_term(binp);
    erl_free_term(widthp);
    erl_free_term(heightp);
    idx = 1;
  }
  // normal
  return 0;

}
Esempio n. 16
0
ETERM *py_to_eterm(PyObject * pobj) {
	int i;
	int count;
	PyObject *pobj2;
	ETERM *eobj = NULL;
	ETERM *eobj2 = NULL;
	ETERM **eobj3;

	if (pobj == NULL) {
		return erl_mk_empty_list();
	}

	if (PyString_Check(pobj)) {
		eobj = erl_mk_atom(PyString_AsString(pobj));
	}
	else if (PyInt_Check(pobj)) {
		eobj = erl_mk_int(PyInt_AsLong(pobj));
	}
	else if (PyList_Check(pobj)) {
		eobj = erl_mk_empty_list();
		for (i = PyList_Size(pobj) - 1; i >= 0; i--) {
			pobj2 = PyList_GetItem(pobj, i);
			eobj2 = py_to_eterm(pobj2);
			eobj = erl_cons(eobj2, eobj);
		}
	}
	else if (PyDict_Check(pobj)) {
		// a pid
		char *er_node;
		int er_number, er_serial, er_creation;
		pobj2 = PyDict_GetItemString(pobj, "node");
		if (!pobj2) {
			PyErr_Print();
			goto clear;
		}
		if (!PyString_Check(pobj2)) {
			goto clear;
		}
		er_node = PyString_AsString(pobj2);

		pobj2 = PyDict_GetItemString(pobj, "number");
		if (!pobj2) {
			PyErr_Print();
			goto clear;
		}
		if (!PyInt_Check(pobj2)) {
			goto clear;
		}
		er_number = PyInt_AsLong(pobj2);

		pobj2 = PyDict_GetItemString(pobj, "serial");
		if (!pobj2) {
			PyErr_Print();
			goto clear;
		}
		if (!PyInt_Check(pobj2)) {
			goto clear;
		}
		er_serial = PyInt_AsLong(pobj2);

		pobj2 = PyDict_GetItemString(pobj, "creation");
		if (!pobj2) {
			PyErr_Print();
			goto clear;
		}
		if (!PyInt_Check(pobj2)) {
			goto clear;
		}
		er_creation = PyInt_AsLong(pobj2);

		eobj = erl_mk_pid(er_node, er_number, er_serial, er_creation);
	}
	else if (PyTuple_Check(pobj)) {
		count = PyTuple_Size(pobj);
		eobj3 = malloc(sizeof(ETERM *) * count);
		for (i = 0; i < count; i++) {
			pobj2 = PyTuple_GetItem(pobj, i);
			if (!pobj2) {
				break;
			}
			eobj3[i] = py_to_eterm(pobj2);
		}
		eobj = erl_mk_tuple(eobj3, count);
		free(eobj3);
	}
	else {
		fprintf(stderr, "UNMANAGED PYTHON TYPE: %s\n", pobj->ob_type->tp_name);
	}

      clear:
	if (eobj == NULL) {
		return erl_mk_empty_list();
	}

	return eobj;
}