Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef ROS
00032 #include "ros/ros.h"
00033 #include "simple_message/message_manager.h"
00034 #include "simple_message/log_wrapper.h"
00035 #include "simple_message/simple_message.h"
00036 #endif
00037
00038 #ifdef MOTOPLUS
00039 #include "message_manager.h"
00040 #include "log_wrapper.h"
00041 #include "simple_message.h"
00042 #endif
00043
00044
00045 using namespace industrial::smpl_msg_connection;
00046 using namespace industrial::message_handler;
00047 using namespace industrial::simple_message;
00048 using namespace industrial::comms_fault_handler;
00049 using namespace industrial::simple_comms_fault_handler;
00050
00051 namespace industrial
00052 {
00053 namespace message_manager
00054 {
00055
00059 MessageManager::MessageManager()
00060 {
00061 this->num_handlers_ = 0;
00062 for (unsigned int i = 0; i < this->getMaxNumHandlers(); i++)
00063 {
00064 this->handlers_[i] = NULL;
00065 }
00066 this->comms_hndlr_ = NULL;
00067 }
00068
00069 MessageManager::~MessageManager()
00070 {
00071
00072 }
00073
00074 bool MessageManager::init(SmplMsgConnection* connection)
00075 {
00076 bool rtn = false;
00077
00078 LOG_INFO("Initializing message manager with default comms fault handler");
00079
00080
00081 if (NULL != connection)
00082 {
00083 this->getDefaultCommsFaultHandler().init(connection);
00084 this->init(connection, (CommsFaultHandler*)(&this->getDefaultCommsFaultHandler()));
00085 rtn = true;
00086 }
00087 else
00088 {
00089 LOG_ERROR("NULL connection passed into manager init");
00090 rtn = false;
00091 }
00092
00093 return rtn;
00094 }
00095
00096 bool MessageManager::init(SmplMsgConnection* connection, CommsFaultHandler* fault_handler)
00097 {
00098 bool rtn = false;
00099
00100 LOG_INFO("Initializing message manager");
00101
00102 if (NULL != connection && NULL != fault_handler)
00103 {
00104 this->setConnection(connection);
00105 this->getPingHandler().init(connection);
00106 this->setCommsFaultHandler(fault_handler);
00107
00108 if (this->add(&this->getPingHandler()))
00109 {
00110 rtn = true;
00111 }
00112 else
00113 {
00114 rtn = false;
00115 LOG_WARN("Failed to add ping handler, manager won't respond to pings");
00116 }
00117
00118 }
00119 else
00120 {
00121 LOG_ERROR("NULL connection or NULL fault handler passed into manager init");
00122 rtn = false;
00123 }
00124
00125 return rtn;
00126 }
00127
00128
00129
00130 void MessageManager::spinOnce()
00131 {
00132 SimpleMessage msg;
00133 MessageHandler* handler = NULL;
00134
00135 if(!this->getConnection()->isConnected())
00136 {
00137 this->getCommsFaultHandler()->connectionFailCB();
00138 }
00139
00140 if (this->getConnection()->receiveMsg(msg))
00141 {
00142 LOG_COMM("Message received");
00143 handler = this->getHandler(msg.getMessageType());
00144
00145 if (NULL != handler)
00146 {
00147 LOG_DEBUG("Executing handler callback for message type: %d", handler->getMsgType());
00148 handler->callback(msg);
00149 }
00150 else
00151 {
00152 if (CommTypes::SERVICE_REQUEST == msg.getCommType())
00153 {
00154 simple_message::SimpleMessage fail;
00155 fail.init(msg.getMessageType(), CommTypes::SERVICE_REPLY, ReplyTypes::FAILURE);
00156 this->getConnection()->sendMsg(fail);
00157 LOG_WARN("Unhandled message type encounters, sending failure reply");
00158 }
00159 LOG_ERROR("Message callback for message type: %d, not exectued", msg.getMessageType());
00160 }
00161 }
00162 else
00163 {
00164 LOG_ERROR("Failed to receive incoming message");
00165 this->getCommsFaultHandler()->sendFailCB();
00166 }
00167 }
00168
00169 void MessageManager::spin()
00170 {
00171 LOG_INFO("Entering message manager spin loop");
00172 #ifdef ROS
00173 while (ros::ok())
00174 #else
00175 while (true)
00176 #endif
00177 {
00178 this->spinOnce();
00179 }
00180 }
00181
00182 bool MessageManager::add(MessageHandler * handler)
00183 {
00184 bool rtn = false;
00185
00186 if (NULL != handler)
00187 {
00188 if (this->getMaxNumHandlers() > this->getNumHandlers())
00189 {
00190
00191
00192
00193 if (NULL == getHandler(handler->getMsgType()))
00194 {
00195 this->handlers_[this->getNumHandlers()] = handler;
00196 this->setNumHandlers(this->getNumHandlers() + 1);
00197 LOG_INFO("Added message handler for message type: %d", handler->getMsgType());
00198 rtn = true;
00199 }
00200 else
00201 {
00202 LOG_ERROR("Failed to add handler for: %d, handler already exists", handler->getMsgType());
00203 rtn = false;
00204 }
00205 }
00206 else
00207 {
00208 LOG_ERROR("Max number of hanlders exceeded");
00209 rtn = false;
00210 }
00211 }
00212 else
00213 {
00214 LOG_ERROR("NULL handler not added");
00215 rtn = false;
00216 }
00217 return rtn;
00218 }
00219
00220 MessageHandler* MessageManager::getHandler(int msg_type)
00221 {
00222 MessageHandler* rtn = NULL;
00223 MessageHandler* temp = NULL;
00224
00225 for (unsigned int i = 0; i < this->getMaxNumHandlers(); i++)
00226 {
00227 temp = this->handlers_[i];
00228
00229
00230 if (NULL != temp)
00231 {
00232 if (temp->getMsgType() == msg_type)
00233 {
00234 rtn = temp;
00235 break;
00236 }
00237 }
00238 else
00239 {
00240 rtn = NULL;
00241 LOG_WARN("Null value encountered, end of handlers reached");
00242 break;
00243 }
00244 }
00245
00246 if (NULL == rtn)
00247 {
00248 LOG_WARN("Handler not found for type: %d", msg_type);
00249 }
00250
00251 return rtn;
00252 }
00253
00254 }
00255 }