summaryrefslogtreecommitdiff
path: root/examples/Reactor/WFMO_Reactor/test_directory_changes.cpp
blob: 48c486380cbd05594bcd3063680cebaea8182c22 (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
// $Id$
//
// ============================================================================
//
// = LIBRARY
//    examples
//
// = FILENAME
//    test_directory_changes.cpp
//
// = DESCRIPTION
//
//    This application tests the working of WFMO_Reactor when users
//    are interested in monitoring changes in the filesystem.
//
// = AUTHOR
//    Irfan Pyarali
//
// ============================================================================

#include "ace/Reactor.h"

ACE_RCSID(WFMO_Reactor, test_directory_changes, "$Id$")

static int stop_test = 0;
static const ACE_TCHAR *directory = ACE_TEXT (".");
static const ACE_TCHAR *temp_file = ACE_TEXT ("foo");

class Event_Handler : public ACE_Event_Handler
{
public:
  Event_Handler (ACE_Reactor &reactor);
  ~Event_Handler (void);
  int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);
  int handle_close (ACE_HANDLE handle,
                    ACE_Reactor_Mask close_mask);

private:
  ACE_HANDLE handle_;
};

Event_Handler::Event_Handler (ACE_Reactor &reactor)
  : handle_ (ACE_INVALID_HANDLE)
{
  this->reactor (&reactor);

  int change_notification_flags = FILE_NOTIFY_CHANGE_FILE_NAME;

  this->handle_ = ACE_TEXT_FindFirstChangeNotification (directory,  // pointer to name of directory to watch
                                                        FALSE, // flag for monitoring directory or directory tree
                                                        change_notification_flags // filter conditions to watch for
                                                        );
  if (this->handle_ == ACE_INVALID_HANDLE)
    ACE_ERROR ((LM_ERROR, "FindFirstChangeNotification could not be setup\n"));

  if (this->reactor ()->register_handler (this,
                                          this->handle_) != 0)
    ACE_ERROR ((LM_ERROR, "Registration with Reactor could not be done\n"));
}

Event_Handler::~Event_Handler (void)
{
}

int
Event_Handler::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  ::FindNextChangeNotification (this->handle_);
  if (stop_test)
    this->reactor ()->close ();
  return 0;
}

int
Event_Handler::handle_close (ACE_HANDLE handle,
                             ACE_Reactor_Mask close_mask)
{
  ACE_DEBUG ((LM_DEBUG, "Event_Handler removed from Reactor\n"));
  ::FindCloseChangeNotification (this->handle_);
  return 0;
}

void
worker (void)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) Thread creation\n"));
  ACE_DEBUG ((LM_DEBUG, "(%t) Thread creating temporary file\n"));
  ACE_HANDLE file = ACE_OS::open (temp_file, _O_CREAT | _O_EXCL);
  if (file == ACE_INVALID_HANDLE)
    ACE_ERROR ((LM_ERROR, "Error in creating %s: %p\n", temp_file, "ACE_OS::open"));
  else
    {
      ACE_OS::close (file);
      ACE_DEBUG ((LM_DEBUG, "(%t) Thread sleeping\n"));
      ACE_OS::sleep (3);
      ACE_DEBUG ((LM_DEBUG, "(%t) Thread removing temporary file\n"));
      stop_test = 1;
      ACE_OS::unlink (temp_file);
    }
}

int
main (int, char *[])
{
  ACE_Reactor reactor;
  Event_Handler handler (reactor);

  int result = ACE_OS::thr_create ((ACE_THR_FUNC) worker, 0, 0, 0);
  ACE_ASSERT (result == 0);

  for (result = 0; result != -1; result = reactor.handle_events ())
    continue;

  return 0;
}