int main( int argc, char **argv ) { struct thread_args *net = malloc(sizeof(struct thread_args)); pthread_cond_t *cv = NULL; pthread_mutex_t *mut = NULL; cv = malloc(sizeof(pthread_cond_t)); if( !cv ) return 1; pthread_cond_init(cv, NULL); net->core_cond = cv; mut = malloc(sizeof(pthread_mutex_t)); if( !mut ) return 1; pthread_mutex_init(mut, NULL); net->core_mut = mut; if( create_network_thread( net ) != 0 ) EXIT_WITH_ERROR("Failed to create network thread in main.\n"); for(;;) { /* wait for signal */ //pthread_mutex_lock(mut); pthread_cond_wait( cv, mut ); /* FIXME: memory leak. This means you, Bryan! */ pthread_t *proc_thread = malloc(sizeof(pthread_t)); if( !proc_thread ) return 1; if( pthread_create( proc_thread, NULL, process_work, net ) != 0 ) return 1; } return 0; }
int create_gateway(gateway_handle* handle, gateway_create_params *params) { gateway_context *gateway = NULL; int return_value = 0; gateway = (gateway_context*)malloc(sizeof(gateway_context)); if(NULL == gateway) { LOG_ERROR(("ERROR: Out of memory\n")); return (E_OUT_OF_MEMORY); } gateway->client_count = 0; /* create network read thread */ return_value = create_network_thread(&gateway->network_thread, params->gateway_ip_address); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating n/w read thread\n")); delete_gateway((gateway_handle)gateway); return (return_value); } /* create connection to server */ return_value = create_server_socket(&gateway->server_socket_fd, params->gateway_ip_address, params->gateway_port_no); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating the socket\n")); delete_gateway((gateway_handle)gateway); return (return_value); } /* add socket to network read thread */ return_value = add_socket(gateway->network_thread, gateway->server_socket_fd, (void*)gateway, &accept_callback); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: add_socket() failed\n")); delete_gateway((gateway_handle)gateway); return (return_value); } *handle = gateway; return (E_SUCCESS); }
int create_gateway(gateway_handle* handle, gateway_create_params *params) { gateway_context *gateway = NULL; int return_value = 0; gateway = (gateway_context*)malloc(sizeof(gateway_context)); if(NULL == gateway) { LOG_ERROR(("ERROR: Out of memory\n")); return (E_OUT_OF_MEMORY); } memset(gateway, 0, sizeof(gateway_context)); gateway->client_count = 0; gateway->storage_file_pointer = fopen(params->storage_file_name, "w"); if(!gateway->storage_file_pointer) { LOG_ERROR(("ERROR: Unable to open persistent storage file\n")); delete_gateway((gateway_handle)gateway); return E_FAILURE; } /* create network read thread */ return_value = create_network_thread(&gateway->network_thread, params->gateway_ip_address); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating n/w read thread\n")); delete_gateway((gateway_handle)gateway); return (return_value); } /* create connection to server */ return_value = create_socket(&gateway->server_socket_fd, params->gateway_ip_address, params->gateway_port_no); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating the socket\n")); delete_gateway((gateway_handle)gateway); return (return_value); } /* add socket to network read thread */ return_value = add_socket(gateway->network_thread, gateway->server_socket_fd, (void*)gateway, &read_callback); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: add_socket() failed\n")); delete_gateway((gateway_handle)gateway); return (return_value); } message msg; /* register device with gateway */ msg.type = REGISTER; msg.u.s.type = BACK_TIER_GATEWAY; msg.u.s.ip_address = params->gateway_ip_address; msg.u.s.port_no = params->gateway_port_no; msg.u.s.area_id = "0"; return_value = write_message(gateway->server_socket_fd, gateway->logical_clock, &msg); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in registering device\n")); return (E_FAILURE); } *handle = gateway; return (E_SUCCESS); }
int create_sensor(sensor_handle *handle, sensor_create_params *params) { sensor_context *sensor = NULL; int return_value = E_FAILURE; sensor = (sensor_context*)malloc(sizeof(sensor_context)); if(NULL == sensor) { delete_sensor((sensor_handle)sensor); LOG_ERROR(("ERROR: Out of memory\n")); return (E_OUT_OF_MEMORY); } memset(sensor, 0, sizeof(sensor_context)); sensor->interval = 5; sensor->sensor_params = params; sensor->clock = 0; sensor->value = 0; sensor->run = 1; sensor->recv_peer_count = 0; sensor->send_peer_count = 0; pthread_mutex_init(&sensor->mutex_lock, NULL); sensor->sensor_value_file_pointer = fopen(params->sensor_value_file_name, "r"); if(!sensor->sensor_value_file_pointer) { LOG_ERROR(("Unable to open sensor value input file\n")); delete_sensor(sensor); return (E_FAILURE); } /* create network read thread */ return_value = create_network_thread(&sensor->network_thread, params->sensor_ip_address); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating n/w read thread\n")); delete_sensor((sensor_handle)sensor); return (return_value); } /* create connection to server */ return_value = create_server_socket(&sensor->server_socket_fd, params->sensor_ip_address, params->sensor_port_no); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in creating the socket\n")); delete_sensor((sensor_handle)sensor); return (return_value); } /* add socket to network read thread */ return_value = add_socket(sensor->network_thread, sensor->server_socket_fd, (void*)sensor, &accept_callback); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: add_socket() failed\n")); delete_sensor((sensor_handle)sensor); return (return_value); } /* create connection to server */ return_value = create_socket(&sensor->socket_fd, params->gateway_ip_address, params->gateway_port_no); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Connection to Server failed\n")); delete_sensor((sensor_handle)sensor); return (return_value); } /* add socket to network read thread */ return_value = add_socket(sensor->network_thread, sensor->socket_fd, (void*)sensor, &read_callback); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: add_socket() filed\n")); delete_sensor((sensor_handle)sensor); return (return_value); } message msg; /* register sensor with gateway */ msg.type = REGISTER; msg.u.s.type = KEY_CHAIN_SENSOR; msg.u.s.ip_address = sensor->sensor_params->sensor_ip_address; msg.u.s.port_no = sensor->sensor_params->sensor_port_no; msg.u.s.area_id = sensor->sensor_params->sensor_area_id; return_value = write_message(sensor->socket_fd, sensor->logical_clock, &msg); if(E_SUCCESS != return_value) { LOG_ERROR(("ERROR: Error in registering sensor\n")); return (E_FAILURE); } struct sigaction actions; memset(&actions, 0, sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags = 0; actions.sa_handler = sighand; sigaction(SIGALRM,&actions,NULL); *handle = sensor; LOG_SCREEN(("INFO: Waiting for Peers to connect...\n")); return (E_SUCCESS); }