Beispiel #1
0
jnx_size jnx_socket_ipc_send(jnx_ipc_socket *s,
                             jnx_uint8 *msg, jnx_size msg_len) {
  JNXCHECK(s);
  JNXCHECK(msg);
  JNXCHECK(msg_len);
  JNXCHECK(s->isclosed == 0);

  if(!s->isconnected) {
    if(connect(s->socket,
               (struct sockaddr *)&(s->address),
               sizeof(struct sockaddr_un)) == -1) {
      perror("jnx IPC socket connect");
      return 0;
    }
    s->isconnected = 1;
  }

  jnx_size tbytes = 0;
  jnx_size rbytes = msg_len;

  while(tbytes < rbytes) {
    jnx_size n = write(s->socket,msg,rbytes);
    if(n == -1) {
      perror("send:");
      return 0;
    }
    tbytes +=n;
    rbytes = msg_len - tbytes;
  }
  return tbytes;
}
Beispiel #2
0
void* jnx_hash_get(jnx_hashmap* hashmap, const jnx_char* key) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  jnx_int32 index = jnx_hash_string(key, hashmap->size);

  if (hashmap->data[index].used) {
    if (hashmap->data[index].bucket_len == 1) {
      jnx_node* head = hashmap->data[index].bucket->head;
      jnx_hash_bucket_el* bucketel = head->_data;
      if (strcmp(bucketel->origin_key, key) == 0) {
        return (void*)bucketel->origin_value;
      }
    }
    if (hashmap->data[index].bucket_len > 1) {
      jnx_node* head = hashmap->data[index].bucket->head;
      jnx_node* rewind_head = head;
      while (head) {
        jnx_hash_bucket_el* bucketel = head->_data;
        if (strcmp(bucketel->origin_key, key) == 0) {
          hashmap->data[index].bucket->head = rewind_head;
          return (void*)bucketel->origin_value;
        }
        head = head->next_node;
      }
      hashmap->data[index].bucket->head = rewind_head;
    }
  }
  return NULL;
}
Beispiel #3
0
jnx_ipc_listener* jnx_socket_ipc_listener_create(jnx_ipc_socket *s,
                                                 jnx_int max_connections) {
  JNXCHECK(s);
  JNXCHECK(max_connections <= 200);
  jnx_int on = 1;
  jnx_ipc_listener *l = malloc(sizeof(jnx_ipc_listener));
  l->socket = s;
  l->hint_exit = 0;
  l->poll_timeout = 500;
  l->nfds = 1;

  ioctl(l->socket->socket,FIONBIO,(char*)&on);
  // attempt bind
  if (bind_ipc_socket(s) == -1) {
    jnx_socket_ipc_listener_destroy(&l);
    return NULL;
  }
  // attempt listen
  if (listen_on_ipc_socket(s, max_connections) == -1) {
    jnx_socket_ipc_listener_destroy(&l);
    return NULL;
  }
  l->ufds[0].fd = l->socket->socket;
  l->ufds[0].events = POLLIN;

  return l;
}
int test_jnxexceptions()
{
  JNXLOG(LDEBUG,"Running test_jnxexception tests");

  int ar[3] = { -1,-2,-3};
  int c;
  for(c=0;c<3;++c) {

    int current_exception = ar[c];
    jnx_try {
      jnx_throw( ar[c] );
    }
    jnx_catch( JNX_EXCEPTION ) {
      JNXLOG(LDEBUG,"Default system exception fired correctly");
      JNXCHECK(current_exception == JNX_EXCEPTION);
      break;
    }
    jnx_catch( FOO_EXCEPTION ) {
      JNXLOG(LDEBUG,"First exception fired correctly");
      JNXCHECK(current_exception == FOO_EXCEPTION);
      break;
    }
    jnx_catch( BAR_EXCEPTION ) {
      JNXLOG(LDEBUG,"Second exception fired correctly");
      JNXCHECK(current_exception == BAR_EXCEPTION);
      break;
    }
    jnx_finally {
      JNXLOG(LDEBUG,"Hit finally clause");
    }
    jnx_try_end
  }
  return 0;
}
Beispiel #5
0
void* jnx_hash_get_ts(jnx_hashmap* hashmap, const jnx_char* key) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  jnx_thread_lock(hashmap->internal_lock);
  void *ret = jnx_hash_get(hashmap,key);
  jnx_thread_unlock(hashmap->internal_lock);
  return ret;
}
Beispiel #6
0
void* jnx_hash_delete_value_ts(jnx_hashmap *hashmap,jnx_char *key) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  jnx_thread_lock(hashmap->internal_lock);
  void *ret = jnx_hash_delete_value(hashmap,key);
  jnx_thread_unlock(hashmap->internal_lock);
  return ret;
}
Beispiel #7
0
void *jnx_btree_lookup(jnx_btree *tree, void *key) {
  JNXCHECK(tree);
  JNXCHECK(key);
  if ( tree == NULL ) {
    return NULL;
  }
  return find_value_for_key_in_node(tree, tree->root, key);
}
Beispiel #8
0
void jnx_btree_remove_ts(jnx_btree *tree, void *key_in, void** key_out, void **val_out ) {
  if(!tree) { return ; }
  JNXCHECK(tree);
  JNXCHECK(key_in);
  jnx_thread_lock(tree->internal_lock);
  jnx_btree_remove(tree,key_in,key_out,val_out);
  jnx_thread_unlock(tree->internal_lock);
}
Beispiel #9
0
void test_contains(){
  JNX_LOG(NULL,"- test_stack_contains");
  jnx_stack *s = jnx_stack_create();
  jnx_stack_push(s,ar[0]);
  JNXCHECK(jnx_stack_contains(s,ar[0],compare_func) == 1);
  JNXCHECK(jnx_stack_contains(s,ar[1],compare_func) == 0);
  jnx_term_printf_in_color(JNX_COL_GREEN, "  OK\n");
}
Beispiel #10
0
int test_setting_peer_update_interval(discovery_service *svc) {
    JNXCHECK(peer_update_interval == 10);
    peer_update_interval = 20;
    JNXCHECK(peer_update_interval == 20);
    peer_update_interval = 10;
    JNXCHECK(peer_update_interval == 10);
    return CLEANUP;
}
Beispiel #11
0
jnx_int32 jnx_hash_string(const jnx_char* str, jnx_int32 map_size) {
    JNXCHECK(str);
    JNXCHECK(map_size > 0);
    jnx_ulong hash = 5381;
    jnx_int c;
    while ((c = *str++))
        hash = ((hash << 5) + hash) + c;
    return hash % map_size;
}
Beispiel #12
0
jnx_int32 jnx_hash_put_ts(jnx_hashmap* hashmap, const jnx_char* key, void* value) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  JNXCHECK(value);
  jnx_thread_lock(hashmap->internal_lock);
  jnx_int32 ret = jnx_hash_put(hashmap,key,value);
  jnx_thread_unlock(hashmap->internal_lock);
  return ret;
}
Beispiel #13
0
int test_stopping_service(discovery_service *svc) {
    test_starting_service(svc);
    discovery_service_stop(svc);
    JNXCHECK(svc->udp_listener == LDEBUG);
    JNXCHECK(svc->sock_send == 0);
    JNXCHECK(svc->isrunning == 0);
    list_message_received = 0;
    return CLEANUP;
}
Beispiel #14
0
void *jnx_btree_lookup_ts(jnx_btree *tree, void *key) {
  JNXCHECK(tree);
  JNXCHECK(key);
  if(!tree) { return NULL; }
  jnx_thread_lock(tree->internal_lock);	
  void *ret = jnx_btree_lookup(tree,key);
  jnx_thread_unlock(tree->internal_lock);	
  return ret;
}
Beispiel #15
0
void jnx_btree_add_ts(jnx_btree *tree, void *key, void *value) {
  JNXCHECK(tree);
  JNXCHECK(key);
  JNXCHECK(value);
  if(!tree) { return ; }
  jnx_thread_lock(tree->internal_lock);	
  jnx_btree_add(tree,key,value);
  jnx_thread_unlock(tree->internal_lock);	
}
Beispiel #16
0
void test_initiator() {
  JNXLOG(NULL,"test_linking");
  session_service *service = session_service_create(linking_test_procedure,
      unlinking_test_procedure);
  session *os;
  session_state e = session_service_create_session(service,&os);
  JNXCHECK(session_service_session_is_linked(service,&os->session_guid) == 0);
  //Lets generate the guid of some remote session
  jnx_guid h;
  jnx_guid_create(&h);

  peerstore *store = peerstore_init(local_peer_for_user("initiator_bob",10,interface), 0);

  get_broadcast_ip(&baddr,interface);
  printf("%s\n", baddr);
  discovery_service *ds = discovery_service_create(1234, AF_INET, baddr, store);

  discovery_service_start(ds,BROADCAST_UPDATE_STRATEGY);

  int remote_peers = 0;
  jnx_guid **active_guids;
  peer *local = peerstore_get_local_peer(store);
  peer *remote_peer = NULL;
  while(!remote_peers) {
    int num_guids = peerstore_get_active_guids(store,&active_guids);
    int i;
    for(i=0;i<num_guids;i++) {
      jnx_guid *guid = active_guids[i];
      peer *p = peerstore_lookup(store,guid);
      if(peers_compare(p,local) != 0) {
        printf("Found a remote peer! Breaking!\n");
        remote_peers = 1;
        remote_peer = p;
        break;
      }
    }
  }
  JNXCHECK(session_is_active(os) == 0);

  session_service_link_sessions(service,E_AM_INITIATOR,
      ds,&(*os).session_guid,local,remote_peer);

  JNXCHECK(session_is_active(os) == 1);

  printf("-------------------------------------\n");
  session_message_write(os,"Hello Ballface! what's going on!");
  printf("-------------------------------------\n");
  printf("Sessions linked - now going to unlink\n");

  session_service_unlink_sessions(service,E_AM_INITIATOR,
      ds,&(*os).session_guid);

  JNXCHECK(session_is_active(os) == 0);
  
  JNXCHECK(session_service_session_is_linked(service,&os->session_guid) == 0);
}
Beispiel #17
0
void test_stack_grow() {
  JNX_LOG(NULL,"- test_stack_grow");
  jnx_stack *s = jnx_stack_create();
  JNXCHECK(s);
  jnx_stack_push(s,A);
  jnx_stack_push(s,B);
  jnx_stack_push(s,C);
  jnx_stack_destroy(&s);
  JNXCHECK(s == NULL);
  jnx_term_printf_in_color(JNX_COL_GREEN, "  OK\n");
}
Beispiel #18
0
void test_compare_guid() {

    jnx_guid g;
    jnx_guid_create(&g);
    jnx_guid h;
    jnx_guid_create(&h);

    JNXCHECK(jnx_guid_compare(&g,&h) == JNX_GUID_STATE_FAILURE);
    JNXCHECK(jnx_guid_compare(&g,&g) == JNX_GUID_STATE_SUCCESS);
    JNXCHECK(jnx_guid_compare_raw(g.guid,h.guid) == JNX_GUID_STATE_FAILURE);
    JNXCHECK(jnx_guid_compare_raw(g.guid,g.guid) == JNX_GUID_STATE_SUCCESS);

}
Beispiel #19
0
static void test_contains() {
  jnx_list *l = jnx_list_create();

  char *text = "Text";
  char *another = "Another";
  char *fake = "Fake";
  jnx_list_add(l,text);
  jnx_list_add(l,another);

  JNXCHECK(jnx_list_contains(l,text,test_contains_comparator) == 1);
  JNXCHECK(jnx_list_contains(l,another,test_contains_comparator) == 1);
  JNXCHECK(jnx_list_contains(l,fake,test_contains_comparator) == 0);

}
void unpair_session_from_gui(void *gui_context) {
  gui_context_t *context = (gui_context_t *) gui_context;
  app_context_t *act = (app_context_t *) context->args;

  context->is_active = 0;
  session_state r = session_service_unlink_sessions(
      act->session_serv,
      E_AM_RECEIVER,
      act,
      &context->s->session_guid);

  JNXCHECK(r == SESSION_STATE_OKAY);
  JNXCHECK(session_service_session_is_linked(
      context->session_serv, &context->s->session_guid) == 0);
}
Beispiel #21
0
void test_stack_pop() {
  JNX_LOG(NULL,"- test_stack_pop");
  jnx_stack *s = jnx_stack_create();
  JNXCHECK(s);
  jnx_stack_push(s,ar[0]);
  jnx_stack_push(s,ar[1]);
  jnx_stack_push(s,ar[2]);

  int c = 2;
  while(!jnx_stack_is_empty(s)) {
    char *d = jnx_stack_pop(s);
    JNXCHECK(strcmp(ar[c],d) == 0);
    --c;
  }
  jnx_term_printf_in_color(JNX_COL_GREEN, "  OK\n");
}
Beispiel #22
0
void jnx_btree_keys_ts(jnx_btree *tree, jnx_list *keys) {
  JNXCHECK(tree);
  if(!tree) { return ; }
  jnx_thread_lock(tree->internal_lock);
  jnx_btree_keys(tree,keys);
  jnx_thread_unlock(tree->internal_lock);
}
Beispiel #23
0
jnx_int32 jnx_hash_get_keys_ts(jnx_hashmap *hashmap,const jnx_char ***keys) {
  JNXCHECK(hashmap);
  jnx_thread_lock(hashmap->internal_lock);
  jnx_int32 ret = jnx_hash_get_keys(hashmap,keys);
  jnx_thread_unlock(hashmap->internal_lock);
  return ret;
}
Beispiel #24
0
static void test_ipc_listener() {
  jnx_ipc_socket *s = jnx_socket_ipc_create(SOCKPATH);
  jnx_ipc_listener *listener =
    jnx_socket_ipc_listener_create(s, 100);

  fire_threaded_ipc_packets(69);
  fire_threaded_ipc_packets(70);
  fire_threaded_ipc_packets(71);
  int results[3] = {0, 0, 0};
  while (!test_ipc_listener_complete) {
    jnx_socket_ipc_listener_tick(listener, test_ipc_listener_callback, (void *) results);
  }
  jnx_socket_ipc_listener_destroy(&listener);
  JNXCHECK(test_ipc_listener_complete);
  JNXCHECK(listener == NULL);
}
Beispiel #25
0
session_state session_service_destroy_session(session_service *service,\
        jnx_guid *g) {
    JNXCHECK(service);
    session_state e = SESSION_STATE_NOT_FOUND;
    jnx_node *h = service->session_list->head,
              *r = service->session_list->head;
    jnx_list *cl = jnx_list_create();
    session *retrieved_session = NULL;
    while(h) {
        session *s = h->_data;
        if(jnx_guid_compare(g,&s->session_guid) == JNX_GUID_STATE_SUCCESS) {
            retrieved_session = s;
            e = SESSION_STATE_OKAY;
        } else {
            jnx_list_add_ts(cl,s);
        }
        h = h->next_node;
    }
    service->session_list->head = r;
    jnx_list_destroy(&service->session_list);
    if(cl) {
        destroy_session(retrieved_session);
    }
    service->session_list = cl;
    return e;
}
Beispiel #26
0
int discovery_service_start(discovery_service *svc, discovery_strategy *strategy) {
  JNXCHECK(svc);

  // *** TODO Set up brodcast or multicast ***
  // It should just be a simple matter of passing a flag to the service
  // or changing the function signature and calling either
  // set_up_sockets_for_broadcast or set_up_sockets_for_multicast.
  // set_up_sockets_for_broadcast(svc);
  svc->sock_send = jnx_socket_udp_create(svc->family);
  svc->udp_listener = jnx_socket_udp_listener_broadcast_create(
      port_to_string(svc), svc->family);

  svc->isrunning = 1;

  if (0 != listen_for_discovery_packets(svc)) {
    JNXLOG(0, "[DISCOVERY] Couldn't start the discovery listener.\n");
    return ERR_DISCOVERY_START;
  }

  initiate_discovery(svc);

  if (strategy == NULL) {
    svc->peers->is_active_peer = is_active_peer_ask_once;
    send_discovery_request(svc);
  }
  else {
    svc->peers->is_active_peer = is_active_peer_periodic_update;
    strategy(svc);
  }

  return 0;
}
Beispiel #27
0
static void test_udp_listener_ipv6() {
  jnx_udp_listener *listener = 
    jnx_socket_udp_listener_create(UDPTESTPORT,AF_INET6,NULL);
  fire_threaded_udp_packet_ipv6(UDPTESTPORT);
  int x = 0;
  while(x < 20) {
    jnx_socket_udp_listener_tick(listener,test_udp_listener_callback,NULL);
    if(test_udp_listener_complete)break;
    sleep(.5);
    ++x;
    sleep(.5);
  }
  jnx_socket_udp_listener_destroy(&listener);
  JNXCHECK(test_udp_listener_complete);
  JNXCHECK(listener == NULL);
}
Beispiel #28
0
static void test_removal_from() {
  JNXLOG(LDEBUG,"- test_removal_from\n");
  jnx_list *list = jnx_list_create();

  // Test the empty list removal should succeed without blowing up
  jnx_list_remove_from(&list, 5, compare_ints);

  int i;
  for (i = 1; i < 10; i++)
    jnx_list_add(list, (void *)i);

  // Remove from the middle
  jnx_list_remove_from(&list, 3, compare_ints);
  // Remove the head
  jnx_list_remove_from(&list, 1, compare_ints);
  // Remove the tail
  jnx_list_remove_from(&list, 9, compare_ints);

  // Check the remaining elements are 245678
  char digit[2];
  char leftover[10];
  leftover[0] = '\0';
  jnx_node *current = list->head;
  while (current != NULL) {
    sprintf(digit, "%d", (int) current->_data);
    strcat(leftover, digit);
    current = current->next_node;		
  }

  JNXCHECK(strcmp(leftover, "245678") == 0);
}
Beispiel #29
0
jnx_int32 jnx_hash_put(jnx_hashmap* hashmap, const jnx_char* key, void* value) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  JNXCHECK(value);
  jnx_int32 index = jnx_hash_string(key, hashmap->size);
  if (hashmap->data[index].used == 0) {
    // we need to setup the bucket
    hashmap->data[index].bucket = jnx_list_create();
    hashmap->data[index].bucket_len = 0;
    //okay so bucket is ready to get given a KVP entry, so we'll use our bucket struct
    jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el));
    current_bucket_el->origin_key = malloc(strlen(key) + 1);
    strncpy(current_bucket_el->origin_key,key,strlen(key) + 1);
    current_bucket_el->origin_value = value;//insert our bucket element...
    jnx_list_add(hashmap->data[index].bucket, current_bucket_el);
    hashmap->data[index].bucket_len++;
    hashmap->data[index].used++;
    hashmap->used_up++;
  } else {
    // So we are here assuming that the bucket exists so we're going to append to the existing bucket...
    //check to see if it already exists!
    jnx_node* head = hashmap->data[index].bucket->head;
    jnx_node* rewind_head = head;
    while (head) {
      jnx_hash_bucket_el* bucketel = head->_data;
      if (strcmp(bucketel->origin_key, key) == 0) {
        bucketel->origin_value = value;
        hashmap->data[index].bucket->head = rewind_head;
        return 0;
      }
      head = head->next_node;
    }
    hashmap->data[index].bucket->head = rewind_head;
    //If the list has been searched and the entry does not exist, we'll do a new insert
    jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el));
    current_bucket_el->origin_key = malloc(strlen(key) + 1);
    strncpy(current_bucket_el->origin_key,key,strlen(key) + 1);
    current_bucket_el->origin_value = value;
    jnx_list_add(hashmap->data[index].bucket, current_bucket_el);
    hashmap->data[index].bucket_len++;
    hashmap->data[index].used = 1;
    hashmap->used_up++;
    //so the linked list length should be incremented....
  }

  return 0;
}
Beispiel #30
0
static jnx_int32 listen_on_ipc_socket(jnx_ipc_socket *s, jnx_int max_connections) {
  JNXCHECK(s);
  if (listen(s->socket, max_connections) == -1) {
    perror("jnx IPC socket listen");
    return -1;
  }
  return 0;
}