#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define BUF_SIZE 10

int buf = 0; // Number of packets currently in buffer.

void do_leak (int x) {

    //this function is the signal handler;
    //everytime it is executed, it lets out
    //one packet in the buffer
    if (buf > 0) {
	printf ("\nLetting out one packet... \n");
	printf ("Buffer is now filled to %d packets.\n", --buf);
    }

    // Set the next alarm.
    alarm (1);
    return;
}

int main () {

    printf ("Hit an enter to send a packet to the buffer...\n\n");
    
    // set the signal handler and the alarm
    signal (SIGALRM, do_leak);
    alarm (1);
    
    while (1) {
	scanf ("%*c");
	if (buf == BUF_SIZE)
	    printf ("Packet discarded...\n");
	else {
	    buf++;
	    printf ("Buffer size incremented to %d.\n", buf);
	}
    }

    return 0;
}

