示例#1
0
/** Thread that handles incoming messages from mio
 * @param context
 * @param mio_sock
 */
static void* _mio_handler_write(void* vcontext)
{
    multiplexer_context_t *context = (multiplexer_context_t*) vcontext;
    char* message = NULL;
    size_t message_len = 0;

    mio_response_t *response;
    while(1)
    {
        response = mio_response_new();
        //printf("waiting for xmpp message\n");
        mio_pubsub_data_receive(context -> mio_info -> mio, response);
        if (response -> stanza == NULL)
        {
            mio_response_free(response);
            printf("IPC ERROR: data receive stanza null\n");

            continue;
        }
        //mio_stanza_to_text(response -> stanza, &message, &message_len);
        xmpp_stanza_to_text(response -> stanza -> xmpp_stanza, &message, &message_len);
       
        // receive message
        printf("Distributing\n%s\n",message);
        _distribute_message(context,message,0);
        mio_response_free(response);
        if (message_len != 0)
            free(message);
    }
    return NULL;
}
示例#2
0
int subscriber_thread(thread_args_t *thread_args)
{
  int sub_index = thread_args -> user_index;
  benchmark_t *setup = thread_args -> setup;
    char  *event_node;
    
    mio_conn_t *conn = subscribers[sub_index];
    if (conn == NULL)
      printf("sub conn is null\n");
    mio_data_t * mio_data = NULL;
    mio_response_t *response = NULL;
    mio_packet_t *mio_packet = NULL;
    int node_index;
    while (1) {

		response = mio_response_new();
		mio_pubsub_data_receive(conn, response);
		//This is a handy toowwwwwwl for printing response information
		// For the purpose of illustration, we will manually print the elements
		// mio_response_print(response);
		//Parse the response packet type
		switch (response->response_type) {
                //For commands that require an acknowledgment, it can be checked with MIO_RESPONSE_OK
            case MIO_RESPONSE_OK:
                break;
                
                //For errors, the code can be printed as described below
            case MIO_RESPONSE_ERROR:
              
                break;
                
                //If the response contains more complex data, it is encapsulated in a MIO_RESPONSE_PACKET
            case MIO_RESPONSE_PACKET:
                mio_packet = (mio_packet_t *) response->response;
                //Since there are multiple types of responses, you typically first switch on the type
                if (mio_packet->type == MIO_PACKET_DATA) {
                    //MIO_PACKET_DATA contains a list of transducer values that contain data
                    mio_data = (mio_data_t *) mio_packet-> payload;
                    event_node = mio_data->event;
                    //Traverse the linked list of sensor values
                    for (node_index = 0; node_index < setup -> subscribe_factor; node_index++ ) {
                        if (strcmp(event_node, subscribe_nodes[sub_index][node_index]) == 0)
                        {

                          subscribe_count[sub_index][node_index]++;
                          break;
                        }
                    }
                } else
                    printf("Unknown MIO response packet\n");
                break;
                
            default:
                printf("unknown response type\n");
                
		}
        
		mio_response_free(response);
	}
    return 0;
}
示例#3
0
void* bacnet_actuate(void* vadapter)
{
    adapter_t *adapter = (adapter_t*) vadapter;
    bacnet_context_t *context = (bacnet_context_t*) (adapter -> context);
    mio_conn_t* pubsub_conn = adapter -> connection;
    mio_packet_t* packet;
    mio_data_t* data;
    mio_response_t* response;
    mio_transducer_value_t* tran;
    int32_t err;

    bacnet_device_t *device;

    while (TRUE) {
        if (pubsub_conn -> xmpp_conn -> state == XMPP_STATE_CONNECTED) {
            response = mio_response_new();
            err = mio_pubsub_data_receive(pubsub_conn, response);
            if (err != MIO_OK)
            {
                printf("mio_pubsub_data_receive failed with error: %d\n", err);
                continue;
            }
            pthread_mutex_lock(mio_lock);
            packet = (mio_packet_t*) response->response;
            data = (mio_data_t*) packet->payload;
            for (tran = data->transducers; tran != NULL; tran = tran->next) {
                if (tran -> type != MIO_TRANSDUCER_SET_VALUE)
                    continue;
                else
                {
                    for (device = context -> node_devices;
                            device; device = device -> nodemap.next)
                    {
                        if (strcmp(device -> node_id,data->event) !=0)
                            continue;
                        int i;
                        for (i = 0; i < device -> n_objects; i++)
                        {
                            if (strcmp(device -> obj_names[i],tran -> name) == 0)
                            {
                                break;
                            }
                        }
                        if (i == device -> n_objects)
                        {
                            continue;
                        }
                        err = write_bacnet_obj(device, i, tran -> typed_value);
                        if (err)
                            printf("error with writing bacnet object\n");
                    }
                }

            }
            pthread_mutex_unlock(mio_lock);
            mio_response_free(response);
        } else
            usleep(1000);

    }
}
示例#4
0
int main(int argc, char **argv) {
	char *username = NULL;
	char *password = NULL;
	char *xmpp_server = NULL;
	char pubsub_server[80];
	int xmpp_server_port = 5223;

	int verbose = 0;

	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;

	struct sigaction sig_int_handler;

	// Add SIGINT handler
	sig_int_handler.sa_handler = int_handler;
	sigemptyset(&sig_int_handler.sa_mask);
	sig_int_handler.sa_flags = 0;
	sigaction(SIGINT, &sig_int_handler, NULL );

	if (argc == 1 || !strcmp(argv[1], "-help")) {
		print_usage(argv[0]);
		return -1;
	}

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			xmpp_server = _mio_get_server(username);
			if (xmpp_server == NULL ) {
				fprintf(stderr, "Invalid JID, use format user@domain\n");
				return MIO_ERROR_INVALID_JID;
			}
			strcpy(pubsub_server, "pubsub.");
			strcat(pubsub_server, xmpp_server);
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (password == NULL ) {
		fprintf(stderr, "Password missing\n");
		print_usage(argv[0]);
		return -1;
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");
	}

	if (verbose == 0){
		conn = mio_conn_new(MIO_LEVEL_ERROR);
		mio_connect(username, password, NULL, NULL, conn);
	}
	else{
		conn = mio_conn_new(MIO_LEVEL_DEBUG);
		mio_connect(username, password, NULL, NULL, conn);
	}

	while (1) {
		response = mio_response_new();
		mio_pubsub_data_receive(conn, response);
		//This is a handy tool for printing response information
		// For the purpose of illustration, we will manually print the elements
		// mio_response_print(response);
		//Parse the response packet type
		switch (response->response_type) {
		//For commands that require an acknowledgment, it can be checked with MIO_RESPONSE_OK
		case MIO_RESPONSE_OK:
			printf("Request Successful\n");
			break;

			//For errors, the code can be printed as described below
		case MIO_RESPONSE_ERROR:
			printf("Response error: ");
			mio_err = response->response;
			fprintf(stderr, "MIO Error:\n");
			fprintf(stderr, "\tError code: %d\n\tError description: %s\n",
					mio_err->err_num, mio_err->description);
			break;

			//If the response contains more complex data, it is encapsulated in a MIO_RESPONSE_PACKET
		case MIO_RESPONSE_PACKET:
			mio_packet = (mio_packet_t *) response->response;
			//Since there are multiple types of responses, you typically first switch on the type
			if (mio_packet->type == MIO_PACKET_DATA) {
				//MIO_PACKET_DATA contains a list of transducer values that contain data
				mio_data = (mio_data_t *) mio_packet->payload;
				printf("MIO Data Packet:\n\tEvent Node:%s\n", mio_data->event);
				mio_tran = mio_data->transducers;
				//Traverse the linked list of sensor values
				while (mio_tran != NULL ) {
					if (mio_tran->type == MIO_TRANSDUCER_VALUE)
						printf("\t\tTrasducerValue:\n");
					else
						printf("\t\tTrasducerSetValue:\n");

					printf(
							"\t\t\tName: %s\n\t\t\tID: %d\n\t\t\tRaw Value: %s\n\t\t\tTyped Value: %s\n\t\t\tTimestamp: %s\n",
							mio_tran->name, mio_tran->id, mio_tran->raw_value,
							mio_tran->typed_value, mio_tran->timestamp);
					mio_tran = mio_tran->next;
				}
			} else
				printf("Unknown MIO response packet\n");
			break;

		default:
			printf("unknown response type\n");

		}

		mio_response_free(response);
	}

	return 0;
}