Example #1
0
	void PacketReader::update(PeerInterface & peer)
	{
		if (error)
			return;

		IncomingPacket::Ptr pck = dequeuePacket();
		while (pck)
		{
			peer.handlePacket(pck->data.data(), pck->size);
			pck = dequeuePacket();
		}
	}
Example #2
0
static void fire_queue(CnetEvent ev, CnetTimer timer, CnetData data) {
    printf("Firing Packet From Queue\n");
    packet_t temp = dequeuePacket();
    if( temp.length != -1) {
        printf("Sending Packet From Queue\n");
        down_to_network( temp.msg, temp.length, temp.dest);
        printf("Packet send to network layer\n");
    }
}
Example #3
0
int main(int argc, char **argv)
{
	srand(time(NULL));
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);
	if(!checkArgs(argc, argv, &args)) {
		fprintf(stderr,"Invalid Arguments\n");
		exit(1);
	}
	
	log_fd = fopen(args.fname, "w");
	
	printf("Emulator Launched");
	
	q1_index = q2_index = q3_index = 0;
	q1 = (queued_packet*) malloc(args.queue_size * sizeof(queued_packet));
	q2 = (queued_packet*) malloc(args.queue_size * sizeof(queued_packet));
	q3 = (queued_packet*) malloc(args.queue_size * sizeof(queued_packet));
	
	//sock = openUDPSocket(); Testing only
	sock = openRawSocket();
	
	if(bind(sock,(struct sockaddr*) &(args.sin), sizeof(args.sin)) < 0) {
		perror("Failed to bind to UDP socket");
		exit(errno);
	}
	
	m1 = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	pthread_mutex_init(m1,NULL);
	m2 = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	pthread_mutex_init(m2,NULL);
	m3 = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	pthread_mutex_init(m3,NULL);
	
	pthread_t mThread;
	if(pthread_create(&mThread, NULL, tMain, (void*) NULL)) {
		perror("Unable to create thread");
		exit(errno);
	}
	while(1) {
		unsigned long time = getMillis();
		
		while(q1_index > 0 && q1[0].timestamp < time) {
			if(q1[0].timestamp == 0) {
				unsigned long delay = (10 * (rand() % 110)) + 100;
				q1[0].timestamp = time + delay;
			}else if(q1[0].timestamp <= time) {
				dequeuePacket(q1, m1, &q1_index);
			}
		}
		while(q2_index > 0 && q2[0].timestamp < time) {
			if(q2[0].timestamp == 0) {
				unsigned long delay = (10 * (rand() % 110)) + 100;
				q2[0].timestamp = time + delay;
			}else if(q2[0].timestamp <= time) {
				dequeuePacket(q2, m1, &q2_index);
			}
		}
		while(q3_index > 0 && q3[0].timestamp < time) {
			if(q3[0].timestamp == 0) {
				unsigned long delay = (10 * (rand() % 110)) + 100;
				q3[0].timestamp = time + delay;
			}else if(q3[0].timestamp <= time) {
				dequeuePacket(q3, m1, &q3_index);
			}
		}
	}
	pthread_cancel(mThread);
	fclose(log_fd);
	return 0;
}