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
|
<HTML>
<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">//// Chapter: "The Acceptor/Connector"
(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">//// 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<ACE_Reactor,ACE_Null_Mutex> Reactor;
<BR><FONT COLOR="#FF0000">//Create an Acceptor</FONT>
<BR>typedef ACE_Acceptor<MyServiceHandler,ACE_SOCK_ACCEPTOR> Acceptor;
<BR><FONT COLOR="#FF0000">//Create a Connector</FONT>
<BR>typedef ACE_Connector<MyServiceHandler,ACE_SOCK_CONNECTOR> Connector;
<P>class MyServiceHandler:
<BR>public ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_NULL_SYNCH>{
<BR>public:
<BR><FONT COLOR="#FF0000"> //Used by the two threads "globally" to
determine their peer stream</FONT>
<BR> static ACE_SOCK_Stream* Peer;
<P><FONT COLOR="#FF0000">//Thread ID used to identify the threads</FONT>
<BR> ACE_thread_t t_id;
<P>int open(void*){
<BR> ACE_DEBUG((LM_DEBUG,"Acceptor: received new connection\n"));
<P><FONT COLOR="#FF0000">//Register with the reactor to remember this handlle</FONT>
<BR> Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK);
<P><FONT COLOR="#FF0000">//Determine the peer stream and record it globally</FONT>
<BR> MyServiceHandler::Peer=&peer();
<BR>
<BR> <FONT COLOR="#FF0000">//Spawn new thread to send string every
second</FONT>
<BR> ACE_Thread::spawn((ACE_THR_FUNC)send_data,0,THR_NEW_LWP,&t_id);
<BR>
<BR> <FONT COLOR="#FF0000"> //keep the service handler registered
by returning 0 to the reactor</FONT>
<BR> return 0;
<BR> }
<BR>
<P>static void* send_data(void*){
<BR> while(1){
<BR> ACE_DEBUG((LM_DEBUG,"Hello World\n"));
<BR> Peer->send_n("Hello World",sizeof("Hello World"));
<BR>
<BR> <FONT COLOR="#FF0000">//Go to sleep for a second before
sending again</FONT>
<BR> ACE_OS::sleep(1);
<BR> }
<BR> return 0;
<BR> }
<BR>
<P>int handle_input(ACE_HANDLE){
<BR> char* data= new char[12];
<BR>
<BR> <FONT COLOR="#FF0000"> //Check if peer aborted the connection</FONT>
<BR> if(Peer.recv_n(data,12)==0){
<BR> ACE_DEBUG((LM_DEBUG,"Peer probably aborted connection\n"));
<BR> ACE_Thread::cancel(t_id); <FONT COLOR="#CC0000">//kill
sending thread ..</FONT>
<BR> return -1; <FONT COLOR="#FF0000">//de-register from the
Reactor.</FONT>
<BR> }
<P> <FONT COLOR="#FF0000"> //Show what you got..</FONT>
<BR> ACE_DEBUG((LM_DEBUG,"<< %s\n",data));
<BR>
<BR> <FONT COLOR="#FF0000">//keep yourself registered</FONT>
<BR> return 0;
<BR> }
<BR>};
<P><FONT COLOR="#FF0000">//Global stream identifier used by both threads</FONT>
<BR>ACE_SOCK_Stream * MyServiceHandler::Peer=0;
<BR>
<P>void main_accept(){
<BR> ACE_INET_Addr addr(PORT_NO);
<BR> Acceptor myacceptor(addr,Reactor::instance());
<BR> while(1)
<BR> Reactor::instance()->handle_events();
<P> return 0;
<BR> }
<P>void main_connect(){
<BR> ACE_INET_Addr addr(PORT_NO,HOSTNAME);
<BR> Connector myconnector;
<BR> myconnector.connect(my_svc_handler,addr);
<BR> while(1)
<BR> Reactor::instance()->handle_events();
<P> }
<BR>
<P>int main(int argc, char* argv[]){
<BR><FONT COLOR="#FF0000"> // Use ACE_Get_Opt to parse and obtain
arguments and then call the</FONT>
<BR> <FONT COLOR="#FF0000">// approriate function for accept or connect.</FONT>
<BR><FONT COLOR="#CC0000">//...</FONT>
<BR>}
<BR>
<BR> <A HREF="ex06.html">Next Example</A>
</BODY>
</HTML>
|