KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TimeStamp.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: TimeStamp
32  created: 16/10/2008
33  author: Karl Jones
34 
35  purpose: Stores absolute/ relative timestamps.
36  Timestamps are used to reduce the error in a simulation.
37 
38  A lot of DIS simulators simply ignore timestamps however if error(I.E inaccuracy
39  between a entities position and its actual position) is an issue then the timestamp
40  can be used to reduce this error.
41 
42  Absolute timestamps tend to require the use of very accurate and expesnive clocks which are synchronized.
43  Relative timestamps are less accurate and tend to use the computers internal clock.
44 
45  The following is taken straight from the DIS standard:
46  "To make sure that relative timestamps are synchronized, you need to compare information about a received
47  PDU that contains a relative timestamp with the time you are maintaining in your simulation application.
48  This is done using software and without any special hardware. It does require that a few packets be observed
49  before time is well synchronized. As packets are received, the difference between their relative timestamps
50  and the receiver's clock is averaged. This average will correspond to the average latency, and the difference
51  represents clock skew. After a few dozen packets, the difference between the observed average and the real
52  average latency is around 5ms. After several hundred, the difference is in the 1ms neighborhood. This is not
53  as long a time to wait as might be imagined. A dozen ES PDU's per minute are emitted by simulators for
54  entities which are stopped. Thus, a few minutes of idle time before the exercise starts provides data for 5ms
55  accuracy, and at 1-2 PDU's per second while moving, 1ms accuracy can be had in a matter of minutes.
56  Exercises with stricter needs really should use absolute based time."
57 
58  size: 32 bits / 4 octets
59 *********************************************************************/
60 
61 #pragma once
62 
63 #include "./DataTypeBase.h"
64 
65 namespace KDIS {
66 namespace DATA_TYPE {
67 
69 {
70 protected:
71 
72  union
73  {
74  struct
75  {
76  KUINT32 m_ui32TimeStampType : 1;
77  KUINT32 m_ui32Time : 31;
78  };
80  } m_TimeStampUnion;
81 
83 
84 public:
85 
86  static const KUINT16 TIME_STAMP_SIZE = 4;
87 
88  TimeStamp();
89 
90  TimeStamp( KDataStream & stream )throw( KException );
91 
92  TimeStamp( KDIS::DATA_TYPE::ENUMS::TimeStampType T, KUINT32 Time, KBOOL AutoCalcRelative = false);
93 
94  virtual ~TimeStamp();
95 
96  //************************************
97  // FullName: KDIS::DATA_TYPE::TimeStamp::SetTimeStampType
98  // KDIS::DATA_TYPE::TimeStamp::GetTimeStampType
99  //!Description: Set the time stamp type, Absolute or Relative.
100  // Parameter: TimeStampType T
101  //************************************
102  void SetTimeStampType( KDIS::DATA_TYPE::ENUMS::TimeStampType T );
103  KDIS::DATA_TYPE::ENUMS::TimeStampType GetTimeStampType() const;
104 
105  //************************************
106  // FullName: KDIS::DATA_TYPE::TimeStamp::SetTime
107  // KDIS::DATA_TYPE::TimeStamp::GetTime
108  //!Description: Time value. Scale of the time is determined
109  //! by setting one hour equal to (2^31 - 1), thereby resulting
110  //! in each time unit representing 3600 s/( 2^31 - 1 ) = 1.676 micro secs
111  //! or 0.000001676 seconds. See EnumHeader.h for further details.
112  //! Note: SetTimeStampAutoCalculate to true to allow KDIS to handle the time stamp.
113  // Parameter: TimeStampType T
114  //************************************
115  void SetTime( KUINT32 T );
116  KUINT32 GetTime() const;
117 
118  //************************************
119  // FullName: KDIS::DATA_TYPE::TimeStamp::SetTimeStampAutoCalculate
120  // KDIS::DATA_TYPE::TimeStamp::IsTimeStampAutoCalculated
121  //!Description: Do you want the time stamp to be automatically calculated?
122  //! Setting this to true will cause CalculateTimeStamp to be called every time
123  //! the PDU is encoded.
124  // Parameter: KBOOL A
125  //************************************
126  void SetTimeStampAutoCalculate( KBOOL A );
127  KBOOL IsTimeStampAutoCalculated() const;
128 
129  //************************************
130  // FullName: KDIS::DATA_TYPE::TimeStamp::CalculateRelativeTimeStamp
131  //!Description: Automatically calculates the timestamp for this moment in time.
132  //************************************
133  void CalculateTimeStamp();
134 
135  //************************************
136  // FullName: KDIS::DATA_TYPE::TimeStamp::GetAsString
137  //!Description: Returns a string representation.
138  //************************************
139  virtual KString GetAsString() const;
140 
141  //************************************
142  // FullName: KDIS::DATA_TYPE::TimeStamp::Decode
143  //!Description: Convert From Network Data.
144  // Parameter: KDataStream & stream
145  //************************************
146  virtual void Decode( KDataStream & stream ) throw( KException );
147 
148  //************************************
149  // FullName: KDIS::DATA_TYPE::TimeStamp::Encode
150  //!Description: Convert To Network Data.
151  // Parameter: KDataStream & stream
152  //************************************
153  virtual KDataStream Encode() const;
154  virtual void Encode( KDataStream & stream ) const;
155 
156  KBOOL operator == ( const TimeStamp & Value ) const;
157  KBOOL operator != ( const TimeStamp & Value ) const;
158 
159  // Note: No check is made if the time stamps are of the same type.
160  KBOOL operator < ( const TimeStamp & Value ) const;
161 };
162 
163 } // END namespace DATA_TYPES
164 } // END namespace KDIS
unsigned int KUINT32
Definition: KDefines.h:103
unsigned short int KUINT16
Definition: KDefines.h:101
Definition: KDefines.h:182
Definition: KDataStream.h:48
Definition: DataTypeBase.h:49
bool KBOOL
Definition: KDefines.h:119
std::string KString
Definition: KDefines.h:116
TimeStampType
Definition: EnumHeader.h:214
KBOOL m_bAutoCalcRel
Definition: TimeStamp.h:82
KUINT32 m_ui32TimeStamp
Definition: TimeStamp.h:79
#define KDIS_EXPORT
Definition: KDefines.h:82
Definition: TimeStamp.h:68