ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / FlReactor_Test.cpp
blob834345bb9636e667394cc50194180b9cff688d1a
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"
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
43 public:
44 /// Constructor
45 Test_Window (int x, int y, int w, int h,
46 const char * l = 0);
48 int sides () const;
49 void sides (int s);
50 void incr_sides (void);
52 private:
53 /// from the Fl_Gl_Window...
54 virtual void draw (void);
56 int sides_;
59 Test_Window::Test_Window (int x, int y,
60 int w, int h,
61 const char* l)
62 : Fl_Gl_Window (x, y, w, h, l),
63 sides_ (3)
67 int
68 Test_Window::sides () const
70 return this->sides_;
73 void
74 Test_Window::sides (int s)
76 this->sides_ = s;
77 this->redraw ();
80 void
81 Test_Window::incr_sides (void)
83 this->sides_++;
84 if (this->sides_ > 10)
85 this->sides_ = 3;
86 this->redraw ();
89 void
90 Test_Window::draw (void)
92 // the valid() property may be used to avoid reinitializing your
93 // GL transformation for each redraw:
94 if (!this->valid ())
96 this->valid (1);
97 glLoadIdentity ();
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,
110 float (i) / s,
111 float (i) / s);
112 glVertex3f (cos (ang), sin (ang), 0);
114 glEnd ();
117 // when you change the data, as in this callback, you must call redraw ():
118 void sides_cb (Fl_Widget *o, void *p)
120 Test_Window *tw =
121 reinterpret_cast<Test_Window *> (p);
122 Fl_Slider *slider =
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>
129 public:
130 Connection_Handler (Test_Window *w = 0,
131 Fl_Box* box = 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);
139 private:
140 Test_Window *w_;
141 Fl_Box *box_;
144 class Acceptor : public ACE_Acceptor<Connection_Handler,ACE_SOCK_ACCEPTOR>
146 public:
147 Acceptor (Test_Window *w = 0,
148 Fl_Box *box = 0);
150 virtual int make_svc_handler (Connection_Handler *&sh);
152 private:
153 Test_Window* w_;
154 Fl_Box *box_;
157 Connection_Handler::Connection_Handler (Test_Window *w,
158 Fl_Box *box)
159 : w_ (w),
160 box_ (box)
165 Connection_Handler::open (void*)
167 if (this->box_ != 0)
169 ACE_INET_Addr from;
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 ();
185 if (this->w_ != 0)
187 this->w_->incr_sides ();
190 return this->peer ().enable (ACE_NONBLOCK);
194 Connection_Handler::handle_input (ACE_HANDLE)
196 char buf[BUFSIZ];
197 if (this->peer ().recv (buf, BUFSIZ) <= 0)
198 return -1;
199 return 0;
202 Acceptor::Acceptor (Test_Window *w, Fl_Box *box)
203 : w_ (w),
204 box_ (box)
209 Acceptor::make_svc_handler (Connection_Handler *&sh)
211 if (sh == 0)
213 ACE_NEW_RETURN (sh, Connection_Handler (this->w_, this->box_), -1);
214 sh->reactor (this->reactor());
216 return 0;
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 ());
232 slider.step (1);
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,
240 "Setting up");
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,
249 "%p\n",
250 "open acceptor"),
251 -1);
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));
264 box->label (msg);
265 box->redraw ();
267 window.end ();
269 ACE_Argv_Type_Converter ct (argc, argv);
271 window.show (argc, ct.get_ASCII_argv ());
272 tw.show ();
274 int const retval = Fl::run ();
276 ACE_END_TEST;
278 return retval;