#ifndef CONNECTOR_H
#define CONNECTOR_H
#include <list>
#include <string>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <iostream>
#include "Connection.h"
#include "InputConnection.h"
#include "OutputConnection.h"
#include "FilterConnection.h"
#include "ConnectionFactory.h"
#include "ConnectionWrap.h"
#include "MessageExecutor.h"
#include "BufferedFileFactory.h"
#include "Clock.h"
/**
 * The Connector is the main class of the Connector.
 * It is the controller or handler.
 */
class Connector {
	public:
		/**
		 * The Connector empty Constructor, uses the default port to listen on
		 * */
		Connector();
		/**
		 * Destructor, disconnects all connections and stops listening on ports.
		 * */
		virtual ~Connector();
		/**
		 * Constructor, specify a starting port
		 * */
		Connector(int start);
		/**
		 * Constructor, explicitly specify inputPort, outputPort, filterPort, configPort.
		 * */
		Connector(int inputPort,int outputPort,int filterPort, int configPort);
		/**
		 * lastUpdate returns when last updated
		 * */
		int lastUpdate();
		/**
		 * sends the last Change Status to the Current ConfigConnection
		 * */
		void lastChange();
		/**
		 * update the time of last Updated. Must be called when state changes.
		 * */
		void updated();
		/**
		 * Retrieves a list of non config connections.
		 * */
		list<ConnectionWrap *> * getConnectionList();
		/**
		 * disconnect from filehandle fd (e.g. connection id).
		 * */
		void disconnect(int fd);
		/**
		 * unpatch from filehandle fhFrom to fhTo
		 * */
		void unPatch(int fhFrom, int fhTo);
		/**
		 * patch a connection between fhFrom to fhTo
		 * */
		void patch(int fhFrom,int fhTo);
		/**
		 * sends the update to the current Config Connection
		 * */
		void sendUpdate();
		/**
		 * Process - iterates through one cycle of move audio, check connections and disconnections.
		 * */
		void process();
		/**
		 * Tells the server to shutdown, not necessarily immediately.
		 * */
		void shutDown();
		/**
		 * is the Connector done (shutdown)?
		 * */
		bool done();
	private:
		bool quit;
		void * messageExecutor;
		list<ConnectionWrap *> * allConnections; //FilterConnections and subclasses
		list<ConfigConnection *> * configConnections; //ConfigConnections
		ConfigConnection * currentConfig;
		ConnectionFactory * connectionFactory;
		BufferedFileFactory * bf;
		void init(int inputPort,int outputPort,int filterPort, int configPort);
		int lastUpdated;
		int processUpdate;
};
#include "ConnectorSingleton.h"
#endif
