#ifndef CONNECTION_H
#define CONNECTION_H
#include <string>
#include <iostream>
#include <vector>
#include "BufferedFile.h"
/**
 * Connection Interface (5.1.1.1)
 * disconnect() Disconnect Connection from Connector
 * getName(): string Get Connections Name sent from Header.
 * readable(): booleanCan this connection be read from. Not to be confused with polling for data.
 * writable(): booleanCan this connection be written to? Not to be confused with polling for non blocking writes.
 * write(data:byte [],size: int)Writes data out to connection.
 * read(data:byte [],maxsize: int)Reads data from a connection. 
 */ 
template<class SAMPLE>
class Connection {
	public:
	/**
	 * Disconnect the current connection
	 * */
	virtual void   disconnect() {
		file->Close();
	}
	/**
	 * Get the Connection's Name
	 * */
	virtual string getName() {
		return connectionName;
	}
	/**
	 * Is the connection readable
	 * */
	virtual bool   readable() {
		return true;
	}
	/**
	 * Is the connection writable;
	 * */
	virtual bool   writable() {
		return true;
	}
	/**
	 * write size samples of data to the Connection
	 * */
	virtual int write(const SAMPLE * data, int size) {
		return file->Write((char *)data,sizeof(SAMPLE)*size);
	}
	/**
	 * read size samples into data from the Connection
	 * */
	virtual int read(SAMPLE * data, int size){
		return file->Read((char *) data,sizeof(SAMPLE)*size);
	}
	/**
	 * gets the file Handle of this connection
	 * */
	virtual int getFileHandle() {
		return file->getFileHandle();
	}
	/**
	 * get the string type reprentation
	 * */
	virtual string getType(){
		return type;
	}
	/**
	 * set the string type
	 * */
	virtual void setType(string type) {
		this->type = string(type);
	}
	/**
	 * Basic constructor
	 * */
	Connection()  {
		//NO INIT CALLED
	}
	/**
	 * Constructor which initalizes with the buffered file
	 * */
	Connection(BufferedFile * fd) {
		init(fd,string("Unnamed"));
	}
	/**
	 * Constructor which initalizes with the buffered file and name
	 * */
	Connection(BufferedFile * fd,string name) {
		init(fd,name);
	}
	/**
	 * Destructor
	 * */
	virtual ~Connection() { 
		//cout << "~Connection()\n";
		delete file;
	}
	/**
	 * Is the connection ready to read w/o blocking
	 * */
	virtual bool readReady() {
		return file->readReady();
	}
	/**
	 * get the number of bytes per sample of this connection type.
	 * */
	virtual int getDataSize() {
		return 1*sizeof(SAMPLE);
	}
	/**
	 * Is the connection ready to write w/o blocking
	 * */
	virtual bool writeReady() {
		return file->writeReady();
	}
	/**
	 * Is the unlying file closed?
	 * */
	virtual bool isClosed() {
		return file->isClosed();
	}
	protected:
	string type;
	string connectionName;
	BufferedFile * file;
	void init(BufferedFile * fd,string name){
		connectionName = string(name);
		this->file = fd;
		type = "short";
	}
};
#endif
