BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
3534d9a
Branches
Tags
master
Blinker
src
noarch
Sender_impl.h
simplified interface of getCurFrame mehtods
Stefan Schuermans
commited
3534d9a
at 2011-12-04 14:57:24
Sender_impl.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_IMPL_H #define SENDER_IMPL_H #include <list> #include <map> #include <string> #include <BlinkenLib/BlinkenProto.h> #include <BlinkenLib/BlinkenFrame.h> #include "CallMgr.h" #include "Directory.h" #include "File.h" #include "IoCallee.h" #include "Module.h" #include "Protocol.h" #include "Sender.h" #include "SenderDest.h" #include "SenderDest_impl.h" #include "SettingFile.h" #include "StreamMgr.h" #include "StreamRecv.h" #include "Time.h" #include "TimeCallee.h" namespace Blinker { /** * @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_fileProtocol(dirBase.getFile("protocol")), m_dirDests(dirBase.getSubdir("destinations")), m_pInStream(NULL), m_pSock(NULL), m_haveProtocol(false) { // read protocol readProto(); // 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 destinations sendAllNoFrame(); // free static destination lists while (!m_destList.empty()) { delete m_destList.back().m_pDest; m_destList.pop_back(); } // destroy socket destroySock(); // detach from input stream and release it releaseInStream(); // cancel time callback m_callMgr.cancelTimeCall(this); } /// 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(); } // protocol file was modified -> re-read protocol if (m_fileProtocol.checkModified()) readProto(); // static destinations update // (directory modified -> full, otherwise -> light) if (m_dirDests.checkModified()) updateDestsFull(); else updateDestsLight(); } /** * @brief set current frame * @param[in] stream stream name * @param[in] pFrame current frame (NULL for none) */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::setFrame(const std::string &stream, stBlinkenFrame *pFrame) { // convert new frame to protocol data frame2data(pFrame, m_data); // send new protocol data to all destinations sendAllProto(); (void)stream; // unused } /// callback when requsted time reached template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::timeCall() { // repeat current protocol data to all destinations sendAllProto(); } /** * @brief callback when I/O object is readable * @param[in] io I/O object that is readable */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::ioReadCall(Io *io) { // reception on socket if (io == m_pSock) receiveFromSock(); } /** * @brief callback when I/O object is writable * @param[in] io I/O object that is writable */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::ioWriteCall(Io *io) { (void)io; // unused } /// (re-)read protocol template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::readProto() { std::string strProto; // send "no frame" to all destinations // (stream with old protocol will stop now) sendAllNoFrame(); // clear dynamic destinations // (they registered with old protocol, which is out of service now) m_dynDests.clear(); // clear old frame data and old no frame data m_noFrameData.clear(); m_data.clear(); // read new protocol from file m_haveProtocol = m_fileProtocol.getStr(strProto) && m_protocol.fromStr(strProto); // create new no frame protocol data and new protocol data if (m_haveProtocol) { frame2data(NULL, m_noFrameData); if (m_pInStream) frame2data(m_pInStream->getCurFrame(), m_data); else frame2data(NULL, m_data); } // send current protocol data to all destinations sendAllProto(); } /// 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; } // request callback on recepetion m_callMgr.requestIoReadCall(this, m_pSock); // send current protocol data to all destinations sendAllProto(); } /// destroy socket template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::destroySock() { // send "no frame" to all destinations // (stream from this socket will stop now) sendAllNoFrame(); // clear dynamic destinations // (they registered with this socket and this socket is gone) m_dynDests.clear(); // cancel callback request m_callMgr.cancelIoReadCall(this, m_pSock); // 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 DestList::iterator itDest; for (itDest = m_destList.begin(); itDest != m_destList.end(); ++itDest) itDest->m_pDest->updateConfig(); } /** * @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 subdirs in input directory typedef std::list<std::string> Subdirlist; Subdirlist curSubdirs; m_dirDests.getEntries(Directory::TypeSubdir, curSubdirs); // walk through current static destinations and subdir list simultaneously Subdirlist::const_iterator itSubdir = curSubdirs.begin(); typename DestList::iterator itDest = m_destList.begin(); while (itSubdir != curSubdirs.end() || itDest != m_destList.end()) { // new static destination inserted if (itDest == m_destList.end() || (itSubdir != curSubdirs.end() && *itSubdir < itDest->m_name)) { // create static destination object DestEntry destEntry(*itSubdir); destEntry.m_pDest = new Dest(*this, m_dirDests.getSubdir(*itSubdir), &m_noFrameData); if (destEntry.m_pDest) // insert static destination entry m_destList.insert(itDest, destEntry); // advance to next subdir ++itSubdir; } // static destination removed else if (itSubdir == curSubdirs.end() || *itSubdir > itDest->m_name) { // remove static destination delete itDest->m_pDest; itDest = m_destList.erase(itDest); // do not advance to next subdir } // static sestination stayed in input list else { // check for update itDest->m_pDest->updateConfig(); // advance to next file and next entry ++itSubdir; ++itDest; } } // while itSubdir itDest } /// remove timed-out dynamic destinations template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::removeTimedOutDynDests() { Time now, timeout; typename DynDests::iterator itDyn; now = Time::now(); timeout = Time(30); for (itDyn = m_dynDests.begin(); itDyn != m_dynDests.end(); ) if (itDyn->second + timeout < now) m_dynDests.erase(itDyn++); else ++itDyn; } /// send current protocol data to all destinations template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendAllProto() { // remove timed-out dynamic destinations removeTimedOutDynDests(); // send current protocol data to all static/dynamic destinations sendDests(&m_data); // request time callback in one second m_callMgr.requestTimeCall(this, Time::now() + Time(1)); } /// send "no frame" to all destinations template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendAllNoFrame() { // remove timed-out dynamic destinations removeTimedOutDynDests(); // get "no frame" protocol data and send to all static/dynamic destinations sendDests(&m_noFrameData); // request time callback in one second m_callMgr.requestTimeCall(this, Time::now() + Time(1)); } /** * @brief send data to static/dynamic destinations * @param[in] data *pData protocol data to send */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendDests(const std::string *pData) { // send data to static destinations typename DestList::const_iterator itDest; for (itDest = m_destList.begin(); itDest != m_destList.end(); ++itDest) itDest->m_pDest->setProtoData(pData); // send data to all dynamic destinations typename DynDests::const_iterator itDyn; for (itDyn = m_dynDests.begin(); itDyn != m_dynDests.end(); ++itDyn) sendProto(*pData, itDyn->first); } /** * @brief send protocol data to address * @param[in] data protocol data of frame (empty if unknown) * @param[in] addr address to send to */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::sendProto(const std::string &data, const ADDR &addr) const { if (m_pSock && !data.empty()) m_pSock->send(data, addr); } /** * @brief convert frame to protocol data * @param[in] pFrame frame (NULL for none) * @param[out] data protocol data */ template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::frame2data(stBlinkenFrame *pFrame, std::string &data) const { char buf[65536]; int len; // no protocol -> leave with empty data if (!m_haveProtocol) { data.clear(); return; } // convert frame to protcol data if (pFrame) len = BlinkenFrameToNetwork(pFrame, m_protocol.m_proto, buf, sizeof(buf)); else len = BlinkenProtoMakePacket(m_protocol.m_proto, BlinkenPacketStreamEnd, buf, sizeof(buf)); if (len < 0) len = 0; data.assign(buf, len); } /// receive data from socket template<typename ADDR, typename SOCK> void Sender<ADDR, SOCK>::receiveFromSock() { etBlinkenProto proto; etBlinkenPacket packet; std::string data; ADDR addr; // make sure socket exists if (!m_pSock) return; // receive (leave if no reception) if (!m_pSock->recv(data, addr)) return; // detect packet type and protocol BlinkenProtoDetectPacket(data.c_str(), data.size(), &proto, &packet); if (m_haveProtocol && proto == m_protocol.m_proto) { switch (packet) { // request -> add to dynamic destinations and send current frame case BlinkenPacketRequest: m_dynDests[addr] = Time::now(); sendProto(m_data, addr); break; // end request -> remove from dynamic destinations case BlinkenPacketEndRequest: m_dynDests.erase(addr); break; default: break; } // switch (packet) } // if (m_haveProtocol ... } /* ##################### # Sender::DestEntry # ##################### */ /// constructor template<typename ADDR, typename SOCK> Sender<ADDR, SOCK>::DestEntry::DestEntry(const std::string &name): m_name(name), m_pDest(NULL) { } } // namespace Blinker #endif // #ifndef SENDER_H