3 //=============================================================================
5 * @file FlReactor_Test.cpp
7 * A simple test that ilustrates the integration of the fast-light
8 * toolkit (http://fltk.easysw.org/) with ACE, it uses FL to create
9 * an OpenGL window and display a polygon, it uses ACE to open an
10 * acceptor. Every time there is a connection the number of polygons
11 * is increased, a little widget can be used to change the number of
14 * @author Carlos O'Ryan <coryan@cs.wustl.edu>
16 //=============================================================================
19 #include "test_config.h"
23 #include "ace/FlReactor/FlReactor.h"
24 #include "ace/Event_Handler.h"
25 #include "ace/Acceptor.h"
26 #include "ace/Argv_Type_Converter.h"
27 #include "ace/SOCK_Acceptor.h"
28 #include "ace/SOCK_Connector.h"
29 #include "ace/Service_Config.h"
30 #include "ace/Thread_Manager.h"
32 #include /**/ <FL/Fl.H>
33 #include /**/ <FL/Fl_Window.H>
34 #include /**/ <FL/Fl_Hor_Slider.H>
35 #include /**/ <FL/Fl_Box.H>
36 #include /**/ <FL/math.h>
37 #include /**/ <FL/gl.h>
38 #include /**/ <FL/Fl_Gl_Window.H>
40 class Test_Window
: public Fl_Gl_Window
44 Test_Window (int x
, int y
, int w
, int h
,
52 /// from the Fl_Gl_Window...
58 Test_Window::Test_Window (int x
, int y
,
61 : Fl_Gl_Window (x
, y
, w
, h
, l
),
67 Test_Window::sides () const
73 Test_Window::sides (int s
)
80 Test_Window::incr_sides ()
83 if (this->sides_
> 10)
91 // the valid() property may be used to avoid reinitializing your
92 // GL transformation for each redraw:
97 glViewport (0, 0, this->w (), this->h ());
99 // draw an amazing but slow graphic:
100 glClear (GL_COLOR_BUFFER_BIT
);
102 glBegin (GL_POLYGON
);
103 int s
= this->sides_
;
105 for (int i
= 0; i
!= s
; ++i
)
107 double ang
= i
* 2 * M_PI
/ s
;
108 glColor3f (float (i
) / s
,
111 glVertex3f (cos (ang
), sin (ang
), 0);
116 // when you change the data, as in this callback, you must call redraw ():
117 void sides_cb (Fl_Widget
*o
, void *p
)
120 reinterpret_cast<Test_Window
*> (p
);
122 dynamic_cast<Fl_Slider
*> (o
);
123 tw
->sides (static_cast<int> (slider
->value ()));
126 class Connection_Handler
: public ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>
129 Connection_Handler (Test_Window
*w
= 0,
132 //FUZZ: disable check_for_lack_ACE_OS
133 virtual int open (void *);
134 //FUZZ: enble check_for_lack_ACE_OS
136 virtual int handle_input (ACE_HANDLE
);
143 class Acceptor
: public ACE_Acceptor
<Connection_Handler
,ACE_SOCK_ACCEPTOR
>
146 Acceptor (Test_Window
*w
= 0,
149 virtual int make_svc_handler (Connection_Handler
*&sh
);
156 Connection_Handler::Connection_Handler (Test_Window
*w
,
164 Connection_Handler::open (void*)
170 this->peer ().get_remote_addr (from
);
171 const int bufsiz
= 128;
172 ACE_TCHAR buf
[bufsiz
];
174 from
.addr_to_string (buf
, bufsiz
, 0);
176 static char msg
[256];
177 ACE_OS::snprintf (msg
, 256, "connection from <%s>\n",
178 ACE_TEXT_ALWAYS_CHAR (buf
));
180 this->box_
->label (msg
);
181 this->box_
->redraw ();
186 this->w_
->incr_sides ();
189 return this->peer ().enable (ACE_NONBLOCK
);
193 Connection_Handler::handle_input (ACE_HANDLE
)
196 if (this->peer ().recv (buf
, BUFSIZ
) <= 0)
201 Acceptor::Acceptor (Test_Window
*w
, Fl_Box
*box
)
208 Acceptor::make_svc_handler (Connection_Handler
*&sh
)
212 ACE_NEW_RETURN (sh
, Connection_Handler (this->w_
, this->box_
), -1);
213 sh
->reactor (this->reactor());
218 int run_main (int argc
, ACE_TCHAR
*argv
[])
220 ACE_START_TEST (ACE_TEXT ("FlReactor_Test"));
222 Fl_Window
window (300, 370);
224 Test_Window
tw (10, 75, window
.w () - 20, window
.h ()-90);
225 window
.resizable (&tw
);
227 Fl_Hor_Slider
slider (60, 5, window
.w () - 70, 30, "Sides:");
228 slider
.align (FL_ALIGN_LEFT
);
229 slider
.callback (sides_cb
, &tw
);
230 slider
.value (tw
.sides ());
232 slider
.bounds (3, 10);
234 ACE_FlReactor reactor
;
235 ACE_Reactor
r (&reactor
);
237 Fl_Box
*box
= new Fl_Box (FL_UP_BOX
, 10, 40,
238 window
.w () - 20, 30,
240 box
->labelfont (FL_BOLD
);
242 Acceptor
acceptor (&tw
, box
);
244 ACE_INET_Addr address
;
246 if (acceptor
.open (address
, &r
) == -1)
247 ACE_ERROR_RETURN ((LM_ERROR
,
252 acceptor
.acceptor ().get_local_addr (address
);
254 const int bufsiz
= 128;
255 ACE_TCHAR buf
[bufsiz
];
257 address
.addr_to_string (buf
, bufsiz
, 0);
259 char msg
[2 * bufsiz
];
260 ACE_OS::snprintf (msg
, 2 * bufsiz
, "Listening on <%s>\n",
261 ACE_TEXT_ALWAYS_CHAR (buf
));
268 ACE_Argv_Type_Converter
ct (argc
, argv
);
270 window
.show (argc
, ct
.get_ASCII_argv ());
273 int const retval
= Fl::run ();