gadget::DeviceServer Class Reference

Device server class. More...

#include <gadget/DeviceServer.h>

Collaboration diagram for gadget::DeviceServer:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 DeviceServer (const std::string &name, gadget::Input *device, const vpr::GUID &plugin_id)
 Create a new DeviceServer.
 ~DeviceServer ()
void send ()
void updateLocalData ()
void addClient (gadget::Node *new_client_node)
void removeClient (const std::string &host_name)
void debugDump (int debug_level)
std::string getName ()
void lockClients ()
 Locks the active list.
void unlockClients ()
 Unlocks the active list.
vpr::GUID getId ()
void start ()
 Starts the control loop.
void controlLoop (void *nullParam)
void go ()
void sync ()
 Blocks until the end of the frame.
void shutdown ()

Detailed Description

Device server class.

Definition at line 50 of file DeviceServer.h.


Constructor & Destructor Documentation

gadget::DeviceServer::DeviceServer ( const std::string &  name,
gadget::Input device,
const vpr::GUID &  plugin_id 
)

Create a new DeviceServer.

Parameters:
name Name of the device that we are sharing.
device Pointer to the device 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 41 of file DeviceServer.cpp.

References start().

00043       : deviceServerTriggerSema(0)
00044       , deviceServerDoneSema(0)
00045    {
00046       vpr::GUID temp;
00047       temp.generate();
00048 
00049       do
00050       {
00051          mId.generate();   // Generate a unique ID for this device
00052          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00053             << "[DeviceServer] Invalid GUID, generating a new one."
00054             << std::endl << vprDEBUG_FLUSH;
00055       }
00056       while(temp == mId);
00057 
00058       mThreadActive = false;
00059       mName = name;
00060       mDevice = device;
00061       mPluginGUID = plugin_guid;
00062 
00063       mDeviceData = new std::vector<vpr::Uint8>;
00064       mDataPacket = new cluster::DataPacket(plugin_guid, mId, mDeviceData);
00065       mBufferObjectWriter = new vpr::BufferObjectWriter(mDeviceData);
00066       start();
00067    }

gadget::DeviceServer::~DeviceServer (  ) 

Definition at line 69 of file DeviceServer.cpp.

References shutdown().

00070    {
00071       shutdown();
00072       delete mDataPacket;
00073       // mDataPacket will clean up the memory that mDeviceData points
00074       // to since mDataPacket contains a reference to the ame memory.
00075       mDeviceData = NULL;
00076    }


Member Function Documentation

void gadget::DeviceServer::send (  ) 

Definition at line 89 of file DeviceServer.cpp.

References debugDump(), gadget::Node::DISCONNECTED, gadgetDBG_RIM(), and cluster::ClusterException::getMessage().

Referenced by controlLoop().

00090    {
00091       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00092 
00093       //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00094       //   << clrOutBOLD(clrMAGENTA,"DeviceServer::send()")
00095       //   << "Sending Device Data for: " << getName() << std::endl
00096       //   << vprDEBUG_FLUSH;
00097 
00098       for ( std::vector<gadget::Node*>::iterator i = mClients.begin();
00099             i != mClients.end();
00100             ++i )
00101       {
00102          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00103          //   << "Sending data to: " << (*i)->getName()
00104          //   << " trying to lock socket" << std::endl << vprDEBUG_FLUSH;
00105 
00106          try
00107          {
00108             (*i)->send(mDataPacket);
00109          }
00110          catch( cluster::ClusterException cluster_exception )
00111          {
00112             vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL )
00113                << "DeviceServer::send() Caught an exception!"
00114                << std::endl << vprDEBUG_FLUSH;
00115             vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL )
00116                << clrSetBOLD( clrRED ) << cluster_exception.getMessage()
00117                << clrRESET << std::endl << vprDEBUG_FLUSH;
00118             vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL )
00119                << "DeviceServer::send() We have lost our connection to: "
00120                << (*i)->getName() << ":" << (*i)->getPort()
00121                << std::endl << vprDEBUG_FLUSH;
00122 
00123             (*i)->setStatus( gadget::Node::DISCONNECTED );
00124             (*i)->shutdown();
00125 
00126             debugDump( vprDBG_CONFIG_LVL );
00127          }
00128       }
00129       //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00130       //   << clrOutBOLD(clrMAGENTA,"DeviceServer::send()")
00131       //   << "Done Sending Device Data for: " << getName() << std::endl
00132       //   << vprDEBUG_FLUSH;
00133    }

