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

vpr::sim::NetworkNode Class Reference

This class must be default constrible, assignable, and copy constructible. More...

#include <NetworkNode.h>

Collaboration diagram for vpr::sim::NetworkNode:

Collaboration graph
[legend]
List of all members.

Public Methods

 NetworkNode (void)
 NetworkNode (const vpr::Uint32 index, const vpr::Uint8 type, const std::string &ip_str)
 Constructs a new network node using the given paramters. More...

 ~NetworkNode (void)
void addSocket (vpr::SocketImplSIM *sock)
vpr::ReturnStatus removeSocket (const vpr::SocketImplSIM *sock)
bool hasSocket (const vpr::Uint32 port, const vpr::SocketTypes::Type type)
 Checks to see if this node currently has a socket of the given type in use on the named port. More...

vpr::SocketImplSIMgetSocket (const vpr::Uint32 port, const vpr::SocketTypes::Type type)
const vpr::Uint32getIndex (void) const
void setIndex (const vpr::Uint32 index)
const vpr::Uint8getType (void) const
void setType (const vpr::Uint8 type)
const vpr::Uint32getIpAddress (void) const
 Retrieves the 32-bit IPv4 address for this network node. More...

const std::string & getIpAddressString (void) const
vpr::Uint32 getUnassignedTcpPortNumber ()
 Get an unused TCP port number. More...

vpr::Uint32 getUnassignedUdpPortNumber ()
 Get an unused UDP port number. More...


Detailed Description

This class must be default constrible, assignable, and copy constructible.

Definition at line 70 of file NetworkNode.h.


Constructor & Destructor Documentation

vpr::sim::NetworkNode::NetworkNode void    [inline]
 

Definition at line 73 of file NetworkNode.h.

00074       : mIndex(0), mType(0), mIpAddr(0)
00075    {
00076       /* Do nothing. */ ;
00077    }

vpr::sim::NetworkNode::NetworkNode const vpr::Uint32    index,
const vpr::Uint8    type,
const std::string &    ip_str
 

Constructs a new network node using the given paramters.

This will parse the given IP address (it must be a standard IPv4 dotted-decimal string) and create the corresponding integer address.

Definition at line 63 of file NetworkNode.cpp.

00065    : mIndex(index), mType(type), mIpStr(ip_str), mIpAddr(0)
00066 {
00067    std::string::size_type cur_pos, next_dot;
00068    std::string cur_val;
00069 
00070    union
00071    {
00072       char bytes[4];
00073       vpr::Uint32 value;
00074    } ip_addr;
00075 
00076    cur_pos = 0;
00077 
00078    for ( vpr::Uint8 i = 0; i < 4; ++i )
00079    {
00080       next_dot = ip_str.find(".", cur_pos);
00081       cur_val  = ip_str.substr(cur_pos, next_dot - cur_pos);
00082 
00083       ip_addr.bytes[i] = atoi(cur_val.c_str());
00084 
00085       cur_pos = next_dot + 1;
00086    }
00087 
00088    mIpAddr = vpr::System::Ntohl(ip_addr.value);
00089 }

vpr::sim::NetworkNode::~NetworkNode void    [inline]
 

Definition at line 87 of file NetworkNode.h.

00088    {
00089       /* Do nothing. */ ;
00090    }


Member Function Documentation

void vpr::sim::NetworkNode::addSocket vpr::SocketImplSIM   sock
 

Definition at line 137 of file NetworkNode.cpp.

References vpr::InetAddrBSD::getAddressValue, vpr::SocketImplSIM::getLocalAddr, vpr::InetAddrBSD::getPort, vpr::SocketImplSIM::getType, hasSocket, vprASSERT, vprDBG_ALL, vprDBG_CRITICAL_LVL, vprDBG_STATE_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00138 {
00139    vpr::Uint32 port = sock->getLocalAddr().getPort();
00140    vprASSERT( mIpAddr == sock->getLocalAddr().getAddressValue() && "Trying to add socket to node of wrong ip addr");
00141    if(hasSocket(port, sock->getType()))
00142       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL) << "NetworkNode::addSocket: Tried to overwrite existing socket: " << sock->getLocalAddr() << std::endl << vprDEBUG_FLUSH;
00143    vprASSERT(! hasSocket(port, sock->getType()) && "Tried to overwrite an existing socket");
00144 
00145    vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00146       << "NetworkNode::addSocket() [" << mIpStr << "]: Adding socket (" << sock
00147       << ") with local address " << sock->getLocalAddr() << " to node "
00148       << mIpStr << "\n" << vprDEBUG_FLUSH;
00149 
00150    switch (sock->getType())
00151    {
00152       case vpr::SocketTypes::DATAGRAM:
00153          {
00154             //vpr::Guard<vpr::Mutex> guard(mDgramMapMutex);
00155             vprASSERT( dynamic_cast<vpr::SocketDatagramImplSIM*>(sock) != NULL);
00156             mDgramSocketMap[port] = sock;
00157          }
00158          break;
00159       case vpr::SocketTypes::STREAM:
00160          {
00161             //vpr::Guard<vpr::Mutex> guard(mStreamMapMutex);
00162             vprASSERT( dynamic_cast<vpr::SocketStreamImplSIM*>(sock) != NULL);
00163             mStreamSocketMap[port] = sock;
00164          }
00165          break;
00166    }
00167 }

