#include "Connector.h" #define DEFAULT_PORT 6780 Connector::Connector() { init(DEFAULT_PORT,DEFAULT_PORT+1,DEFAULT_PORT+2,DEFAULT_PORT+3); } Connector::~Connector() { { vector::iterator iter; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { allConnections->erase(iter); delete((*iter)); } } { vector::iterator iter; delete allConnections; for (iter = configConnections->begin(); iter < configConnections->end(); iter++) { configConnections->erase(iter); delete((*iter)); } } delete connectionFactory; } Connector::Connector(int start) { init(start,start+1,start+2,start+3); } Connector::Connector(int inputport,int outputport,int filterport, int configport) { init(inputport,outputport,filterport,configport); } void Connector::init(int inputport,int outputport,int filterport, int configport) { connectionFactory = new ConnectionFactory(inputport,outputport,filterport,configport); updated(); allConnections = new vector(); messageExecutor = new MessageExecutor(); configConnections = new vector(); ConnectorSingleton::connector = this; } int Connector::lastUpdate() { return lastUpdated; } void Connector::updated() { lastUpdated = time(NULL); } vector * Connector::getConnectionList() { return allConnections; } void Connector::disconnect(int fd) { vector::iterator iter; int fh; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { fh = (*iter)->getFileHandle(); if (fd == fh) { (*iter)->disconnect(); allConnections->erase(iter); delete (*iter); updated(); return; } } //ERROR throw new string("No connection found. Could not disconnect connection"); } void Connector::unPatch(int from, int to) { vector::iterator iter; ConnectionWrap * fromConn = NULL; ConnectionWrap * toConn = NULL; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { int fd = (*iter)->getFileHandle(); if (fd == from) { fromConn = (*iter); } else if (fd == to) { toConn = (*iter); } } if (fromConn == NULL || toConn == NULL) { throw new string("There is no patch between these connections!"); } toConn->disconnectFrom(fromConn); updated(); } void Connector::patch(int from, int to) { vector::iterator iter; ConnectionWrap * fromConn = NULL; ConnectionWrap * toConn = NULL; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { int fd = (*iter)->getFileHandle(); if (fd == from) { fromConn = (*iter); if (toConn!=NULL) { break;} } else if (fd == to) { toConn = (*iter); if (fromConn!=NULL) { break;} } } if (fromConn == NULL || toConn == NULL) { throw new string("There is no patch between these connections!"); } if (!fromConn->readable()) { throw new string("This is not a valid patch!"); } toConn->connectFrom(fromConn); updated(); } void Connector::sendUpdate() { if (currentConfig==NULL) { vector::iterator iter; for (iter = configConnections->begin(); iter < configConnections->end(); iter++) { currentConfig = (*iter); break; } if (currentConfig==NULL) { return; //No ConfigConnections } } string final = ""; vector::iterator iter; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { final += *((*iter)->getXML()); } final += ""; currentConfig->write(final.c_str(),final.size()); } void Connector::process() { //Call process on data connections //Check config connections //Check for new connections vector::iterator iter; for (iter = allConnections->begin(); iter < allConnections->end(); iter++) { (*iter)->process(); } vector::iterator citer; for (citer = configConnections->begin(); citer < configConnections->end(); citer++) { if ((*citer)->isMessage()) { currentConfig = (*citer); Message m = (*citer)->getMessage(); try { ((MessageExecutor*)messageExecutor)->executeMessage(m); } catch (string * err) { string error = ""; error += *err; error += ""; currentConfig->write(error.c_str(),error.size()); } } } if (connectionFactory->isThereANewConnection()) { ConnectionWrap * wrap = connectionFactory->processConnection(); if (wrap->type == CONFIGCONN) { configConnections->push_back(wrap->conn.configConn); } else { allConnections->push_back(wrap); } updated(); } }