$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2011, Southwest Research Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the Southwest Research Institute, nor the names 00016 * of its contributors may be used to endorse or promote products derived 00017 * from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 #ifdef ROS 00032 #include "simple_message/message_manager.h" 00033 #include "simple_message/log_wrapper.h" 00034 #include "simple_message/simple_message.h" 00035 #endif 00036 00037 #ifdef MOTOPLUS 00038 #include "message_manager.h" 00039 #include "log_wrapper.h" 00040 #include "simple_message.h" 00041 #endif 00042 00043 00044 using namespace industrial::smpl_msg_connection; 00045 using namespace industrial::message_handler; 00046 using namespace industrial::simple_message; 00047 using namespace industrial::comms_fault_handler; 00048 using namespace industrial::simple_comms_fault_handler; 00049 00050 namespace industrial 00051 { 00052 namespace message_manager 00053 { 00054 00058 MessageManager::MessageManager() 00059 { 00060 this->num_handlers_ = 0; 00061 for (unsigned int i = 0; i < this->getMaxNumHandlers(); i++) 00062 { 00063 this->handlers_[i] = NULL; 00064 } 00065 this->comms_hndlr_ = NULL; 00066 } 00067 00068 MessageManager::~MessageManager() 00069 { 00070 00071 } 00072 00073 bool MessageManager::init(SmplMsgConnection* connection) 00074 { 00075 bool rtn = false; 00076 00077 LOG_INFO("Initializing message manager with default comms fault handler"); 00078 00079 00080 if (NULL != connection) 00081 { 00082 this->getDefaultCommsFaultHandler().init(connection); 00083 this->init(connection, (CommsFaultHandler*)(&this->getDefaultCommsFaultHandler())); 00084 rtn = true; 00085 } 00086 else 00087 { 00088 LOG_ERROR("NULL connection passed into manager init"); 00089 rtn = false; 00090 } 00091 00092 return rtn; 00093 } 00094 00095 bool MessageManager::init(SmplMsgConnection* connection, CommsFaultHandler* fault_handler) 00096 { 00097 bool rtn = false; 00098 00099 LOG_INFO("Initializing message manager"); 00100 00101 if (NULL != connection && NULL != fault_handler) 00102 { 00103 this->setConnection(connection); 00104 this->getPingHandler().init(connection); 00105 this->setCommsFaultHandler(fault_handler); 00106 00107 if (this->add(&this->getPingHandler())) 00108 { 00109 rtn = true; 00110 } 00111 else 00112 { 00113 rtn = false; 00114 LOG_WARN("Failed to add ping handler, manager won't respond to pings"); 00115 } 00116 00117 } 00118 else 00119 { 00120 LOG_ERROR("NULL connection or NULL fault handler passed into manager init"); 00121 rtn = false; 00122 } 00123 00124 return rtn; 00125 } 00126 00127 00128 00129 void MessageManager::spinOnce() 00130 { 00131 SimpleMessage msg; 00132 MessageHandler* handler = NULL; 00133 00134 if(!this->getConnection()->isConnected()) 00135 { 00136 this->getCommsFaultHandler()->connectionFailCB(); 00137 } 00138 00139 if (this->getConnection()->receiveMsg(msg)) 00140 { 00141 LOG_INFO("Message received"); 00142 handler = this->getHandler(msg.getMessageType()); 00143 00144 if (NULL != handler) 00145 { 00146 LOG_INFO("Executing handler callback for message type: %d", handler->getMsgType()); 00147 handler->callback(msg); 00148 } 00149 else 00150 { 00151 if (CommTypes::SERVICE_REQUEST == msg.getCommType()) 00152 { 00153 simple_message::SimpleMessage fail; 00154 fail.init(msg.getMessageType(), CommTypes::SERVICE_REPLY, ReplyTypes::FAILURE); 00155 this->getConnection()->sendMsg(fail); 00156 LOG_WARN("Unhandled message type encounters, sending failure reply"); 00157 } 00158 LOG_ERROR("Message callback for message type: %d, not exectued", msg.getMessageType()); 00159 } 00160 } 00161 else 00162 { 00163 LOG_ERROR("Failed to receive incoming message"); 00164 this->getCommsFaultHandler()->sendFailCB(); 00165 } 00166 } 00167 00168 void MessageManager::spin() 00169 { 00170 LOG_INFO("Entering message manager spin loop"); 00171 while (true) 00172 { 00173 this->spinOnce(); 00174 } 00175 } 00176 00177 bool MessageManager::add(MessageHandler * handler) 00178 { 00179 bool rtn = false; 00180 00181 if (NULL != handler) 00182 { 00183 if (this->getMaxNumHandlers() > this->getNumHandlers()) 00184 { 00185 // If get handler returns NULL then a hander for the message type 00186 // does not exist and this one can be added, otherwise return 00187 // and error 00188 if (NULL == getHandler(handler->getMsgType())) 00189 { 00190 this->handlers_[this->getNumHandlers()] = handler; 00191 this->setNumHandlers(this->getNumHandlers() + 1); 00192 LOG_INFO("Added message handler for message type: %d", handler->getMsgType()); 00193 rtn = true; 00194 } 00195 else 00196 { 00197 LOG_ERROR("Failed to add handler for: %d, handler already exists", handler->getMsgType()); 00198 rtn = false; 00199 } 00200 } 00201 else 00202 { 00203 LOG_ERROR("Max number of hanlders exceeded"); 00204 rtn = false; 00205 } 00206 } 00207 else 00208 { 00209 LOG_ERROR("NULL handler not added"); 00210 rtn = false; 00211 } 00212 return rtn; 00213 } 00214 00215 MessageHandler* MessageManager::getHandler(int msg_type) 00216 { 00217 MessageHandler* rtn = NULL; 00218 MessageHandler* temp = NULL; 00219 00220 for (unsigned int i = 0; i < this->getMaxNumHandlers(); i++) 00221 { 00222 temp = this->handlers_[i]; 00223 // The handlers are searched until the appropriate handler is found 00224 // or a NULL value is found (signifies the end of the buffer); 00225 if (NULL != temp) 00226 { 00227 if (temp->getMsgType() == msg_type) 00228 { 00229 rtn = temp; 00230 break; 00231 } 00232 } 00233 else 00234 { 00235 rtn = NULL; 00236 LOG_WARN("Null value encountered, end of handlers reached"); 00237 break; 00238 } 00239 } 00240 00241 if (NULL == rtn) 00242 { 00243 LOG_WARN("Handler not found for type: %d", msg_type); 00244 } 00245 00246 return rtn; 00247 } 00248 00249 } // namespace message_manager 00250 } // namespace industrial