static gssize channel_linkedWrite(Channel* channel, gconstpointer buffer, gsize nBytes) { MAGIC_ASSERT(channel); /* our linked channel is trying to send us data, make sure we can read it */ g_assert(!(channel->type & CT_WRITEONLY)); gsize available = channel->bufferSize - channel->bufferLength; if(available == 0) { /* we have no space */ return -1; } /* accept some data from the other end of the pipe */ guint copyLength = (guint) MIN(nBytes, available); bytequeue_push(channel->buffer, buffer, copyLength); channel->bufferLength += copyLength; /* we just got some data in our buffer */ descriptor_adjustStatus((Descriptor*)channel, DS_READABLE, TRUE); return copyLength; }
int main(int argc, char *argv[]) { if(argc != 7) { printf("usage: host port R stream_id window_size filename\n"); exit(0); } char *endptr; long R = strtol(argv[3], &endptr, 10); char *stream_id = argv[4]; int window_size = strtol(argv[5], NULL, 10); if(endptr != (argv[3] + strlen(argv[3]))) { fprintf(stderr, "R must be an integer (milliseconds).\n"); exit(1); } struct addrinfo* p; int send_sock = send_port(argv[1], argv[2], &p); int recv_sock = recv_port(NULL, argv[2]);// TODO fix printf("Stream id = %s\n", stream_id); ee122_packet pkt; pkt.R = R; pkt.stream = *stream_id; printf("pkt.stream = %c\n", pkt.stream); pkt.avg_len = 0; pkt.window_size = window_size; int read_count; char fbuff[sizeof(pkt.payload)]; memset(fbuff,0,sizeof(fbuff)); FILE *fd = fopen(argv[6], "rb"); if(NULL == fd) { fprintf(stderr, "fopen() error\n"); return 1; } ee122_packet rcv_pkt; struct timespec sleep_spec; sleep_spec.tv_sec = 0; struct timeval curr_time, start_time, last_generated, diff_time; gettimeofday(&start_time, NULL); gettimeofday(&last_generated, NULL); int seq_no = 0; int available_window = window_size; struct timeval timeouts[window_size+1]; ee122_packet packets[window_size+1]; int i; struct timeval zero_timeout; zero_timeout.tv_usec = 0; zero_timeout.tv_sec = 0; for(i = 0; i < window_size+1; i++) { memcpy(&timeouts[i], &zero_timeout, sizeof(zero_timeout)); } bytequeue q; bytequeue_init(&q, sizeof(ee122_packet), window_size); float rtt = 400*1000; char buff[sizeof(ee122_packet)]; float next_wait = rand_poisson(R); printf("next wait: %f\n", next_wait); int bytes_read; struct sockaddr src_addr; int src_len = sizeof(src_addr); int last_received = -1; unsigned total_attempts = 0; unsigned seconds = 0; unsigned errors = 0; while (1) { gettimeofday(&curr_time, NULL); timeval_subtract(&diff_time, &curr_time, &start_time); if (diff_time.tv_sec * 1000000 + diff_time.tv_usec > seconds * 1000000) { printf("%f,%d\n", rtt, seconds); seconds++; } // Stop sending after 60 seconds if(diff_time.tv_sec * 1000000 + diff_time.tv_usec > 60*1000000) { break; } // Check timeouts. If timeout reached, retransmit that packet and all following. int retransmitting = -1; int i; for(i=0; i < window_size+1; i++) { struct timeval timeout = timeouts[i]; if(timeout.tv_sec == 0 && timeout.tv_usec == 0) continue; timeval_subtract(&diff_time, &curr_time, &timeouts[i]); if(diff_time.tv_sec * 1000000 + diff_time.tv_usec > rtt) { retransmitting = i; break; } } if(retransmitting != -1) { i = retransmitting; do { struct timeval timeout = timeouts[i]; if(timeout.tv_sec == 0 && timeout.tv_usec == 0) { i = (i + 1) % (window_size+1); continue; } //printf("Retransmitting seq_no == %d, stream == %c, rtt == %f\n", packets[i].seq_number, packets[i].stream, rtt); // Reset the timeout for this packet and send. gettimeofday(&(timeouts[i]), NULL); serialize_packet(buff, packets[i]); sendto(send_sock, buff, sizeof(packets[i]), 0, p->ai_addr, p->ai_addrlen); i = (i + 1) % (window_size+1); total_attempts++; errors++; } while (i != retransmitting); } // Check if new packet should be generated by now timeval_subtract(&diff_time, &curr_time, &last_generated); if(diff_time.tv_sec * 1000000 + diff_time.tv_usec > next_wait * 1000) { // Enqueue the packet. bytequeue_push(&q, &pkt); next_wait = rand_poisson(R); } // Read from socket. Increase available window for each ack received int bytes_read = recvfrom(recv_sock, buff, sizeof(ee122_packet), 0, &src_addr, &src_len); if(bytes_read > 0) { rcv_pkt = deserialize_packet(buff); if(rcv_pkt.stream == 'Z' && rcv_pkt.seq_number == (last_received + 1) % (window_size+1)) { struct timeval timeout_start = rcv_pkt.timestamp; // Learn RTT timeval_subtract(&diff_time, &curr_time, &timeout_start); //printf("RTT difftime, tv_sec == %d, tv_usec == %d\n", diff_time.tv_sec, diff_time.tv_usec); rtt = (0.6 * (diff_time.tv_sec * 1000000 + diff_time.tv_usec)) + 0.4*rtt; // Reset this timeout timeouts[rcv_pkt.seq_number].tv_usec = 0; timeouts[rcv_pkt.seq_number].tv_sec = 0; available_window += 1; last_received = rcv_pkt.seq_number % (window_size+1); } else { //printf("Received ACK with seq = %d, expecting = %d\n", rcv_pkt.seq_number, (last_received + 1) % window_size); } } // Check available window. If available, dequeue and send a packet. if(available_window > 0 && q.filled != 0) { bytequeue_pop(&q, &pkt); total_attempts++; pkt.seq_number = (seq_no) % (window_size+1); pkt.timestamp = curr_time; pkt.total_attempts = total_attempts; pkt.timeout = rtt; read_count = fread(pkt.payload, sizeof(char), sizeof(pkt.payload), fd); //printf("Sending. Seq_no == %d, stream == %c\n", pkt.seq_number, pkt.stream); serialize_packet(buff, pkt); //Store the packet into the packets buffer for possible transmission memcpy(&packets[pkt.seq_number], &pkt, sizeof(pkt)); sendto(send_sock, buff, sizeof(pkt), 0, p->ai_addr, p->ai_addrlen); pkt = deserialize_packet(buff); available_window -= 1; if (feof(fd)) break; if(ferror(fd)) { fprintf(stderr, "error: %s\n", strerror(errno)); exit(3); } // Set this timeout gettimeofday(&timeouts[pkt.seq_number], NULL); seq_no++; } } printf("Total errors: %d. Total total:%d\n", errors, total_attempts); close(send_sock); close(recv_sock); exit(0); }
int main(int argc, char *argv[]){ if(argc != 2){ printf("usage: port\n"); exit(0); } int status; int sockfd; struct addrinfo hints; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; struct addrinfo *res, *p; if ((status = getaddrinfo(NULL, argv[1], &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } for(p = res; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("listener: socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("listener: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); return 2; } struct sockaddr src_addr; int src_len = sizeof src_addr; char buff[BUFFER_SIZE]; int read_count; int write_count; int firsttime = 1; bytequeue queue; if (bytequeue_init(&queue, sizeof(buff), QUEUE_MAX_PACKETS) < 0) { printf("Queue error.\n"); return 3; } struct timeval timeout; while (1) { timeout.tv_sec = 5; timeout.tv_usec = 0; time_t starttime = time(NULL); time_t looptime = time(NULL); while(timeout.tv_sec > 0) { setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); read_count = recvfrom(sockfd, &buff, BUFFER_SIZE, 0, &src_addr, &src_len); if(read_count <= 0) { /*printf("Socket timeout.\n");*/ } else if(strcmp(buff, BLINK_MSG) == 0){ if (bytequeue_push(&queue, buff) < 0) printf("Buffer overflow.\n"); } else { printf("Unknown packet received: %s\n", buff); } time_t currtime = time(NULL); timeout.tv_sec -= difftime(currtime, starttime); starttime = currtime; } if (bytequeue_pop(&queue, buff) < 0) { printf("Buffer underflow.\n"); } else { printf(">>>>>BLINK: Delay is %f seconds.\n", difftime(time(NULL), looptime)); } } freeaddrinfo(res); close(sockfd); exit(0); }