Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / XtMotifReactor_Test.cpp
blob4cc1ef9436f2a25bc7a3a40c02c8042ca75cd867
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file XtMotifReactor_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 * The program needs Motif or lestiff to be compiled and run.
18 * @author Kirill Rybaltchenko <Kirill.Rybaltchenko@cern.ch>
20 //=============================================================================
23 #include "test_config.h"
25 #include "ace/XtReactor/XtReactor.h"
26 #include "ace/Event_Handler.h"
27 #include "ace/Acceptor.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 "ace/OS_NS_unistd.h"
35 #include /**/ <X11/Intrinsic.h>
36 #include /**/ <X11/Xatom.h>
37 #include /**/ <X11/Shell.h>
39 #include /**/ <Xm/Xm.h>
40 #include /**/ <Xm/Label.h>
41 #include /**/ <Xm/PushB.h>
42 #include /**/ <Xm/RowColumn.h>
44 static void set_label(Widget w, const char *p)
46 XtVaSetValues (w,
47 XmNlabelString,
48 XmStringCreateLocalized( (char*) p),
49 static_cast<void *>(0));
51 #define LABEL_WIDGET xmLabelWidgetClass
52 #define BUTTON_WIDGET xmPushButtonWidgetClass
53 #define PRESS_ME_CALLBACK XmNactivateCallback
54 static Widget create_box(Widget parent, const char *name)
56 Arg al[10];
57 int ac = 0;
58 XtSetArg (al[ac], XmNnumColumns, 3); ac++;
59 XtSetArg (al[ac], XmNpacking, XmPACK_COLUMN); ac++;
60 XtSetArg (al[ac], XmNentryAlignment, XmALIGNMENT_CENTER); ac++;
61 return XmCreateRowColumn (parent, (char *) name, al, ac);
64 // Port we listen on.
65 static const u_short SERV_TCP_PORT = 6670;
67 // counter for events from "Press Me" button.
68 static int count1 = 0;
70 // counter for events from X Timer.
71 static int count2 = 0;
73 // counter for events from ACE Timer.
74 static int count3 = 0;
76 // Callback for "Stop Test" buton - quit the program.
77 void
78 Quit (Widget, XtPointer, XtPointer)
80 ACE_OS::exit (0);
83 static void *
84 client (void *)
86 char buf[100];
87 size_t mes_len;
88 ACE_OS::sleep (1);
90 ACE_DEBUG ((LM_DEBUG,
91 " (%P) Client: Starting...\n"));
93 ACE_SOCK_Stream stream;
94 ACE_SOCK_Connector connector;
95 ACE_OS::snprintf (buf, 100, "Client: the life was good!");
97 mes_len = (int) htonl (ACE_OS::strlen (buf) + 1);
99 if (connector.connect (stream,
100 ACE_INET_Addr (SERV_TCP_PORT,
101 ACE_DEFAULT_SERVER_HOST)) == -1)
102 ACE_ERROR ((LM_ERROR,
103 "(%P) %p\n",
104 "Socket open"));
106 if (stream.send (4,
107 (void *) &mes_len,
108 sizeof (size_t),
109 (void *)buf,
110 ACE_OS::strlen (buf) + 1) == -1)
112 ACE_ERROR ((LM_ERROR,
113 "(%P) %p\n",
114 "Socket send"));
116 if (stream.close () == -1)
117 ACE_ERROR ((LM_ERROR,
118 "(%P) %p\n",
119 "Socket close"));
121 ACE_DEBUG ((LM_DEBUG,
122 "(%P) Client: Message has been sent, about to exit...\n"));
123 return 0;
126 // Callback for "Press Me" button.
128 static void
129 inc_count (Widget, XtPointer client_data, XtPointer)
131 char new_string[80];
133 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
134 count1++, count2, count3);
136 set_label((Widget) client_data, new_string);
139 // Callback for X Timer.
141 static void
142 inc_tmo (void *w, XtIntervalId *)
144 char new_string[80];
146 if (count2 > 10)
147 ACE_OS::exit (0);
148 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
149 count1, count2++, count3);
151 set_label((Widget) w, new_string);
153 (void) XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
154 1000,
155 inc_tmo,
156 (Widget) w);
159 class EV_handler : public ACE_Event_Handler
161 public:
162 virtual int handle_timeout (const ACE_Time_Value &,
163 const void *arg)
165 char new_string[80];
166 ACE_OS::snprintf (new_string, 80, "Events: [%d] [%d] [%d]",
167 count1, count2, count3++);
168 set_label((Widget) arg, new_string);
169 return 0;
173 class Connection_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
175 public:
176 //FUZZ: disable check_for_lack_ACE_OS
177 virtual int open (void *)
179 //FUZZ: enable check_for_lack_ACE_OS
180 char buf[100];
181 int head;
182 ssize_t ret = this->peer ().recv_n ((char *) &head,
183 sizeof (int));
184 if (ret != sizeof (int))
185 ACE_ERROR_RETURN ((LM_ERROR,
186 "(%P) %p\n",
187 "read header"),
188 -1);
190 ret = this->peer ().recv_n (buf,
191 (int) ntohl (head));
193 if (ret != (int) ntohl (head))
194 ACE_ERROR_RETURN ((LM_ERROR,
195 "(%P) %p\n",
196 "read message"),
197 -1);
198 ACE_DEBUG ((LM_DEBUG,
199 " (%P)Server (ACE_SOCKET channel message): [%s]\n",
200 buf));
201 return 0;
205 #if defined (HummingBird_X)
206 extern "C" void HCLXmInit ();
207 #endif /* HummingBird_X */
210 run_main (int argc, ACE_TCHAR *argv[])
212 ACE_START_TEST (ACE_TEXT ("XtMotifReactor_Test"));
214 XtAppContext app_context;
215 Widget topLevel, goodbye, PressMe, lbl, digits_rc;
216 Widget children[5];
218 #if defined (HummingBird_X)
219 HCLXmInit ();
220 #endif /* HummingBird_X */
221 topLevel = XtVaAppInitialize (&app_context,
222 "XTReactor_Test",
225 &argc,
226 argv,
228 static_cast<void *>(0));
230 digits_rc = create_box(topLevel, "digits_rc");
232 //"Stop Test" button.
233 goodbye = XtCreateWidget ( (char *) "goodbye",
234 BUTTON_WIDGET,
235 digits_rc,
238 set_label(goodbye, "Stop Test");
240 //"Press Me" button
241 PressMe = XtCreateWidget ((char *) "PressMe",
242 BUTTON_WIDGET,
243 digits_rc,
247 //Display for event counter
248 lbl = XtCreateWidget ((char *) "label_for_event_one",
249 LABEL_WIDGET,
250 digits_rc,
253 set_label(lbl, "label_for_all_events");
254 int ac = 0;
255 children[ac++] = goodbye;
256 children[ac++] = PressMe;
257 children[ac++] = lbl;
258 XtManageChildren (children, ac);
259 XtManageChild (digits_rc);
261 //Register callback for "Stop Test" button
262 XtAddCallback (goodbye, PRESS_ME_CALLBACK, Quit, 0);
264 //Register callback for "Press Me" button
265 XtAddCallback (PressMe,
266 PRESS_ME_CALLBACK,
267 inc_count,
268 (XtPointer) lbl);
270 // Register callback for X Timer
271 (void) XtAppAddTimeOut (app_context,
272 1000,
273 inc_tmo,
274 (XtPointer) lbl);
276 XtRealizeWidget (topLevel);
278 // It will perform X Main Loop
279 ACE_XtReactor reactor (app_context);
281 ACE_Reactor r (&reactor);
283 //Event Handler for ACE Timer.
284 EV_handler evh;
286 ACE_Acceptor <Connection_Handler, ACE_SOCK_ACCEPTOR> acceptor;
288 if (acceptor.open (ACE_INET_Addr ((u_short) SERV_TCP_PORT),
289 &r) == -1)
290 ACE_ERROR_RETURN ((LM_ERROR,
291 "%p\n",
292 "open"),
293 -1);
295 if (reactor.schedule_timer (&evh,
296 (const void *) lbl,
297 ACE_Time_Value (2),
298 ACE_Time_Value (2))==-1)
299 ACE_ERROR_RETURN ((LM_ERROR,
300 " (%P|%t) can't register with reactor\n"),
301 -1);
303 ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) client,
305 THR_NEW_LWP | THR_DETACHED);
307 XtAppMainLoop (XtWidgetToApplicationContext (topLevel));
309 ACE_END_TEST;
310 return 0;