Scarab
Project 8 C++ Utility Library
param_msgpack.cc
Go to the documentation of this file.
1 /*
2  * mt_param_msgpack.cc
3  *
4  * Created on: March 27, 2015
5  * Author: bhlaroque
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "msgpack.hpp"
11 
12 #include <sstream>
13 using std::string;
14 using std::stringstream;
15 
16 
17 #include <cstdio>
18 
19 #include "param_msgpack.hh"
20 
21 
22 namespace scarab
23 {
24  LOGGER( dlog, "param_msgpack" );
25 
27  {
28  }
30  {
31  }
32 
33  param* param_input_msgpack::read_string( const std::string& a_msgpack_string )
34  {
35  // deserialize the buffer
36  msgpack::unpacked t_unpacked;
37  msgpack::unpack( t_unpacked, a_msgpack_string.data(), a_msgpack_string.size()) ;
38  msgpack::object t_deserialized = t_unpacked.get();
39  LDEBUG( dlog, "msgpack deserialization result is: (size = " << t_deserialized.via.array.size << ")\n" << t_deserialized );
40 
41 
42  // convert it to a param node
43  if( t_deserialized.type != 7 ) // message payload should deserialize to a map
44  {
45  return NULL;
46  }
47  param_node* t_config = new param_node();
48  for( unsigned i_element=0; i_element < t_deserialized.via.array.size; ++i_element )
49  {
50  t_config->replace( t_deserialized.via.array.ptr[2 * i_element].as<std::string>(),
51  param_input_msgpack::read_msgpack_element( t_deserialized.via.array.ptr[2 * i_element + 1] ) );
52  }
53  LDEBUG( dlog, "Unpacked string:\n" << *t_config );
54 
55  return t_config;
56  }
57 
58  param* param_input_msgpack::read_msgpack_element( const msgpack::object& a_msgpack_element )
59  {
60  LDEBUG( dlog, "Unpacking msgpack element of type: " << a_msgpack_element.type );
61  switch ( a_msgpack_element.type )
62  {
63  case 0: // NULL
64  return new param();
65  break;
66  case 1: // BOOL
67  return new param_value( a_msgpack_element.as<bool>() );
68  break;
69  case 2: // positive int
70  return new param_value( a_msgpack_element.as< unsigned >() );
71  break;
72  case 3: // negative int
73  return new param_value( a_msgpack_element.as< int >() );
74  break;
75  case 4: // float
76  return new param_value( a_msgpack_element.as< float >() );
77  break;
78  case 5: // string
79  return new param_value( a_msgpack_element.as< std::string >() );
80  break;
81  case 6: // array
82  {
83  param_array* t_config_array = new param_array();
84  for( unsigned i_element = 0; i_element < a_msgpack_element.via.array.size; ++i_element )
85  {
86  t_config_array->push_back( param_input_msgpack::read_msgpack_element( a_msgpack_element.via.array.ptr[i_element] ) );
87  }
88  return t_config_array;
89  break;
90  }
91  case 7: // map
92  {
93  param_node* t_config_node = new param_node();
94  for( unsigned i_element = 0; i_element < a_msgpack_element.via.array.size; ++i_element )
95  {
96  t_config_node->replace( a_msgpack_element.via.array.ptr[2 * i_element].as<std::string>(),
97  param_input_msgpack::read_msgpack_element( a_msgpack_element.via.array.ptr[2 * i_element + 1] ) );
98  }
99  return t_config_node;
100  break;
101  }
102  case 8: // BIN
103  LWARN( dlog, "Cannot handle 'bin'" );
104  return NULL;
105  break;
106  default:
107  LWARN( dlog, "Type unrecognized: " << a_msgpack_element.type );
108  return NULL;
109  break;
110  }
111  LWARN( dlog, "Code should not reach this point" );
112  return NULL;
113  }
114 
116  {}
117 
119  {}
120 
121  bool param_output_msgpack::write_string( const param& /*a_to_write*/, std::string& /*a_string*/, json_writing_style /*a_style*/ )
122  {
123  return false;
124  }
125 
126 } /* namespace mantis */
void replace(const std::string &a_name, const param &a_value)
creates a copy of a_value
Definition: param.hh:1141
#define LWARN(...)
Definition: logger.hh:364
static bool write_string(const param &a_to_write, std::string &a_string, json_writing_style a_style)
static param * read_string(const std::string &a_msgpack_str)
#define LDEBUG(...)
Definition: logger.hh:360
void push_back(const param &a_value)
Definition: param.hh:858
static param * read_msgpack_element(const msgpack::object &a_msgpack_array)
LOGGER(mtlog,"authentication")