Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / tests / XtAthenaReactor_Test.cpp
blob49941d2e895a0a8ebdc6a26f54042936ec5feaa9
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file XtAthenaReactor_Test.cpp
7 * This is a simple test that illustrates the possibility to integrate
8 * ACE to the X Main Loop. This program uses ACE_XtReactor class to
9 * schedule three additional event sources:
10 * 1. Events from button "Stop Test" (registed with XtAddCallback)
11 * 2. Events from button "Press Me" (registed with XtAddCallback)
12 * 3. Events from X timer (registed with XtAppAddTimeOut)
13 * 4. Events from ACE timer (registed with ACE_XtReactor::schedule_timer)
14 * 5. Events from the TCP/IP channel using ACE_Acceptor
15 * No command line arguments are needed to run the test.
16 * Programs needs Athena Widgets to be compiled and run.
18 * @author Kirill Rybaltchenko <Kirill.Rybaltchenko@cern.ch>
20 //=============================================================================
23 #include "test_config.h"
24 #include "ace/XtReactor/XtReactor.h"
25 #include "ace/Event_Handler.h"
26 #include "ace/Acceptor.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 "ace/OS_NS_unistd.h"
34 #include /**/ <X11/Intrinsic.h>
35 #include /**/ <X11/Xatom.h>
36 #include /**/ <X11/Shell.h>
37 #include /**/ <X11/StringDefs.h>
39 #if defined (ACE_HAS_ATHENA3D)
40 # include /**/ <X11/Xaw3d/Command.h>
41 # include /**/ <X11/Xaw3d/Label.h>
42 # include /**/ <X11/Xaw3d/Box.h>
43 #else
44 # include /**/ <X11/Xaw/Command.h>
45 # include /**/ <X11/Xaw/Label.h>
46 # include /**/ <X11/Xaw/Box.h>
47 #endif
49 static void set_label(Widget w, const char *p)
51 XtVaSetValues (w, XtNlabel, p, static_cast<void *>(0));
53 #define LABEL_WIDGET labelWidgetClass
54 #define BUTTON_WIDGET commandWidgetClass
55 #define PRESS_ME_CALLBACK XtNcallback
56 static Widget create_box(Widget parent, const char * name)
58 return XtCreateWidget( (char*) name, boxWidgetClass, parent, 0, 0);
61 // Port we listen on.
62 static const u_short SERV_TCP_PORT = 6670;
64 // counter for events from "Press Me" button.
65 static int count1 = 0;
67 // counter for events from X Timer.
68 static int count2 = 0;
70 // counter for events from ACE Timer.
71 static int count3 = 0;
73 // Callback for "Stop Test" buton - quit the program.
74 void
75 Quit (Widget, XtPointer, XtPointer)
77 ACE_OS::exit (0);
80 static void *
81 client (void *)
83 char buf[100];
84 size_t mes_len;
85 ACE_OS::sleep (1);
87 ACE_DEBUG ((LM_DEBUG,
88 " (%P) Client: Starting...\n"));
90 ACE_SOCK_Stream stream;
91 ACE_SOCK_Connector connector;
92 ACE_OS::snprintf (buf, 100, "Client: the life was good!");
94 mes_len = (int) htonl (ACE_OS::strlen (buf) + 1);
96 if (connector.connect (stream,
97 ACE_INET_Addr (SERV_TCP_PORT,
98 ACE_DEFAULT_SERVER_HOST)) == -1)
99 ACE_ERROR ((LM_ERROR,
100 "(%P) %p\n",
101 "Socket open"));
103 if (stream.send (4,
104 (void *) &mes_len,
105 sizeof (size_t),
106 (void *)buf,
107 ACE_OS::strlen (buf) + 1) == -1)
109 ACE_ERROR ((LM_ERROR,
110 "(%P) %p\n",
111 "Socket send"));
113 if (stream.close () == -1)
114 ACE_ERROR ((LM_ERROR,
115 "(%P) %p\n",
116 "Socket close"));
118 ACE_DEBUG ((LM_DEBUG,
119 "(%P) Client: Message has been sent, about to exit...\n"));
120 return 0;
123 // Callback for "Press Me" button.
125 static void
126 inc_count (Widget, XtPointer client_data, XtPointer)
128 char new_string[80];
130 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
131 count1++, count2, count3);
133 set_label((Widget) client_data, new_string);
136 // Callback for X Timer.
138 static void
139 inc_tmo (void *w,XtIntervalId *)
141 char new_string[80];
143 if (count2 > 10)
144 ACE_OS::exit (0);
145 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
146 count1, count2++, count3);
148 set_label((Widget) w, new_string);
150 (void) XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
151 1000,
152 inc_tmo,
153 (Widget) w);
156 class EV_handler : public ACE_Event_Handler
158 public:
159 virtual int handle_timeout (const ACE_Time_Value &,
160 const void *arg)
162 char new_string[80];
163 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
164 count1, count2, count3++);
165 set_label((Widget) arg, new_string);
166 return 0;
170 class Connection_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
172 public:
173 //FUZZ: disable check_for_lack_ACE_OS
174 virtual int open (void *)
176 //FUZZ: enable check_for_lack_ACE_OS
177 char buf[100];
178 int head;
179 ssize_t ret = this->peer ().recv_n ((char *) &head,
180 sizeof (int));
181 if (ret != sizeof (int))
182 ACE_ERROR_RETURN ((LM_ERROR,
183 "(%P) %p\n",
184 "read header"),
185 -1);
187 ret = this->peer ().recv_n (buf,
188 (int) ntohl (head));
190 if (ret != (int) ntohl (head))
191 ACE_ERROR_RETURN ((LM_ERROR,
192 "(%P) %p\n",
193 "read message"),
194 -1);
195 ACE_DEBUG ((LM_DEBUG,
196 " (%P)Server (ACE_SOCKET channel message): [%s]\n",
197 buf));
198 return 0;
202 #if defined (HummingBird_X)
203 extern "C" void HCLXmInit ();
204 #endif /* HummingBird_X */
207 run_main (int argc, ACE_TCHAR *argv[])
209 ACE_START_TEST (ACE_TEXT ("XtAthenaReactor_Test"));
211 XtAppContext app_context;
212 Widget topLevel, goodbye, PressMe, lbl, digits_rc;
213 Widget children[5];
215 #if defined (HummingBird_X)
216 HCLXmInit ();
217 #endif /* HummingBird_X */
218 topLevel = XtVaAppInitialize (&app_context,
219 "XTReactor_Test",
222 &argc,
223 argv,
225 static_cast<void *>(0));
227 digits_rc = create_box(topLevel, "digits_rc");
229 //"Stop Test" button.
230 goodbye = XtCreateWidget ( (char *) "goodbye",
231 BUTTON_WIDGET,
232 digits_rc,
235 set_label(goodbye, "Stop Test");
237 //"Press Me" button
238 PressMe = XtCreateWidget ((char *) "PressMe",
239 BUTTON_WIDGET,
240 digits_rc,
244 //Display for event counter
245 lbl = XtCreateWidget ((char *) "label_for_event_one",
246 LABEL_WIDGET,
247 digits_rc,
250 set_label(lbl, "label_for_all_events");
251 int ac = 0;
252 children[ac++] = goodbye;
253 children[ac++] = PressMe;
254 children[ac++] = lbl;
255 XtManageChildren (children, ac);
256 XtManageChild (digits_rc);
258 //Register callback for "Stop Test" button
259 XtAddCallback (goodbye, PRESS_ME_CALLBACK, Quit, 0);
261 //Register callback for "Press Me" button
262 XtAddCallback (PressMe,
263 PRESS_ME_CALLBACK,
264 inc_count,
265 (XtPointer) lbl);
267 // Register callback for X Timer
268 (void) XtAppAddTimeOut (app_context,
269 1000,
270 inc_tmo,
271 (XtPointer) lbl);
273 XtRealizeWidget (topLevel);
275 // It will perform X Main Loop
276 ACE_XtReactor reactor (app_context);
278 ACE_Reactor r (&reactor);
280 //Event Handler for ACE Timer.
281 EV_handler evh;
283 ACE_Acceptor <Connection_Handler, ACE_SOCK_ACCEPTOR> acceptor;
285 if (acceptor.open (ACE_INET_Addr ((u_short) SERV_TCP_PORT),
286 &r) == -1)
287 ACE_ERROR_RETURN ((LM_ERROR,
288 "%p\n",
289 "open"),
290 -1);
292 if (reactor.schedule_timer (&evh,
293 (const void *) lbl,
294 ACE_Time_Value (2),
295 ACE_Time_Value (2))==-1)
296 ACE_ERROR_RETURN ((LM_ERROR,
297 " (%P|%t) can't register with reactor\n"),
298 -1);
300 ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) client,
302 THR_NEW_LWP | THR_DETACHED);
304 XtAppMainLoop (XtWidgetToApplicationContext (topLevel));
306 ACE_END_TEST;
307 return 0;