summaryrefslogtreecommitdiff
path: root/ACE/tests/FlReactor_Test.cpp
blob: f529c9ec6d1fe0a27ad20b82f90d3cd881baa40c (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
281
282
283
/* -*- C++ -*- */

//=============================================================================
/**
 *  @file   FlReactor_Test.cpp
 *
 *  $Id$
 *
 * A simple test that ilustrates the integration of the fast-light
 * toolkit (http://fltk.easysw.org/) with ACE, it uses FL to create
 * an OpenGL window and display a polygon, it uses ACE to open an
 * acceptor. Every time there is a connection the number of polygons
 * is increased, a little widget can be used to change the number of
 * polygons too.
 *
 *
 *  @author Carlos O'Ryan <coryan@cs.wustl.edu>
 */
//=============================================================================


#include "test_config.h"




#include "ace/FlReactor/FlReactor.h"
#include "ace/Event_Handler.h"
#include "ace/Acceptor.h"
#include "ace/Argv_Type_Converter.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/Service_Config.h"
#include "ace/Thread_Manager.h"

#include /**/ <FL/Fl.H>
#include /**/ <FL/Fl_Window.H>
#include /**/ <FL/Fl_Hor_Slider.H>
#include /**/ <FL/Fl_Box.H>
#include /**/ <FL/math.h>
#include /**/ <FL/gl.h>
#include /**/ <FL/Fl_Gl_Window.H>

class Test_Window : public Fl_Gl_Window
{
public:
  /// Constructor
  Test_Window (int x, int y, int w, int h,
               const char * l = 0);

  int sides (void) const;
  void sides (int s);
  void incr_sides (void);

private:
  /// from the Fl_Gl_Window...
  virtual void draw (void);

  int sides_;
};

Test_Window::Test_Window (int x, int y,
                      int w, int h,
                      const char* l)
  :  Fl_Gl_Window (x, y, w, h, l),
     sides_ (3)
{
}

int
Test_Window::sides (void) const
{
  return this->sides_;
}

void
Test_Window::sides (int s)
{
  this->sides_ = s;
  this->redraw ();
}

void
Test_Window::incr_sides (void)
{
  this->sides_++;
  if (this->sides_ > 10)
    this->sides_ = 3;
  this->redraw ();
}

void
Test_Window::draw (void)
{
  // the valid() property may be used to avoid reinitializing your
  // GL transformation for each redraw:
  if (!this->valid ())
    {
      this->valid (1);
      glLoadIdentity ();
      glViewport (0, 0, this->w (), this->h ());
    }
  // draw an amazing but slow graphic:
  glClear (GL_COLOR_BUFFER_BIT);

  glBegin (GL_POLYGON);
  int s = this->sides_;

  for (int i = 0; i != s; ++i)
    {
      double ang = i * 2 * M_PI / s;
      glColor3f (float (i) / s,
                 float (i) / s,
                 float (i) / s);
      glVertex3f (cos (ang), sin (ang), 0);
    }
  glEnd ();
}

// when you change the data, as in this callback, you must call redraw ():
void sides_cb (Fl_Widget *o, void *p)
{
  Test_Window *tw =
    reinterpret_cast<Test_Window *> (p);
  Fl_Slider *slider =
    dynamic_cast<Fl_Slider*> (o);
  tw->sides (static_cast<int> (slider->value ()));
}

class Connection_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
public:
  Connection_Handler (Test_Window *w = 0,
                      Fl_Box* box = 0);

  //FUZZ: disable check_for_lack_ACE_OS
  virtual int open (void *);
  //FUZZ: enble check_for_lack_ACE_OS

  virtual int handle_input (ACE_HANDLE);

private:
  Test_Window *w_;
  Fl_Box *box_;
};

class Acceptor : public ACE_Acceptor<Connection_Handler,ACE_SOCK_ACCEPTOR>
{
public:
  Acceptor (Test_Window *w = 0,
            Fl_Box *box = 0);

  virtual int make_svc_handler (Connection_Handler *&sh);

private:
  Test_Window* w_;
  Fl_Box *box_;
};

Connection_Handler::Connection_Handler (Test_Window *w,
                                        Fl_Box *box)
  :  w_ (w),
     box_ (box)
{
}

int
Connection_Handler::open (void*)
{
  if (this->box_ != 0)
    {
      ACE_INET_Addr from;

      this->peer ().get_remote_addr (from);
      const int bufsiz = 128;
      ACE_TCHAR buf[bufsiz];

      from.addr_to_string (buf, bufsiz, 0);

      static char msg[256];
      ACE_OS::sprintf (msg, "connection from <%s>\n",
                       ACE_TEXT_ALWAYS_CHAR (buf));

      this->box_->label (msg);
      this->box_->redraw ();
    }

  if (this->w_ != 0)
    {
      this->w_->incr_sides ();
    }

  return this->peer ().enable (ACE_NONBLOCK);
}

int
Connection_Handler::handle_input (ACE_HANDLE)
{
  char buf[BUFSIZ];
  if (this->peer ().recv (buf, BUFSIZ) <= 0)
    return -1;
  return 0;
}

Acceptor::Acceptor (Test_Window *w, Fl_Box *box)
  :  w_ (w),
     box_ (box)
{
}

int
Acceptor::make_svc_handler (Connection_Handler *&sh)
{
  if (sh == 0)
    {
      ACE_NEW_RETURN (sh, Connection_Handler (this->w_, this->box_), -1);
      sh->reactor (this->reactor());
    }
  return 0;
}

int run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("FlReactor_Test"));

  Fl_Window window (300, 370);

  Test_Window tw (10, 75, window.w () - 20, window.h ()-90);
  window.resizable (&tw);

  Fl_Hor_Slider slider (60, 5, window.w () - 70, 30, "Sides:");
  slider.align (FL_ALIGN_LEFT);
  slider.callback (sides_cb, &tw);
  slider.value (tw.sides ());
  slider.step (1);
  slider.bounds (3, 10);

  ACE_FlReactor reactor;
  ACE_Reactor r (&reactor);

  Fl_Box *box = new Fl_Box (FL_UP_BOX, 10, 40,
                            window.w () - 20, 30,
                            "Setting up");
  box->labelfont (FL_BOLD);

  Acceptor acceptor (&tw, box);

  ACE_INET_Addr address;

  if (acceptor.open (address, &r) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open acceptor"),
                      -1);

  acceptor.acceptor ().get_local_addr (address);

  const int bufsiz = 128;
  ACE_TCHAR buf[bufsiz];

  address.addr_to_string (buf, bufsiz, 0);

  char msg[2 * bufsiz];
  ACE_OS::sprintf (msg, "Listening on <%s>\n",
                   ACE_TEXT_ALWAYS_CHAR (buf));

  box->label (msg);
  box->redraw ();

  window.end ();

  ACE_Argv_Type_Converter ct (argc, argv);

  window.show (argc, ct.get_ASCII_argv ());
  tw.show ();

  int const retval = Fl::run ();

  ACE_END_TEST;

  return retval;
}