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"
24 #include "ace/FlReactor/FlReactor.h"
25 #include "ace/Event_Handler.h"
26 #include "ace/Acceptor.h"
27 #include "ace/Argv_Type_Converter.h"
28 #include "ace/SOCK_Acceptor.h"
29 #include "ace/SOCK_Connector.h"
30 #include "ace/Service_Config.h"
31 #include "ace/Thread_Manager.h"
33 #include /**/ <FL/Fl.H>
34 #include /**/ <FL/Fl_Window.H>
35 #include /**/ <FL/Fl_Hor_Slider.H>
36 #include /**/ <FL/Fl_Box.H>
37 #include /**/ <FL/math.h>
38 #include /**/ <FL/gl.h>
39 #include /**/ <FL/Fl_Gl_Window.H>
41 class Test_Window
: public Fl_Gl_Window
45 Test_Window (int x
, int y
, int w
, int h
,
50 void incr_sides (void);
53 /// from the Fl_Gl_Window...
54 virtual void draw (void);
59 Test_Window::Test_Window (int x
, int y
,
62 : Fl_Gl_Window (x
, y
, w
, h
, l
),
68 Test_Window::sides () const
74 Test_Window::sides (int s
)
81 Test_Window::incr_sides (void)
84 if (this->sides_
> 10)
90 Test_Window::draw (void)
92 // the valid() property may be used to avoid reinitializing your
93 // GL transformation for each redraw:
98 glViewport (0, 0, this->w (), this->h ());
100 // draw an amazing but slow graphic:
101 glClear (GL_COLOR_BUFFER_BIT
);
103 glBegin (GL_POLYGON
);
104 int s
= this->sides_
;
106 for (int i
= 0; i
!= s
; ++i
)
108 double ang
= i
* 2 * M_PI
/ s
;
109 glColor3f (float (i
) / s
,
112 glVertex3f (cos (ang
), sin (ang
), 0);
117 // when you change the data, as in this callback, you must call redraw ():
118 void sides_cb (Fl_Widget
*o
, void *p
)
121 reinterpret_cast<Test_Window
*> (p
);
123 dynamic_cast<Fl_Slider
*> (o
);
124 tw
->sides (static_cast<int> (slider
->value ()));
127 class Connection_Handler
: public ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
>
130 Connection_Handler (Test_Window
*w
= 0,
133 //FUZZ: disable check_for_lack_ACE_OS
134 virtual int open (void *);
135 //FUZZ: enble check_for_lack_ACE_OS
137 virtual int handle_input (ACE_HANDLE
);
144 class Acceptor
: public ACE_Acceptor
<Connection_Handler
,ACE_SOCK_ACCEPTOR
>
147 Acceptor (Test_Window
*w
= 0,
150 virtual int make_svc_handler (Connection_Handler
*&sh
);
157 Connection_Handler::Connection_Handler (Test_Window
*w
,
165 Connection_Handler::open (void*)
171 this->peer ().get_remote_addr (from
);
172 const int bufsiz
= 128;
173 ACE_TCHAR buf
[bufsiz
];
175 from
.addr_to_string (buf
, bufsiz
, 0);
177 static char msg
[256];
178 ACE_OS::snprintf (msg
, 256, "connection from <%s>\n",
179 ACE_TEXT_ALWAYS_CHAR (buf
));
181 this->box_
->label (msg
);
182 this->box_
->redraw ();
187 this->w_
->incr_sides ();
190 return this->peer ().enable (ACE_NONBLOCK
);
194 Connection_Handler::handle_input (ACE_HANDLE
)
197 if (this->peer ().recv (buf
, BUFSIZ
) <= 0)
202 Acceptor::Acceptor (Test_Window
*w
, Fl_Box
*box
)
209 Acceptor::make_svc_handler (Connection_Handler
*&sh
)
213 ACE_NEW_RETURN (sh
, Connection_Handler (this->w_
, this->box_
), -1);
214 sh
->reactor (this->reactor());
219 int run_main (int argc
, ACE_TCHAR
*argv
[])
221 ACE_START_TEST (ACE_TEXT ("FlReactor_Test"));
223 Fl_Window
window (300, 370);
225 Test_Window
tw (10, 75, window
.w () - 20, window
.h ()-90);
226 window
.resizable (&tw
);
228 Fl_Hor_Slider
slider (60, 5, window
.w () - 70, 30, "Sides:");
229 slider
.align (FL_ALIGN_LEFT
);
230 slider
.callback (sides_cb
, &tw
);
231 slider
.value (tw
.sides ());
233 slider
.bounds (3, 10);
235 ACE_FlReactor reactor
;
236 ACE_Reactor
r (&reactor
);
238 Fl_Box
*box
= new Fl_Box (FL_UP_BOX
, 10, 40,
239 window
.w () - 20, 30,
241 box
->labelfont (FL_BOLD
);
243 Acceptor
acceptor (&tw
, box
);
245 ACE_INET_Addr address
;
247 if (acceptor
.open (address
, &r
) == -1)
248 ACE_ERROR_RETURN ((LM_ERROR
,
253 acceptor
.acceptor ().get_local_addr (address
);
255 const int bufsiz
= 128;
256 ACE_TCHAR buf
[bufsiz
];
258 address
.addr_to_string (buf
, bufsiz
, 0);
260 char msg
[2 * bufsiz
];
261 ACE_OS::snprintf (msg
, 2 * bufsiz
, "Listening on <%s>\n",
262 ACE_TEXT_ALWAYS_CHAR (buf
));
269 ACE_Argv_Type_Converter
ct (argc
, argv
);
271 window
.show (argc
, ct
.get_ASCII_argv ());
274 int const retval
= Fl::run ();