void gadget::DeviceServer::updateLocalData (  ) 

Definition at line 135 of file DeviceServer.cpp.

References cluster::Packet::getHeader(), cluster::Header::RIM_PACKET_HEAD_SIZE, and gadget::Input::writeObject().

Referenced by controlLoop().

00136    {
00137       // -BufferObjectWriter
00138       mBufferObjectWriter->getData()->clear();
00139       mBufferObjectWriter->setCurPos(0);
00140 
00141       // This updates the mDeviceData which both mBufferedObjectReader and
00142       // mDevicePacket point to.
00143       mDevice->writeObject(mBufferObjectWriter);
00144 
00145       // We must update the size of the actual data that we are going to send
00146       mDataPacket->getHeader()->setPacketLength(
00147          cluster::Header::RIM_PACKET_HEAD_SIZE
00148             + 16 /*Plugin GUID*/
00149             + 16 /*Plugin GUID*/
00150             + mDeviceData->size()
00151       );
00152 
00153       // We must serialize the header again so that we can reset the size.
00154       mDataPacket->getHeader()->serializeHeader();
00155    }

void gadget::DeviceServer::addClient ( gadget::Node new_client_node  ) 

Definition at line 157 of file DeviceServer.cpp.

Referenced by gadget::RemoteInputManager::handlePacket().

00158    {
00159       vprASSERT(new_client_node != NULL &&
00160                 "You can not add a new client that is NULL");
00161       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00162 
00163       mClients.push_back(new_client_node);
00164    }

void gadget::DeviceServer::removeClient ( const std::string &  host_name  ) 

Definition at line 166 of file DeviceServer.cpp.

00167    {
00168       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00169 
00170       for (std::vector<gadget::Node*>::iterator i = mClients.begin() ;
00171             i!= mClients.end() ; i++)
00172       {
00173          if ((*i)->getHostname() == host_name)
00174          {
00175             mClients.erase(i);
00176             return;
00177          }
00178       }
00179    }

void gadget::DeviceServer::debugDump ( int  debug_level  ) 

Definition at line 181 of file DeviceServer.cpp.

References gadgetDBG_RIM().

Referenced by send().

00182    {
00183       vpr::Guard<vpr::Mutex> guard(mClientsLock);
00184 
00185       vpr::DebugOutputGuard dbg_output(
00186          gadgetDBG_RIM, debugLevel,
00187          "-------------- DeviceServer --------------\n",
00188          "------------------------------------------\n"
00189       );
00190 
00191       vprDEBUG(gadgetDBG_RIM, debugLevel)
00192          << "Name:     " << mName << std::endl << vprDEBUG_FLUSH;
00193 
00194       { // Used simply to make the following DebugOutputGuard go out of scope
00195          vpr::DebugOutputGuard dbg_output2(gadgetDBG_RIM, debugLevel,
00196                            std::string("------------ Clients ------------\n"),
00197                            std::string("---------------------------------\n"));
00198          for ( std::vector<gadget::Node*>::iterator i = mClients.begin();
00199                 i != mClients.end();
00200                 ++i )
00201          {
00202             vprDEBUG(gadgetDBG_RIM, debugLevel)
00203                << "-------- " << (*i)->getName() << " --------" << std::endl
00204                << vprDEBUG_FLUSH;
00205             vprDEBUG(gadgetDBG_RIM, debugLevel)
00206                << "       Hostname: " << (*i)->getHostname() << std::endl
00207                << vprDEBUG_FLUSH;
00208             vprDEBUG(gadgetDBG_RIM, debugLevel)
00209                << "----------------------------------" << std::endl
00210                << vprDEBUG_FLUSH;
00211          }
00212       }
00213    }

std::string gadget::DeviceServer::getName (  )  [inline]

Definition at line 75 of file DeviceServer.h.

Referenced by start().

00075 { return mName; }

void gadget::DeviceServer::lockClients (  )  [inline]

Locks the active list.

This function blocks until it can lock the std::vector of active Nodes.

