i6engine  1.0
Message.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_CORE_MESSAGE_H__
26 #define __I6ENGINE_CORE_MESSAGE_H__
27 
29 
31 
32 #include "boost/archive/text_iarchive.hpp"
33 #include "boost/archive/text_oarchive.hpp"
34 #include "boost/serialization/export.hpp"
35 
36 namespace i6e {
37 namespace core {
38 
39  enum class Subsystem;
40 
41  enum class Method : uint8_t {
42  Create,
43  Update,
44  Delete
45  };
46 
65  public:
66  typedef boost::shared_ptr<Message> Ptr;
67 
72  Message() : _messageType(), _subType(), _method(), _content(), _sender() {
73  }
74 
82  Message(uint16_t messageType, uint16_t subtype, const Method method, MessageStruct * content, Subsystem sender) : _messageType(messageType), _subType(subtype), _method(method), _content(content), _sender(sender) {
83  }
84 
89  Message(const Message & msg) : _messageType(msg.getMessageType()), _subType(msg.getSubtype()), _method(msg.getMethod()), _content(), _sender(msg.getSender()) {
90  if (msg.getContent() != nullptr) {
91  _content = msg.getContent()->copy();
92  }
93  }
94 
95  const Message & operator=(const Message &);
96 
100  virtual ~Message() {
101  delete _content;
102  }
103 
108  inline uint16_t getMessageType() const {
109  return _messageType;
110  }
111 
116  inline uint16_t getSubtype() const {
117  return _subType;
118  }
119 
124  inline Method getMethod() const { return _method; }
125 
130  inline MessageStruct * getContent() const { return _content; }
131 
132 #ifdef ISIXE_WITH_PROFILING
133 
138  void insertTimestamp(const std::string & text);
139 
143  std::string getTimestamps();
144 #endif /* ISIXE_WITH_PROFILING */
145 
150  inline void setMessageType(uint16_t messageType) {
151  _messageType = messageType;
152  }
153 
158  return _sender;
159  }
160 
165  std::string getMessageInfo() const;
166 
167  std::string Serialize() const;
168 
169  static Message * Deserialize(const std::string & s);
170 
171  protected:
172  friend class boost::serialization::access;
173 
179  template <typename Archive>
180  void serialize(Archive & ar, const unsigned int) {
181  ar & _messageType;
182  ar & _subType;
183  ar & _method;
184  ar & _content;
185  }
186 
187  uint16_t _messageType;
188  uint16_t _subType;
190 
192 
194  };
195 
196  std::ostream & operator<<(std::ostream & stream, const Message & msg);
197  std::ostream & operator<<(std::ostream & stream, const Message::Ptr & msg);
198 
199 } /* namespace core */
200 } /* namespace i6e */
201 
202 #endif /* __I6ENGINE_CORE_MESSAGE_H__ */
203 
MessageStruct * _content
Definition: Message.h:191
Message()
Default constructor necessary for serialization.
Definition: Message.h:72
Method getMethod() const
Getter for the message method.
Definition: Message.h:124
uint16_t _subType
Definition: Message.h:188
Message(const Message &msg)
Standard copy constructor.
Definition: Message.h:89
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
Subsystem _sender
Definition: Message.h:193
virtual ~Message()
Destructor, deleting message content.
Definition: Message.h:100
Class representing a message to be sent.
Definition: Message.h:64
uint16_t getMessageType() const
Getter for the message type.
Definition: Message.h:108
virtual MessageStruct * copy()
Copy method returning an exact copy of itself.
uint16_t _messageType
Definition: Message.h:187
Message(uint16_t messageType, uint16_t subtype, const Method method, MessageStruct *content, Subsystem sender)
Constructor.
Definition: Message.h:82
void serialize(Archive &ar, const unsigned int)
Prepares a Message to be sent over the Network.
Definition: Message.h:180
Subsystem getSender() const
returns the Subsystem sending this message
Definition: Message.h:157
void setMessageType(uint16_t messageType)
Changes the message type to the given parameter.
Definition: Message.h:150
MessageStruct * getContent() const
Getter for the message content.
Definition: Message.h:130
#define ISIXE_CORE_API
uint16_t getSubtype() const
Getter for the message subtype.
Definition: Message.h:116
ISIXE_CORE_API std::ostream & operator<<(std::ostream &stream, const IPKey &key)