Psyllid  v1.12.4
Project 8 Data Acquisisition Software
stream_preset.cc
Go to the documentation of this file.
1 /*
2  * stream_preset.cc
3  *
4  * Created on: Jan 27, 2016
5  * Author: nsoblath
6  */
7 
8 #include "stream_preset.hh"
9 
10 #include "psyllid_error.hh"
11 
12 #include "logger.hh"
13 #include "param.hh"
14 
15 namespace psyllid
16 {
17  LOGGER( plog, "stream_preset" );
18 
19  //*****************
20  // stream_preset
21  //*****************
22 
24  f_type( "unknown" ),
25  f_nodes(),
26  f_connections()
27  {
28  }
29 
30  stream_preset::stream_preset( const std::string& a_type ) :
31  f_type( a_type ),
32  f_nodes(),
34  {
35  }
36 
38  f_type( a_orig.f_type ),
39  f_nodes( a_orig.f_nodes ),
41  {
42  }
43 
45  {
46  }
47 
49  {
50  f_type = a_rhs.f_type;
51  f_nodes = a_rhs.f_nodes;
53  return *this;
54  }
55 
56  void stream_preset::node( const std::string& a_type, const std::string& a_name )
57  {
58  if( f_nodes.find( a_name ) != f_nodes.end() )
59  {
60  throw error() << "Invalid preset: node is already present: <" + a_name + "> of type <" + a_type + ">";
61  }
62  f_nodes.insert( nodes_t::value_type( a_name, a_type ) );
63  return;
64  }
65 
66  void stream_preset::connection( const std::string& a_conn )
67  {
68  if( f_connections.find( a_conn ) != f_connections.end() )
69  {
70  throw error() << "Invalid preset; connection is already present: <" + a_conn + ">";
71  }
72  f_connections.insert( a_conn );
73  }
74 
75 
76  //*************************
77  // runtime_stream_preset
78  //*************************
79 
82 
85  {
86  }
87 
88  runtime_stream_preset::runtime_stream_preset( const std::string& a_type ) :
89  stream_preset( a_type )
90  {
91  //std::unique_lock< std::mutex > t_lock( s_runtime_presets_mutex );
92  runtime_presets::const_iterator t_preset_it = s_runtime_presets.find( a_type );
93  if( t_preset_it != s_runtime_presets.end() )
94  {
95  *this = *t_preset_it->second.f_preset_ptr.get();
96  //f_nodes = s_runtime_presets.at( a_type ).f_preset.f_nodes;
97  //f_connections = s_runtime_presets.at( a_type ).f_preset.f_connections;
98  }
99  }
100 
102  stream_preset( a_orig )
103  {
104  }
105 
107  {
108  }
109 
111  {
112  stream_preset::operator=( a_rhs );
113  return *this;
114  }
115 
116  bool runtime_stream_preset::add_preset( const scarab::param_node& a_preset_node )
117  {
118  if( ! a_preset_node.has( "type" ) )
119  {
120  LERROR( plog, "Preset must have a type. Preset config:\n" << a_preset_node );
121  return false;
122  }
123  std::string t_preset_type = a_preset_node["type"]().as_string();
124 
125  LDEBUG( plog, "Adding preset of type <" << t_preset_type << ">" );
126 
127  if( ! a_preset_node.has( "nodes" ) )
128  {
129  LERROR( plog, "No \"nodes\" configuration was present for preset <" << t_preset_type << ">" );
130  return false;
131  }
132  const scarab::param_array& t_nodes_array = a_preset_node["nodes"].as_array();
133 
134  std::unique_lock< std::mutex > t_lock( s_runtime_presets_mutex );
135 
136  auto t_rp_pair = s_runtime_presets.insert( runtime_presets::value_type( t_preset_type, rsp_creator( t_preset_type ) ) );
137  if( ! t_rp_pair.second )
138  {
139  LERROR( plog, "Unable to add new runtime preset <" << t_preset_type << ">" );
140  return false;
141  }
142  runtime_presets::iterator t_new_rsp_creator = t_rp_pair.first;
143 
144  std::string t_type;
145  for( scarab::param_array::const_iterator t_nodes_it = t_nodes_array.begin(); t_nodes_it != t_nodes_array.end(); ++t_nodes_it )
146  {
147  if( ! t_nodes_it->is_node() )
148  {
149  LERROR( plog, "Invalid node specification in preset <" << t_preset_type << ">" );
150  scarab::factory< stream_preset, runtime_stream_preset, const std::string& >::get_instance()->remove_class( t_preset_type );
151  s_runtime_presets.erase( t_new_rsp_creator );
152  return false;
153  }
154 
155  t_type = t_nodes_it->as_node().get_value( "type", "" );
156  if( t_type.empty() )
157  {
158  LERROR( plog, "No type given for one of the nodes in preset <" << t_preset_type << ">" );
159  scarab::factory< stream_preset, runtime_stream_preset, const std::string& >::get_instance()->remove_class( t_preset_type );
160  s_runtime_presets.erase( t_new_rsp_creator );
161  return false;
162  }
163 
164  LDEBUG( plog, "Adding node <" << t_type << ":" << t_nodes_it->as_node().get_value( "name", t_type ) << "> to preset <" << t_preset_type << ">" );
165  t_new_rsp_creator->second.f_preset_ptr->node( t_type, t_nodes_it->as_node().get_value( "name", t_type ) );
166  }
167 
168  if( ! a_preset_node.has( "connections" ) )
169  {
170  LDEBUG( plog, "Preset <" << t_preset_type << "> is being setup with no connections" );
171  }
172  else
173  {
174  const scarab::param_array& t_conn_array = a_preset_node["connections"].as_array();
175  for( scarab::param_array::const_iterator t_conn_it = t_conn_array.begin(); t_conn_it != t_conn_array.end(); ++t_conn_it )
176  {
177  if( ! t_conn_it->is_value() )
178  {
179  LERROR( plog, "Invalid connection specification in preset <" << t_preset_type << ">" );
180  scarab::factory< stream_preset, runtime_stream_preset, const std::string& >::get_instance()->remove_class( t_preset_type );
181  s_runtime_presets.erase( t_new_rsp_creator );
182  return false;
183  }
184 
185  LDEBUG( plog, "Adding connection <" << t_conn_it->as_value().as_string() << "> to preset <" << t_preset_type << ">");
186  t_new_rsp_creator->second.f_preset_ptr->connection( t_conn_it->as_value().as_string() );
187  }
188  }
189 
190  LINFO( plog, "Preset <" << t_preset_type << "> is now available" );
191 
192  return true;
193  }
194 
195 } /* namespace psyllid */
static bool add_preset(const scarab::param_node &a_preset_node)
static runtime_presets s_runtime_presets
connections_t f_connections
static scarab::logger plog("batch_executor")
runtime_stream_preset & operator=(const runtime_stream_preset &a_rhs)
std::map< std::string, rsp_creator > runtime_presets
void connection(const std::string &a_conn)
stream_preset & operator=(const stream_preset &a_rhs)
LOGGER(plog, "egg_writer")
static std::mutex s_runtime_presets_mutex
void node(const std::string &a_type, const std::string &a_name)