vpr::ReturnStatus vpr::sim::NetworkNode::removeSocket const vpr::SocketImplSIM   sock
 

Definition at line 169 of file NetworkNode.cpp.

References vpr::ReturnStatus::Fail, vpr::InetAddrBSD::getAddressValue, vpr::SocketImplSIM::getLocalAddr, vpr::InetAddrBSD::getPort, vpr::SocketImplSIM::getType, hasSocket, vpr::ReturnStatus::setCode, vpr::ReturnStatus::Succeed, vprASSERT, vprDBG_ALL, vprDBG_STATE_LVL, vprDBG_VERB_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00170 {
00171    vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00172    socket_map_t::iterator i;
00173 
00174    vpr::Uint32 port( sock->getLocalAddr().getPort() );
00175    vpr::Uint32 ip_addr( sock->getLocalAddr().getAddressValue() );
00176 
00177    vprASSERT( hasSocket(port, sock->getType())  && "Trying to remove a socket that isn't registered with node");
00178    vprASSERT( mIpAddr == ip_addr && "Removing socket from wrong node [ip addresses differ]");
00179 
00180    vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00181       << "NetworkNode::removeSocket() [" << mIpStr
00182       << "]: Removing socket bound to " << sock->getLocalAddr() << "\n"
00183       << vprDEBUG_FLUSH;
00184 
00185    switch (sock->getType())
00186    {
00187       case vpr::SocketTypes::DATAGRAM:
00188          {
00189             //vpr::Guard<vpr::Mutex> guard(mDgramMapMutex);
00190             i = mDgramSocketMap.find(port);
00191 
00192             if ( i != mDgramSocketMap.end() )
00193             {
00194                mDgramSocketMap.erase(i);
00195                status.setCode(vpr::ReturnStatus::Succeed);
00196                vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL) << "NetworkNode::removeSocket() [" << mIpStr
00197                                                      << "]: Removed datagram socket\n" << vprDEBUG_FLUSH;
00198                vprASSERT(!hasSocket(port, sock->getType()) && "Erasing did nothing");
00199             }
00200          }
00201          break;
00202       case vpr::SocketTypes::STREAM:
00203          {
00204             //vpr::Guard<vpr::Mutex> guard(mStreamMapMutex);
00205          
00206             i = mStreamSocketMap.find(port);
00207    
00208             if ( i != mStreamSocketMap.end() )
00209             {
00210                mStreamSocketMap.erase(i);
00211                status.setCode(vpr::ReturnStatus::Succeed);
00212                vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL) << "NetworkNode::removeSocket() [" << mIpStr
00213                                                      << "]: Removed stream socket\n" << vprDEBUG_FLUSH;
00214                vprASSERT(!hasSocket(port, sock->getType()) && "Erasing did nothing");
00215             }
00216          }
00217          break;
00218    }
00219 
00220    return status;
00221 }

bool vpr::sim::NetworkNode::hasSocket const vpr::Uint32    port,
const vpr::SocketTypes::Type    type
 

Checks to see if this node currently has a socket of the given type in use on the named port.

Definition at line 91 of file NetworkNode.cpp.

Referenced by addSocket, getSocket, and removeSocket.

00093 {
00094    bool result = false;
00095 
00096    switch (type)
00097    {
00098       case vpr::SocketTypes::DATAGRAM:
00099          //mDgramMapMutex.acquire();
00100          result = (mDgramSocketMap.find(port) != mDgramSocketMap.end());
00101          //mDgramMapMutex.release();
00102          break;
00103       case vpr::SocketTypes::STREAM:
00104          //mStreamMapMutex.acquire();
00105          result = (mStreamSocketMap.find(port) != mStreamSocketMap.end());;
00106          //mStreamMapMutex.release();
00107          break;
00108    }
00109 
00110    return  result;
00111 }

