Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

l1394_nodecontainer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           l1394nodecontainer.cpp  -  description
00003                              -------------------
00004     begin                : Fri Sep 8 2000
00005     copyright            : (C) 2000-2004 by Michael Repplinger
00006     email                : repplinger@cs.uni-sb.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "l1394_nodecontainer.h"
00019 #include "l1394_node.h"
00020 
00021 using std::hex;
00022 using std::dec;
00023 
00024 namespace L1394{
00025 namespace internal{
00026 
00027 NodeContainer::NodeContainer() {
00028   message = SMessage::getInstance();
00029 }
00030 
00031 NodeContainer* NodeContainer::getNodeContainer()
00032 {
00033   mutex.lock();
00034   if (container == NULL)
00035     container = new NodeContainer();
00036   mutex.unlock();
00037   return container;
00038 }
00039 
00040 NodeContainer::~NodeContainer()
00041 {
00042   message->debugStream() << "NodeContainer > delete NodeContainer " << endl;
00043   mutex.lock();
00044   if (inactive_node_list.size() > 0)  {
00045     for (map<u_int64_t, Node*>::iterator it = inactive_node_list.begin();  it != inactive_node_list.end();it++)    {
00046       delete (*it).second;
00047      }
00048      inactive_node_list.clear();
00049   }
00050   else
00051       message->debugStream() << "NodeContainer > no devices in inactive list" << endl;
00052 
00053   if (node_list.size() > 0)  {
00054     for ( map<u_int64_t, Node*>::iterator it = node_list.begin();  it != node_list.end(); it++)    {
00055       delete (*it).second;
00056     }
00057     node_list.clear();
00058   }
00059   else
00060      message->debugStream() << "NodeContainer > no devices in active node list" << endl;
00061 
00062    container = 0;
00063    mutex.unlock();
00064 
00065   message->debugStream() << "NodeContainer < delete NodeContainer " << endl;
00066 }
00067 
00068 
00069 Node* NodeContainer::getNode(u_int64_t guid){
00070   mutex.lock();
00071 
00072   map<u_int64_t, Node*>::iterator tmp_iterator = node_list.find(guid);
00073 
00074   if (tmp_iterator != node_list.end())  {
00075     (*tmp_iterator).second->increaseRefCount();
00076     mutex.unlock();
00077     return (*tmp_iterator).second;
00078   }  else {//look int the inactive node_list.
00079     tmp_iterator = inactive_node_list.find(guid);
00080   }
00081 
00082   if( tmp_iterator != inactive_node_list.end() )
00083   {
00084     (*tmp_iterator).second->increaseRefCount();
00085     inactive_node_list.erase(tmp_iterator);
00086     mutex.unlock();
00087     insert((*tmp_iterator).second);
00088     mutex.lock();
00089     message->debugStream() << "NodeContainer > Hey, i'am back " << endl;
00090      mutex.unlock();
00091     return (*tmp_iterator).second;
00092   }
00093 
00094    message->debugStream() << "NodeContainer > No node wiht this GUID available" << endl;
00095   mutex.unlock();
00096   return NULL;
00097 }
00098 
00099 Node* NodeContainer::findNode(u_int64_t guid) {
00100   mutex.lock();
00101 
00102   message->debugStream() << "L1394NodeContainer > Search Node with GUID : 0x" <<hex << guid << dec << endl;
00103   map<u_int64_t, Node*>::iterator it = node_list.find(guid);
00104   mutex.unlock();
00105   if (it != node_list.end())
00106     return (*it).second;
00107   return 0;
00108 }
00109 
00110 void NodeContainer::release(Node* node)
00111 {
00112   message->debugStream() << "releasing node " << endl;
00113 
00114   int i = node->decreaseRefCount();
00115   if (i <= 0 && node->getNodeType()!= _Card)
00116   {
00117     //remove it from inactive node_list
00118     message->debugStream() << "NodeContainer > node object no longer need." << " with number " << node->getNodeID() << endl;
00119 
00120     node_list.erase(node->getGuid());
00121 
00122     //and insert it in inactive_node_list
00123     inactive_node_list[node->getGuid()] = node;
00124     message->debugStream()<< "NodeContainer > Insert node in inactive_node_list" << endl;
00125   }
00126 }
00127 
00128 void NodeContainer::insert(Node* node)
00129 {
00130   mutex.lock();
00131   if (nodeExist(node->getGuid()))  {
00132      message->errorStream() << "NodeContainer > A node with this guid already exist. Node is not inserted" << endl;
00133      message->errorStream()  << "NodeContainer > Node is deleted " << endl;
00134     delete node; //!?
00135   }
00136   else  {
00137     node_list[node->getGuid()] = node;
00138      message->debugStream() << "NodeContainer > Inserting Node in NodeContainer " << endl;
00139   }
00140   mutex.unlock();
00141 }
00142 
00143 
00144 bool NodeContainer::nodeExist(u_int64_t guid)
00145 {
00146   if (node_list.find(guid) != node_list.end())
00147     return true;
00148   if (inactive_node_list.find(guid) != inactive_node_list.end())
00149     return true;
00150   return false;
00151 }
00152 
00153 void NodeContainer::removeNode(const Node* node)
00154 {
00155   mutex.lock();
00156   if (! ( node_list.erase(node->getGuid()) || inactive_node_list.erase(node->getGuid()) ) )
00157     message->warningStream() << "NodeContainer > Node with GUID " << node->getGuid() <<" does not exist in NodeContainer" << endl;
00158 
00159   mutex.unlock();
00160 }
00161 
00162 NodeContainer* NodeContainer::container = NULL;
00163 
00164 ThreadMutex NodeContainer::mutex;
00165 } //end namespace internal
00166 } //end namespace L1394

Generated on Wed Aug 24 00:36:40 2005 for L1394 by doxygen 1.4.2
L1394 library (NMM) grahics.cs.uni-sb.de/~repplix/l1394_home/