summaryrefslogtreecommitdiff
path: root/ACE/tests/XtAthenaReactor_Test.cpp
blob: 3bba4a52a6cb4bbac2c52116d301266321ddb4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    XtReactor_Test.cpp
//
// = DESCRIPTION
//      This is a simple test that illustrates the possibility to integrate
//      ACE to the X Main Loop. This program uses ACE_XtReactor class to
//      schedule three additional event sources:
//      1. Events from button "Stop Test" (registed with XtAddCallback)
//      2. Events from button "Press Me" (registed with XtAddCallback)
//      3. Events from X timer (registed with XtAppAddTimeOut)
//      4. Events from ACE timer (registed with ACE_XtReactor::schedule_timer)
//      5. Events from the TCP/IP channel using ACE_Acceptor
//      No command line arguments are needed to run the test.
//      Programs needs Athena Widgets to be compiled and run.
//
// = AUTHOR
//    Kirill Rybaltchenko <Kirill.Rybaltchenko@cern.ch>
//
// ============================================================================

#include "test_config.h"

ACE_RCSID (tests,
           XtAthenaReactor_Test,
           "$Id$")

#include "ace/XtReactor/XtReactor.h"
#include "ace/Event_Handler.h"
#include "ace/Acceptor.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/Service_Config.h"
#include "ace/Thread_Manager.h"

#include "ace/OS_NS_unistd.h"

#include /**/ <X11/Intrinsic.h>
#include /**/ <X11/Xatom.h>
#include /**/ <X11/Shell.h>

#include /**/ <X11/Xaw/Command.h>
#include /**/ <X11/Xaw/Label.h>
#include /**/ <X11/Xaw/Box.h>
#include /**/ <X11/StringDefs.h>

static void set_label(Widget w, const char *p)
{
    XtVaSetValues (w, XtNlabel, p, 0);
}
#define LABEL_WIDGET labelWidgetClass
#define BUTTON_WIDGET commandWidgetClass
#define PRESS_ME_CALLBACK XtNcallback
static Widget create_box(Widget parent, const char * name)
{
    return XtCreateWidget( (char*) name, boxWidgetClass, parent, 0, 0);
}

// Port we listen on.
static const u_short SERV_TCP_PORT = 6670;

// counter for events from "Press Me" button.
static int count1 = 0;

// counter for events from X Timer.
static int count2 = 0;

// counter for events from ACE Timer.
static int count3 = 0;

// Callback for "Stop Test" buton - quit the program.
void
Quit (Widget, XtPointer, XtPointer)
{
  ACE_OS::exit (0);
}

static void *
client (void *)
{
  char buf[100];
  size_t mes_len;
  ACE_OS::sleep (1);

  ACE_DEBUG ((LM_DEBUG,
              " (%P) Client: Starting...\n"));

  ACE_SOCK_Stream stream;
  ACE_SOCK_Connector connector;
  ACE_OS::sprintf (buf, "Client: the life was good!");

  mes_len = (int) htonl (ACE_OS::strlen (buf) + 1);

  if (connector.connect (stream,
                         ACE_INET_Addr (SERV_TCP_PORT,
                                        ACE_DEFAULT_SERVER_HOST)) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket open"));

  if (stream.send (4,
                   (void *) &mes_len,
                   sizeof (size_t),
                   (void *)buf,
                   ACE_OS::strlen (buf) + 1) == -1)

    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket send"));

  if (stream.close () == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket close"));

  ACE_DEBUG ((LM_DEBUG,
              "(%P) Client: Message has been sent, about to exit...\n"));
  return 0;
}

// Callback for "Press Me" button.

static void
inc_count (Widget, XtPointer client_data, XtPointer)
{
  char new_string[80];

  ACE_OS::sprintf (new_string,
                   "Events: [%d] [%d] [%d]",
                   count1++,
                   count2,
                   count3);
  set_label((Widget) client_data, new_string);
}

// Callback for X Timer.

static void
inc_tmo (void *w,XtIntervalId *)
{
  char new_string[80];

  if (count2 > 10)
    ACE_OS::exit (0);
  ACE_OS::sprintf (new_string,
                   "Events: [%d] [%d] [%d]",
                   count1,
                   count2++,
                   count3);

  set_label((Widget) w, new_string);

  (void) XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
                          1000,
                          inc_tmo,
                          (Widget) w);
}

class EV_handler : public ACE_Event_Handler
{
public:
  virtual int handle_timeout (const ACE_Time_Value &,
                              const void *arg)
  {
    char new_string[80];
    ACE_OS::sprintf (new_string,
                     "Events: [%d] [%d] [%d]",
                     count1,
                     count2,
                     count3++);
    set_label((Widget) arg, new_string);
    return 0;
  }
};

class Connection_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
public:
  //FUZZ: disable check_for_lack_ACE_OS
  virtual int open (void *)
  {
  //FUZZ: enable check_for_lack_ACE_OS
    char buf[100];
    int head;
    ssize_t ret = this->peer ().recv_n ((char *) &head,
                                        sizeof (int));
    if (ret != sizeof (int))
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P) %p\n",
                         "read header"),
                        -1);

    ret = this->peer ().recv_n (buf,
                                (int) ntohl (head));

    if (ret != (int) ntohl (head))
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P) %p\n",
                         "read message"),
                        -1);
    ACE_DEBUG ((LM_DEBUG,
                " (%P)Server (ACE_SOCKET channel message): [%s]\n",
                buf));
    return 0;
  }
};

