Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

catcher.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "headers.h"
00008 
00012 Catcher::Catcher()
00013 {
00014 } // ctor
00015 
00016 
00020 Catcher::~Catcher()
00021 {
00022     deleteAll();
00023 } // detour
00024 
00025 
00029 Node*
00030 Catcher::isDup(IpAddress ipAddr)
00031 {
00032     Guard guard(&m_nodeListLock);
00033 
00034     vector<Node*>::const_iterator nodeIterator;
00035 
00036     for(nodeIterator = m_nodeList.begin(); nodeIterator != m_nodeList.end(); ++nodeIterator) {
00037         if(((*nodeIterator) != NULL) &&  ((*nodeIterator)->getSocketAddress()->getIpAddress().equals(ipAddr))) {
00038             return (*nodeIterator);
00039         }
00040     }
00041     return NULL;
00042 } // fn isDup
00043 
00044 
00052 int
00053 Catcher::addNode(Node* node) {
00054     // check if it is a duplicate
00055     Node* existingNode;
00056     if((existingNode = isDup(node->getSocketAddress()->getIpAddress())) != NULL) {
00057         return 0;
00058     }
00059      
00060     // check if there is enough room in the list to add another one.
00061     // if there isnt, try to free up space for a new node.
00062     m_nodeListLock.lock();
00063     if ((int)m_nodeList.size() >= GlobalObjects::instance()->getConfig()->getMaxHosts()) {
00064         if (!deleteNode()) {
00065             debug(DEBUG_CATCHER, "Could not add a node - out of space and could find one bad enough to delete");
00066             m_nodeListLock.unlock();
00067             return -1;
00068         }
00069     }
00070 
00071     // copy the node passed in
00072     Node* newNode = new Node();
00073     *newNode = *node;
00074 
00075     // Add node to the end of the list
00076     m_nodeList.push_back(newNode);
00077 
00078     // save list
00079     this->save();
00080 
00081     m_nodeListLock.unlock();
00082 
00083     fireEvent(0);
00084 
00085     return true;
00086 } // fn addNode
00087 
00088 
00092 int
00093 Catcher::getNumberOfNodes() {
00094     return m_nodeList.size();
00095 } // fn getNumberOfNodes
00096 
00097 
00103 bool
00104 Catcher::getNode(int index, Node* node) {
00105     Guard guard(&m_nodeListLock);
00106 
00107     if ((index >= (int)m_nodeList.size()) || (index < 0)) {
00108         return false;
00109     }
00110 
00111     *node = *(m_nodeList[index]);
00112     return true;
00113 } // fn getNode
00114 
00115 
00126 Node*
00127 Catcher::getUnconnectedNode()
00128 {
00129     Guard guard(&m_nodeListLock);
00130 
00131     if (m_nodeList.size() == 0) {
00132         return NULL;
00133     }
00134 
00135     int i;
00136     int unconnectedNodes = 0;
00137     // figure out the number of unconnected nodes
00138     for (i = 0; i < (int)m_nodeList.size() ; ++i) {
00139         if (!m_nodeList[i]->isConnected() && !m_nodeList[i]->cantConnect()) {
00140             ++unconnectedNodes;
00141         }
00142     }
00143 
00144     // if there arent any unconnected nodes left, we're done
00145     if (unconnectedNodes == 0) {
00146         return NULL;
00147     }
00148 
00149     // select an unconnected node at random
00150     int selectedNode = m_randomNumberGenerator.IRandom(0, (unconnectedNodes - 1) );
00151 
00152     // find that node and return it
00153     int counter = -1;
00154     for (i = 0; i < (int)m_nodeList.size(); ++i) {
00155         if (!m_nodeList[i]->isConnected() && !m_nodeList[i]->cantConnect()) {
00156             ++counter;
00157         }
00158         if (counter == selectedNode) {
00159             return m_nodeList[i];
00160         }
00161     }
00162 
00163     return NULL;
00164 } // fn getUnconnectedNode
00165 
00166 
00170 /*
00171 Node*
00172 Catcher::getConnectedNode()
00173 {
00174 
00175         int connectedNodes[MAX_CONNECTIONS];
00176         int j=0; // counter for how many connected nodes there are
00177     Guard guard(&m_nodeListLock);
00178     vector<Node*>::const_iterator nodeIterator;
00179 
00180     if (m_nodeList.size() == 0) {
00181         return NULL;
00182     }
00183     
00184     int i = 0;
00185     for(nodeIterator = m_nodeList.begin(); nodeIterator != m_nodeList.end(); ++nodeIterator) {
00186         if(((*nodeIterator)->isConnected()) && (j < MAX_CONNECTIONS)) {
00187             connectedNodes[j++] = i;
00188         }
00189         i++;
00190     }
00191         if (j != 0) {
00192                 return m_nodeList[getRand(0,j-1)];
00193         }
00194 
00195     return NULL;
00196 } // fn getNode
00197 */
00198 
00199   
00211 bool
00212 Catcher::deleteNode()
00213 {
00214     bool success = false;
00215     Guard guard(&m_nodeListLock);
00216 
00217     vector<Node*>::iterator iter;
00218 
00219     for (iter = m_nodeList.begin(); iter != m_nodeList.end(); ++iter) {
00220         // First priority: delete one you cant connect to
00221         if ((*iter)->cantConnect()) {
00222                 if ((*iter)->isConnected() == true) {
00223                     debug(DEBUG_CATCHER, "deleteNode: strange: connected to History_CantConnect node?!");
00224                 continue;
00225             }
00226             delete (*iter);
00227             m_nodeList.erase(iter);
00228             success = true;
00229             break;
00230         }
00231     }
00232     return success;
00233 } // fn deleteNode
00234 
00235 
00239 bool
00240 Catcher::deleteNode(Node* node) {
00241     bool success = false;
00242     Guard guard(&m_nodeListLock);
00243 
00244     vector<Node*>::iterator iter;
00245 
00246     for (iter = m_nodeList.begin(); iter != m_nodeList.end(); ++iter) {
00247         // First priority: delete one you cant connect to
00248         if ((*iter)->equals(node)) {
00249                 if ((*iter)->isConnected() == true) {
00250                 // cant delete a node that is connected
00251                 break;
00252             }
00253             else {
00254                 delete (*iter);
00255                 m_nodeList.erase(iter);
00256                 success = true;
00257                 this->save();
00258                 break;
00259             }
00260         }
00261     }
00262     return success;
00263 } // fn deleteNode
00264 
00265 
00269 void 
00270 Catcher::deleteAll() {
00271     Guard guard(&m_nodeListLock);
00272     int index; 
00273     if (m_nodeList.size() > 0) {
00274         for (index = 0; index < (int)m_nodeList.size(); ++index) {
00275             if (m_nodeList[index] != NULL) {
00276                 delete m_nodeList[index];
00277             }
00278         }
00279         m_nodeList.clear();
00280     }
00281 } // fn deleteAll
00282 
00283 
00287 int
00288 Catcher::load() {
00289     return load(DEFAULT_HOST_FILE);
00290 } // fn load
00291 
00292 
00298 int
00299 Catcher::load(string path)
00300 {
00301     Guard guard(&m_nodeListLock);
00302     std::ifstream in(path.c_str());
00303 
00304     if (!in.is_open()) {
00305         debug(DEBUG_CATCHER, "ERROR: Could not open file: %s", path);
00306         return false;
00307     }
00308 
00309     deleteAll();
00310 
00311     // read in the new list from the file
00312     Node* node = new Node();
00313     in >> *node;
00314     while (!in.eof()) {
00315         m_nodeList.push_back(node);
00316         node = new Node();
00317         in >> *node;
00318     }
00319 
00320     delete node;
00321     in.close();
00322     return 1;
00323 } // fn load
00324 
00325 
00329 bool
00330 Catcher::save() {
00331     return save(DEFAULT_HOST_FILE);
00332 } // fn save
00333 
00334 
00339 bool
00340 Catcher::save(string path)
00341 {
00342     Guard guard(&m_nodeListLock);
00343     ofstream out(path.c_str());
00344     out << *this;
00345     out.close();
00346     if (out.fail()) {
00347         debug(DEBUG_CATCHER, "Failed to write to output file.");
00348         return false;
00349     }
00350     return true;
00351 } // fn save
00352 
00353 
00360 Node*
00361 Catcher::lookup(IpAddress ipAddr)
00362 {
00363     Guard guard(&m_nodeListLock);
00364 
00365     for (int i = 0; i < (int)m_nodeList.size(); ++i) {
00366         if ((m_nodeList[i] != NULL) && (m_nodeList[i]->getSocketAddress()->getIpAddress().equals(ipAddr))) {
00367             return m_nodeList[i];
00368         }
00369     }
00370     return NULL;
00371 } // fn lookup
00372 
00373 
00374 void
00375 Catcher::toStream(std::ostream& out) {
00376     for (int j = 0; j < (int)m_nodeList.size(); j++) {
00377         if (m_nodeList[j] != NULL) {
00378             out << *m_nodeList[j];
00379         }
00380     }
00381 } // fn toStream
00382 
00383 

Generated at Thu Jul 11 13:31:49 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001