summaryrefslogtreecommitdiff
path: root/examples/Misc/test_XtReactor1.cpp
blob: 27fb9d4b02d18df27ae29ded82c123351abde814 (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
// $Id$

// The following is another test that exercises the Eric C. Newton's
// <ecn@clark.net> XtReactor implementation.

#include "ace/OS_main.h"
#include "ace/XtReactor.h"
#include "ace/Reactor.h"
#include "ace/Message_Block.h"

ACE_RCSID(Misc, test_XtReactor1, "$Id$")

#if defined (ACE_HAS_XT)
//#define String XtString
#include <Xm/PushB.h>

class Stdout : public ACE_Event_Handler
{
public:
  Stdout (ACE_Reactor * r)
    : reactor_ (r),
      msg_ (1000000) // Make a very big message block.
  {
    int flags = ACE_OS::fcntl (ACE_STDOUT, F_GETFL);

    if (flags != -1
	&& ACE_OS::fcntl (ACE_STDOUT,
                          F_SETFL, flags | O_NONBLOCK) != -1)
      return;
    else
      ACE_DEBUG ((LM_DEBUG,
                  "Unable to set stdout to non-block."));
  }

  ACE_HANDLE get_handle (void) const { return ACE_STDOUT; }

  int handle_output (ACE_HANDLE)
  {
    char *s = msg_.rd_ptr ();

    if (ACE_OS::write (ACE_STDOUT, s, 1) == 1)
      {
	ACE_DEBUG ((LM_DEBUG,
                    "wrote output '%d'\n",
                    (int) *s));
	msg_.rd_ptr (1);
      }

    if (msg_.length () == 0)
      {
	reactor_->remove_handler (this,
                                  ACE_Event_Handler::WRITE_MASK);
	msg_.rd_ptr (msg_.base ());
	msg_.wr_ptr (msg_.base ());
      }
    return 0;
  }

  void put (char c)
  {
    if (msg_.length () == 0)
      reactor_->register_handler (this,
                                  ACE_Event_Handler::WRITE_MASK);

    if (msg_.wr_ptr () < msg_.end ())
      {
	*msg_.wr_ptr () = c;
	msg_.wr_ptr (1);
      }
    else
      ACE_DEBUG ((LM_DEBUG,
                  "Oops... data falling off the end of the buffer!\n"));
  }

private:
  ACE_Reactor *reactor_;
  ACE_Message_Block msg_;
};

class Stdin : public ACE_Event_Handler
{
public:
  Stdin (Stdout *out)
    : out_ (out) {}

  ACE_HANDLE get_handle () const { return ACE_STDIN; }

  int handle_input (ACE_HANDLE)
  {
    char c;

    if (ACE_OS::read (ACE_STDIN, &c, 1) == 1)
      out_->put (c);

    return 0;
  }

  int handle_timeout (const ACE_Time_Value &tv, const void *)
  {
    ACE_DEBUG ((LM_DEBUG,
                "Timeout! %f\n",
                (double) (tv.msec () / 1000.)));
    return 0;
  }

private:
  Stdout *out_;
};

static void
ActivateCB (Widget, XtPointer, XtPointer)
{
  ACE_DEBUG ((LM_DEBUG,
              "Button pushed!\n"));
}

int
ACE_TMAIN (int argc, ACE_TCHAR**argv)
{
  // The worlds most useless user interface
  Widget top_level = XtVaAppInitialize (NULL,
                                        "buttontest",
                                        NULL,
                                        0,
					&argc,
                                        argv,
                                        NULL,
                                        NULL);
  Widget button = XmCreatePushButton (top_level,
                                      "change",
                                      0,
                                      0);
  XtManageChild (button);
  XtAddCallback (button,
                 XmNactivateCallback,
                 ActivateCB,
                 NULL);

  // A reactor beastie.
  ACE_XtReactor xreactor (XtWidgetToApplicationContext (top_level));
  ACE_Reactor reactor (&xreactor);

  // Print a message when data is recv'd on stdin...
  ACE_Event_Handler *stdin_;
  ACE_NEW_RETURN (stdin_,
                  Stdin (new Stdout (&reactor)),
                  -1);
  reactor.register_handler (stdin_,
                            ACE_Event_Handler::READ_MASK);

  // Print a message every 10 seconds.
  if (reactor.schedule_timer (stdin_, 0,
			      ACE_Time_Value (10),
			      ACE_Time_Value (10)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "schedule_timer"),
                      -1);

  // Show the top_level widget.
  XtRealizeWidget (top_level);

  // Demonstrate Reactor/Xt event loop unification.
  XtAppMainLoop (XtWidgetToApplicationContext (top_level));

  return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "XT not configured for this platform\n"),
                    0);
}
#endif /* ACE_HAS_XT */