00001 #include "Connector.h"
00002 #define DEFAULT_PORT 6780
00003
00004 Connector::Connector() {
00005 init(DEFAULT_PORT,DEFAULT_PORT+1,DEFAULT_PORT+2,DEFAULT_PORT+3);
00006 }
00007 Connector::~Connector() {
00008 {
00009 vector<ConnectionWrap *>::iterator iter;
00010 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00011 allConnections->erase(iter);
00012 delete((*iter));
00013 }
00014 }
00015 {
00016 vector<ConfigConnection *>::iterator iter;
00017 delete allConnections;
00018 for (iter = configConnections->begin(); iter < configConnections->end(); iter++) {
00019 configConnections->erase(iter);
00020 delete((*iter));
00021 }
00022 }
00023 delete connectionFactory;
00024 }
00025 Connector::Connector(int start) {
00026 init(start,start+1,start+2,start+3);
00027 }
00028 Connector::Connector(int inputport,int outputport,int filterport, int configport) {
00029 init(inputport,outputport,filterport,configport);
00030 }
00031 void Connector::init(int inputport,int outputport,int filterport, int configport) {
00032 connectionFactory = new ConnectionFactory(inputport,outputport,filterport,configport);
00033 updated();
00034 allConnections = new vector<ConnectionWrap *>();
00035 messageExecutor = new MessageExecutor();
00036 configConnections = new vector<ConfigConnection *>();
00037 ConnectorSingleton::connector = this;
00038 }
00039 int Connector::lastUpdate() {
00040 return lastUpdated;
00041 }
00042 void Connector::updated() {
00043 lastUpdated = time(NULL);
00044 }
00045 vector<ConnectionWrap *> * Connector::getConnectionList() {
00046 return allConnections;
00047 }
00048 void Connector::disconnect(int fd) {
00049 vector<ConnectionWrap *>::iterator iter;
00050 int fh;
00051 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00052
00053 fh = (*iter)->getFileHandle();
00054 if (fd == fh) {
00055 (*iter)->disconnect();
00056 allConnections->erase(iter);
00057 delete (*iter);
00058 updated();
00059 return;
00060 }
00061 }
00062
00063 throw new string("No connection found. Could not disconnect connection");
00064 }
00065 void Connector::unPatch(int from, int to) {
00066 vector<ConnectionWrap *>::iterator iter;
00067 ConnectionWrap * fromConn = NULL;
00068 ConnectionWrap * toConn = NULL;
00069 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00070 int fd = (*iter)->getFileHandle();
00071 if (fd == from) {
00072 fromConn = (*iter);
00073 } else if (fd == to) {
00074 toConn = (*iter);
00075 }
00076 }
00077 if (fromConn == NULL || toConn == NULL) {
00078 throw new string("There is no patch between these connections!");
00079 }
00080 toConn->disconnectFrom(fromConn);
00081 updated();
00082 }
00083 void Connector::patch(int from, int to) {
00084 vector<ConnectionWrap *>::iterator iter;
00085 ConnectionWrap * fromConn = NULL;
00086 ConnectionWrap * toConn = NULL;
00087 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00088 int fd = (*iter)->getFileHandle();
00089 if (fd == from) {
00090 fromConn = (*iter);
00091 if (toConn!=NULL) { break;}
00092
00093 } else if (fd == to) {
00094 toConn = (*iter);
00095 if (fromConn!=NULL) { break;}
00096 }
00097 }
00098 if (fromConn == NULL || toConn == NULL) {
00099 throw new string("There is no patch between these connections!");
00100 }
00101 if (!fromConn->readable()) {
00102 throw new string("This is not a valid patch!");
00103 }
00104 toConn->connectFrom(fromConn);
00105 updated();
00106 }
00107 void Connector::sendUpdate() {
00108 if (currentConfig==NULL) {
00109 vector<ConfigConnection *>::iterator iter;
00110 for (iter = configConnections->begin(); iter < configConnections->end(); iter++) {
00111 currentConfig = (*iter);
00112 break;
00113 }
00114 if (currentConfig==NULL) {
00115 return;
00116 }
00117 }
00118 string final = "<update>";
00119 vector<ConnectionWrap *>::iterator iter;
00120 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00121 final += *((*iter)->getXML());
00122 }
00123 final += "<time>";
00124 final += lastUpdate();
00125 final += "</time></update>";
00126 currentConfig->write(final.c_str(),final.size());
00127 }
00128
00129 void Connector::process() {
00130
00131
00132
00133 vector<ConnectionWrap *>::iterator iter;
00134 for (iter = allConnections->begin(); iter < allConnections->end(); iter++) {
00135 (*iter)->process();
00136 }
00137 vector<ConfigConnection *>::iterator citer;
00138 for (citer = configConnections->begin(); citer < configConnections->end(); citer++) {
00139 if ((*citer)->isMessage()) {
00140 currentConfig = (*citer);
00141 Message m = (*citer)->getMessage();
00142 try {
00143 ((MessageExecutor*)messageExecutor)->executeMessage(m);
00144 } catch (string * err) {
00145 string error = "<error>";
00146 error += *err;
00147 error += "</error>";
00148 currentConfig->write(error.c_str(),error.size());
00149 }
00150 }
00151 }
00152 if (connectionFactory->isThereANewConnection()) {
00153 ConnectionWrap * wrap = connectionFactory->processConnection();
00154 if (wrap->type == CONFIGCONN) {
00155 configConnections->push_back(wrap->conn.configConn);
00156 } else {
00157 allConnections->push_back(wrap);
00158 }
00159 updated();
00160 }
00161 }