#include #define MAXLINE 1000 static struct buf_entry { float s_time; int p_id; struct buf_entry *next; } *head, *tail; void init_buf() { head = tail = NULL; } void insert_buf(float time_id, int packet_id) { struct buf_entry *entry; entry = (struct buf_entry *) malloc(sizeof(struct buf_entry)); entry->s_time = time_id; entry->p_id = packet_id; entry->next = NULL; if (head == NULL) { head = tail = entry; } else { tail->next = entry; tail = entry; } } // find the matching entry with the given packet_id // return the sent time of the packet float remove_buf(int packet_id) { struct buf_entry *np, *old_np; float save_time; if (head->p_id == packet_id) { np = head; save_time = np->s_time; if (head == tail) head = tail = NULL; else head = np->next; free(np); return(save_time); } for (np = head; np != NULL; np = np->next) { if (np->p_id == packet_id) { save_time = np->s_time; old_np->next = np->next; if (tail == np) tail = old_np; free(np); return(save_time); } old_np = np; } return(0.0); } // count the number of packets that are not received yet int check_buf(float last_receive_time) { int count; struct buf_entry *np; for (count = 0, np = head; np != NULL; np = np->next) if (np->s_time char sr, float time_id, int packet_id sscanf(line, "%c\t%f\t%d", &sr, &time_id, &packet_id); // if receive, check the buffer for mathching 's' // calculate the delay, update total_delay // delete the matching 's' entry from the buffer if (sr == 'r') { if ((sent_time = remove_buf(packet_id)) == 0.0) { re_received ++; } else if (sent_time >= time_id) { printf("sent time is later than receiving time...\n"); exit(1); } else { total_delay += (time_id - sent_time); success ++; } if (time_id>last_time) last_time = time_id; } // if send, insert to the buffer // if the packet_id > last send's packet_id+1, // then, dropped += packet_id - old_packet_id else if (sr == 's') { insert_buf(time_id, packet_id); dropped += (packet_id - old_packet_id - 1); old_packet_id = packet_id; } else { printf("it's neither s nor r...\n"); exit(1); } } remain = check_buf(last_time); // we have 'success' packets successful, 'dropped' packets dropped, 'remain' packets remained in buffer // for successful packets, delay is total_delay/success printf("Success:\t%d\tPDR:\t%f\tDelay:\t%f\n", success, (float)success/(success+remain), total_delay/success); }