summaryrefslogtreecommitdiff
path: root/ace/UUID.h
blob: 78ea7b6700ef990df6fb7fbebd90bb5324d40508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//$Id$
#ifndef ACE_UUID_H
#define ACE_UUID_H

#include "ace/pre.h"
#include "ace/OS.h"
#include "ace/SString.h"
#include "ace/Singleton.h"

//typedef unsigned char Octet;
class ACE_Lock;

/// Class to hold a MAC address
class ACE_Export UUID_node 
{
 public:

  UUID_Node (void);

  typedef enum {NODE_ID_SIZE = 6};
  typedef u_char NodeID[NODE_ID_SIZE];
  
  NodeID& nodeID (void);
  void nodeID (NodeID&);
  
  /*
  bool operator == (const UUID_node_t& right) const;
  bool operator != (const UUID_node_t& right) const;
  bool operator <  (const UUID_node_t& right) const;
  */

 private:
  NodeID nodeID_;
};


/**
 *  @class ACE_UUID
 *
 *	ACE_UUID represebt a Universally Unique IDentifier (UUID)
 * 	as described in (the expired) INTERNET-DRAFT
 *	specification entitled UUIDs and GUIDs. All instances of
 *	UUID are of the time-based variety. That is, the version
 *	number part of the timeHiAndVersion field is 1.
 *
 *	The default constructor creates a nil UUID.
 *
 *	UUIDs have value semantics. In addition, they may be
 *	compared for ordering and equality.
 *
 *	Note: the statement of UUID uniqueness is less stringent
 *	than the requirement for UUID in general. There is no
 *	guarantee that a UUID generated by by this class is
 *	universally unique with respect to another arbitrary
 *	UUID. 
 */
class ACE_Export ACE_UUID 
{
 public:

  ACE_UUID(void);
 
  /// Copy constructor
  ACE_UUID(const ACE_UUID &right);
  
  /// Constructs a UUID from a string representation.
  ACE_UUID (const ACE_SString& uuidString);

  ~ACE_UUID(void);

  /// Data Members for Class Attributes
  ACE_UINT32 timeLow (void);
  void timeLow (ACE_UINT32);

  ACE_UINT16 timeMid (void);    
  void timeMid (ACE_UINT16);    

  ACE_UINT16 timeHiAndVersion (void);
  void timeHiAndVersion (ACE_UINT16);

  u_char clockSeqHiAndReserved (void);    
  void clockSeqHiAndReserved (u_char);    

  u_char clockSeqLow (void);
  void clockSeqLow (u_char);
  
  UUID_node* node (void); 
  void node (UUID_node*); 

  /// Returns a string representation of the UUID. 
  const ACE_SString* to_string (void);
    
  static ACE_UUID NIL_UUID;

  /*
  /// Assignment Operation
  ACE_UUID & operator=(const ACE_UUID &right);

  /// Equality Operations 
  bool operator==(const ACE_UUID &right) const;
  bool operator!=(const ACE_UUID &right) const;

  /// Relational Operations 
  bool operator< (const ACE_UUID &right) const;
  bool operator> (const ACE_UUID &right) const;
  bool operator<=(const ACE_UUID &right) const;
  bool operator>=(const ACE_UUID &right) const;
  */
 private:

  /// Data Members for Class Attributes
  ACE_UINT32 timeLow_;
  ACE_UINT16 timeMid_;    
  ACE_UINT16 timeHiAndVersion_;    
  u_char clockSeqHiAndReserved_;    
  u_char clockSeqLow_;    
  UUID_node* node_; 
  
  /// The string representation of the UUID. This is created */
  /// and updated only on demand. 
  ACE_SString *as_string_; 
};


/* class ACE_UUID_Ext: public ACE_UUID */
/* { */
/*  public: */
/*   ACE_UUID_Ext (void); */
/*   ~ACE_UUID_Ext (void); */

/*   ACE_CString* thr_id (void); */
  
/*   ACE_CString* pid (void); */

/*  private: */
/*   ACE_CString* thr_id_; */
/*   ACE_CString* pid_; */
//};


/**
 * @class ACE_UUID_Generator
 * Singleton class that generates UUIDs.
 */
class ACE_Export ACE_UUID_Generator
{
 public:

  enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};

  void init (void);

  /// Format timestamp, clockseq, and nodeID into a VI UUID.
  void generateUUID(ACE_UUID&);
  
  /// Format timestamp, clockseq, and nodeID into a VI UUID.
  ACE_UUID* generateUUID (void);

  /// Type to represent UTC as a count of 100 nanosecond intervals since
  /// 00:00:00.00, 15 October 1582.
  typedef ACE_UINT64 UUID_time;

  ACE_UUID_Generator(void);
  ~ACE_UUID_Generator();

  /// The locking strategy prevents multiple generators
  /// from accessing the UUID_state at the same time.
  /// Get the locking strategy.
  ACE_Lock* lock (void);

  /// Set a new locking strategy and return the hold one.
  ACE_Lock* lock (ACE_Lock *lock);


 private:

  /// The maximum number of uuids that can be generated per tick of the
  /// system clock. This number should be the number of 100ns ticks of the
  /// actual resolution of the system's clock.
  enum {UUIDS_PER_TICK = 1000};

  /// The number of uuids generated in this process since the last clock
  /// tick. Value never exceeds uuidsPerTick - 1.
  ACE_UINT32 uuidsThisTick_;

  /// The system time when that last uuid was generated.
  UUID_time timeLast_;

  /// Type to map to contain the UUID generator persistent state. This
  /// will be kept in memory mapped shared memory
  struct UUID_State
  {
    UUID_time timestamp;
    UUID_node node;
    ACE_UINT16 clockSequence;
  };

  /// Obtain a UUID timestamp. Compensate for the fact that the time
  /// obtained from getSystem time has a resolution less than 100ns.
  void get_timestamp (UUID_time& timestamp);

  /// Obtain the system time in UTC as a count of 100 nanosecond intervals
  /// since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
  /// the Christian calendar).
  void get_systemtime( UUID_time& timeNow);
  
  /// The UUID generator persistent state.
  UUID_State uuid_state_;

  ACE_Lock* lock_;
};

typedef ACE_Singleton <ACE_UUID_Generator, ACE_SYNCH_MUTEX> ACE_UUID_GENERATOR;

#if defined (__ACE_INLINE__)
#include "ace/UUID.i"
#endif /* __ACE_INLINE__ */

#include "ace/post.h"
#endif // ACE_UUID_H