cluster::ApplicationDataServer Class Reference

#include <ApplicationDataServer.h>

Collaboration diagram for cluster::ApplicationDataServer:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ApplicationDataServer (const vpr::GUID &guid, ApplicationData *user_data, const vpr::GUID &plugin_id)
 Create a new ApplicationDataServer.
 ~ApplicationDataServer ()
 Release all memory that is no longer needed.
void serializeAndSend ()
 Send mDataPacket, which has been updated in updateLocalData, to each client.
void addClient (gadget::Node *new_client_node)
 Add a Node to the list of clients that should receive data for this ApplicationData.
void removeClient (const std::string &host_name)
 Remove a Node from the list of clients that should receive data for this ApplicationData.
void debugDump (int debug_level)
 Print information about this ApplicationDataServer to the screen.
vpr::GUID getId ()
 Return the GUID of the ApplicationData that this server is responsible for.

Detailed Description

Definition at line 53 of file ApplicationDataServer.h.


Constructor & Destructor Documentation

cluster::ApplicationDataServer::ApplicationDataServer ( const vpr::GUID &  guid,
ApplicationData user_data,
const vpr::GUID &  plugin_id 
)

Create a new ApplicationDataServer.

Parameters:
guid GUID of the given ApplicationData.
user_data Pointer to the ApplicationData that we are sharing.
plugin_id GUID that should be placed at the beginning of each data packet so that the receiver knows which plugin the data is coming from.

Definition at line 48 of file ApplicationDataServer.cpp.

00049    {
00050       mApplicationData = user_data;
00051 
00052       // mDataPacket and mBufferObjectWriter both use mDeviceData as their data buffer.
00053       mDeviceData = new std::vector<vpr::Uint8>;
00054       
00055       // Create a DataPacket that will be updated and sent continually.
00056       mDataPacket = new DataPacket(plugin_guid, guid, mDeviceData);
00057       mBufferObjectWriter = new vpr::BufferObjectWriter(mDeviceData);
00058    }

cluster::ApplicationDataServer::~ApplicationDataServer (  ) 

Release all memory that is no longer needed.

Definition at line 60 of file ApplicationDataServer.cpp.

00061    {
00062       delete mApplicationData;
00063       
00064       // mDataPacket will clean up the memory that mDeviceData points
00065       // to since mDataPacket contains a reference to the ame memory.
00066       delete mDataPacket;
00067       // vpr::BufferObjectWritter does not release mDeviceData
00068       delete mBufferObjectWriter;
00069       mDeviceData = NULL;   
00070    }


Member Function Documentation

void cluster::ApplicationDataServer::serializeAndSend (  ) 

Send mDataPacket, which has been updated in updateLocalData, to each client.

Definition at line 72 of file ApplicationDataServer.cpp.

References debugDump(), gadget::Node::DISCONNECTED, gadgetDBG_RIM(), cluster::Packet::getHeader(), cluster::ClusterException::getMessage(), cluster::Header::RIM_PACKET_HEAD_SIZE, cluster::Header::serializeHeader(), and cluster::Header::setPacketLength().

00073    {
00074       // Clear old data and reset the position of mBufferObjectWriter
00075       mBufferObjectWriter->getData()->clear();
00076       mBufferObjectWriter->setCurPos(0);
00077 
00078       // This updates the mApplicationData which both mBufferedObjectReader and mDevicePacket point to
00079       vpr::ReturnStatus status = mApplicationData->writeObject(mBufferObjectWriter);
00080 
00081       // If we do not successfully serialize the object, don't send it.
00082       // This allows the developer to create a object that only gets
00083       // sent when a change occurs.
00084       if (!status.success())
00085       {
00086          return;
00087       }
00088 
00089       // We must update the size of the actual data that we are going to send
00090       mDataPacket->getHeader()->setPacketLength(Header::RIM_PACKET_HEAD_SIZE 
00091                                        + 16 /*Plugin GUID*/
00092                                        + 16 /*Plugin GUID*/
00093                                        + mDeviceData->size());
00094 
00095       // We must serialize the header again so that we can reset the size.
00096       mDataPacket->getHeader()->serializeHeader();
00097 
00098 
00099 
00100       // Send the serialized data to each client
00101       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00102       
00103       // Send DataPacket to all nodes in mClients
00104       for (std::vector<gadget::Node*>::iterator i = mClients.begin();
00105            i != mClients.end() ; i++)
00106       {
00107          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) << "Sending data to: "
00108          //   << (*i)->getName() << std::endl << vprDEBUG_FLUSH;
00109          try
00110          {
00111             (*i)->send(mDataPacket);
00112          }
00113          catch(cluster::ClusterException cluster_exception)
00114          {
00115             vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) << "ApplicationDataServer::send() Caught an exception!"
00116                << std::endl << vprDEBUG_FLUSH;
00117             vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) << clrSetBOLD(clrRED)
00118                << cluster_exception.getMessage() << clrRESET
00119                << std::endl << vprDEBUG_FLUSH;
00120 
00121             vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL) <<
00122                "ApplicationDataServer::send() We have lost our connection to: " << (*i)->getName() << ":" << (*i)->getPort()
00123                << std::endl << vprDEBUG_FLUSH;
00124 
00125             // If we receive an exception because we have lost the connection we need
00126             // to flag the Node as disconnected and print debug information
00127             (*i)->setStatus(gadget::Node::DISCONNECTED);
00128             debugDump(vprDBG_CONFIG_LVL);
00129          }
00130       }
00131    }

