コード例 #1
0
int register_user(mio_conn_t *conn, char *user, char *pass)
{
	int err;
	mio_response_t *response = mio_response_new();
    xmpp_stanza_t *iq = NULL, *query = NULL, *username = NULL, *username_tag = NULL, *password = NULL, *password_tag = NULL;
    mio_stanza_t *stanza = mio_stanza_new(conn);
    // Check if connection is active
	if (!conn->xmpp_conn->authenticated) {
		return MIO_ERROR_DISCONNECTED;
	}

    // Create a new user stanza
	iq = xmpp_stanza_new(conn->xmpp_conn->ctx);
	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_attribute(iq, "id", "reg2");
	xmpp_stanza_set_type(iq, "set");

    // Creqte query stanza
    query = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(query,"query");
    xmpp_stanza_set_ns(query,"jabber:iq:register");
    
    // create username stanza 
    username = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    username_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(username,"username");
    xmpp_stanza_set_text(username_tag, user);



    // create password stanza
    password = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    password_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(password, "password");
    xmpp_stanza_set_text(password_tag,pass);

    // create password stanza
    password = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    password_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(password, "password");
    xmpp_stanza_set_text(password_tag,pass);
    // Build xmpp message
	xmpp_stanza_add_child(password, password_tag);
	xmpp_stanza_add_child(username, username_tag);
	xmpp_stanza_add_child(query, username);
	xmpp_stanza_add_child(query, password);
	xmpp_stanza_add_child(iq, query);
    stanza -> xmpp_stanza = iq;
    
    // Send out the stanza
	err = _mio_send_nonblocking(conn, stanza);
    
    // Release the stanzas
	mio_stanza_free(stanza);
    
	mio_response_free(response);
	return 0;
}
コード例 #2
0
ファイル: multiplexer.c プロジェクト: rogerioal/Mio_Billy
/** Thread that handles outgoing messages to mio and incoming messages from
 * @param context
 * @param mio_sock
 */
