Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Proactor / HA_Proactive_Status.cpp
blob689123772847e15bd155831bb3bb6b87f9eaf47a
1 /*
2 ** Example program from The ACE Programmer's Guide, Chapter 8.
3 ** Copyright 2003 Addison-Wesley. All Rights Reserved.
4 */
6 #include "HA_Proactive_Status.h"
7 #include "ace/Log_Msg.h"
8 #include "ace/Message_Block.h"
9 #include "ace/Proactor.h"
10 #include "ace/os_include/arpa/os_inet.h"
12 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
14 // Listing 1 code/ch08
15 void
16 HA_Proactive_Service::open (ACE_HANDLE h, ACE_Message_Block&)
18 this->handle (h);
19 if (this->reader_.open (*this) != 0 ||
20 this->writer_.open (*this) != 0 )
22 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
23 ACE_TEXT ("HA_Proactive_Service open")));
24 delete this;
25 return;
28 ACE_Message_Block *mb = 0;
29 ACE_NEW_NORETURN (mb, ACE_Message_Block (1024));
30 if (this->reader_.read (*mb, mb->space ()) != 0)
32 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
33 ACE_TEXT ("HA_Proactive_Service begin read")));
34 mb->release ();
35 delete this;
36 return;
39 // mb is now controlled by Proactor framework.
40 return;
42 // Listing 1
44 // Listing 2 code/ch08
45 void
46 HA_Proactive_Service::handle_read_stream
47 (const ACE_Asynch_Read_Stream::Result &result)
49 ACE_Message_Block &mb = result.message_block ();
50 if (!result.success () || result.bytes_transferred () == 0)
52 mb.release ();
53 delete this;
55 else
57 if (this->writer_.write (mb, mb.length ()) != 0)
59 ACE_ERROR ((LM_ERROR,
60 ACE_TEXT ("%p\n"),
61 ACE_TEXT ("starting write")));
62 mb.release ();
64 else
66 ACE_Message_Block *new_mb;
67 ACE_NEW_NORETURN (new_mb, ACE_Message_Block (1024));
68 this->reader_.read (*new_mb, new_mb->space ());
71 return;
73 // Listing 2
75 // Listing 3 code/ch08
76 void
77 HA_Proactive_Service::handle_write_stream
78 (const ACE_Asynch_Write_Stream::Result &result)
80 result.message_block ().release ();
81 return;
83 // Listing 3
85 // The network address check only works for BSD-ish systems. This
86 // sort of network number accessor should be added to ACE_INET_Addr
87 // at some point...
88 #if defined (ACE_WIN32)
89 int
90 HA_Proactive_Acceptor::validate_connection
91 (const ACE_Asynch_Accept::Result&,
92 const ACE_INET_Addr&,
93 const ACE_INET_Addr&)
95 return 0;
97 #else
99 // Listing 4 code/ch08
101 HA_Proactive_Acceptor::validate_connection (
102 const ACE_Asynch_Accept::Result&,
103 const ACE_INET_Addr& remote,
104 const ACE_INET_Addr& local)
106 struct in_addr *remote_addr =
107 reinterpret_cast<struct in_addr*> (remote.get_addr ());
108 struct in_addr *local_addr =
109 reinterpret_cast<struct in_addr*> (local.get_addr ());
110 if (inet_netof (*local_addr) == inet_netof (*remote_addr))
111 return 0;
113 return -1;
115 // Listing 4
117 #endif /* ACE_WIN32 */
120 ACE_TMAIN (int, ACE_TCHAR *[])
122 // Listing 5 code/ch08
123 ACE_INET_Addr listen_addr; // Set up with listen port
124 HA_Proactive_Acceptor aio_acceptor;
125 if (0 != aio_acceptor.open (listen_addr,
126 0, // bytes_to_read
127 0, // pass_addresses
128 ACE_DEFAULT_ASYNCH_BACKLOG,
129 1, // reuse_addr
130 0, // proactor
131 1)) // validate_new_connection
132 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
133 ACE_TEXT ("acceptor open")), 1);
134 // Listing 5
136 #if 0
137 // Listing 6 code/ch08
138 ACE_INET_Addr peer_addr; // Set up peer addr
139 ACE_Asynch_Connector<HA_Proactive_Service> aio_connect;
140 aio_connect.connect (peer_addr);
141 // Listing 6
142 #endif
144 // Listing 7 code/ch08
145 ACE_Proactor::instance ()->proactor_run_event_loop ();
146 // Listing 7
147 return 0;
150 #else
153 ACE_TMAIN (int, ACE_TCHAR *[])
155 ACE_DEBUG ((LM_DEBUG,
156 ACE_TEXT ("This example requires asynchronous I/O support.\n")));
157 return 1;
160 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */