Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / FlReactor_Test.cpp
blob06df42125ff3a886cd63b20743f978c0fec506b1
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
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
12 * polygons too.
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
42 public:
43 /// Constructor
44 Test_Window (int x, int y, int w, int h,
45 const char * l = 0);
47 int sides () const;
48 void sides (int s);
49 void incr_sides ();
51 private:
52 /// from the Fl_Gl_Window...
53 virtual void draw ();
55 int sides_;
58 Test_Window::Test_Window (int x, int y,
59 int w, int h,
60 const char* l)
61 : Fl_Gl_Window (x, y, w, h, l),
62 sides_ (3)
66 int
67 Test_Window::sides () const
69 return this->sides_;
72 void
73 Test_Window::sides (int s)
75 this->sides_ = s;
76 this->redraw ();
79 void
80 Test_Window::incr_sides ()
82 this->sides_++;
83 if (this->sides_ > 10)
84 this->sides_ = 3;
85 this->redraw ();
88 void
89 Test_Window::draw ()
91 // the valid() property may be used to avoid reinitializing your
92 // GL transformation for each redraw:
93 if (!this->valid ())
95 this->valid (1);
96 glLoadIdentity ();
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,
109 float (i) / s,
110 float (i) / s);
111 glVertex3f (cos (ang), sin (ang), 0);
113 glEnd ();
116 // when you change the data, as in this callback, you must call redraw ():
117 void sides_cb (Fl_Widget *o, void *p)
119 Test_Window *tw =
120 reinterpret_cast<Test_Window *> (p);
121 Fl_Slider *slider =
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>
128 public:
129 Connection_Handler (Test_Window *w = 0,
130 Fl_Box* box = 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);
138 private:
139 Test_Window *w_;
140 Fl_Box *box_;
143 class Acceptor : public ACE_Acceptor<Connection_Handler,ACE_SOCK_ACCEPTOR>
145 public:
146 Acceptor (Test_Window *w = 0,
147 Fl_Box *box = 0);
149 virtual int make_svc_handler (Connection_Handler *&sh);
151 private:
152 Test_Window* w_;
153 Fl_Box *box_;
156 Connection_Handler::Connection_Handler (Test_Window *w,
157 Fl_Box *box)
158 : w_ (w),
159 box_ (box)
164 Connection_Handler::open (void*)
166 if (this->box_ != 0)
168 ACE_INET_Addr from;
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 ();
184 if (this->w_ != 0)
186 this->w_->incr_sides ();
189 return this->peer ().enable (ACE_NONBLOCK);
193 Connection_Handler::handle_input (ACE_HANDLE)
195 char buf[BUFSIZ];
196 if (this->peer ().recv (buf, BUFSIZ) <= 0)
197 return -1;
198 return 0;
201 Acceptor::Acceptor (Test_Window *w, Fl_Box *box)
202 : w_ (w),
203 box_ (box)
208 Acceptor::make_svc_handler (Connection_Handler *&sh)
210 if (sh == 0)
212 ACE_NEW_RETURN (sh, Connection_Handler (this->w_, this->box_), -1);
213 sh->reactor (this->reactor());
215 return 0;
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 ());
231 slider.step (1);
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,
239 "Setting up");
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,
248 "%p\n",
249 "open acceptor"),
250 -1);
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));
263 box->label (msg);
264 box->redraw ();
266 window.end ();
268 ACE_Argv_Type_Converter ct (argc, argv);
270 window.show (argc, ct.get_ASCII_argv ());
271 tw.show ();
273 int const retval = Fl::run ();
275 ACE_END_TEST;
277 return retval;