static void* _mio_handler_read(void* vcontext)
{
    multiplexer_context_t * context = vcontext;
    int fd = context -> mio_info -> miofd;
    char *stanza_response,*event_response ;
    mio_response_t *response;
    mio_stanza_t *stanza = NULL;
    mio_info_t *mio_info = context -> mio_info;

    while (mio_info -> mio -> xmpp_conn -> state != XMPP_STATE_CONNECTED)
    {
        usleep(1000);
    }

    mio_parser_t *parser = mio_parser_new(mio_info -> mio);
    char* publish_request;

    while(1)
    {
        /* read in buffer */
        publish_request = _buffered_read(fd);
        if (publish_request == NULL)
        {
            sleep(1);
            continue;
        }
        printf("got a publish request\n");
        stanza_response = malloc(strlen(publish_request));
        event_response = malloc(500);
        sscanf(publish_request,"%s,%s",event_response,publish_request);
        stanza = mio_parse(parser, (char*) publish_request);

        /* Publish stanza to mio */
        response = mio_response_new();
        while (mio_info -> mio -> xmpp_conn -> state != XMPP_STATE_CONNECTED)
            usleep(1000);
        mio_item_publish_data(mio_info -> mio,stanza,event_response,response);
        mio_response_print(response);
        mio_response_free(response);
        mio_stanza_free(stanza);
        mio_response_free(response);
        free(stanza_response);
        free(publish_request);
    }
    return NULL;
}
コード例 #3
0
void* publisher_thread(thread_args_t *thread_args)
{
    int pub_index = thread_args -> user_index;
    benchmark_t *setup = thread_args -> setup;
    
    char *time_str = NULL, *random_message = NULL, *event_node;
	mio_stanza_t *item;
    mio_conn_t *conn = publishers[pub_index];
    if (conn == NULL)
      printf("conn is null %d\n", pub_index);
    // generate random message
    random_message = malloc(sizeof(char)* 1000);
    memset(random_message,0x41,1000);
    random_message[999]='\0';
    int node_index;
    mio_response_t *response;
  while(TRUE)
  {
      for ( node_index = 0; node_index < setup -> publish_factor; node_index++)
      {
          event_node = publish_nodes[pub_index][node_index];
          time_str = mio_timestamp_create();
          item = mio_pubsub_item_data_new(conn);
          mio_item_transducer_value_add(item, "name", "100",
              random_message, random_message,time_str);
         
          response = mio_response_new();
          mio_item_publish(conn, item, event_node,response);
          free(time_str);
          free(response);
          mio_stanza_free(item);
          publish_count[pub_index][node_index]++;
      }
  }
  return (void*) NULL;
}
コード例 #4
0
ファイル: bacnet_adapter.c プロジェクト: rogerioal/Mio_Billy
int publish_data(adapter_t *adapter)
{
    static uint8_t rx_buf[MAX_MPDU] = {0};
    bacnet_context_t *context = (bacnet_context_t*) (adapter -> context);
    bacnet_device_t *device;
    time_t current_seconds, elapsed_seconds = 0;
    int pdu_len;
    char *time_str, *node_id;
    time_t last_seconds,timeout_seconds;
    unsigned timeout = 100;
    BACNET_ADDRESS  src = { 0 };
    unsigned max_apdu = 0;
    mio_stanza_t *item;
    mio_response_t *publish_response;
    for (device = context -> node_devices; device; device = device -> nodemap.next)
    {
        time_str = mio_timestamp_create();
        item = mio_pubsub_item_data_new(adapter -> connection);
        node_id = device -> node_id;

        last_seconds = time(NULL);
        timeout_seconds = (apdu_timeout() / 1000) * apdu_retries();
        //if (!found)
        found =
            address_bind_request(device -> instance, &max_apdu,
                                 &target_address);
        if (!found)
        {
            Send_WhoIs(device -> instance, device -> instance);
        }
        while (!found)
        {
            current_seconds = time(NULL);
            if (current_seconds != last_seconds)
                tsm_timer_milliseconds((uint16_t) ((current_seconds -
                                                    last_seconds) * 1000));
            if (!found)
            {
                found =
                    address_bind_request(device -> instance,
                                         &max_apdu,&target_address);
            }
            if (!found)
            {
                elapsed_seconds += (current_seconds - last_seconds);
                if (elapsed_seconds > timeout_seconds) {
                    printf("\rError: APDU Timeout!\r\n");
                    return 1;
                }
            }
            pdu_len = datalink_receive(&src, &rx_buf[0], MAX_MPDU,timeout);
            if (pdu_len) {
                npdu_handler(&src, &rx_buf[0], pdu_len);
            }
            last_seconds = current_seconds;
        }
        int i;
        printf("n_objects %d\n", device -> n_objects);
        for ( i = 0; i < device -> n_objects; i++)
        {
            request_invoke_id = 0;
            while(TRUE)
            {
                printf("true\n");
                if (found)
                {
                    if (request_invoke_id == 0)
                    {
                        request_invoke_id =
                            Send_Read_Property_Request(
                                device->instance, device -> obj_types[i],
                                device -> obj_ids[i], 85,
                                -1);
                    } else if (tsm_invoke_id_free(request_invoke_id))
                        break;
                    else if (tsm_invoke_id_failed(request_invoke_id))
                    {
                        fprintf(stderr,"\rError: TSM Timeout!\r\n");
                        tsm_free_invoke_id(request_invoke_id);
                        return 1;
                    }
                }
                pdu_len = datalink_receive(&src, &rx_buf[0], MAX_MPDU,timeout);
                if (pdu_len) {
                    npdu_handler(&src, &rx_buf[0], pdu_len);
                }
                last_seconds = current_seconds;
            }
            if (error_detected)
            {
                error_detected = 0;
                return 1;
            } else
            {
                if (strcmp(value,"active") == 0)
                    mio_item_transducer_value_add(item, NULL, device -> obj_names[i] , "1", value,  time_str);
                else if (strcmp(value,"inactive") == 0)
                    mio_item_transducer_value_add(item, NULL, device -> obj_names[i] , "0",value,  time_str);
                else
                    mio_item_transducer_value_add(item, NULL, device -> obj_names[i] , value, value, time_str);
            }

        }
        publish_response = mio_response_new();
        while (adapter -> connection -> xmpp_conn -> state != XMPP_STATE_CONNECTED)
            usleep(1000);
        mio_item_publish_data(adapter -> connection,item,node_id,publish_response);
        free(time_str);
        mio_response_print(publish_response);
        mio_response_free(publish_response);
        mio_stanza_free(item);
    }
    return 0;
}