Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Misc / test_XtReactor1.cpp
blob3a946887c178494d10b4e5074e5acbd7f63f815d
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"
12 #include <Xm/PushB.h>
14 class Stdout : public ACE_Event_Handler
16 public:
17 Stdout (ACE_Reactor * r)
18 : reactor_ (r),
19 msg_ (1000000) // Make a very big message block.
21 int flags = ACE_OS::fcntl (ACE_STDOUT, F_GETFL);
23 if (flags != -1
24 && ACE_OS::fcntl (ACE_STDOUT,
25 F_SETFL, flags | O_NONBLOCK) != -1)
26 return;
27 else
28 ACE_DEBUG ((LM_DEBUG,
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)
40 ACE_DEBUG ((LM_DEBUG,
41 "wrote output '%d'\n",
42 (int) *s));
43 msg_.rd_ptr (1);
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 ());
53 return 0;
56 void put (char c)
58 if (msg_.length () == 0)
59 reactor_->register_handler (this,
60 ACE_Event_Handler::WRITE_MASK);
62 if (msg_.wr_ptr () < msg_.end ())
64 *msg_.wr_ptr () = c;
65 msg_.wr_ptr (1);
67 else
68 ACE_DEBUG ((LM_DEBUG,
69 "Oops... data falling off the end of the buffer!\n"));
72 private:
73 ACE_Reactor *reactor_;
74 ACE_Message_Block msg_;
77 class Stdin : public ACE_Event_Handler
79 public:
80 Stdin (Stdout *out)
81 : out_ (out) {}
83 ACE_HANDLE get_handle () const { return ACE_STDIN; }
85 int handle_input (ACE_HANDLE)
87 char c;
89 if (ACE_OS::read (ACE_STDIN, &c, 1) == 1)
90 out_->put (c);
92 return 0;
95 int handle_timeout (const ACE_Time_Value &tv, const void *)
97 ACE_DEBUG ((LM_DEBUG,
98 "Timeout! %f\n",
99 (double) (tv.msec () / 1000.)));
100 return 0;
103 private:
104 Stdout *out_;
107 static void
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,
119 "buttontest",
122 &argc,
123 argv,
125 static_cast<void *>(0));
126 char change[] = "change"; // XmCreatePushButton() wants a non-const
127 // string.
128 Widget button = XmCreatePushButton (top_level,
129 change,
132 XtManageChild (button);
133 XtAddCallback (button,
134 XmNactivateCallback,
135 ActivateCB,
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)),
146 -1);
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,
152 ACE_Time_Value (10),
153 ACE_Time_Value (10)) == -1)
154 ACE_ERROR_RETURN ((LM_ERROR,
155 "%p\n",
156 "schedule_timer"),
157 -1);
159 // Show the top_level widget.
160 XtRealizeWidget (top_level);
162 // Demonstrate Reactor/Xt event loop unification.
163 XtAppMainLoop (XtWidgetToApplicationContext (top_level));
165 return 0;