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];
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;
00043 unsigned short m_sv_size;
00044 key_t m_comm_id;
00045
00046 unsigned short m_id;
00047 Fifo_chan_type m_fifo;
00048 mutable boost::mutex m_fifomutex;
00049 CHAN_SYNC_STATUS m_sync_status;
00050 SN_INFO_TYPE m_direction;
00051 bool m_initialized;
00052
00053 protected:
00054 bool m_just_received;
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);}
00074 void GetClosestInfo(time const& horizon, InfoContainer &info);
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;
00079 };
00080
00081 }
00082 }
00083 #endif