/src/libs/acewrapper/lifecycle_frame_serializer.cpp

https://github.com/HiroyukiSeki/qtplatz · C++ · 310 lines · 241 code · 39 blank · 30 comment · 5 complexity · 7c3bde0833e4669fc30415ffc1837cd7 MD5 · raw file

  1. // -*- C++ -*-
  2. /**************************************************************************
  3. ** Copyright (C) 2010-2011 Toshinobu Hondo, Ph.D.
  4. ** Science Liaison / Advanced Instrumentation Project
  5. *
  6. ** Contact: toshi.hondo@scienceliaison.com
  7. **
  8. ** Commercial Usage
  9. **
  10. ** Licensees holding valid ScienceLiaison commercial licenses may use this
  11. ** file in accordance with the ScienceLiaison Commercial License Agreement
  12. ** provided with the Software or, alternatively, in accordance with the terms
  13. ** contained in a written agreement between you and ScienceLiaison.
  14. **
  15. ** GNU Lesser General Public License Usage
  16. **
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.TXT included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. **************************************************************************/
  25. #include "lifecycle_frame_serializer.hpp"
  26. #include <adportable/protocollifecycle.hpp>
  27. #include <acewrapper/outputcdr.hpp>
  28. #include <acewrapper/inputcdr.hpp>
  29. #include <boost/variant/static_visitor.hpp>
  30. using namespace acewrapper;
  31. using namespace adportable::protocol;
  32. lifecycle_frame_serializer::lifecycle_frame_serializer()
  33. {
  34. }
  35. //////////////////////////////////////////////////////////////////////////////////////
  36. namespace acewrapper {
  37. namespace internal {
  38. using namespace adportable::protocol;
  39. using namespace acewrapper;
  40. class lifecycle_serializer {
  41. public:
  42. static void serialize( OutputCDR& cdr, const LifeCycleFrame& frame ) {
  43. cdr << frame.endian_mark_;
  44. cdr << frame.proto_version_;
  45. cdr << frame.ctrl_;
  46. cdr << frame.hoffset_;
  47. cdr << frame.command_;
  48. }
  49. static void serialize( ACE_OutputCDR& cdr, const LifeCycleFrame& frame ) {
  50. cdr.write_ushort( frame.endian_mark_ );
  51. cdr.write_ushort( frame.proto_version_ );
  52. cdr.write_ushort( frame.ctrl_);
  53. cdr.write_ushort( frame.hoffset_ );
  54. cdr.write_ulong( frame.command_ );
  55. }
  56. };
  57. //-----------
  58. class lifecycle_command_visitor : public boost::static_visitor< adportable::protocol::LifeCycleCommand > {
  59. public:
  60. template<class T> LifeCycleCommand operator()( T& ) const { return T::command(); }
  61. };
  62. //-----------------
  63. class lifecycle_serializer_visitor : public boost::static_visitor< unsigned int > {
  64. ACE_OutputCDR& cdr_;
  65. public:
  66. lifecycle_serializer_visitor( ACE_OutputCDR& cdr ) : cdr_(cdr) {}
  67. unsigned int operator()( const LifeCycle_Hello& data ) const;
  68. unsigned int operator()( const LifeCycle_SYN& data ) const;
  69. unsigned int operator()( const LifeCycle_SYN_Ack& data ) const;
  70. unsigned int operator()( const LifeCycle_Data& data ) const;
  71. unsigned int operator()( const LifeCycle_DataAck& data ) const;
  72. unsigned int operator()( const LifeCycle_Close& data ) const;
  73. };
  74. //-----------------
  75. class lifecycle_deserializer_visitor : public boost::static_visitor< void > {
  76. ACE_InputCDR& cdr_;
  77. public:
  78. lifecycle_deserializer_visitor( ACE_InputCDR& in ) : cdr_(in) {}
  79. void operator()( LifeCycleFrame& frame ) const;
  80. void operator()( LifeCycle_Hello& data ) const;
  81. void operator()( LifeCycle_SYN& data ) const;
  82. void operator()( LifeCycle_SYN_Ack& data ) const;
  83. void operator()( LifeCycle_Data& data ) const;
  84. void operator()( LifeCycle_DataAck& data ) const;
  85. void operator()( LifeCycle_Close& data ) const;
  86. };
  87. }
  88. }
  89. //////////////////////////////////////////////////////////////////////////////////////
  90. namespace acewrapper {
  91. template<> bool
  92. lifecycle_frame_serializer::pack( ACE_OutputCDR& cdr, const LifeCycleData& v )
  93. {
  94. LifeCycleFrame frame( boost::apply_visitor( internal::lifecycle_command_visitor(), v ) );
  95. internal::lifecycle_serializer::serialize(cdr, frame);
  96. unsigned int size = boost::apply_visitor( internal::lifecycle_serializer_visitor(cdr), v );
  97. if ( size )
  98. return true;
  99. return false;
  100. }
  101. template<> ACE_Message_Block *
  102. lifecycle_frame_serializer::pack( const LifeCycleData& v )
  103. {
  104. ACE_OutputCDR ace_cdr;
  105. OutputCDR cdr(ace_cdr);
  106. LifeCycleFrame frame( boost::apply_visitor( internal::lifecycle_command_visitor(), v ) );
  107. internal::lifecycle_serializer::serialize(cdr, frame);
  108. unsigned int size = boost::apply_visitor( internal::lifecycle_serializer_visitor(cdr), v );
  109. ACE_UNUSED_ARG(size);
  110. ACE_Message_Block * mb = cdr.begin()->clone();
  111. mb->length( cdr.length() );
  112. return mb;
  113. }
  114. template<> bool
  115. lifecycle_frame_serializer::unpack( ACE_InputCDR& cdr, LifeCycleFrame& frame, LifeCycleData& v )
  116. {
  117. internal::lifecycle_deserializer_visitor unpacker(cdr);
  118. unpacker( frame );
  119. switch( frame.command_ ) {
  120. case HELO:
  121. v = LifeCycle_Hello();
  122. break;
  123. case CONN_SYN:
  124. v = LifeCycle_SYN();
  125. break;
  126. case CONN_SYN_ACK:
  127. v = LifeCycle_SYN_Ack();
  128. break;
  129. case DATA:
  130. v = LifeCycle_Data();
  131. break;
  132. case DATA_ACK:
  133. v = LifeCycle_DataAck();
  134. break;
  135. case CLOSE:
  136. v = LifeCycle_Close();
  137. break;
  138. default:
  139. return false;
  140. }
  141. boost::apply_visitor( unpacker, v );
  142. return true;
  143. }
  144. template<> bool
  145. lifecycle_frame_serializer::unpack( ACE_Message_Block * mb, LifeCycleFrame& frame, LifeCycleData& v )
  146. {
  147. ACE_InputCDR input( mb );
  148. InputCDR cdr( input );
  149. return unpack( cdr, frame, v );
  150. }
  151. }; // namespace acewrapper
  152. /////////////////////////////////////////
  153. namespace acewrapper {
  154. namespace internal {
  155. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_Hello& data ) const
  156. {
  157. unsigned int orign = cdr_.length();
  158. OutputCDR cdr( cdr_ );
  159. cdr << data.portnumber_;
  160. cdr << data.proto_;
  161. cdr << data.ipaddr_;
  162. cdr << data.device_name_;
  163. cdr << data.serial_number_;
  164. cdr << data.revision_;
  165. cdr << data.model_name_;
  166. cdr << data.manufacturer_;
  167. cdr << data.copyright_;
  168. return cdr.length() - orign;
  169. }
  170. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_SYN& data ) const
  171. {
  172. unsigned int orign = cdr_.length();
  173. OutputCDR cdr( cdr_ );
  174. cdr << data.sequence_;
  175. cdr << data.remote_sequence_;
  176. return cdr.length() - orign;
  177. }
  178. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_SYN_Ack& data ) const
  179. {
  180. unsigned int orign = cdr_.length();
  181. OutputCDR cdr( cdr_ );
  182. cdr << data.sequence_;
  183. cdr << data.remote_sequence_;
  184. return cdr.length() - orign;
  185. }
  186. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_Data& data ) const
  187. {
  188. unsigned int orign = cdr_.length();
  189. OutputCDR cdr( cdr_ );
  190. cdr << data.sequence_;
  191. cdr << data.flags_;
  192. cdr << data.offset_;
  193. return cdr.length() - orign;
  194. }
  195. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_DataAck& data ) const
  196. {
  197. unsigned int orign = cdr_.length();
  198. OutputCDR cdr( cdr_ );
  199. cdr << data.sequence_;
  200. cdr << data.remote_sequence_;
  201. return cdr.length() - orign;
  202. }
  203. unsigned int lifecycle_serializer_visitor::operator()( const LifeCycle_Close& data ) const
  204. {
  205. unsigned int orign = cdr_.length();
  206. OutputCDR cdr( cdr_ );
  207. cdr << data.sequence_;
  208. cdr << data.remote_sequence_;
  209. return cdr.length() - orign;
  210. }
  211. }
  212. }
  213. namespace acewrapper {
  214. namespace internal {
  215. void lifecycle_deserializer_visitor::operator()( LifeCycleFrame& frame ) const
  216. {
  217. InputCDR cdr( cdr_ );
  218. cdr >> frame.endian_mark_;
  219. if ( frame.endian_mark_ == 0xfeff )
  220. static_cast<ACE_InputCDR&>(cdr).reset_byte_order( ACE_CDR_BYTE_ORDER == 1 ? 0 : 1 );
  221. cdr >> frame.proto_version_;
  222. cdr >> frame.ctrl_;
  223. cdr >> frame.hoffset_;
  224. cdr >> frame.command_;
  225. }
  226. void lifecycle_deserializer_visitor::operator()( LifeCycle_Hello& data ) const
  227. {
  228. InputCDR cdr( cdr_ );
  229. cdr >> data.portnumber_;
  230. cdr >> data.proto_;
  231. cdr >> data.ipaddr_;
  232. cdr >> data.device_name_;
  233. cdr >> data.serial_number_;
  234. cdr >> data.revision_;
  235. cdr >> data.model_name_;
  236. cdr >> data.manufacturer_;
  237. cdr >> data.copyright_;
  238. }
  239. void lifecycle_deserializer_visitor::operator()( LifeCycle_SYN& data ) const
  240. {
  241. InputCDR cdr( cdr_ );
  242. cdr >> data.sequence_;
  243. cdr >> data.remote_sequence_;
  244. }
  245. void lifecycle_deserializer_visitor::operator()( LifeCycle_SYN_Ack& data ) const
  246. {
  247. InputCDR cdr( cdr_ );
  248. cdr >> data.sequence_;
  249. cdr >> data.remote_sequence_;
  250. }
  251. void lifecycle_deserializer_visitor::operator()( LifeCycle_Data& data ) const
  252. {
  253. InputCDR cdr( cdr_ );
  254. cdr >> data.sequence_;
  255. cdr >> data.flags_;
  256. cdr >> data.offset_;
  257. }
  258. void lifecycle_deserializer_visitor::operator()( LifeCycle_DataAck& data ) const
  259. {
  260. InputCDR cdr( cdr_ );
  261. cdr >> data.sequence_;
  262. cdr >> data.remote_sequence_;
  263. }
  264. void lifecycle_deserializer_visitor::operator()( LifeCycle_Close& data ) const
  265. {
  266. InputCDR cdr( cdr_ );
  267. cdr >> data.sequence_;
  268. cdr >> data.remote_sequence_;
  269. }
  270. }
  271. }