iop_xdevice.h
Go to the documentation of this file.
1/** @file iop_xdevice.h Virtual device for interface definition */
2
3/*
4 FAU Discrete Event Systems Library (libfaudes)
5
6 Copyright (C) 2008, Thomas Moor
7 Exclusive copyright is granted to Klaus Schmidt
8
9*/
10
11
12
13#ifndef FAUDES_XDEVICE_H
14#define FAUDES_XDEVICE_H
15
16#include "corefaudes.h"
17#include "tp_include.h"
18#include "iop_vdevice.h"
19
20
21
22
23
24namespace faudes {
25
26
27
28
29/**
30 * Container of devices.
31 *
32 * The xDevices is a container of vDevices. Input readings are
33 * combined in a union fashion over all participating devices,
34 * output writings are dispatched to the relevant device. Formally,
35 * the xDevice class is derived from the vDevice class to provide the
36 * same interaface to a simulator. Ie, the application does not
37 * need to know whether is acts on a single vDevice or an xDevice. This
38 * is also true for configuration from file, but of cause not for programatical
39 * configuration. An xDevice may not be configured to have individual
40 * outputs or inputs, but to hold particular vDevices.
41 *
42 * Technical detail: the xDevice uses the vDevice interface to register a
43 * common event fifo buffer and a common condition variable. Thus, the xDevice only works
44 * with devices that support this configuration feature.
45 *
46 * @ingroup IODevicePlugin
47 */
48
49class FAUDES_API xDevice : public vDevice {
50
52
53
54 public:
55
56
57 /** Iterator for const access to individual devices*/
58 typedef std::vector<vDevice*>::const_iterator Iterator;
59
60
61 /**
62 * Default constructor
63 */
64 xDevice(void);
65
66 /**
67 * Copy constructor
68 */
69 xDevice(const xDevice& rOther);
70
71 /**
72 * Construct on heap from token reader.
73 *
74 * This constructor examines the token strean, determines the coressponding
75 * class and constructs the device on the heap. Todo: the implementation
76 * of this function is a hack, there must be proper
77 * solution to this issue.
78 *
79 * @param rTr
80 * TokenReader to read from
81 * @return
82 * vDevice pointer
83 *
84 * @exception Exception
85 * - token mismatch (id 552)
86 * - IO errors (id 1)
87 */
88 static xDevice* FromTokenReader(TokenReader& rTr);
89
90 /**
91 * Construct on heap from file.
92 *
93 * This constructor examines the file, determines the coressponding
94 * class and constructs the device on the heap.
95 *
96 * @param rFileName
97 * Filename
98 * @return
99 * vDevice pointer
100 *
101 * @exception Exception
102 * - token mismatch (id 552)
103 * - IO errors (id 1)
104 */
105 static xDevice* FromFile(const std::string& rFileName);
106
107 /**
108 * Explicit destructor.
109 */
110 virtual ~xDevice(void);
111
112
113 /**
114 * Set Iterator to first device
115 *
116 * @return
117 * Iterator to first device
118 */
119 Iterator Begin(void) const { return mDevices.begin() ; };
120
121 /**
122 * Set Iterator to last device
123 *
124 * @return
125 * Iterator to last device
126 */
127 Iterator End(void) const { return mDevices.end() ; };
128
129 /**
130 *
131 * Get number of devices
132 */
133 Idx Size(void) const { return (Idx) mDevices.size(); };
134
135
136 /**
137 * Insert a new device.
138 * An xDevice is configured by inserting vDevices. An xDevice cannot be
139 * configured by individual output/sensro events.
140 *
141 * Note: by inserting a device into an xdevice its ownership
142 * will be assumed by the xdevice and therewith the right to destoy
143 * it.
144 *
145 * @param device
146 * Pointer to vDevice
147 */
148 void Insert(vDevice* device);
149
150 /**
151 * Insert a new device by Filename
152 *
153 * An xDevice is configured by inserting vDevices. An xDevice cannot be
154 * configured by individual output/sensro events.
155 *
156 * Note: by inserting a device into an xdevice its ownership
157 * will be assumed by the xdevice and therewith the right to destoy
158 *
159 * For information on how to write such a file see the "iodevice" - tutorial
160 *
161 * @param rFileName
162 * Configuration-file to build up device
163 */
164
165 void Insert(const std::string& rFileName);
166
167
168
169 /**
170 * Dummy. An xDevice does not provide event based configuration. Use Insert instead. This function
171 * will throw an execption
172 */
173 void Configure(Idx event, const AttributeDeviceEvent& attr);
174
175 /**
176 * An xDevice does not provide event based configuration. Use Insert instead.
177 * This function will throw an execption
178 */
179 void Configure(const EventSet& rEvents);
180
181 /**
182 *
183 * Build up internal data structures. I.e. (event-idx,int) - map
184 */
185 void Compile(void) ;
186
187
188 /**
189 * Clear all configuarations and destroy existing devices
190 *
191 */
192 void Clear(void);
193
194
195 /** reset all dynamic state, i.e. call reset on each device */
196 void Reset(void);
197
198 /** Test for reset request */
199 bool ResetRequest(void);
200
201 /**
202 * Activate the devices. This function enables output execution and input reading.
203 *
204 * @exception Exception
205 * - Not yet configured (id 551)
206 */
207 void Start(void);
208
209 /**
210 * Deactivate the device. This function disables output execution and input reading.
211 *
212 */
213 void Stop(void);
214
215 /**
216 * Get status. This function returns the current status of the device.
217 * In derived classes that use background threads for input reading etc,
218 * a device may change its status without notice. Dont forget to reimplement
219 * this method with an appropriate mutex.
220 *
221 */
222 DeviceState Status(void);
223
224 /**
225 * Run output command on relevant device.
226 *
227 * @exception Exception
228 * - unknown output event (id 65)
229 *
230 */
231 void WriteOutput(Idx output);
232
233 /**
234 * Flush any pending IO Operations.
235 *
236 * Pass on flush output buffers to participating devices.
237 *
238 */
239 virtual void FlushOutputs(void);
240
241
242 /**
243 * Report global fauDES-time
244 * Note: per convention we take the time of the first
245 * device inserted in xDevice as global time
246 *
247 * @return
248 * fauDES-time
249 */
250 Time::Type CurrentTime(void);
251
252 /**
253 * Report global fauDES-time
254 * Note: per convention we take the time of the first
255 * device inserted in xDevice as global time
256 *
257 * @return
258 * fauDES-time
259 */
260 long int CurrentTimeMs(void);
261
262 /**
263 * Set physical time in ftu.
264 *
265 * @param now
266 * now in faudes time units (ftu).
267 */
268 virtual void CurrentTime(Time::Type now);
269
270 /**
271 * Set physical time in ms.
272 *
273 * @param nowms
274 * now in msec
275 */
276 virtual void CurrentTimeMs(long int nowms);
277
278
279
280protected:
281
282 // internal iterator type
283 typedef std::vector<vDevice*>::iterator iterator;
284
285
286 /**
287 * Actual method to read device configuration from tokenreader.
288 *
289 * DoRead basically calls the DoWrite-function of all devices which are part of xDevice
290 *
291 * @param rTr
292 * TokenReader to read from
293 * @param rLabel
294 * Section to read
295 * @param pContext
296 * Read context to provide contextual information
297 *
298 * @exception Exception
299 * - IO error (id 1)
300 */
301 virtual void DoReadConfiguration(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
302
303 /**
304 * Write the device patrameters to a TokenWriter.
305 *
306 *
307 * @param rTw
308 * Reference to TokenWriter
309 * @param rLabel
310 * Label of section to write
311 * @param pContext
312 * Read context to provide contextual information
313 * @exception Exception
314 * - IO errors (id 2)
315 */
316 virtual void DoWriteConfiguration(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
317
318 /** Return first Device */
319 iterator Begin(void) { return mDevices.begin() ; };
320
321 /** Return last Device */
322 iterator End(void) { return mDevices.end() ; };
323
324 /** Vector of member-devices */
325 std::vector<vDevice*> mDevices;
326
327 /** Vector of member-device-names*/
328 std::vector<std::string> mDeviceNames;
329
330 /** Compiled data: Input map to map input idx to device no */
331 std::map<Idx,int> mInputToDevice;
332
333 /** Compiled data: Output Map to map input idx to device no */
334 std::map<Idx,int> mOutputToDevice;
335
336 /** Current device state: remember last stop/down command */
338
339
340};
341
342
343
344}
345
346
347#endif
348
#define FAUDES_API
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
Definition cfl_types.h:918
Iterator End(void) const
std::vector< vDevice * > mDevices
iterator Begin(void)
Iterator Begin(void) const
Idx Size(void) const
std::map< Idx, int > mInputToDevice
iterator End(void)
std::map< Idx, int > mOutputToDevice
std::vector< vDevice * >::const_iterator Iterator
Definition iop_xdevice.h:58
std::vector< std::string > mDeviceNames
std::vector< vDevice * >::iterator iterator
uint32_t Idx

libFAUDES 2.34d --- 2026.03.11 --- c++ api documentaion by doxygen