3 //=============================================================================
5 * @file TS_Clerk_Handler.h
7 * @author Prashant Jain
9 //=============================================================================
12 #ifndef ACE_TS_CLERK_HANDLER_H
13 #define ACE_TS_CLERK_HANDLER_H
15 #if !defined (ACE_LACKS_PRAGMA_ONCE)
17 #endif /* ACE_LACKS_PRAGMA_ONCE */
19 #include "ace/SOCK_Connector.h"
20 #include "ace/Svc_Handler.h"
21 #include "ace/Connector.h"
22 #include "ace/MMAP_Memory_Pool.h"
23 #include "ace/Malloc_T.h"
24 #include "ace/Null_Mutex.h"
25 #include "ace/svc_export.h"
26 #include "ace/os_include/os_dirent.h"
28 #include "Time_Request_Reply.h"
31 * @class ACE_Time_Info
33 * @brief A simple struct containing delta time and a sequence number.
41 ACE_UINT32 sequence_num_
;
44 class ACE_TS_Clerk_Processor
; // forward declaration
46 #if defined ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION_EXPORT
47 template class ACE_Svc_Export ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>;
48 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION_EXPORT */
51 * @class ACE_TS_Clerk_Handler
53 * @brief The Clerk Handler provides the interface that is used by the
54 * Clerk Processor to send time update requests to all the
55 * servers. It obtains these updates from the servers and passes
56 * the updates to the Clerk Processor
58 * The Clerk Processor uses send_request() to send a request for
59 * time update to a server. The Clerk Handler internally computes
60 * the round trip delay for the reply to come back. Once it gets
61 * the reply back from the server (handle_input), it adjusts the
62 * system time using the round trip delay estimate and then
63 * passes the delta time by reference back to the Clerk
66 class ACE_Svc_Export ACE_TS_Clerk_Handler
: public ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>
69 /// Default constructor.
70 ACE_TS_Clerk_Handler (ACE_TS_Clerk_Processor
*processor
= 0,
71 ACE_INET_Addr
&addr
= (ACE_INET_Addr
&) ACE_Addr::sap_any
);
73 // = Set/get the current state
76 IDLE
= 1, // Prior to initialization.
77 CONNECTING
, // During connection establishment.
78 ESTABLISHED
, // Connection is established and active.
79 DISCONNECTING
, // In the process of disconnecting.
80 FAILED
// Connection has failed.
83 // = Set/get the current state.
87 // = Set/get the current retry timeout delay.
91 // = Set/get the maximum retry timeout delay.
92 long max_timeout (void);
93 void max_timeout (long);
95 /// Activate this instance of the <ACE_TS_Clerk_Handler>
96 /// (called by the <ACE_TS_Clerk_Processor>).
97 virtual int open (void * = 0);
99 /// Return the handle of the message_fifo_;
100 virtual ACE_HANDLE
get_handle (void) const;
102 /// Called when object is removed from the ACE_Reactor
103 virtual int handle_close (ACE_HANDLE
= ACE_INVALID_HANDLE
,
104 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
);
106 /// Receive time update from a server.
107 virtual int handle_input (ACE_HANDLE
);
109 /// Restart connection asynchronously when timeout occurs.
110 virtual int handle_timeout (const ACE_Time_Value
&tv
,
113 /// Get/Set remote addr
114 void remote_addr (ACE_INET_Addr
&addr
);
115 ACE_INET_Addr
&remote_addr (void);
117 /// Send request for time update to the server as well as return the
118 /// current time info by reference.
119 int send_request (ACE_UINT32 sequence_num
,
120 ACE_Time_Info
&time_info
);
124 virtual int handle_signal (int signum
,
128 static void stderr_output (int = 0);
132 MAX_RETRY_TIMEOUT
= 300
133 // 5 minutes is the maximum timeout.
137 /// Receive a reply from a server containing time update
138 int recv_reply (ACE_Time_Request
&reply
);
140 /// Reinitiate connection with the server
141 int reinitiate_connection (void);
143 /// The current state of the connection
146 /// Amount of time to wait between reconnection attempts
149 /// Maximum amount of time to wait between reconnection attempts
152 /// Remote Addr used for connecting to the server
153 ACE_INET_Addr remote_addr_
;
155 /// Instance of Clerk Processor used for re-establishing connections
156 ACE_TS_Clerk_Processor
*processor_
;
158 /// Time at which request was sent (used to compute round trip delay)
161 /// Next sequence number of time request (waiting for this update from
163 ACE_UINT32 cur_sequence_num_
;
165 /// Record of current delta time and current sequence number
166 ACE_Time_Info time_info_
;
170 * @class ACE_TS_Clerk_Processor
172 * @brief This class manages all the connections to the servers along
173 * with querying them periodically for time updates.
175 * The Clerk Processor creates connections to all the servers and
176 * creates an ACE_TS_Clerk_Handler for each connection to handle
177 * the requests and replies. It periodically sends a request for
178 * time update through each of the handlers and uses the replies
179 * for computing a synchronized system time.
181 class ACE_TS_Clerk_Processor
: public ACE_Connector
<ACE_TS_Clerk_Handler
, ACE_SOCK_CONNECTOR
>
184 /// Default constructor
185 ACE_TS_Clerk_Processor (void);
187 /// Query servers for time periodically (timeout value)
188 virtual int handle_timeout (const ACE_Time_Value
&tv
,
191 /// Set up connections to all servers
192 int initiate_connection (ACE_TS_Clerk_Handler
*,
193 ACE_Synch_Options
&);
196 // = Dynamic linking hooks.
197 /// Called when service is linked.
198 virtual int init (int argc
, ACE_TCHAR
*argv
[]);
200 /// Called when service is unlinked.
201 virtual int fini (void);
203 /// Called to determine info about the service.
204 virtual int info (ACE_TCHAR
**strp
, size_t length
) const;
206 // = Scheduling hooks.
207 virtual int suspend (void);
208 virtual int resume (void);
211 /// Parse svc.conf arguments.
212 int parse_args (int argc
, ACE_TCHAR
*argv
[]);
214 /// Allocate entry in shared memory for system time
217 /// Update delta_time using times obtained from all servers
220 /// Allocator (used for reading/writing system time from/to shared memory)
221 typedef ACE_Malloc
<ACE_MMAP_MEMORY_POOL
, ACE_Null_Mutex
> MALLOC
;
222 typedef ACE_Allocator_Adapter
<MALLOC
> ALLOCATOR
;
225 /// Set of TS_Clerk_Handlers and iterator over the set.
226 typedef ACE_Unbounded_Set
<ACE_TS_Clerk_Handler
*> HANDLER_SET
;
227 typedef ACE_Unbounded_Set_Iterator
<ACE_TS_Clerk_Handler
*> HANDLER_SET_ITERATOR
;
228 HANDLER_SET handler_set_
;
232 time_t *delta_time_
; // Diff between system time and local time
233 time_t *last_local_time_
; // Last local time
236 /// Clerk system time containing pointers to entries in shared memory
237 System_Time system_time_
;
239 /// Timer id returned by Reactor
242 /// Time period for updating system time
245 /// Pool name for backing store
246 ACE_TCHAR poolname_
[MAXNAMLEN
+ 1];
248 /// Do a blocking/non-blocking connect
249 int blocking_semantics_
;
251 /// Sequence number of next expected update from servers
252 ACE_UINT32 cur_sequence_num_
;
255 ACE_SVC_FACTORY_DECLARE (ACE_TS_Clerk_Processor
)
257 #endif /* ACE_TS_CLERK_HANDLER_H */