vpr::SocketImplSIM * vpr::sim::NetworkNode::getSocket const vpr::Uint32    port,
const vpr::SocketTypes::Type    type
 

Definition at line 113 of file NetworkNode.cpp.

References hasSocket, and vprASSERT.

00115 {
00116    vprASSERT( hasSocket(port,type) && "getSocket doesn't have a socket of the given type");
00117 
00118    switch (type)
00119    {
00120       case vpr::SocketTypes::DATAGRAM:
00121          {
00122             //vpr::Guard<vpr::Mutex> guard(mDgramMapMutex);
00123             return mDgramSocketMap[port];
00124          }         
00125          break;
00126       case vpr::SocketTypes::STREAM:
00127          {
00128             //vpr::Guard<vpr::Mutex> guard(mStreamMapMutex);
00129             return mStreamSocketMap[port];
00130          }
00131          break;
00132    }
00133 
00134    return NULL;
00135 }

const vpr::Uint32& vpr::sim::NetworkNode::getIndex void    const [inline]
 

Definition at line 106 of file NetworkNode.h.

00107    {
00108       return mIndex;
00109    }

void vpr::sim::NetworkNode::setIndex const vpr::Uint32    index [inline]
 

Definition at line 111 of file NetworkNode.h.

00112    {
00113       mIndex = index;
00114    }

const vpr::Uint8& vpr::sim::NetworkNode::getType void    const [inline]
 

Definition at line 116 of file NetworkNode.h.

00117    {
00118       return mType;
00119    }

void vpr::sim::NetworkNode::setType const vpr::Uint8    type [inline]
 

Definition at line 121 of file NetworkNode.h.

00122    {
00123       mType = type;
00124    }

const vpr::Uint32& vpr::sim::NetworkNode::getIpAddress void    const [inline]
 

Retrieves the 32-bit IPv4 address for this network node.

Definition at line 129 of file NetworkNode.h.

00130    {
00131       return mIpAddr;
00132    }

const std::string& vpr::sim::NetworkNode::getIpAddressString void    const [inline]
 

Definition at line 134 of file NetworkNode.h.

00135    {
00136       return mIpStr;
00137    }

vpr::Uint32 vpr::sim::NetworkNode::getUnassignedTcpPortNumber  
 

Get an unused TCP port number.

Definition at line 224 of file NetworkNode.cpp.

References vprASSERT.

00225 {
00226 //vpr::Guard<vpr::Mutex> guard(mStreamMapMutex);
00227 
00228    vpr::Uint32 ret_val(1);
00229    bool        found_one(false);
00230    for(socket_map_t::iterator i = mStreamSocketMap.begin();
00231        (!found_one) && (i != mStreamSocketMap.end()); ++i)
00232    {
00233       socket_map_t::iterator next = i; ++next;
00234       if(next != mStreamSocketMap.end())
00235       {
00236          if((*next).first != ((*i).first + 1) )     // If not sequential
00237          {
00238             ret_val = (*i).first + 1;
00239             found_one = true;
00240          }
00241       }
00242       else // There is no next, so use the one after the current
00243       {
00244          ret_val = (*i).first +1;
00245          found_one = true;
00246       }
00247 
00248    }
00249 
00250    vprASSERT( mStreamSocketMap.find(ret_val) == mStreamSocketMap.end() && "Returned a value that already exists");
00251    return ret_val;
00252 }

vpr::Uint32 vpr::sim::NetworkNode::getUnassignedUdpPortNumber  
 

Get an unused UDP port number.

Definition at line 255 of file NetworkNode.cpp.

References vprASSERT.

00256 {
00257 //vpr::Guard<vpr::Mutex> guard(mDgramMapMutex);
00258 
00259    vpr::Uint32 ret_val(1);
00260    bool        found_one(false);
00261    for(socket_map_t::iterator i = mDgramSocketMap.begin();
00262        (!found_one) && (i != mDgramSocketMap.end()); ++i)
00263    {
00264       socket_map_t::iterator next = i; ++next;
00265       if(next != mDgramSocketMap.end())
00266       {
00267          if((*next).first != ((*i).first + 1) )     // If not sequential
00268          {
00269             ret_val = (*i).first + 1;
00270             found_one = true;
00271          }
00272       }
00273       else // There is no next, so use the one after the current
00274       {
00275          ret_val = (*i).first +1;
00276          found_one = true;
00277       }
00278    }
00279 
00280    vprASSERT( mDgramSocketMap.find(ret_val) == mDgramSocketMap.end() && "Returned a value that already exists");
00281    return ret_val;
00282 }


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:47:20 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002