summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_6/ex05.html
blob: 9763486a71f8716132de0cca4e863c13bfc56363 (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
<HTML>
<!-- $Id$ -->
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Ambreen Ilyas">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
   <TITLE>Example 5</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter:&nbsp; "The Acceptor/Connector"&nbsp;
(Connection Initialization)</FONT>
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>

<P><FONT COLOR="#CC0000">//Example 5</FONT>
<BR><FONT COLOR="#000099">#include</FONT><FONT COLOR="#006600"> "ace/Reactor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> "<FONT COLOR="#006600">ace/Svc_Handler.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> "<FONT COLOR="#006600">ace/Acceptor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Acceptor.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Thread.h"</FONT>

<P><FONT COLOR="#FF0000">//Add our own Reactor singleton</FONT>
<BR>typedef ACE_Singleton&lt;ACE_Reactor,ACE_Null_Mutex> Reactor;
<BR><FONT COLOR="#FF0000">//Create an Acceptor</FONT>
<BR>typedef ACE_Acceptor&lt;MyServiceHandler,ACE_SOCK_ACCEPTOR> Acceptor;
<BR><FONT COLOR="#FF0000">//Create a Connector</FONT>
<BR>typedef ACE_Connector&lt;MyServiceHandler,ACE_SOCK_CONNECTOR> Connector;

<P>class MyServiceHandler:
<BR>public ACE_Svc_Handler&lt;ACE_SOCK_STREAM,ACE_NULL_SYNCH>{
<BR>public:
<BR><FONT COLOR="#FF0000">&nbsp;//Used by the two threads "globally" to
determine their peer stream</FONT>
<BR>&nbsp;static ACE_SOCK_Stream* Peer;

<P><FONT COLOR="#FF0000">//Thread ID used to identify the threads</FONT>
<BR>&nbsp;ACE_thread_t t_id;

<P>int open(void*){
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Acceptor: received new connection\n"));

<P><FONT COLOR="#FF0000">//Register with the reactor to remember this handlle</FONT>
<BR>&nbsp; Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK);

<P><FONT COLOR="#FF0000">//Determine the peer stream and record it globally</FONT>
<BR>&nbsp; MyServiceHandler::Peer=&amp;peer();
<BR>&nbsp;
<BR>&nbsp; <FONT COLOR="#FF0000">//Spawn new thread to send string every
second</FONT>
<BR>&nbsp; ACE_Thread::spawn((ACE_THR_FUNC)send_data,0,THR_NEW_LWP,&amp;t_id);
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000"> //keep the service handler registered
by returning 0 to the reactor</FONT>
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>&nbsp;

<P>static void* send_data(void*){
<BR>&nbsp; while(1){
<BR>&nbsp;&nbsp; ACE_DEBUG((LM_DEBUG,"Hello World\n"));
<BR>&nbsp;&nbsp; Peer->send_n("Hello World",sizeof("Hello World"));
<BR>&nbsp;
<BR>&nbsp;&nbsp; <FONT COLOR="#FF0000">//Go to sleep for a second before
sending again</FONT>
<BR>&nbsp;&nbsp; ACE_OS::sleep(1);
<BR>&nbsp;&nbsp;&nbsp; }
<BR>&nbsp; return 0;
<BR>&nbsp; }
<BR>&nbsp;

<P>int handle_input(ACE_HANDLE){
<BR>&nbsp; char* data= new char[12];
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000"> //Check if peer aborted the connection</FONT>
<BR>&nbsp; if(Peer.recv_n(data,12)==0){
<BR>&nbsp;&nbsp; ACE_DEBUG((LM_DEBUG,"Peer probably aborted connection\n"));
<BR>&nbsp;&nbsp; ACE_Thread::cancel(t_id); <FONT COLOR="#CC0000">//kill
sending thread ..</FONT>
<BR>&nbsp;&nbsp; return -1; <FONT COLOR="#FF0000">//de-register from the
Reactor.</FONT>
<BR>&nbsp;&nbsp; }

<P>&nbsp;<FONT COLOR="#FF0000"> //Show what you got..</FONT>
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"&lt;&lt; %s\n",data));
<BR>&nbsp;
<BR>&nbsp; <FONT COLOR="#FF0000">//keep yourself registered</FONT>
<BR>&nbsp; return 0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<BR>};

<P><FONT COLOR="#FF0000">//Global stream identifier used by both threads</FONT>
<BR>ACE_SOCK_Stream * MyServiceHandler::Peer=0;
<BR>&nbsp;

<P>void main_accept(){
<BR>&nbsp;ACE_INET_Addr addr(PORT_NO);
<BR>&nbsp;Acceptor myacceptor(addr,Reactor::instance());
<BR>&nbsp;while(1)
<BR>&nbsp; Reactor::instance()->handle_events();

<P>&nbsp;return 0;
<BR>&nbsp;}

<P>void main_connect(){
<BR>&nbsp;ACE_INET_Addr addr(PORT_NO,HOSTNAME);
<BR>&nbsp;Connector myconnector;
<BR>&nbsp;myconnector.connect(my_svc_handler,addr);
<BR>&nbsp;while(1)
<BR>&nbsp; Reactor::instance()->handle_events();

<P>&nbsp;}
<BR>&nbsp;

<P>int main(int argc, char* argv[]){
<BR><FONT COLOR="#FF0000">&nbsp;// Use ACE_Get_Opt to parse and obtain
arguments and then call the</FONT>
<BR>&nbsp;<FONT COLOR="#FF0000">// approriate function for accept or connect.</FONT>
<BR><FONT COLOR="#CC0000">//...</FONT>
<BR>}
<BR>&nbsp;
<BR>&nbsp;<A HREF="ex06.html">Next Example</A>
</BODY>
</HTML>