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.
40 ACE_UINT32 sequence_num_
;
43 class ACE_TS_Clerk_Processor
; // forward declaration
45 #if defined ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION_EXPORT
46 template class ACE_Svc_Export ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>;
47 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION_EXPORT */
50 * @class ACE_TS_Clerk_Handler
52 * @brief The Clerk Handler provides the interface that is used by the
53 * Clerk Processor to send time update requests to all the
54 * servers. It obtains these updates from the servers and passes
55 * the updates to the Clerk Processor
57 * The Clerk Processor uses send_request() to send a request for
58 * time update to a server. The Clerk Handler internally computes
59 * the round trip delay for the reply to come back. Once it gets
60 * the reply back from the server (handle_input), it adjusts the
61 * system time using the round trip delay estimate and then
62 * passes the delta time by reference back to the Clerk
65 class ACE_Svc_Export ACE_TS_Clerk_Handler
: public ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>
68 /// Default constructor.
69 ACE_TS_Clerk_Handler (ACE_TS_Clerk_Processor
*processor
= 0,
70 ACE_INET_Addr
&addr
= (ACE_INET_Addr
&) ACE_Addr::sap_any
);
72 // = Set/get the current state
75 IDLE
= 1, // Prior to initialization.
76 CONNECTING
, // During connection establishment.
77 ESTABLISHED
, // Connection is established and active.
78 DISCONNECTING
, // In the process of disconnecting.
79 FAILED
// Connection has failed.
82 // = Set/get the current state.
86 // = Set/get the current retry timeout delay.
90 // = Set/get the maximum retry timeout delay.
92 void max_timeout (long);
94 /// Activate this instance of the <ACE_TS_Clerk_Handler>
95 /// (called by the <ACE_TS_Clerk_Processor>).
96 virtual int open (void * = 0);
98 /// Return the handle of the message_fifo_;
99 virtual ACE_HANDLE
get_handle () const;
101 /// Called when object is removed from the ACE_Reactor
102 virtual int handle_close (ACE_HANDLE
= ACE_INVALID_HANDLE
,
103 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
);
105 /// Receive time update from a server.
106 virtual int handle_input (ACE_HANDLE
);
108 /// Restart connection asynchronously when timeout occurs.
109 virtual int handle_timeout (const ACE_Time_Value
&tv
,
112 /// Get/Set remote addr
113 void remote_addr (ACE_INET_Addr
&addr
);
114 ACE_INET_Addr
&remote_addr ();
116 /// Send request for time update to the server as well as return the
117 /// current time info by reference.
118 int send_request (ACE_UINT32 sequence_num
,
119 ACE_Time_Info
&time_info
);
123 virtual int handle_signal (int signum
, siginfo_t
*, ucontext_t
*);
125 static void stderr_output (int = 0);
129 MAX_RETRY_TIMEOUT
= 300
130 // 5 minutes is the maximum timeout.
134 /// Receive a reply from a server containing time update
135 int recv_reply (ACE_Time_Request
&reply
);
137 /// Reinitiate connection with the server
138 int reinitiate_connection ();
140 /// The current state of the connection
143 /// Amount of time to wait between reconnection attempts
146 /// Maximum amount of time to wait between reconnection attempts
149 /// Remote Addr used for connecting to the server
150 ACE_INET_Addr remote_addr_
;
152 /// Instance of Clerk Processor used for re-establishing connections
153 ACE_TS_Clerk_Processor
*processor_
;
155 /// Time at which request was sent (used to compute round trip delay)
158 /// Next sequence number of time request (waiting for this update from
160 ACE_UINT32 cur_sequence_num_
;
162 /// Record of current delta time and current sequence number
163 ACE_Time_Info time_info_
;
167 * @class ACE_TS_Clerk_Processor
169 * @brief This class manages all the connections to the servers along
170 * with querying them periodically for time updates.
172 * The Clerk Processor creates connections to all the servers and
173 * creates an ACE_TS_Clerk_Handler for each connection to handle
174 * the requests and replies. It periodically sends a request for
175 * time update through each of the handlers and uses the replies
176 * for computing a synchronized system time.
178 class ACE_TS_Clerk_Processor
: public ACE_Connector
<ACE_TS_Clerk_Handler
, ACE_SOCK_CONNECTOR
>
181 /// Default constructor
182 ACE_TS_Clerk_Processor ();
184 /// Query servers for time periodically (timeout value)
185 virtual int handle_timeout (const ACE_Time_Value
&tv
,
188 /// Set up connections to all servers
189 int initiate_connection (ACE_TS_Clerk_Handler
*,
190 ACE_Synch_Options
&);
193 // = Dynamic linking hooks.
194 /// Called when service is linked.
195 virtual int init (int argc
, ACE_TCHAR
*argv
[]);
197 /// Called when service is unlinked.
200 /// Called to determine info about the service.
201 virtual int info (ACE_TCHAR
**strp
, size_t length
) const;
203 // = Scheduling hooks.
204 virtual int suspend ();
205 virtual int resume ();
208 /// Parse svc.conf arguments.
209 int parse_args (int argc
, ACE_TCHAR
*argv
[]);
211 /// Allocate entry in shared memory for system time
214 /// Update delta_time using times obtained from all servers
217 /// Allocator (used for reading/writing system time from/to shared memory)
218 typedef ACE_Malloc
<ACE_MMAP_MEMORY_POOL
, ACE_Null_Mutex
> MALLOC
;
219 typedef ACE_Allocator_Adapter
<MALLOC
> ALLOCATOR
;
222 /// Set of TS_Clerk_Handlers and iterator over the set.
223 typedef ACE_Unbounded_Set
<ACE_TS_Clerk_Handler
*> HANDLER_SET
;
224 typedef ACE_Unbounded_Set_Iterator
<ACE_TS_Clerk_Handler
*> HANDLER_SET_ITERATOR
;
225 HANDLER_SET handler_set_
;
229 time_t *delta_time_
; // Diff between system time and local time
230 time_t *last_local_time_
; // Last local time
233 /// Clerk system time containing pointers to entries in shared memory
234 System_Time system_time_
;
236 /// Timer id returned by Reactor
239 /// Time period for updating system time
242 /// Pool name for backing store
243 ACE_TCHAR poolname_
[MAXNAMLEN
+ 1];
245 /// Do a blocking/non-blocking connect
246 int blocking_semantics_
;
248 /// Sequence number of next expected update from servers
249 ACE_UINT32 cur_sequence_num_
;
252 ACE_SVC_FACTORY_DECLARE (ACE_TS_Clerk_Processor
)
254 #endif /* ACE_TS_CLERK_HANDLER_H */