BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
21f4a14
Branches
Tags
master
Blinker
src
noarch
Sender.h
MCUF sender module implemented
Stefan Schuermans
commited
21f4a14
at 2011-11-13 20:54:03
Sender.h
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #ifndef SENDER_H #define SENDER_H #include <list> #include <string> #include <BlinkenLib/BlinkenFrame.h> #include "CallMgr.h" #include "Directory.h" #include "File.h" #include "Module.h" #include "SettingFile.h" #include "StreamMgr.h" #include "StreamRecv.h" namespace Blinker { /// stream sender template<typename ADDR, typename SOCK> class Sender: public Module, public StreamRecv { protected: /// static destination struct Dest { std::string m_name; ///< name of static destination SettingFile m_file; ///< setting file object ADDR m_addr; ///< parsed address Dest(const std::string &name, const File &file); ///< constructor bool parse(); ///< parse address from current file }; /// static destinations typedef std::list<Dest> Dests; public: /** * @brief constructor * @param[in] callMgr callback manager * @param[in] streamMgr stream manager * @param[in] dirBase base directory */ Sender(CallMgr &callMgr, StreamMgr &streamMgr, const Directory &dirBase); /// virtual destructor virtual ~Sender(); private: /// copy constructor disabled Sender(const Sender &that); /// assignment operator disabled const Sender & operator=(const Sender &that); public: /// check for update of configuration virtual void updateConfig(); /** * @brief set current frame * @param[in] pFrame current frame */ virtual void setFrame(stBlinkenFrame *pFrame); /// set current frame to none virtual void setNoFrame(); protected: /// get input stream and attach to it void getInStream(); /// detach from input stream and release it void releaseInStream(); /// create socket and bind it void createSock(); /// destroy socket void destroySock(); /** * @brief light update of static destinations, * i.e. stat all files in current static destination directory */ void updateDestsLight(); /** * @brief full update of static destinations, * i.e. scan files in playlist directory */ void updateDestsFull(); /** * @brief send current frame to address * @param[in] addr address to send to */ void sendCurFrame(const ADDR &addr); /** * @brief send frame to address * @param[in] mcuf MCUF data of frame * @param[in] addr address to send to */ void sendFrame(const std::string &mcuf, const ADDR &addr); /** * @brief send "no frame" to address * @param[in] addr address to send to */ void sendNoFrame(const ADDR &addr); /** * @brief convert frame to MCUF data * @param[in] pFrame frame * @param[out] mcuf MCUF data */ void frame2mcuf(stBlinkenFrame *pFrame, std::string &mcuf); protected: SettingFile m_fileInStream; ///< input stream name file SettingFile m_fileBind; ///< bind address file Directory m_dirDests; ///< static destinations directory std::string m_nameInStream; ///< name of input stream Stream *m_pInStream; ///< input stream SOCK *m_pSock; ///< socket to use for sending streams Dests m_dests; ///< current static destinations }; // class Sender /* ########## # Sender # ########## */ /** * @brief constructor * @param[in] callMgr callback manager * @param[in] streamMgr stream manager * @param[in] dirBase base directory */ template<typename ADDR, typename SOCK> Sender<ADDR, SOCK>::Sender(CallMgr &callMgr, StreamMgr &streamMgr, const Directory &dirBase): Module(callMgr, streamMgr, dirBase), m_fileInStream(dirBase.getFile("instream")), m_fileBind(dirBase.getFile("bind")), m_dirDests(dirBase.getSubdir("destinations")), m_pInStream(NULL), m_pSock(NULL) { // get input stream and attach to it getInStream(); // create and bind socket createSock(); // load static destinations updateDestsFull(); } /// virtual destructor template<typename ADDR, typename SOCK> Sender<ADDR, SOCK>::~Sender() { // send no frame to all static destinations typename Dests::const_iterator itDest; for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest) sendNoFrame(itDest->m_addr); // destroy socket destroySock(); // detach from input stream and release it releaseInStream(); } /// check for update of configuration template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::updateConfig() { // input stream name file was modified -> re-get input stream if (m_fileInStream.checkModified()) { releaseInStream(); getInStream(); } // bind address file was modified -> re-create socket if (m_fileBind.checkModified()) { destroySock(); createSock(); } // static destinations update // (directory modified -> full, otherwise -> light) if (m_dirDests.checkModified()) updateDestsFull(); else updateDestsLight(); } /** * @brief set current frame * @param[in] pFrame current frame */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::setFrame(stBlinkenFrame *pFrame) { std::string mcuf; // convert frame to MCUF packet frame2mcuf(pFrame, mcuf); // send frame to all static destinations typename Dests::const_iterator itDest; for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest) sendFrame(mcuf, itDest->m_addr); } /// set current frame to none template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::setNoFrame() { // send "no frame" to all static destinations typename Dests::const_iterator itDest; for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest) sendNoFrame(itDest->m_addr); } /// get input stream and attach to it template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::getInStream() { // get input stream m_fileInStream.getStr(m_nameInStream); m_pInStream = &m_streamMgr.refStream(m_nameInStream); // attach to input stream if (m_pInStream) m_pInStream->attach(this); } /// detach from input stream and release it template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::releaseInStream() { // detach from input stream if (m_pInStream) m_pInStream->detach(this); // unreference stream m_pInStream = NULL; m_streamMgr.unrefStream(m_nameInStream); } /// create socket and bind it template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::createSock() { std::string strAddr; ADDR addr; // create socket if (!m_pSock) { m_pSock = new SOCK(); if (!m_pSock) return; } // get bind address from bind address setting file if (!m_fileBind.getStr(strAddr) || !addr.fromStr(strAddr)) { delete m_pSock; m_pSock = NULL; return; } // bind socket if (!m_pSock->bind(addr)) { delete m_pSock; m_pSock = NULL; return; } } /// destroy socket template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::destroySock() { // destroy socket if (m_pSock) { delete m_pSock; m_pSock = NULL; } } /** * @brief light update of static destinations, * i.e. stat all files in current static destination directory */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::updateDestsLight() { // walk through all files in static dest dir and check for modification typename Dests::iterator itDest = m_dests.begin(); while (itDest != m_dests.end()) { // address changed if (itDest->m_file.checkModified()) { // send "no frame" to old address sendNoFrame(itDest->m_addr); // re-load address if (!itDest->parse()) { // parsing address failed -> remove destination itDest = m_dests.erase(itDest); // do not advance to next destination continue; } // send current frame to new address sendCurFrame(itDest->m_addr); } // advance to next destination ++itDest; } // while itDest } /** * @brief full update of static destinations, * i.e. scan files in playlist directory */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::updateDestsFull() { // get list of files in static destinations directory typedef std::list<std::string> Filelist; Filelist curFiles; m_dirDests.getEntries(Directory::TypeFile, curFiles); // walk through current static destinations and file list simultaneously Filelist::const_iterator itFile = curFiles.begin(); typename Dests::iterator itDest = m_dests.begin(); while (itFile != curFiles.end() || itDest != m_dests.end()) { // new static destination inserted if (itDest == m_dests.end() || (itFile != curFiles.end() && *itFile < itDest->m_name)) { // parse new address Dest dest(*itFile, m_dirDests.getFile(*itFile)); if (dest.parse()) { // insert new static destination m_dests.insert(itDest, dest); // send current frame to new static destination sendCurFrame(itDest->m_addr); } // advance to next file ++itFile; } // static destination removed // static destination changed (=> remove and re-insert in next iteration) else if (itFile == curFiles.end() || *itFile > itDest->m_name || itDest->m_file.checkModified()) { // send "no frame" to old static destination sendNoFrame(itDest->m_addr); // remove static destination itDest = m_dests.erase(itDest); // do not advance to next file } // static destination stayed in playlist and did not change else { // advance to next file and next static destination ++itFile; ++itDest; } } // while itFile itDest } /** * @brief send current frame to address * @param[in] addr address to send to */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendCurFrame(const ADDR &addr) { stBlinkenFrame *pFrame; std::string mcuf; // get current frame from stream if (m_pInStream && m_pInStream->getCurFrame(pFrame)) { // convert frame to MCUF packet frame2mcuf(pFrame, mcuf); // send frame to address sendFrame(mcuf, addr); } // no stream of no current frame else { // send "no frame" to address sendNoFrame(addr); } } /** * @brief send frame to address * @param[in] mcuf MCUF data of frame * @param[in] addr address to send to */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendFrame(const std::string &mcuf, const ADDR &addr) { if (m_pSock) { m_pSock->send(mcuf, addr); } } /** * @brief send "no frame" to address * @param[in] addr address to send to */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendNoFrame(const ADDR &addr) { std::string noframe; // FIXME if (m_pSock) { m_pSock->send(noframe, addr); } } /** * @brief convert frame to MCUF data * @param[in] pFrame frame * @param[out] mcuf MCUF data */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::frame2mcuf(stBlinkenFrame *pFrame, std::string &mcuf) { char buf[65536]; int len; // convert frame to MCUF packet len = BlinkenFrameToNetwork(pFrame, BlinkenProtoMcuf, buf, sizeof(buf)); mcuf.assign(buf, len); } /* ################ # Sender::Dest # ################ */ /// constructor template<typename ADDR, typename SOCK> Sender<ADDR, SOCK>::Dest::Dest(const std::string &name, const File &file): m_name(name), m_file(file) { } /// parse address from current file template<typename ADDR, typename SOCK> bool Sender<ADDR, SOCK>::Dest::parse() { std::string strAddr; return m_file.getStr(strAddr) && m_addr.fromStr(strAddr); } } // namespace Blinker #endif // #ifndef SENDER_H