summaryrefslogtreecommitdiff
path: root/apps/Gateway/Gateway/Channel.h
blob: 339716bc55ab02dce28db45bb07476be9c5c3d8b (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    apps
// 
// = FILENAME
//    Channel.h
//
// = AUTHOR
//    Doug Schmidt 
// 
// ============================================================================

#if !defined (_CHANNEL)
#define _CHANNEL

#include "ace/Service_Config.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Connector.h"
#include "ace/Svc_Handler.h"
#include "Routing_Table.h"
#include "Routing_Entry.h"
#include "Peer_Message.h"

// The following typedefs are used in order to parameterize the
// synchronization policies without changing the source code!

// If we don't have threads then use the single-threaded synchronization.
#if !defined (ACE_HAS_THREADS)
#define SYNCH ACE_NULL_SYNCH
typedef ACE_Null_Mutex MUTEX;
#define CHANNEL_PEER_STREAM ACE_SOCK_STREAM 
#define CHANNEL_PEER_CONNECTOR ACE_SOCK_CONNECTOR 
#else /* ACE_HAS_THREADS */

// Select communication mechanisms.
#if 0 // defined (ACE_HAS_TLI)
// Note that due to inconsistencies between the semantics of sockets
// and TLI with respect to establishing non-blocking connections it's
// not a good idea to use TLI...
#include "ace/TLI_Connector.h"
#define CHANNEL_PEER_STREAM ACE_TLI_STREAM
#define CHANNEL_PEER_CONNECTOR ACE_TLI_CONNECTOR 
#else
#define CHANNEL_PEER_STREAM ACE_SOCK_STREAM 
#define CHANNEL_PEER_CONNECTOR ACE_SOCK_CONNECTOR 
#endif /* 0 */

// Note that we only need to make the ACE_Task thread-safe if we
// are using the multi-threaded Thr_Output_Channel...
#if defined (USE_OUTPUT_MT) 
#define SYNCH ACE_MT_SYNCH
#else
#define SYNCH ACE_NULL_SYNCH
#endif /* USE_OUTPUT_MT || USE_INPUT_MT */

// Note that we only need to make the ACE_Map_Manager thread-safe if
// we are using the multi-threaded Thr_Input_Channel...
#if defined (USE_INPUT_MT)
typedef ACE_RW_Mutex MUTEX;
#else
typedef ACE_Null_Mutex MUTEX;
#endif /* USE_INPUT_MT */
#endif /* ACE_HAS_THREADS */

// Typedef for the routing table.
typedef Routing_Table<Peer_Addr, Routing_Entry, MUTEX>
	ROUTING_TABLE;

// Forward declaration.
class Channel_Connector;

class Channel : public ACE_Svc_Handler<CHANNEL_PEER_STREAM, SYNCH>
  // = TITLE
  //    Channel contains info about connection state and addressing.
  //
  // = DESCRIPTION
  //     The Channel classes process messages sent from the peers to the
  //     gateway.  These classes works as follows:
  //   
  // 1. Channel_Connector creates a number of connections with the set of 
  //    peers specified in a configuration file.  
  //
  // 2. For each peer that connects successfully, Channel_Connector
  //    creates an Channel object.  Each object assigns a unique routing
  //    id to its associated peer.  The Channels are used by gatewayd
  //    that to receive, route, and forward messages from source peer(s)
  //    to destination peer(s).
{
public:
  Channel (ROUTING_TABLE *, 
	   Channel_Connector *, 
	   ACE_Thread_Manager * = 0,
	   int socket_queue_size = 0);

  virtual int open (void * = 0);
  // Initialize and activate a single-threaded Channel (called by
  // ACE_Connector::handle_output()).

  int bind (const ACE_INET_Addr &remote_addr, 
	    const ACE_INET_Addr &local_addr, 
	    CONN_ID);
  // Set the peer's addressing and routing information.

  ACE_INET_Addr &remote_addr (void);
  // Returns the peer's routing address.

  ACE_INET_Addr &local_addr (void);
  // Returns our local address.

  // = Set/get routing id.
  CONN_ID id (void);
  void id (CONN_ID);

  // = Set/get the current state of the Channel.
  enum State
  {
    IDLE = 1,      // Prior to initialization.
    CONNECTING,    // During connection establishment.
    ESTABLISHED,   // Channel is established and active.
    DISCONNECTING, // Channel is in the process of connecting.
    FAILED	   // Channel has failed.
  };

  // = Set/get the current state.
  State state (void);
  void state (State);

  // = Set/get the current retry timeout delay.
  int timeout (void);
  void timeout (int);

  // = Set/get the maximum retry timeout delay.
  int max_timeout (void);
  void max_timeout (int);

  // = Set/get Channel activity status.
  int active (void);
  void active (int);

  // = Set/get direction (necessary for error checking).
  char direction (void);
  void direction (char);

  // = The total number of bytes sent/received on this channel.
  size_t total_bytes (void);
  void total_bytes (size_t bytes);
  // Increment count by <bytes>.

  virtual int handle_timeout (const ACE_Time_Value &, const void *arg);
  // Perform timer-based Channel reconnection.

protected:
  enum
  {
    MAX_RETRY_TIMEOUT = 300 // 5 minutes is the maximum timeout.
  };

  int initialize_connection (void);
  // Perform the first-time initiation of a connection to the peer.

  int reinitiate_connection (void);
  // Reinitiate a connection asynchronously when peers fail.

  void socket_queue_size (void);
  // Set the socket queue size.

  virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE,
			    ACE_Reactor_Mask = ACE_Event_Handler::RWE_MASK);
  // Perform Channel termination.

  ROUTING_TABLE *routing_table_;
  // Pointer to table that maps a Peer_Addr
  // to a Set of Channel *'s for output.

  ACE_INET_Addr remote_addr_;
  // Address of peer.

  ACE_INET_Addr local_addr_;
  // Address of us.

  CONN_ID id_;  
  // The assigned routing ID of this entry.

  size_t total_bytes_;
  // The total number of bytes sent/received on this channel.

  State state_;
  // The current state of the channel.

  Channel_Connector *connector_;
  // Back pointer to Channel_Connector to reestablish broken
  // connections.

  int timeout_;
  // Amount of time to wait between reconnection attempts.

  int max_timeout_;
  // Maximum amount of time to wait between reconnection attempts.

  char direction_;
  // Indicates which direction data flows through the channel ('O' ==
  // output and 'I' == input).

  int socket_queue_size_;
  // Size of the socket queue (0 means "use default").
};

