
//SOLUZIONE PROPOSTA DA FRANCESCA

#include <iostream>
#include <JTC/JTC.h>


#define NM 3	//numero mail server
#define UT 5	//numero utenti
#define LEN 10 //dimensione casella di posta (buffer)

template <class T>
class Buffer : public JTCMonitor

{
	T thebuffer[LEN];
	int in;
	int out;
	int chars;

public:
	Buffer(T initValue)
	{
		in = 0;
		out = 0;
		chars = 0;
		for(int i = 0; i < LEN; i++)
			thebuffer[i] = initValue;
	}

	T get()
	{
		JTCSynchronized sync(*this);
		while (chars == 0) {
			wait();
		}
		chars--; 
		T c = thebuffer[out];
		out = ( out + 1 ) % LEN;
		//cout << "Chars=" << chars << " Out=" << out << endl;
		//printBuffer();
		notify();
		return c;
	}
 
	void put(T c)
	{
		JTCSynchronized sync(*this);
		while (chars == LEN) {
			wait();
		}
		chars++;
        thebuffer[in] = c;
		in = (in + 1) % LEN;
	    	//cout << "Chars=" << chars << " In=" << in << endl;
		//printBuffer();
		notify();
	}
};

class Messaggio 
{
	 char* testoMsg;
	 int IDMittente;
	 int IDDestinatario;

public: 
	Messaggio()
	{
		testoMsg= "";
		IDMittente= -1;
		IDDestinatario=-1;
		
	}
	
	Messaggio(char* testo, int mittente, int destinatario)
	{
		testoMsg = testo;
		IDMittente = mittente;
		IDDestinatario = destinatario;
	}

	int getDestinatario()
	{
		return IDDestinatario;
	}
	
	int getMittente()
	{
		return IDMittente;
	}
};

class CentroMsg : public JTCMonitor
{
	Buffer<Messaggio> * casellaDiPosta;//di dimensione LEN
	Buffer<Messaggio> * mailBox[NM]; 

public:

	CentroMsg ()
	{
		for( int i=0; i<NM; i++)
			mailBox[i] = new Buffer<Messaggio>(Messaggio());

		casellaDiPosta = new Buffer<Messaggio>(Messaggio());

	}
	
		
	void putMsgCP(Messaggio m)
	{
		casellaDiPosta->put(m);
	}

	Messaggio getMsgCP()
	{
		return casellaDiPosta->get();
	}

	void putMsgMB (Messaggio m, int serverID)
	{
		mailBox[serverID]->put(m);
	}

	Messaggio getMsgMB (int serverID)
	{
		return mailBox[serverID]->get();
	}

};

CentroMsg* centro = new CentroMsg();

class Utente : public JTCThread
{
   int id;

public:
	Utente(int i)
	{
		id=i;        
	}
	
	virtual void run ()
	{
		while(true)
		{
			sleep( rand()%  3000);
			
			int destinatario=rand()%NM;

			printf("l'utente %i inserisce un messaggio per il server %i nel centro messaggi. \n ",id,destinatario);
			
			Messaggio m("sms",id, destinatario);
			
			centro->putMsgCP(m);
				
			printf("l'utente %i va via. \n ",id);
			
		}
	}

};


class Smistatore : public JTCThread
{
	int id;

public:

	Smistatore(int i)
	{
		id = i;
	}

	virtual void run ()
	{

		while(true){
			
			sleep(rand() % 1000);

			printf("lo smistatore è in attesa di prelevare un messaggio dalla casella di posta.\n ");

			Messaggio m = centro->getMsgCP();

			sleep(rand() % 500);//tempo di elaborazione

			int dest= m.getDestinatario();

			int mitt= m.getMittente();

			printf("lo smistatore inserisce il messaggio dell'utente %i nella mail box %i.\n ",mitt,dest);

			centro->putMsgMB(m,dest);

		}
	
	}

};

class MailServer : public JTCThread
{

	int id;

public:

	MailServer(int i)
	{
		id=i;	
	}

virtual void run()
{
	while(true)
	{
		sleep(rand() % 1000);
		
		printf("il mail server %i è in attesa di prelevare un messaggio dalla sua mail box.\n ",id);

		Messaggio messaggio=centro->getMsgMB(id);
		
		int mittente=messaggio.getMittente();	
		
		sleep(rand() % 300);//tempo elaborazione messaggio

		printf("il mail server %i preleva ed elabora il messaggio dell'utente %i.\n ",id,mittente);

		
        
			
	}
}
};

int main(int argc, char* argv[])

{   
	JTCInitialize initialize;
       
	Utente* u[UT];
	Smistatore* s = new Smistatore(0);
	MailServer* ms[NM];

       s->start();

       for (int i = 0; i < UT; i++) {
		u[i] = new Utente(i);
		u[i]->start();
	}
	for (int i = 0; i < NM; i++) {
		ms[i] = new MailServer(i);
		ms[i]->start();
	}

	

	return 0;	
}
