void send_notify_message(){ struct message_info *message_info_ptr = (struct message_info *) malloc(sizeof(struct message_info)); message_info_ptr->seq_num = curr_seq_num; struct message message_struct; message_struct.identity = ID; message_struct.type = NOTIFY; message_struct.seq_num = curr_seq_num; message_struct.data = (char*)malloc(3 + 2); strcpy(message_struct.data, "END"); message_struct.length = 9; char *message_stream = message_to_stream(message_struct); int i =0,n; while(i++ < 10) n = sendto(client_udp_socket,message_stream,message_struct.length,0,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); if(message_info_ptr != NULL){ free(message_info_ptr); } if(message_struct.data != NULL) free(message_struct.data); if(message_stream != NULL) free(message_stream); if(n < 0){ error("\nError Sending over send to: "); } }
static void inject_retained(const char *topic, const char *msg, void *userdata) { stream_t *stream = (stream_t *)userdata; struct topic_message tm = {topic, msg}; if (NULL == stream) return; if (topic_match_string(stream->topic_patt, topic)) { LOG_INFO("inj %s %s", topic, msg); message_to_stream(stream->streamid, stream, &tm); } }
/* * Sender Function * According to the window, sends packet with sequence number. * Also, updates the window & map with new sequence number and updated time of packet sent * if current window counter is equal to window size, wait for signal from receiver */ void sender(void *param) { char *message_stream; struct timeval tvtime; tvtime.tv_sec = 0; tvtime.tv_usec = 50000; start_time = time(NULL); while(1) { int no_of_elements_sent = 0; uint64_t current_seq_no_to_be_sent = 0; int to_be_sent_queue[WINDOW_SIZE]; int to_be_sent_count = 0; //acquire lock pthread_mutex_lock(&MsgMap_lock); if(notify_status == 1 || total_bytes_sent >= fileData.st_size){ pthread_mutex_unlock(&MsgMap_lock); break; } if(cwnd_current_count == WINDOW_SIZE){ pthread_cond_wait(&MsgMap_CV,&MsgMap_lock); } while(cwnd_current_count < WINDOW_SIZE && total_bytes_sent < fileData.st_size && received_state != 1){ curr_seq_num++; cwnd_current_count++; struct message_info *message_info_ptr = (struct message_info *) malloc(sizeof(struct message_info)); message_info_ptr->seq_num = curr_seq_num; message_info_ptr->file_offset = (message_info_ptr->seq_num - 1) * PAYLOAD_SIZE; struct message message_struct; message_struct.type = DATA; message_struct.seq_num = curr_seq_num; int bytes_read = 0; if((DataMapIterSend = DataMap.find((curr_seq_num))) == DataMap.end()){ message_struct.data = (char*)malloc(PAYLOAD_SIZE + 1); fseek(fp,message_info_ptr->file_offset, SEEK_SET); bytes_read = fread (message_struct.data, 1, PAYLOAD_SIZE, fp); }else{ bytes_read = DataMapIterSend->second->num_bytes; message_struct.data = DataMapIterSend->second->data; } total_bytes_sent = total_bytes_sent + bytes_read; message_struct.length = bytes_read + 6; message_struct.identity = ID; message_stream = message_to_stream(message_struct); int n = sendto(client_udp_socket,message_stream,message_struct.length,0,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); if(message_stream != NULL) free(message_stream); if(n < 0){ error("\nError Sending over send to: "); } gettimeofday(&message_info_ptr->timestamp,NULL); MsgMap.insert(MSGMAPTYPE::value_type(curr_seq_num,message_info_ptr)); queue_of_sent_seq.push_back(curr_seq_num); } pthread_mutex_unlock(&MsgMap_lock); select(0,NULL,NULL,NULL,&tvtime); tvtime.tv_sec = 0; tvtime.tv_usec = 0; } pthread_mutex_unlock(&MsgMap_lock); pthread_exit(NULL); }
/* * Resends the packets that have expired and not received ACK from receiver. * This thread is signalled by timer everytime, timer comes across expired unacknowledged packets. */ void resender(void *param){ uint64_t current_seq_no_to_be_sent = 0; char *message_stream; while(1){ pthread_mutex_lock(&udp_control_queue_Lock); //if all acks received, exit. if(timer_resender_exit_status == 1){ pthread_mutex_unlock(&udp_control_queue_Lock); break; } if(udp_control_queue.empty()){ // if nothing in queue wait pthread_cond_wait(&udp_control_queue_CV,&udp_control_queue_Lock); } if(timer_resender_exit_status == 1){ pthread_mutex_unlock(&udp_control_queue_Lock); break; } pthread_mutex_lock(&MsgMap_lock); udp_control_queue_itr = udp_control_queue.begin(); while(udp_control_queue_itr < udp_control_queue.end()){ current_seq_no_to_be_sent = *udp_control_queue_itr; udp_control_queue.erase(udp_control_queue.begin()); udp_control_queue_itr = udp_control_queue.begin(); if((MsgMapIter = MsgMap.find(current_seq_no_to_be_sent)) != MsgMap.end()){ //Need to resend struct message_info *message_info_ptr = MsgMapIter->second; struct message message_struct; message_struct.type = DATA; message_struct.seq_num = current_seq_no_to_be_sent; int bytes_read = 0; if((DataMapIterReSend = DataMap.find((current_seq_no_to_be_sent))) == DataMap.end()){ message_struct.data = (char*)malloc(PAYLOAD_SIZE + 1); fseek(fp, message_info_ptr->file_offset, SEEK_SET); bytes_read = fread (message_struct.data, 1, PAYLOAD_SIZE, fp); }else{ bytes_read = DataMapIterReSend->second->num_bytes; message_struct.data = DataMapIterReSend->second->data; } message_struct.identity = ID; message_struct.length = bytes_read + 6; message_stream = message_to_stream(message_struct); int n = sendto(client_udp_socket,message_stream,message_struct.length,0,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); if(message_stream != NULL) free(message_stream); if(n < 0) error("\nError Sending over send to\n"); gettimeofday(&message_info_ptr->timestamp,NULL); // As recently sent, push to queue queue_of_sent_seq.push_back(current_seq_no_to_be_sent); } // end if } // end queue while loop pthread_mutex_unlock(&MsgMap_lock); pthread_mutex_unlock(&udp_control_queue_Lock); }// end while loop pthread_exit(NULL); }