void cluster::ApplicationDataServer::addClient ( gadget::Node new_client_node  ) 

Add a Node to the list of clients that should receive data for this ApplicationData.

Parameters:
new_client_node Node that will be added to the list.

Definition at line 133 of file ApplicationDataServer.cpp.

Referenced by cluster::ApplicationDataManager::handlePacket().

00134    {
00135       vprASSERT(new_client_node != NULL && "You can not add a new client that is NULL");
00136       
00137       // Lock mutex to make this thread safe
00138       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00139 
00140       mClients.push_back(new_client_node);
00141    }

void cluster::ApplicationDataServer::removeClient ( const std::string &  host_name  ) 

Remove a Node from the list of clients that should receive data for this ApplicationData.

Parameters:
host_name Hostname of the Node that will be removed from the list.

Definition at line 143 of file ApplicationDataServer.cpp.

00144    {
00145       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00146 
00147       for (std::vector<gadget::Node*>::iterator i = mClients.begin() ;
00148             i!= mClients.end() ; i++)
00149       {
00150          if ((*i)->getHostname() == host_name)
00151          {
00152             mClients.erase(i);
00153             return;
00154          }
00155       }
00156    }

void cluster::ApplicationDataServer::debugDump ( int  debug_level  ) 

Print information about this ApplicationDataServer to the screen.

Parameters:
debug_level The debug level that the information will be displayed at.

Definition at line 158 of file ApplicationDataServer.cpp.

References gadgetDBG_RIM(), and getId().

Referenced by serializeAndSend().

00159    {
00160       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00161 
00162       vpr::DebugOutputGuard dbg_output(gadgetDBG_RIM,debug_level,
00163                                  std::string("-------------- ApplicationDataServer --------------\n"),
00164                                  std::string("------------------------------------------\n"));
00165 
00166       vprDEBUG(gadgetDBG_RIM,debug_level) << "Name:     " << getId().toString() << std::endl << vprDEBUG_FLUSH;
00167 
00168       vpr::DebugOutputGuard dbg_output2(gadgetDBG_RIM,debug_level,
00169                         std::string("------------ Clients ------------\n"),
00170                         std::string("---------------------------------\n"));
00171       for (std::vector<gadget::Node*>::iterator i = mClients.begin() ;
00172             i!= mClients.end() ; i++)
00173       {
00174          vprDEBUG(gadgetDBG_RIM,debug_level) << "-------- " << (*i)->getName() << " --------" << std::endl << vprDEBUG_FLUSH;
00175          vprDEBUG(gadgetDBG_RIM,debug_level) << "       Hostname: " << (*i)->getHostname() << std::endl << vprDEBUG_FLUSH;
00176          vprDEBUG(gadgetDBG_RIM,debug_level) << "----------------------------------" << std::endl << vprDEBUG_FLUSH;
00177       }
00178    }

vpr::GUID cluster::ApplicationDataServer::getId (  ) 

Return the GUID of the ApplicationData that this server is responsible for.

Definition at line 180 of file ApplicationDataServer.cpp.

References cluster::ApplicationData::getId().

Referenced by debugDump().

00181    { 
00182       return(mApplicationData->getId()); 
00183    }


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:44:27 2007 for Gadgeteer by  doxygen 1.5.1