message_handler.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef FLATHEADERS
34 #else
35 #include "message_handler.h"
36 #include "log_wrapper.h"
37 #endif
38 
39 namespace industrial
40 {
41 namespace message_handler
42 {
43 
44 using namespace industrial::smpl_msg_connection;
45 using namespace industrial::simple_message;
46 
48 {
49  this->setConnection(NULL);
50  this->setMsgType(StandardMsgTypes::INVALID);
51 }
52 
53 
55 {
56 }
57 
58 
59 bool MessageHandler::init(int msg_type, SmplMsgConnection* connection)
60 {
61  bool rtn = false;
62 
64  {
65  if (NULL != connection)
66  {
67  this->setConnection(connection);
68  this->setMsgType(msg_type);
69  rtn = true;
70  }
71  else
72  {
73  LOG_ERROR("Message connection is NULL");
74  rtn = false;
75  }
76  }
77  else
78  {
79  LOG_ERROR("Message handler type: %d, not valid", msg_type);
80  rtn = false;
81  }
82 
83  return rtn;
84 }
85 
86 
87 
89 {
90  bool rtn = false;
91 
92  if (validateMsg(in))
93  {
94  this->internalCB(in);
95  }
96  else
97  {
98  LOG_ERROR("Invalid message passed to callback");
99  rtn = true;
100  }
101 
102  return rtn;
103 }
104 
105 
107 {
108  bool rtn = false;
109 
110  if (in.validateMessage())
111  {
112  if (in.getMessageType() == this->getMsgType())
113  {
114  rtn = true;
115  }
116  else
117  {
118  LOG_WARN("Message type: %d, doesn't match handler type: %d",
119  in.getMessageType(), this->getMsgType());
120  rtn = false;
121  }
122  }
123  else
124  {
125  LOG_WARN("Passed in message invalid");
126  }
127 
128  return rtn;
129 
130 }
131 
132 } // namespace message_handler
133 } // namespace industrial
industrial::message_handler::MessageHandler::validateMsg
bool validateMsg(industrial::simple_message::SimpleMessage &in)
Validates incoming message for processing by internal callback.
Definition: message_handler.cpp:106
industrial::message_handler::MessageHandler::callback
bool callback(industrial::simple_message::SimpleMessage &in)
Callback function that should be executed when a message arrives DO NOT OVERRIDE THIS FUNCTION....
Definition: message_handler.cpp:88
industrial::message_handler::MessageHandler::~MessageHandler
~MessageHandler()
Destructor.
Definition: message_handler.cpp:54
LOG_WARN
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
industrial
Definition: byte_array.h:45
industrial::message_handler::MessageHandler::init
bool init(int msg_type, industrial::smpl_msg_connection::SmplMsgConnection *connection)
Class initializer.
Definition: message_handler.cpp:59
industrial::smpl_msg_connection
Definition: smpl_msg_connection.h:48
message_handler.h
industrial::message_handler::MessageHandler::MessageHandler
MessageHandler()
Constructor.
Definition: message_handler.cpp:47
LOG_ERROR
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
industrial::simple_message::SimpleMessage
This class defines a simple messaging protocol for communicating with an industrial robot controller.
Definition: simple_message.h:164
industrial::simple_message
Definition: simple_message.h:49
industrial::simple_message::StandardMsgTypes::INVALID
@ INVALID
Definition: simple_message.h:60
industrial::simple_message::SimpleMessage::validateMessage
bool validateMessage()
performs logical checks to ensure that the message is fully defined and adheres to the message conven...
Definition: simple_message.cpp:134
log_wrapper.h
industrial::smpl_msg_connection::SmplMsgConnection
Defines an interface and common methods for sending simple messages (see simple_message)....
Definition: smpl_msg_connection.h:61
msg_type
def msg_type(f)
industrial::simple_message::SimpleMessage::getMessageType
int getMessageType()
Gets message type(see StandardMsgType)
Definition: simple_message.h:239


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53