1 // The following is another test that exercises the Eric C. Newton's
2 // <ecn@clark.net> XtReactor implementation.
4 #include "ace/OS_main.h"
5 #include "ace/XtReactor/XtReactor.h"
6 #include "ace/Reactor.h"
7 #include "ace/Message_Block.h"
8 #include "ace/Log_Msg.h"
9 #include "ace/OS_NS_unistd.h"
10 #include "ace/OS_NS_fcntl.h"
14 class Stdout
: public ACE_Event_Handler
17 Stdout (ACE_Reactor
* r
)
19 msg_ (1000000) // Make a very big message block.
21 int flags
= ACE_OS::fcntl (ACE_STDOUT
, F_GETFL
);
24 && ACE_OS::fcntl (ACE_STDOUT
,
25 F_SETFL
, flags
| O_NONBLOCK
) != -1)
29 "Unable to set stdout to non-block."));
32 ACE_HANDLE
get_handle () const { return ACE_STDOUT
; }
34 int handle_output (ACE_HANDLE
)
36 char *s
= msg_
.rd_ptr ();
38 if (ACE_OS::write (ACE_STDOUT
, s
, 1) == 1)
41 "wrote output '%d'\n",
46 if (msg_
.length () == 0)
48 reactor_
->remove_handler (this,
49 ACE_Event_Handler::WRITE_MASK
);
50 msg_
.rd_ptr (msg_
.base ());
51 msg_
.wr_ptr (msg_
.base ());
58 if (msg_
.length () == 0)
59 reactor_
->register_handler (this,
60 ACE_Event_Handler::WRITE_MASK
);
62 if (msg_
.wr_ptr () < msg_
.end ())
69 "Oops... data falling off the end of the buffer!\n"));
73 ACE_Reactor
*reactor_
;
74 ACE_Message_Block msg_
;
77 class Stdin
: public ACE_Event_Handler
83 ACE_HANDLE
get_handle () const { return ACE_STDIN
; }
85 int handle_input (ACE_HANDLE
)
89 if (ACE_OS::read (ACE_STDIN
, &c
, 1) == 1)
95 int handle_timeout (const ACE_Time_Value
&tv
, const void *)
99 (double) (tv
.msec () / 1000.)));
108 ActivateCB (Widget
, XtPointer
, XtPointer
)
110 ACE_DEBUG ((LM_DEBUG
,
111 "Button pushed!\n"));
115 ACE_TMAIN (int argc
, ACE_TCHAR
**argv
)
117 // The worlds most useless user interface
118 Widget top_level
= XtVaAppInitialize (0,
125 static_cast<void *>(0));
126 char change
[] = "change"; // XmCreatePushButton() wants a non-const
128 Widget button
= XmCreatePushButton (top_level
,
132 XtManageChild (button
);
133 XtAddCallback (button
,
138 // A reactor beastie.
139 ACE_XtReactor
xreactor (XtWidgetToApplicationContext (top_level
));
140 ACE_Reactor
reactor (&xreactor
);
142 // Print a message when data is recv'd on stdin...
143 ACE_Event_Handler
*stdin_
;
144 ACE_NEW_RETURN (stdin_
,
145 Stdin (new Stdout (&reactor
)),
147 reactor
.register_handler (stdin_
,
148 ACE_Event_Handler::READ_MASK
);
150 // Print a message every 10 seconds.
151 if (reactor
.schedule_timer (stdin_
, 0,
153 ACE_Time_Value (10)) == -1)
154 ACE_ERROR_RETURN ((LM_ERROR
,
159 // Show the top_level widget.
160 XtRealizeWidget (top_level
);
162 // Demonstrate Reactor/Xt event loop unification.
163 XtAppMainLoop (XtWidgetToApplicationContext (top_level
));