/*
 * main.cpp
 *
 *  Created on: Jun 16, 2011
 *      Author: ianni
 */
#include <JTC/JTC.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define LEN 40

template <class T>
class Buffer : public JTCMonitor

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

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

	T get()
	{
		JTCSynchronized sync(*this);
		while (elements == 0) {
			wait();
		}
		elements--;
		T c = thebuffer[out];
		out = ( out + 1 ) % LEN;
		notify();
		return c;
	}

	void put(T c)
	{
		JTCSynchronized sync(*this);
		while (elements == LEN) {
			wait();
		}
		elements++;
        thebuffer[in] = c;
		in = (in + 1) % LEN;
		notify();
	}
	//
	// Fails instead of blocking if buffer full
	//
	bool nonBlockingPut(T c)
	{
		JTCSynchronized sync(*this);
		if (elements == LEN) {
			return false;
		}
		elements++;
        thebuffer[in] = c;
		in = (in + 1) % LEN;
		notify();
		return true;
	}

};

class SBox;

class SProcessor : public JTCThread
{
	SBox *S;
public:
	SProcessor (SBox* s);
	virtual void run();
};

class RandomFrameGenerator : public JTCThread

{
	Buffer<string*>* line;
	char c;
public:
	RandomFrameGenerator(Buffer<string*>* l, char c);
	virtual void run();
};


class SBox : public JTCMonitor
{
	int MEMconnection; // 1 = IN1, 2 = IN2
	int OUTconnection; // 0 = MEM, 1 = IN1, 2 = IN2

public:
	Buffer<string*> IN1;
	Buffer<string*> IN2;
	Buffer<string*> MEM;

	SBox() : IN1(0) , IN2(0) , MEM(0) {};
	void start()
	{
		MEMconnection = 1;
		OUTconnection = 1;
		SProcessor* s = new SProcessor(this);
		RandomFrameGenerator* r1 = new RandomFrameGenerator(&IN1,'@');
		RandomFrameGenerator* r2 = new RandomFrameGenerator(&IN2,'+');
		s->start();
		r1->start();
		r2->start();
	}
	void setOut(int c)
	{
		JTCSynchronized sync(*this);
		OUTconnection = c;
	}
	void setMem(int c)
	{
		JTCSynchronized sync(*this);
		MEMconnection = c;
	}
	int getOut()
	{
		JTCSynchronized sync(*this);
		return OUTconnection;
	}
	int getMem()
	{
		JTCSynchronized sync(*this);
		return MEMconnection;
	}

};

SProcessor::SProcessor (SBox* s)
	{
		S = s;
	}
void SProcessor::run()
	{
		while(true)
		{
			sleep(1000/50); // Sleeps 1/50-th of second;
			string* linea1 = S->IN1.get();
			string* linea2 = S->IN2.get();
			int mem = S->getMem();
			switch(mem)
			{
				//
				// Evita di bloccare lo SProcessor se MEM si riempie.
				//
				case 1:
					if (!S->MEM.nonBlockingPut(linea1))
						cout << "MEM is full" << endl;

					break;
				case 2:
					if (!S->MEM.nonBlockingPut(linea2))
						cout << "MEM is full" << endl;
					break;
			}
			int out = S->getOut();
			switch (out)
			{
				case 0:
					cout << *(S->MEM.get());
					break;
				case 1:
					cout << *linea1;
					break;
				case 2:
					cout << *linea2;
					break;
			}
		}
	}



	RandomFrameGenerator::RandomFrameGenerator(Buffer<string*>* l, char c)
	{
		line = l;
		this->c = c;
	}
	void RandomFrameGenerator::run()
	{
		srand(time(0)*c);
		string theLine;
		while(true)
		{
			//cout << "Cycle " << c << endl;
			for (int i=0; i < 80; i++)
				if (rand() % 2 >= 1)
					theLine += c;
				else theLine += '-';
			theLine += '\n';
			int framerate = 40 + rand() % 21; // random frame rate from 1/40-th to 1/60-th sec.
			sleep(1000/framerate);
			line->put(&theLine);
		}
	}


int main()

{
	JTCInitialize init;
	SBox* s = new SBox();
	s->start();
	JTCThread::sleep(5000);
	s->setOut(2);
	JTCThread::sleep(5000);
	s->setOut(1);
	JTCThread::sleep(5000);
	s->setOut(0);
	cout << "FINE" << endl;
}
