static int _s2s_router_connect(s2s_t s2s) { log_write(s2s->log, LOG_NOTICE, "attempting connection to router at %s, port=%d", s2s->router_ip, s2s->router_port); s2s->fd = mio_connect(s2s->mio, s2s->router_port, s2s->router_ip, NULL, s2s_router_mio_callback, (void *) s2s); if(s2s->fd == NULL) { if(errno == ECONNREFUSED) s2s_lost_router = 1; log_write(s2s->log, LOG_NOTICE, "connection attempt to router failed: %s (%d)", MIO_STRERROR(MIO_ERROR), MIO_ERROR); return 1; } s2s->router = sx_new(s2s->sx_env, s2s->fd->fd, s2s_router_sx_callback, (void *) s2s); sx_client_init(s2s->router, 0, NULL, NULL, NULL, "1.0"); return 0; }
/** Initializes multiplexer and associated threads. * Spawns mio_connection thread. A registration thread * * param jid The Jabberd Unique Identifier of user * param password The password associated with jid. * param port The port the ipc registration thread should listen on. * returns Initialized */ multiplexer_context_t* multiplexer_setup(char* jid, char* password, int port) { int error; multiplexer_context_t *context = _new_multiplexer_context(); if (jid == NULL) { printf("IPC ERROR: Null jid"); return NULL; } if (password == NULL) { password = getpass("MIO password: "******"IPC ERROR: Could not connect to Mio server\n"); return NULL; } sleep(1); /* copy mio info over */ context -> mio_info -> jid = malloc(strlen(jid)+1); strcpy(context -> mio_info -> jid, jid); context -> mio_info -> password = malloc(strlen(password)); strcpy(context -> mio_info -> password, password); context -> mio_info -> miofd = _fd_setup(LOCAL_HOST,0); printf("IPC status: MIO context setup\n"); pthread_create(context -> mio_info -> mio_thread_r,NULL,_mio_handler_read, context); pthread_create(context -> mio_info -> mio_thread_w,NULL,_mio_handler_write, context); printf("IPC status: MIO threads started\n"); /*begin registration thread*/ context -> register_info -> register_port = port; pthread_create(context -> register_info -> register_thread, NULL, _register_thread,context); printf("IPC status: Register thread started\n"); return context; }
int main(int argc, char **argv) { //parse command line arguments benchmark_t *setup = parse_args(argc,argv); // setup user connection mio_conn_t *conn = mio_conn_new(); mio_connect(setup -> user, setup -> pass, MIO_LEVEL_DEBUG, NULL, NULL, conn); // - register new publishers // - connect new publishers // - register new publishers event nodes int user_index, error, node_index; char tmp_username[UUID_LENGTH]; char tmp_password[UUID_LENGTH]; char *tmp_node_id,*tmp_jid; mio_response_t *response = NULL; uuid_t tmp_uuid; srand(time(NULL)); thread_args_t *thread_args; publishers = malloc(setup -> n_publishers * sizeof(mio_conn_t*)); memset(publishers, 0x0, setup -> n_publishers * sizeof(mio_conn_t*)); publish_nodes = (char***) malloc(setup -> n_publishers * sizeof(char**)); publish_count = (int**) malloc(setup -> n_publishers *sizeof(int*)); for (user_index = 0; user_index < setup -> n_publishers; user_index++) { uuid_generate(tmp_uuid); uuid_unparse(tmp_uuid,tmp_username); uuid_generate(tmp_uuid); uuid_unparse(tmp_uuid,tmp_password); error = register_user(conn, tmp_username,tmp_password); publishers[user_index] = mio_conn_new(); if (error) { printf("error registering user %s \n", tmp_username); } else { tmp_jid = malloc(100); sprintf(tmp_jid,"%s@%s", tmp_username, setup -> server); mio_connect(tmp_jid, tmp_password, MIO_LEVEL_ERROR, NULL,NULL,publishers[user_index]); } publish_nodes[user_index] = malloc(setup -> publish_factor * sizeof(char*)); for (node_index = 0; node_index < setup -> publish_factor; node_index++) { tmp_node_id = malloc(UUID_LENGTH * sizeof(char)); uuid_generate(tmp_uuid); uuid_unparse(tmp_uuid,tmp_node_id); publish_nodes[user_index][node_index] = tmp_node_id; response = mio_response_new(); mio_node_create(publishers[user_index], tmp_node_id, "", NULL, response); mio_response_free(response); tmp_node_id = NULL; } publish_count[user_index] = (int*) malloc(sizeof(int)*setup -> publish_factor); memset(publish_count[user_index],0x0,sizeof(int)*setup -> publish_factor); memset(tmp_username,0x0,UUID_LENGTH); memset(tmp_password,0x0,UUID_LENGTH); } // register new subscribers // connect new subscribers // randomly assign the nodes to subscribe to // initialize count matrix subscribers = malloc(setup -> n_subscribers * sizeof(mio_conn_t*)); memset(subscribers, 0x0, setup -> n_subscribers * sizeof(mio_conn_t*)); subscribe_nodes = (char***) malloc(setup -> n_subscribers * sizeof(char**)); subscribe_count = (int**) malloc(setup -> n_subscribers *sizeof(int*)); printf("%d\n", setup -> n_subscribers); for (user_index = 0; user_index < setup -> n_subscribers; user_index++) { uuid_generate(tmp_uuid); uuid_unparse(tmp_uuid,tmp_username); uuid_generate(tmp_uuid); uuid_unparse(tmp_uuid,tmp_password); subscribers[user_index] = mio_conn_new(); error = register_user(conn, tmp_username,tmp_password); if (error) { printf("error registering user %s \n", tmp_username); } else { tmp_jid = malloc(100); sprintf(tmp_jid,"%s@%s", tmp_username,setup -> server); mio_connect(tmp_jid, tmp_password, MIO_LEVEL_ERROR, NULL,NULL,subscribers[user_index]); } subscribe_nodes[user_index] = malloc(setup -> subscribe_factor * sizeof(char*)); int pub_index = (user_index * setup -> subscribe_factor)/(setup -> publish_factor) % setup -> n_publishers; int event_index = (user_index * setup -> subscribe_factor) % (setup -> publish_factor); for (node_index = 0; node_index < setup -> subscribe_factor; node_index++) { printf("%d node, %d pub, %d event\n", node_index, pub_index, event_index); tmp_node_id = publish_nodes[pub_index][ event_index]; subscribe_nodes[user_index][node_index] = tmp_node_id; response = mio_response_new(); error = mio_subscribe(subscribers[user_index], tmp_node_id, response); mio_response_free(response); event_index++; pub_index += event_index / setup -> publish_factor; pub_index = pub_index % setup -> n_publishers; event_index = event_index % setup -> publish_factor; } subscribe_count[user_index] = (int*) malloc(sizeof(int)*setup -> subscribe_factor); memset(subscribe_count[user_index],0x0,sizeof(int)*setup -> subscribe_factor); memset(tmp_username,0x0,UUID_LENGTH); memset(tmp_password,0x0,UUID_LENGTH); } publish_threads = (pthread_t*) malloc(setup -> n_publishers*sizeof(pthread_t)); subscribe_threads = (pthread_t*) malloc(setup -> n_publishers*sizeof(pthread_t)); // spawn process for each subscriber // spawn process for each publisher for (user_index = 0; user_index < setup -> n_subscribers; user_index++) { thread_args = (thread_args_t*) malloc(sizeof(thread_args_t)); thread_args -> user_index = user_index; thread_args -> setup = setup; pthread_create(&subscribe_threads[user_index], NULL, (void*) &subscriber_thread, thread_args); } for (user_index = 0; user_index < setup -> n_publishers; user_index++) { thread_args = (thread_args_t*) malloc(sizeof(thread_args_t)); thread_args -> user_index = user_index; thread_args -> setup = setup; pthread_create(&publish_threads[user_index], NULL, (void*) &publisher_thread, thread_args); } // have subscribers and publishers increment // kill all threads after proveided time long elapsed = 0; long last_time = time(NULL), time_tmp; while(elapsed < setup -> time) { sleep(setup -> time /10); time_tmp = time(NULL); elapsed += time_tmp - last_time; last_time = time_tmp; } for (user_index = 0; user_index < setup -> n_subscribers; user_index++) { pthread_cancel(subscribe_threads[user_index]); } for (user_index = 0; user_index < setup -> n_publishers; user_index++) { pthread_cancel(publish_threads[user_index]); } print_results(setup); return 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; int err; 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(stdout, "%s's ", username); fflush(stdout); password = getpass("password: "******"Invalid password\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"); conn = mio_conn_new(MIO_LEVEL_DEBUG); err = mio_connect(username, password, NULL, NULL, conn); } else{ conn = mio_conn_new(MIO_LEVEL_ERROR); err = mio_connect(username, password, NULL, NULL, conn); } if (err == MIO_OK) fprintf(stdout, "Authentication successful\n"); else fprintf(stderr, "Authentication failed\n"); mio_disconnect(conn); mio_conn_free(conn); return err; }
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; }