Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
commbase.hpp
00001 
00002 #ifndef DDF_COMMBASE_HPP
00003 #define DDF_COMMBASE_HPP
00004 
00005 #include <deque>
00006 
00007 extern "C"
00008 {
00009 #include <sys/types.h>
00010 #include <sys/ipc.h>
00011 }
00012 
00013 #include "infocontainer.hpp"
00014 #include "definitions.hpp"
00015 
00016 namespace jafar
00017 {
00018   namespace ddf
00019   {
00020 
00021     typedef enum {SN_INFO_IJ=1,SN_INFO_JI} SN_INFO_TYPE;
00022     #define TOGGLE_INFO_TYPE(info_type) ((info_type) == SN_INFO_IJ ? SN_INFO_JI : SN_INFO_IJ)
00023 
00024     const char NODE_IJ_TEXT[][20] = {"VOID", "NODE j receive", "NODE i receive"};
00025 
00026     struct ChanFltrMsg
00027     {
00028       long              mtype;
00029       unsigned char     mtext[4];   // contains the information vector, half of the information matrix (symmetric) and a timeval
00030     };
00031 
00037     class CommBase
00038     {
00039       typedef std::deque<InfoContainer>   Fifo_chan_type;
00040       typedef enum {CHAN_FIFO_EMPTY, CHAN_FIFO_OK, CHAN_FIFO_FAILURE} CHAN_SYNC_STATUS;
00041 
00042       static unsigned short   m_static_id;         // used for generating a unique key for communication (see m_comm_id)
00043       unsigned short          m_sv_size;
00044       key_t                   m_comm_id;           // a unique identifier for the communication channel
00045                                                    // communication endpoints with the same id communicate together
00046       unsigned short          m_id;                // a unique id for the comm object (the endpoint)
00047       Fifo_chan_type          m_fifo;              // Buffer storing incoming data
00048       mutable  boost::mutex   m_fifomutex;         // m_fifo is accessed asynchronously
00049       CHAN_SYNC_STATUS        m_sync_status;       // return status set by the last call of GetClosestInfo
00050       SN_INFO_TYPE            m_direction;         // from i to j or j to i (mtype of a ChanFltrMsg structure)
00051       bool                    m_initialized;       // set to true when it is ready to handle communication
00052 
00053     protected:
00054       bool                    m_just_received;     // debug purpose
00055    
00056     public:
00057       typedef enum{EC_OK, EC_TIMEOUT, EC_SEND_FAILURE, EC_READ_FAILURE, EC_READ_EMPTY} EC_STATUS;
00058    
00059       CommBase(unsigned short sv_size);
00060       CommBase(unsigned short sv_size, key_t comm_key);
00061       virtual ~CommBase();
00062 
00063       void SetInitialized() { m_initialized = true; }
00064       void WaitInitialized() {time per = time::from_us(1); while(!m_initialized) per.sleep_period(); }
00065    
00066       unsigned short GetSvSize() const { return m_sv_size; }
00067       SN_INFO_TYPE   GetDirection() const { return m_direction; }
00068       key_t GetCommId() const { return m_comm_id; }
00069       unsigned short GetID() { return m_id; }
00070       void ResetJustReceived() { m_just_received = false; }
00071       bool HasJustReceivedInfo() const { return m_just_received; }
00072    
00073       void StoreIncoming(InfoContainer const& info) { boost::mutex::scoped_lock lock(m_fifomutex);  m_fifo.push_back(info);}     // called by Read
00074       void GetClosestInfo(time const& horizon, InfoContainer &info);                // called when the channel filter synchronize
00075       bool Sync_Ok() const { return m_sync_status == CHAN_FIFO_OK; }
00076    
00077       virtual EC_STATUS Send(InfoContainer const& inno)=0;
00078       virtual EC_STATUS Read()=0;                                                   // fill the fifo with incoming information
00079     };
00080 
00081   } // namespace ddf
00082 } // namespace jafar
00083 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:17 for Jafar by doxygen 1.7.6.1
LAAS-CNRS