NEST main@caf0ae8
 
Loading...
Searching...
No Matches
music_message_in_proxy.h
Go to the documentation of this file.
1/*
2 * music_message_in_proxy.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef MUSIC_MESSAGE_IN_PROXY_H
24#define MUSIC_MESSAGE_IN_PROXY_H
25
26// Generated includes:
27#include "config.h"
28
29#ifdef HAVE_MUSIC
30
31// C includes:
32#include <mpi.h>
33
34// C++ includes:
35#include <string>
36#include <vector>
37
38// Includes from libnestutil:
39#include "dict_util.h"
40
41// External includes:
42#include <music.hh>
43
44// Includes from nestkernel:
45#include "device_node.h"
46#include "nest_types.h"
47
48
49namespace nest
50{
51void register_music_message_in_proxy( const std::string& name );
52
53/* BeginUserDocs: device, MUSIC
54
55Short description
56+++++++++++++++++
57
58A device which receives message strings from MUSIC
59
60Description
61+++++++++++
62
63A ``music_message_in_proxy`` can be used to receive message strings from
64remote MUSIC applications in NEST.
65
66It uses the MUSIC library to receive message strings from other
67applications. The ``music_message_in_proxy`` represents an input port to
68which MUSIC can connect a message source. The ``music_message_in_proxy``
69can queried using GetStatus to retrieve the messages.
70
71To clear the data array, the parameter ``n_messages`` can be set to 0.
72
73This model is only available if NEST was compiled with MUSIC.
74
75Parameters
76++++++++++
77
78The following properties are available in the status Dictionary:
79
80============ ======= =========================================================
81 port_name string The name of the MUSIC input port to listen to (default:
82 message_in)
83 port_width integer The width of the MUSIC input port
84 data array A sub-Dictionary that contains the string messages
85 in the form of two arrays:
86 messages - The strings
87 message_times - The times the messages were sent (ms)
88 n_messages integer The number of messages
89 published boolean A bool indicating if the port has been already published
90 with MUSIC
91============ ======= =========================================================
92
93See also
94++++++++
95
96music_event_out_proxy, music_event_in_proxy, music_cont_in_proxy
97
98Examples using this model
99+++++++++++++++++++++++++
100
101.. listexamples:: music_message_in_proxy
102
103EndUserDocs */
104
105class MsgHandler : public MUSIC::MessageHandler
106{
107 std::vector< std::string > messages;
108 std::vector< double > message_times;
109
110 void
111 operator()( double t, void* msg, size_t size )
112 {
113 message_times.push_back( t * 1000.0 );
114 messages.push_back( std::string( static_cast< char* >( msg ), size ) );
115 }
116
117public:
118 void
119 get_status( Dictionary& d ) const
120 {
121 Dictionary dict;
122 dict[ names::messages ] = messages;
123 dict[ names::message_times ] = std::vector< double >( message_times );
124 d[ names::n_messages ] = static_cast< long >( messages.size() );
125 d[ names::data ] = dict;
126 }
127
128 void
129 clear()
130 {
131 message_times.clear();
132 messages.clear();
133 }
134};
135
141class music_message_in_proxy : public DeviceNode
142{
143
144public:
145 music_message_in_proxy();
146 music_message_in_proxy( const music_message_in_proxy& );
147
148 bool
149 has_proxies() const
150 {
151 return false;
152 }
153 bool
154 one_node_per_process() const
155 {
156 return true;
157 }
158
159 void get_status( Dictionary& ) const;
160 void set_status( const Dictionary& );
161
162private:
163 void init_buffers_();
164 void pre_run_hook();
165
166 void
167 update( Time const&, const long, const long )
168 {
169 }
170
171 // ------------------------------------------------------------
172 struct State_;
173
174 struct Parameters_
175 {
176 std::string port_name_;
177 double acceptable_latency_;
178
179 Parameters_();
180
181 void get( Dictionary& ) const;
182
186 void set( const Dictionary&, State_&, Node* );
187 };
188
189 // ------------------------------------------------------------
190
191 struct State_
192 {
193 bool published_;
195 int port_width_;
196
197 State_();
198
199 void get( Dictionary& ) const;
201 void set( const Dictionary&, const Parameters_&, Node* );
202 };
203
204 // ------------------------------------------------------------
205
206 struct Buffers_
207 {
208 MsgHandler message_handler_;
209 };
210
211 // ------------------------------------------------------------
212
213 struct Variables_
214 {
215 MUSIC::MessageInputPort* MP_;
216 };
217
218 // ------------------------------------------------------------
219
220 Parameters_ P_;
221 State_ S_;
222 Buffers_ B_;
223 Variables_ V_;
224};
225
226inline void
227music_message_in_proxy::get_status( Dictionary& d ) const
228{
229 P_.get( d );
230 S_.get( d );
231
232 B_.message_handler_.get_status( d );
233}
234
235inline void
236music_message_in_proxy::set_status( const Dictionary& d )
237{
238 Parameters_ ptmp = P_; // temporary copy in case of errors
239 ptmp.set( d, S_, this ); // throws if BadProperty
240
241 State_ stmp = S_;
242 stmp.set( d, P_, this ); // throws if BadProperty
243
244 long nm = 0;
245 if ( update_value_param( d, names::n_messages, nm, this ) )
246 {
247 if ( nm == 0 )
248 {
249 B_.message_handler_.clear();
250 }
251 else
252 {
253 throw BadProperty( "n_messaged can only be set to 0." );
254 }
255 }
256
257 // if we get here, temporaries contain consistent set of properties
258 P_ = ptmp;
259 S_ = stmp;
260}
261
262} // namespace
263
264#endif
265
266#endif /* #ifndef MUSIC_MESSAGE_IN_PROXY_H */
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
const std::string clear("clear")
const std::string message_times("messages_times")
const std::string messages("messages")
const std::string d("d")
const std::string n_messages("n_messages")
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
bool update_value_param(Dictionary const &d, const std::string &key, T &value, nest::Node *node)
Obtain value from parameter dictionary including evaluation of random or spatial parameters.
Definition dict_util.h:42