summaryrefslogtreecommitdiff
path: root/tests/TP_Reactor_Test.h
blob: 6ebfe56ceced2f571ca0ccf9512f5e5ea010be2c (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    TP_Reactor_Test.h
//
// = DESCRIPTION
//    Define class needed for generating templates. IBM C++ requires this to
//    be in its own file for auto template instantiation.
//
// = AUTHOR
//    Alexander Libman <alibman@ihug.com.au>,<alexl@rumblgroup.com>
//
// ============================================================================

#ifndef ACE_TESTS_TP_REACTOR_TEST_H
#define ACE_TESTS_TP_REACTOR_TEST_H

#include "ace/INET_Addr.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "ace/Acceptor.h"
#include "ace/Connector.h"
#include "ace/Svc_Handler.h"
#include "ace/Recursive_Thread_Mutex.h"

const  size_t MAX_SENDERS = 1000;
const  size_t MAX_RECEIVERS = 1000;


// *************************************************************
//   Receiver and Acceptor
// *************************************************************
// forward declaration
class Acceptor;

class Receiver : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>
{
  friend class Acceptor;
public:

  Receiver (Acceptor * acceptor=0, size_t index=MAX_RECEIVERS+1);

  ~Receiver (void);

  long get_total_snd (void) { return this->total_snd_; }
  long get_total_rcv (void) { return this->total_rcv_; }
  long get_total_w   (void) { return this->total_w_; }
  long get_total_r   (void) { return this->total_r_; }

  // virtual from ACE_Svc_Handler<>
  virtual int open (void * pVoid);

  // virtual from ACE_Event_Handler
  virtual int handle_input (ACE_HANDLE h);
  virtual int handle_output (ACE_HANDLE h);
  virtual int handle_close (ACE_HANDLE h , ACE_Reactor_Mask mask);

private:
  int  terminate_io (ACE_Reactor_Mask mask);
  int  initiate_io (ACE_Reactor_Mask mask);
  int  check_destroy (void);

  Acceptor * acceptor_;
  size_t index_;
  int  flg_mask_;

  ACE_Recursive_Thread_Mutex mutex_;
  long total_snd_;
  long total_rcv_;
  long total_w_;
  long total_r_;
};

// *************************************************************

class Acceptor : public ACE_Acceptor<Receiver,ACE_SOCK_ACCEPTOR>
{
 friend class Receiver;
public:
  size_t get_number_sessions (void) { return sessions_; }
  long get_total_snd (void) { return this->total_snd_; }
  long get_total_rcv (void) { return this->total_rcv_; }
  long get_total_w   (void) { return this->total_w_; }
  long get_total_r   (void) { return this->total_r_; }

  Acceptor (void);
  virtual ~Acceptor (void);

  void stop (void);
  int start (const ACE_INET_Addr & addr);

  //	virtual from ACE_Acceptor<Receiver,ACE_SOCK_ACCEPTOR>
  virtual int make_svc_handler (Receiver * & sh);

private:

  ACE_Recursive_Thread_Mutex mutex_;
  size_t sessions_;
  Receiver *list_receivers_[MAX_RECEIVERS];
  long total_snd_;
  long total_rcv_;
  long total_w_;
  long total_r_;

  void on_new_receiver (Receiver & rcvr);
  void on_delete_receiver (Receiver & rcvr);
};


// *******************************************
//   Sender
// *******************************************

class Connector;

class Sender : public ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_MT_SYNCH>
{
  friend class Connector;

public:
  Sender (Connector * connector=0, size_t index=MAX_SENDERS+1);

  ~Sender (void);

  long get_total_snd (void) { return this->total_snd_; }
  long get_total_rcv (void) { return this->total_rcv_; }
  long get_total_w   (void) { return this->total_w_; }
  long get_total_r   (void) { return this->total_r_; }

  // virtual from ACE_Svc_Handler<>
  virtual int open (void * pVoid);

  // virtual from ACE_Event_Handler
  virtual int handle_input (ACE_HANDLE h);
  virtual int handle_output (ACE_HANDLE h);
  virtual int handle_close (ACE_HANDLE h , ACE_Reactor_Mask mask);

private:
  int  terminate_io (ACE_Reactor_Mask mask);
  int  initiate_io (ACE_Reactor_Mask mask);
  int  initiate_write ();
  int  check_destroy (void);

  Connector * connector_;
  size_t  index_;
  int  flg_mask_;

  ACE_Recursive_Thread_Mutex mutex_;

  char send_buf_ [1024];
  long total_snd_;
  long total_rcv_;
  long total_w_;
  long total_r_;
};

// *************************************************************

class Connector: public ACE_Connector<Sender,ACE_SOCK_CONNECTOR>
{
  friend class Sender;
public:
  size_t get_number_sessions (void) { return sessions_; }
  long get_total_snd (void) { return this->total_snd_; }
  long get_total_rcv (void) { return this->total_rcv_; }
  long get_total_w   (void) { return this->total_w_; }
  long get_total_r   (void) { return this->total_r_; }


  Connector ();
  virtual ~Connector ();

  void stop ();
  int  start (const ACE_INET_Addr & addr , int num);

  //	virtual from ACE_Connector<>
  virtual int make_svc_handler (Sender * & sh);

private:

  ACE_Recursive_Thread_Mutex mutex_;
  size_t  sessions_;
  Sender * list_senders_ [MAX_SENDERS];
  long total_snd_;
  long total_rcv_;
  long total_w_;
  long total_r_;

  void on_new_sender (Sender & sndr);
  void on_delete_sender (Sender & sndr);
};


#endif /* ACE_TESTS_TP_REACTOR_TEST_H */