DataDirector API
ChunkProducer.h
Go to the documentation of this file.
1 /*! \file ChunkProducer.h
2  \brief The ChunkProducer class.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2012 The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBLITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 
41 #ifndef _CHUNK_PRODUCER_H_
42 #define _CHUNK_PRODUCER_H_
43 
44 #include <string>
45 #include "DDD_AttributeBase.h"
46 #include "DDD_LogFacade.h"
47 #include "DDD_AttributeList.h"
49 
50 namespace disti
51 {
52 class DDD_AttributeDouble;
53 class DDD_AttributeString;
54 class DDD_AttributeBool;
55 class DDD_AttributeUInt;
56 class ChunkDescription;
57 
58 /** A ChunkAttribute maps a specific part of a chunk to a DataDirector attribute */
59 class DDD_EXPORT ChunkAttribute : public DDD_AttributeObserver
60 {
61  public:
62  std::string _name; /**< Name of the attribute */
63  unsigned int _offset; /**< Offset from beginning of packet */
64  unsigned int _size; /**< Size of field in bytes */
65  bool _bigEndian; /**< True if field is "big endian" */
66  bool _isBitfield; /**< True if attribute is a bitfield */
67  unsigned int _bitOffset; /**< Offset of the bitfield in bits relative to the start of this attribute (optional) */
68  unsigned int _bitLength; /**< Length of the bitfield in bits. (optional) */
69  unsigned int _bitMask; /**< Bitmask computed from offset and length (optional) */
70  bool _attributeChanged; /**< Dirty flag used by ChunkProducers to determine if an attribute update needs to be sent */
71  ChunkDescription *_chunk; /**< Pointer to the ChunkDescription that "owns" this ChunkAttribute */
72 
73  DDD_AttributeBase::DataType _type; /**< Representation of the data in the chunk */
74  DDD_AttributeBase *_directorAttribute; /**< Pointer to the DataDirector attribute linked
75  to this attribute */
76  DDD_AttributeBase *_initialValue; /**< Initial value of field (optional) */
77 
78  /** A callback method that will be called when the attribute being observed changes
79  * if this observer has registered to be notified on attribute change
80  * \param attribute A pointer to the attribute that changed
81  */
82  void OnAttributeChanged(DDD_AttributeBase *attribute);
83 
84  /** Constructor */
86  _directorAttribute(NULL),
87  _initialValue(NULL),
88  _offset(0),
89  _size(0),
90  _bigEndian(false),
91  _bitOffset(0),
92  _bitLength(0),
93  _bitMask(0),
94  _isBitfield(false),
95  _attributeChanged(false),
96  _chunk(chunk)
97  {
98  }
99 
100  /** Destructor */
102 };
103 
104 /** A list of ChunkAttributes */
105 class DDD_EXPORT ChunkAttributeList : public DynamicPtrArray<ChunkAttribute *>
106 {
107  public:
108 
109  /** Searches for a ChunkAttribute with the given name
110  * \param name The name of the attribute to fun
111  * \return A pointer to the attribute if found, NULL otherwise
112  */
113  ChunkAttribute *FindAttributeByName(std::string &name);
114 
115  /** Constructor */
117 
118  /** Destructor */
120  {
121  EmptyList();
122  }
123 };
124 
125 /** A RuleElement. One clause of a RuleDefinition */
126 class DDD_EXPORT RuleElement
127 {
128  std::string _attributeName; /**< Name of the attribute to match */
129  std::string _value; /**< Value of the attribute to match */
130 
131  ChunkAttribute *_attr; /**< The chunk attribute associated with this rule, cached to speed up rule checking */
132 
133 public:
134 
135  /** Constructor */
137  _attr(NULL)
138  {
139  }
140 
141  /** Set the attribute associated with this rule
142  * \param attr The Chunk attribute associated with this rule
143  * \param attrName The name of the attribute
144  * \param attrValue The attribute value to match
145  */
146  void SetAttribute(ChunkAttribute *attr,const std::string &attrName,const std::string &attrValue)
147  {
148  _attributeName = attrName;
149  _value = attrValue;
150  _attr = attr;
151  }
152 
153  /** \return Gets the attribute associated with this rule
154  */
155  ChunkAttribute *GetAttribute() const { return _attr; }
156 
157  /** \return Gets the attribute name with this rule
158  */
159  std::string &GetName() { return _attributeName; }
160 
161  /** \return Gets the attribute value with this rule
162  */
163  std::string &GetValue() { return _value; }
164 };
165 
166 /** A Rule definition. A collection of RuleElements to be matched */
167 class DDD_EXPORT RuleDefinition
168 {
169 public:
170  DynamicPtrArray<RuleElement*> _elements; /**< The collection of rule elements making up this rule */
171 
172  /** Constructor */
174 
175  /** Destructor */
177  {
178  _elements.EmptyList();
179  }
180 };
181 
182 /** A Chunk Description. A chunk has a name and a list of attributes it produces. */
183 class DDD_EXPORT ChunkDescription
184 {
185 public:
186  std::string _name; /**< The name of this chunk. It will be used as the rootname for its
187  attributes */
188 
189  std::string _type; /**< The 'type' of this chunk (optional). Can be used by chunk producers for whatever purpose */
190 
191  ChunkAttributeList _attributes; /**< The list of attributes that this chunk produces */
192 
193  RuleDefinition *_rule; /**< Rule for determining that this chunk is present */
194 
195  unsigned int _packetLength; /**< (Optional). The length of the packet/chunk. If this is set to a value other
196  * than zero, then it means one of two things.
197  * 1. If this Chunk is part of a producer that sends messages, this is the length of
198  * the message that will be sent
199  * 2. If this Chunk is part of a producer that receives messages, the length of the
200  * chunk will be validated on reception and must match this value
201  */
202 
203  std::string _headerName;
204  bool _dirty;
205 
206  /** Constructor */
208  _rule(NULL),
209  _packetLength(0),
210  _headerName(""),
211  _dirty(false)
212  {
213  }
214 
215 };
216 
217 class DDD_EXPORT ChunkDescriptionList : public DynamicPtrArray<ChunkDescription*>
218 {
219 public:
220  /** Find the chunk attribute by fully qualified name, e.g.
221  * chunkDesc.attributeName
222  * \param attributeName The fully qualified attribute name to find
223  * \return A pointer to the attribute if found, NULL otherwise
224  */
225  ChunkAttribute *FindAttributeByName(std::string &name);
226 
227  /** Find the specified chunk description by name
228  * \param chunkName The name of the chunk to find
229  * \return A pointer to the chunk if found, NULL otherwise
230  */
231  ChunkDescription *FindChunkDescriptionByName(std::string &name);
232 };
233 
234 
235 /** The Chunk Producer class. Abstract base class for other "Producers" that will produce
236  * attributes for use by the UDP_Asset */
237 class DDD_EXPORT ChunkProducer : public DDD_AttributeContainer, public DDD_Base, public DDD_AttributeObserver
238 {
239 protected:
240 
241  /** Processes an inbound bitfield attribute update. Performs the actual decoding,
242  * byte swapping, etc of the attribute
243  * \param attr The chunk attribute describing the bitfield attribute
244  * \param msgBuf A buffer containing a chunk of data
245  */
246  void ReadBitfield(ChunkAttribute *attr,unsigned char *msgBuf);
247 
248  /** Processes an inbound integer attribute update. Performs the actual decoding,
249  * byte swapping, etc of the attribute
250  * \param attr The chunk attribute describing the integer
251  * \param msgBuf A buffer containing a chunk of data
252  */
253  void ReadInt(ChunkAttribute *attr,unsigned char *msgBuf);
254 
255  /** Processes an inbound unsigned integer attribute update. Performs the
256  * actual decoding, byte swapping, etc of the attribute
257  * \param attr The chunk attribute describing the unsigned integer
258  * \param msgBuf A buffer containing a chunk of data
259  */
260  void ReadUInt(ChunkAttribute *attr,unsigned char *msgBuf);
261 
262  /** Processes an inbound double/float attribute update. Performs the actual decoding,
263  * byte swapping, etc of the attribute
264  * \param attr The chunk attribute describing the double
265  * \param msgBuf A buffer containing a chunk of data
266  */
267  void ReadDouble(ChunkAttribute *attr,unsigned char *msgBuf);
268 
269  /** Processes an inbound boolean attribute update. Performs the actual decoding,
270  * byte swapping, etc of the attribute
271  * \param attr The chunk attribute describing the boolean
272  * \param msgBuf A buffer containing a chunk of data
273  */
274  void ReadBoolean(ChunkAttribute *attr,unsigned char *msgBuf);
275 
276  /** Processes an inbound string attribute update. Strings are not byte swapped
277  * byte swapping, etc of the attribute
278  * \param attr The chunk attribute describing the string
279  * \param msgBuf A buffer containing a chunk of data
280  */
281  void ReadString(ChunkAttribute *attr,unsigned char *msgBuf);
282 
283  /** Processes an inbound chunk and updates any connected attributes
284  * \param chunk The chunk specification
285  * \param msgBuf The UDP message buffer
286  * \param receivedLen The length of the UDP message
287  */
288  void ReadChunk(ChunkDescription *chunk,unsigned char *msgBuf,unsigned int receivedLen);
289 
290  /** Processes an outbound bitfield attribute update. Performs the actual encoding,
291  * byte swapping, etc of the attribute
292  * \param attr The chunk attribute describing the bitfield attribute
293  * \param msgBuf A buffer containing a chunk of data
294  */
295  int WriteBitfield(ChunkAttribute *attr,unsigned char *msgBuf);
296 
297  /** Processes an outbound integer attribute update. Performs the actual encoding,
298  * byte swapping, etc of the attribute
299  * \param attr The chunk attribute describing the integer
300  * \param msgBuf A buffer containing a chunk of data
301  */
302  int WriteInt(ChunkAttribute *attr,unsigned char *msgBuf);
303 
304  /** Processes an outbound unsigned integer attribute update. Performs the
305  * actual encoding, byte swapping, etc of the attribute
306  * \param attr The chunk attribute describing the unsigned integer
307  * \param msgBuf A buffer containing a chunk of data
308  */
309  int WriteUInt(ChunkAttribute *attr,unsigned char *msgBuf);
310 
311  /** Processes an outbound double/float attribute update. Performs the actual encoding,
312  * byte swapping, etc of the attribute
313  * \param attr The chunk attribute describing the double
314  * \param msgBuf A buffer containing a chunk of data
315  */
316  int WriteDouble(ChunkAttribute *attr,unsigned char *msgBuf);
317 
318  /** Processes an outbound boolean attribute update. Performs the actual encoding,
319  * byte swapping, etc of the attribute
320  * \param attr The chunk attribute describing the boolean
321  * \param msgBuf A buffer containing a chunk of data
322  */
323  int WriteBoolean(ChunkAttribute *attr,unsigned char *msgBuf);
324 
325  /** Processes an outbound string attribute update. Strings are not byte swapped
326  * byte swapping, etc of the attribute
327  * \param attr The chunk attribute describing the string
328  * \param msgBuf A buffer containing a chunk of data
329  */
330  int WriteString(ChunkAttribute *attr,unsigned char *msgBuf);
331 
332  /** Processes an outbound chunk
333  * \param chunk The chunk specification
334  * \param msgBuf The UDP message buffer
335  * \param receivedLen The length of the UDP message
336  */
337  int WriteChunk(ChunkDescription *chunk,unsigned char *msgBuf,unsigned int maxLen);
338 
339  /** Processes an inbound message, only for printing debug information to the console.
340  * \param msgBuf A buffer containing a message
341  * \param receivedLength The length of the received message
342  */
343  void PrintDebugInfo(unsigned char *msgBuf,unsigned int receivedLength);
344 
345  /* Function that implements the DDD_AttributeObserver interface
346  * \param attribute Pointer to the attribute whose value just
347  * changed
348  */
349  virtual void OnAttributeChanged(DDD_AttributeBase *attribute)
350  {
351  }
352 
353  unsigned int _packetCount; /**< For debugging only */
354  unsigned int _debugMode; /**< 0=None, 1=Light, 2=Verbose */
355  bool _checkForChangeBeforeUpdate; /**< Whether or not to check the values for change before updating the connection value */
356 
357 public:
358 
359  ChunkDescriptionList _chunkDescriptions; /** A list of chunk descriptions */
360 
361  /** Constructor */
362  ChunkProducer();
363 
364  /** Destructor */
365  ~ChunkProducer();
366 
367  /** Processes an inbound message, classifying it into chunks and then updating their associated
368  * attributes. Required for producers which receive messages. Only implemented by ChunkProducers
369  * that receive data.
370  * \param msgBuf A buffer containing a message
371  * \param receivedLength The length of the received message
372  */
373  virtual void ProcessMessage(unsigned char *msgBuf,unsigned int receivedLength) {};
374 
375  /** Builds an outbound message buffer. Only implemented by ChunkProducers that send data.
376  * \param msgBuf A buffer to contain the message
377  * \param maxMessageLength The max length of the message to build
378  * \return The length of the message built or -1 if no message built
379  */
380  virtual int BuildMessage(unsigned char *msgBuf,unsigned int maxMessageLength, unsigned int chunkNumber) { return -1; }
381 
382  /** Find the chunk attribute by fully qualified name, e.g.
383  * chunkDesc.attributeName
384  * \param attributeName The fully qualified attribute name to find
385  * \return A pointer to the attribute if found, NULL otherwise
386  */
387  ChunkAttribute *FindAttributeByName(std::string &name);
388 
389  /** Find the specified chunk description by name
390  * \param chunkName The name of the chunk to find
391  * \return A pointer to the chunk if found, NULL otherwise
392  */
393  ChunkDescription *FindChunkDescriptionByName(std::string &name);
394 
395  /** Swaps a 2 byte quanity into the native byte order of this machine
396  * \param val Pointer to the value to swap
397  */
398  static void Swap2(unsigned char *val);
399 
400  /** Swaps a 4 byte quanity into the native byte order of this machine
401  * \param val Pointer to the value to swap
402  */
403  static void Swap4(unsigned char *val);
404 
405  /** Swaps an 8 byte quanity into the native byte order of this machine
406  * \param val Pointer to the value to swap
407  */
408  static void Swap8(unsigned char *val);
409 
410  // override from DDD_AttributeContainer
411  virtual const char *GetName(void) { return "ChunkProducer"; }
412 
413  /** Sets the debug mode for the chunk producer
414  * \param debugMode New debug Mode: 0=None, 1=Light, 2=Verbose
415  */
416  void SetDebugMode(unsigned int debugMode) { _debugMode = debugMode; }
417 
418  /**Setter for the flag specifying whether to check for value change before updating over connections
419  * \param value true = check for change before updating over connections, false = do not
420  */
421  void SetCheckForChangeBeforeUpdate(const bool value){_checkForChangeBeforeUpdate = value;}
422 
423  /** Perform any producer specific initialization
424  */
425  virtual void Start() {}
426 
427 };
428 
429 } // end of namespace disti
430 #endif
431 
DDD_AttributeContainer. A virtual interface class for containers of attributes.
Definition: DDD_AttributeBase.h:57
std::string _name
Definition: ChunkProducer.h:62
A templated array of object pointers. The array dynamically resizes as needed.
Definition: dynamic_ptr_array.h:55
Definition: ChunkProducer.h:183
ChunkDescription()
Definition: ChunkProducer.h:207
~RuleDefinition()
Definition: ChunkProducer.h:176
unsigned int _size
Definition: ChunkProducer.h:64
bool _checkForChangeBeforeUpdate
Definition: ChunkProducer.h:355
Definition: ChunkProducer.h:217
virtual void OnAttributeChanged(DDD_AttributeBase *attribute)
Definition: ChunkProducer.h:349
A virtual interface class for all DataDirector attribute types.
Definition: DDD_AttributeBase.h:87
~ChunkAttribute()
Definition: ChunkProducer.h:101
ChunkAttribute * GetAttribute() const
Definition: ChunkProducer.h:155
DDD_AttributeBase * _directorAttribute
Definition: ChunkProducer.h:74
virtual void ProcessMessage(unsigned char *msgBuf, unsigned int receivedLength)
Definition: ChunkProducer.h:373
unsigned int _bitOffset
Definition: ChunkProducer.h:67
std::string _type
Definition: ChunkProducer.h:189
RuleDefinition * _rule
Definition: ChunkProducer.h:193
unsigned int _offset
Definition: ChunkProducer.h:63
Definition: ChunkProducer.h:237
Definition: ChunkProducer.h:167
DDD_AttributeBase * _initialValue
Definition: ChunkProducer.h:76
bool _isBitfield
Definition: ChunkProducer.h:66
unsigned int _debugMode
Definition: ChunkProducer.h:354
virtual void Start()
Definition: ChunkProducer.h:425
virtual int BuildMessage(unsigned char *msgBuf, unsigned int maxMessageLength, unsigned int chunkNumber)
Definition: ChunkProducer.h:380
DataType
Definition: DDD_AttributeBase.h:104
std::string _name
Definition: ChunkProducer.h:186
std::string & GetValue()
Definition: ChunkProducer.h:163
The DDD_AttributeList class. Maintains a list of Data Director attributes.
A virtual interface class for observers of attributes. AttributeObserver-derived objects are able to ...
Definition: DDD_AttributeBase.h:66
DynamicPtrArray< RuleElement * > _elements
Definition: ChunkProducer.h:170
void SetCheckForChangeBeforeUpdate(const bool value)
Definition: ChunkProducer.h:421
The DDD_RevertableAttribute class. Container for.
void SetDebugMode(unsigned int debugMode)
Definition: ChunkProducer.h:416
unsigned int _packetCount
Definition: ChunkProducer.h:353
bool _bigEndian
Definition: ChunkProducer.h:65
ChunkDescription * _chunk
Definition: ChunkProducer.h:71
ChunkAttributeList _attributes
Definition: ChunkProducer.h:191
void SetAttribute(ChunkAttribute *attr, const std::string &attrName, const std::string &attrValue)
Definition: ChunkProducer.h:146
bool _attributeChanged
Definition: ChunkProducer.h:70
unsigned int _bitLength
Definition: ChunkProducer.h:68
The DDD_Base class. Base class for Data Director objects.
Definition: DDD_Base.h:53
DDD_AttributeBase::DataType _type
Definition: ChunkProducer.h:73
std::string & GetName()
Definition: ChunkProducer.h:159
ChunkAttributeList()
Definition: ChunkProducer.h:116
RuleDefinition()
Definition: ChunkProducer.h:173
Definition: ChunkProducer.h:126
void EmptyList(void)
Definition: dynamic_ptr_array.h:322
unsigned int _bitMask
Definition: ChunkProducer.h:69
Definition: ChunkProducer.h:105
RuleElement()
Definition: ChunkProducer.h:136
ChunkAttribute(ChunkDescription *chunk)
Definition: ChunkProducer.h:85
unsigned int _packetLength
Definition: ChunkProducer.h:195
Definition: ChunkProducer.h:59
Definition: AttributeChangedEmitter.h:46
~ChunkAttributeList()
Definition: ChunkProducer.h:119