#if defined (HummingBird_X)
extern "C" void HCLXmInit (void);
#endif /* HummingBird_X */

int
run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("XtAthenaReactor_Test"));

  XtAppContext app_context;
  Widget topLevel, goodbye, PressMe, lbl, digits_rc;
  Widget children[5];

#if defined (HummingBird_X)
  HCLXmInit ();
#endif /* HummingBird_X */
  topLevel = XtVaAppInitialize (&app_context,
                                "XTReactor_Test",
                                0,
                                0,
                                &argc,
                                argv,
                                0,
                                0);

  digits_rc = create_box(topLevel, "digits_rc");

  //"Stop Test" button.
  goodbye = XtCreateWidget ( (char *) "goodbye",
                             BUTTON_WIDGET,
                             digits_rc,
                             0,
                             0);
  set_label(goodbye, "Stop Test");

  //"Press Me" button
  PressMe = XtCreateWidget ((char *) "PressMe",
                            BUTTON_WIDGET,
                            digits_rc,
                            0,
                            0);

  //Display for event counter
  lbl = XtCreateWidget ((char *) "label_for_event_one",
                        LABEL_WIDGET,
                        digits_rc,
                        0,
                        0);
  set_label(lbl, "label_for_all_events");
  int ac = 0;
  children[ac++] = goodbye;
  children[ac++] = PressMe;
  children[ac++] = lbl;
  XtManageChildren (children, ac);
  XtManageChild (digits_rc);

  //Register callback for "Stop Test" button
  XtAddCallback (goodbye, PRESS_ME_CALLBACK, Quit, 0);

  //Register callback for "Press Me" button
  XtAddCallback (PressMe,
                 PRESS_ME_CALLBACK,
                 inc_count,
                 (XtPointer) lbl);

  // Register callback for X Timer
  (void) XtAppAddTimeOut (app_context,
                          1000,
                          inc_tmo,
                          (XtPointer) lbl);

  XtRealizeWidget (topLevel);

  // It will perform X Main Loop
  ACE_XtReactor reactor (app_context);

  ACE_Reactor r (&reactor);

  //Event Handler for ACE Timer.
  EV_handler evh;

  ACE_Acceptor <Connection_Handler, ACE_SOCK_ACCEPTOR> acceptor;

  if (acceptor.open (ACE_INET_Addr ((u_short) SERV_TCP_PORT),
                     &r) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  if (reactor.schedule_timer (&evh,
                              (const void *) lbl,
                              ACE_Time_Value (2),
                              ACE_Time_Value (2))==-1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) can't register with reactor\n"),
                      -1);

  ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) client,
                                          0,
                                          THR_NEW_LWP | THR_DETACHED);

  XtAppMainLoop (XtWidgetToApplicationContext (topLevel));

  ACE_END_TEST;
  return 0;
}