m2etis  0.4
MessageSerialization.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_MESSAGE_SERIALIZATION_H__
23 #define __M2ETIS_MESSAGE_SERIALIZATION_H__
24 
26 
28 
29 #include "boost/archive/text_iarchive.hpp"
30 #include "boost/archive/text_oarchive.hpp"
31 
32 #ifdef WITH_MESSAGECOMPRESSION
33  #include "boost/iostreams/filtering_streambuf.hpp"
34  #include "boost/iostreams/copy.hpp"
35  #include "boost/iostreams/filter/zlib.hpp"
36 #endif
37 
38 namespace m2etis {
39 namespace message {
40 namespace serialization {
41 
42  template<class NetworkType>
43  typename NetworkMessage<NetworkType>::Ptr deserializeNetworkMsg(const std::string & msg) {
44  std::stringstream objStringStream(msg);
45 
46 #ifdef WITH_MESSAGECOMPRESSION
47  boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
48  out.push(boost::iostreams::zlib_decompressor());
49  out.push(objStringStream);
50  std::stringstream tmp;
51  boost::iostreams::copy(out, tmp);
52 
53  boost::archive::text_iarchive objOArchive(tmp, boost::archive::no_header | boost::archive::no_codecvt | boost::archive::no_xml_tag_checking | boost::archive::archive_flags::no_tracking);
54 #else
55  boost::archive::text_iarchive objOArchive(objStringStream, boost::archive::no_header | boost::archive::no_codecvt | boost::archive::no_xml_tag_checking | boost::archive::archive_flags::no_tracking);
56 #endif
57 
59  objOArchive >> nm;
60 
61  return nm;
62  }
63 
64  template<class MessageType>
65  std::string serializeNetworkMsg(typename MessageType::Ptr msg) {
66  std::stringstream objStringStream;
67  boost::archive::text_oarchive objOArchive(objStringStream, boost::archive::no_header | boost::archive::no_codecvt | boost::archive::no_xml_tag_checking | boost::archive::archive_flags::no_tracking);
68  objOArchive << msg;
69 
70 #ifdef WITH_MESSAGECOMPRESSION
71  boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
72  out.push(boost::iostreams::zlib_compressor());
73  out.push(objStringStream);
74  std::stringstream tmp;
75  boost::iostreams::copy(out, tmp);
76 
77  return tmp.str();
78 #else
79  return objStringStream.str();
80 #endif
81  }
82 
83 } /* namespace serialization */
84 } /* namespace message */
85 } /* namespace m2etis */
86 
87 #endif /* __M2ETIS_MESSAGE_SERIALIZATION_H__ */
88 
std::string serializeNetworkMsg(typename MessageType::Ptr msg)
boost::shared_ptr< NetworkMessage > Ptr
NetworkMessage< NetworkType >::Ptr deserializeNetworkMsg(const std::string &msg)