msgManager.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * @Brief     Manage msg senders and receiver. ROS-free and mav-free
00003  * @Version   0.3.0
00004  * @Author    Chris Liu
00005  * @Created   2015/10/30
00006  * @Modified  2015/12/28
00007  *****************************************************************************/
00008 
00009 #ifndef _DJI2MAV_MSGMANAGER_H_
00010 #define _DJI2MAV_MSGMANAGER_H_
00011 
00012 
00013 #include <new>
00014 #include <string>
00015 #include <mutex>
00016 
00017 #include "msgSender.h"
00018 #include "msgReceiver.h"
00019 #include "log.h"
00020 
00021 namespace dji2mav{
00022 
00023     class MsgManager {
00024         public:
00031             MsgManager(uint16_t senderListSize, uint16_t sendBufSize, 
00032                     uint16_t recvBufSize) : m_maxListSize(senderListSize), 
00033                     m_defaultSendBufSize(sendBufSize) {
00034 
00035                 DJI2MAV_DEBUG("Going to construct MsgManager with max " 
00036                         "senderListSize %u, default sendBufSize %u and " 
00037                         "recvBufSize %u...", senderListSize, sendBufSize, 
00038                         recvBufSize);
00039 
00040                 try {
00041                     m_senderList = new MsgSender*[m_maxListSize];
00042                     memset(m_senderList, 0, m_maxListSize * sizeof(MsgSender*));
00043                     m_currListSize = 0;
00044                     m_comm = new SocketComm();
00045                     m_receiver = new MsgReceiver(recvBufSize);
00046                 } catch(std::bad_alloc& m) {
00047                     DJI2MAV_FATAL( "Fail to alloc memory for MsgManager! " 
00048                             "Exception: %s!", m.what() );
00049                     exit(EXIT_FAILURE);
00050                 }
00051 
00052                 DJI2MAV_DEBUG("...finish constructing MsgManager.");
00053 
00054             }
00055 
00056 
00057             ~MsgManager() {
00058                 DJI2MAV_DEBUG("Going to construct MsgManager...");
00059                 if(NULL != m_senderList) {
00060                     for(uint16_t i = 0; i < m_currListSize; ++i) {
00061                         if(NULL != m_senderList[i]) {
00062                             delete m_senderList[i];
00063                             m_senderList[i] = NULL;
00064                         }
00065                     }
00066                     delete []m_senderList;
00067                     m_senderList = NULL;
00068                 }
00069                 if(NULL != m_receiver) {
00070                     delete m_receiver;
00071                     m_receiver = NULL;
00072                 }
00073                 if(NULL != m_comm) {
00074                     delete m_comm;
00075                     m_comm = NULL;
00076                 }
00077                 DJI2MAV_DEBUG("...finish destructing MsgManager.");
00078             }
00079 
00080 
00086             inline bool isValidSenderIdx(uint16_t senderIdx) {
00087                 return (senderIdx < m_currListSize);
00088             }
00089 
00090 
00096             int registerSender(uint16_t bufSize) {
00097                 DJI2MAV_DEBUG("Register a sender of %u bytes buf...", bufSize);
00098                 m_registerMutex.lock();
00099                 if(m_currListSize >= m_maxListSize) {
00100                     DJI2MAV_ERROR("The sender list with %u sender is full!", 
00101                             m_currListSize);
00102                     return -1;
00103                 }
00104                 try {
00105                     m_senderList[m_currListSize] = new MsgSender(bufSize);
00106                 } catch(std::bad_alloc &m) {
00107                     DJI2MAV_FATAL( "Fail to new MsgSender object! " 
00108                             "Exception: %s!", m.what() );
00109                     exit(EXIT_FAILURE);
00110                 }
00111                 m_registerMutex.unlock();
00112                 DJI2MAV_DEBUG("...succeed in registering the sender with "
00113                         "index %u", m_currListSize);
00114                 return m_currListSize++;
00115             }
00116 
00117 
00122             inline int registerSender() {
00123                 return registerSender(m_defaultSendBufSize);
00124             }
00125 
00126 
00132             inline uint8_t* getSendBuf(uint16_t idx) {
00133                 if( !isValidSenderIdx(idx) ) {
00134                     DJI2MAV_ERROR("Invalid sender index %u!", idx);
00135                     return NULL;
00136                 }
00137                 return m_senderList[idx]->getBuf();
00138             }
00139 
00140 
00146             inline uint16_t getSendBufSize(uint16_t idx) {
00147                 if( !isValidSenderIdx(idx) ) {
00148                     DJI2MAV_ERROR("Invalid sender index %u!", idx);
00149                     return -1;
00150                 }
00151                 return m_senderList[idx]->getBufSize();
00152             }
00153 
00154 
00161             int send(uint16_t idx, int len) {
00162                 if( !isValidSenderIdx(idx) ) {
00163                     DJI2MAV_ERROR("Send fail! Invalid sender index %u!", idx);
00164                     return -2;
00165                 }
00166                 if(len < 0 || m_senderList[idx]->getBufSize() < len) {
00167                     DJI2MAV_ERROR("Send fail! Invalid length value %d!", len);
00168                     return -3;
00169                 }
00170                 return m_senderList[idx]->send(m_comm, len);
00171             }
00172 
00173 
00178             inline uint8_t* getRecvBuf() {
00179                 return m_receiver->getBuf();
00180             }
00181 
00182 
00187             inline uint16_t getRecvBufSize() {
00188                 return m_receiver->getBufSize();
00189             }
00190 
00191 
00196             inline int recv() {
00197                 return m_receiver->recv(m_comm);
00198             }
00199 
00200 
00208             bool establish(std::string targetIP, uint16_t targetPort, 
00209                     uint16_t srcPort) {
00210 
00211                 DJI2MAV_INFO("Going to establish connection with %s:%u " 
00212                         "using port %u...", targetIP.c_str(), targetPort, 
00213                         srcPort);
00214 
00215                 m_comm->setConf(targetIP, targetPort, srcPort);
00216                 bool ret = m_comm->connect();
00217 
00218                 if(ret)
00219                     DJI2MAV_INFO("...succeed in establishing connection.");
00220                 else
00221                     DJI2MAV_ERROR("...fail to establish connection!");
00222 
00223                 return ret;
00224 
00225             }
00226 
00227 
00232             inline uint16_t getCurrSenderListSize() {
00233                 return m_currListSize;
00234             }
00235 
00236 
00241             inline uint16_t getMaxSenderListSize() {
00242                 return m_maxListSize;
00243             }
00244 
00245 
00246         private:
00247             MsgSender** m_senderList;
00248             uint16_t m_maxListSize;
00249             uint16_t m_currListSize;
00250             uint16_t m_defaultSendBufSize;
00251             MsgReceiver* m_receiver;
00252             SocketComm* m_comm;
00253 
00254             std::mutex m_registerMutex;
00255     };
00256 
00257 } //namespace dji2mav
00258 
00259 
00260 #endif


dji_sdk_dji2mav
Author(s):
autogenerated on Thu Jun 6 2019 17:55:35