The caller of this method must call unlockActive() when it is finished viewing/modifying the active list.

Definition at line 85 of file DeviceServer.h.

00086       { mClientsLock.acquire(); }

void gadget::DeviceServer::unlockClients (  )  [inline]

Unlocks the active list.

The method releases the lock on the active connections std::vector.

The caller of this method must have previously locked the active list with lockActive().

Definition at line 95 of file DeviceServer.h.

00096       { mClientsLock.release(); }

vpr::GUID gadget::DeviceServer::getId (  )  [inline]

Definition at line 98 of file DeviceServer.h.

Referenced by gadget::RemoteInputManager::handlePacket().

00099       {
00100          return mId;
00101       }

void gadget::DeviceServer::start (  ) 

Starts the control loop.

Definition at line 251 of file DeviceServer.cpp.

References controlLoop(), gadgetDBG_RIM(), and getName().

Referenced by DeviceServer().

00252    {
00253       // --- Setup Multi-Process stuff --- //
00254       // Create a new thread to handle the control
00255 
00256       vpr::ThreadMemberFunctor<DeviceServer>* memberFunctor =
00257          new vpr::ThreadMemberFunctor<DeviceServer>(this,
00258                                                     &DeviceServer::controlLoop,
00259                                                     NULL);
00260 
00261       mControlThread = new vpr::Thread(memberFunctor);
00262 
00263       if (mControlThread->valid())
00264       {
00265          mThreadActive = true;
00266       }
00267       vprDEBUG(gadgetDBG_RIM, vprDBG_CONFIG_LVL)
00268          << "DeviceServer " << getName() << " started. thread: "
00269          << mControlThread << std::endl << vprDEBUG_FLUSH;
00270    }

void gadget::DeviceServer::controlLoop ( void *  nullParam  ) 

Definition at line 215 of file DeviceServer.cpp.

References send(), and updateLocalData().

Referenced by start().

00216    {
00217       // -Block on an update call
00218       // -Update Local Data
00219       // -Send
00220       // -Signal Sync
00221 
00222       boost::ignore_unused_variable_warning(nullParam);
00223 
00224       while(true)
00225       {
00226          // Wait for trigger
00227          deviceServerTriggerSema.acquire();
00228          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00229          //   << "DeviceServer: " << getName() << " triggered\n"
00230          //   << vprDEBUG_FLUSH;
00231 
00232          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00233          //   << "Before Update Data\n" << vprDEBUG_FLUSH;
00234          updateLocalData();
00235          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00236          //   << "After Update Data\n" << vprDEBUG_FLUSH;
00237          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00238          //   << "Before send Data\n" << vprDEBUG_FLUSH;
00239          send();
00240          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00241          //   << "After Send Data\n" << vprDEBUG_FLUSH;
00242 
00243          // Signal Done Rendering
00244          deviceServerDoneSema.release();
00245          //vprDEBUG(gadgetDBG_RIM,vprDBG_CONFIG_LVL)
00246          //   << "DeviceServer synced\n" << vprDEBUG_FLUSH;
00247       }
00248    }

void gadget::DeviceServer::go (  ) 

Definition at line 272 of file DeviceServer.cpp.

References gadgetDBG_RIM().

00273    {
00274       while(!mThreadActive)
00275       {
00276          vprDEBUG(gadgetDBG_RIM,/*vprDBG_HVERB_LVL*/1)
00277             << "Waiting in for thread to start DeviceServer::go().\n"
00278             << vprDEBUG_FLUSH;
00279          vpr::Thread::yield();
00280       }
00281       deviceServerTriggerSema.release();
00282    }

void gadget::DeviceServer::sync (  ) 

Blocks until the end of the frame.

Postcondition:
The frame has been drawn.

Definition at line 288 of file DeviceServer.cpp.

00289    {
00290       vprASSERT(mThreadActive == true);
00291       deviceServerDoneSema.acquire();
00292    }

void gadget::DeviceServer::shutdown (  ) 

Definition at line 78 of file DeviceServer.cpp.

Referenced by ~DeviceServer().

00079    {
00080       // TODO: Make the device server actually shutdown
00081       if ( mControlThread )
00082       {
00083          mThreadActive = false;
00084          mControlThread->kill();
00085          mControlThread = NULL;
00086       }
00087    }


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