class Input_Channel : public Channel
  // = TITLE
  //     Handle reception of Peer messages arriving as events.
{
public:
  Input_Channel (ROUTING_TABLE *, 
		 Channel_Connector *, 
		 ACE_Thread_Manager * = 0,
		 int socket_queue_size = 0);
  // Constructor sets the routing table pointer.

  virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE);
  // Receive and process peer messages.

protected:
  virtual int recv_peer (ACE_Message_Block *&);
  // Receive a message from a peer.

  int route_message (ACE_Message_Block *);
  // Action that receives messages from peerd.

  ACE_Message_Block *msg_frag_;
  // Keep track of message fragment to handle non-blocking recv's from
  // peers.

  virtual int svc (void);
  // This method is not used since we are single-threaded.

private:
  virtual int put (ACE_Message_Block *, ACE_Time_Value *tv = 0);  
  // This methods should not be called to handle input.
};

class Output_Channel : public Channel
  // = TITLE
  //     Handle transmission of messages to other Peers using a
  //     single-threaded approach.
{
public:
  Output_Channel (ROUTING_TABLE *, 
		  Channel_Connector *, 
		  ACE_Thread_Manager * = 0,
		  int socket_queue_size = 0);

  virtual int put (ACE_Message_Block *, ACE_Time_Value * = 0);
  // Send a message to a gateway (may be queued if necessary).

protected:
  // = We'll allow up to 16 megabytes to be queued per-output
  // channel.
  enum {QUEUE_SIZE = 1024 * 1024 * 16};

  virtual int handle_input (ACE_HANDLE);
  // Receive and process shutdowns from peer.

  virtual int handle_output (ACE_HANDLE);
  // Finish sending a message when flow control conditions abate.

  int nonblk_put (ACE_Message_Block *mb);
  // Perform a non-blocking put().

  virtual int send_peer (ACE_Message_Block *);
  // Send a message to a peer.

  virtual int svc (void);
  // This method is not used since we are single-threaded.
};

#endif /* _CHANNEL */