summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_6/ex07.html
blob: 7ecf0e57bb34024a0cd45eea15839967b72af84d (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
<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 7</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 7</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>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">NETWORK_SPEED
3</FONT>
<BR>class MyServiceHandler;<FONT COLOR="#FF0000"> //forward declaration</FONT>
<BR>typedef ACE_Singleton&lt;ACE_Reactor,ACE_Null_Mutex> Reactor;
<BR>typedef ACE_Acceptor&lt;MyServiceHandler,ACE_SOCK_ACCEPTOR> Acceptor;

<P>class MyServiceHandler:
<BR>public ACE_Svc_Handler&lt;ACE_SOCK_STREAM,ACE_MT_SYNCH>{
<BR><FONT COLOR="#FF0000">// The message sender and creator threads are
handled here.</FONT>
<BR>ACE_thread_t thread_names[2];

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

<P><FONT COLOR="#FF0000">&nbsp;//Register with the reactor to remember
this handler..</FONT>
<BR>&nbsp;Reactor::instance() ->register_handler(this,ACE_Event_Handler::READ_MASK);
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Acceptor: ThreadID:(%t) open\n"));

<P><FONT COLOR="#CC0000">&nbsp;//Create two new threads to create and send
messages to the</FONT>
<BR><FONT COLOR="#CC0000">&nbsp;//remote machine.</FONT>
<BR>&nbsp;activate(THR_NEW_LWP,
<BR>&nbsp;&nbsp; 2, <FONT COLOR="#FF0000">//2 new threads</FONT>
<BR>&nbsp;&nbsp; 0,
<BR>&nbsp;&nbsp; ACE_DEFAULT_THREAD_PRIORITY,
<BR>&nbsp;&nbsp; -1,
<BR>&nbsp;&nbsp; this,
<BR>&nbsp;&nbsp; 0,
<BR>&nbsp;&nbsp; 0,
<BR>&nbsp;&nbsp; 0,
<BR>&nbsp;&nbsp; thread_names); <FONT COLOR="#FF0000">// identifiers in
thread_handles</FONT>
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">//keep the service handler registered with
the acceptor.</FONT>
<BR>&nbsp;return 0;
<BR>&nbsp;}

<P>void send_message(void){
<BR><FONT COLOR="#FF0000">&nbsp;//Dequeue the message and send it off</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t)Sending message >>"));

<P><FONT COLOR="#FF0000">&nbsp;//dequeue the message from the message queue</FONT>
<BR>&nbsp;ACE_Message_Block *mb;
<BR>&nbsp;ACE_ASSERT(this->getq(mb)!=-1);
<BR>&nbsp;int length=mb->length();
<BR>&nbsp;char *data =mb->rd_ptr();

<P>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Send the data to the remote peer</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"%s \n",data,length));
<BR>&nbsp;peer().send_n(data,length);

<P>&nbsp;<FONT COLOR="#FF0000">//Simulate very SLOW network.</FONT>
<BR>&nbsp;ACE_OS::sleep(NETWORK_SPEED);

<P><FONT COLOR="#FF0000">&nbsp;//release the message block</FONT>
<BR>&nbsp;mb->release();
<BR>&nbsp;} <FONT COLOR="#FF0000">//end send_message</FONT>

<P>int construct_message(void){
<BR><FONT COLOR="#FF0000">&nbsp;// A very fast message creation algorithm</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// would lead to the need for queuing messages..</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// here. These messages are created and
then sent</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// using the SLOW send_message() routine
which is</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// running in a different thread so that
the message</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//construction thread isn?t blocked.</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t)Constructing message >> "));

<P>&nbsp; <FONT COLOR="#FF0000">// Create a new message to send</FONT>
<BR>&nbsp;ACE_Message_Block *mb;
<BR>&nbsp;char *data="Hello Connector";
<BR>&nbsp;ACE_NEW_RETURN (mb,ACE_Message_Block (16,<FONT COLOR="#FF0000">//Message
16 bytes long</FONT>
<BR>&nbsp;&nbsp; ACE_Message_Block::MB_DATA,<FONT COLOR="#FF0000">//Set
header to data</FONT>
<BR>&nbsp;&nbsp; 0,<FONT COLOR="#FF0000">//No continuations</FONT>.
<BR>&nbsp;&nbsp; data<FONT COLOR="#FF0000">//The data we want to send</FONT>
<BR>&nbsp;&nbsp; ), 0);
<BR>&nbsp;mb->wr_ptr(16); <FONT COLOR="#FF0000">//Set the write pointer.</FONT>
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">// Enqueue the message into the message
queue</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// we COULD have done a timed wait for
enqueuing in case</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// someone else holds the lock to the queue
so it doesn?t block</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//forever..</FONT>
<BR>&nbsp;ACE_ASSERT(this->putq(mb)!=-1);
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Enqueued msg successfully\n"));
<BR>}
<BR>&nbsp;
<BR>int svc(void){
<BR>&nbsp;ACE_DEBUG( (LM_DEBUG,?(%t) Svc thread \n?));
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//call the message creator thread</FONT>
<BR>&nbsp;if(ACE_Thread::self()== thread_names[0])
<BR>&nbsp; while(1) construct_message(); <FONT COLOR="#FF0000">//create
messages forever</FONT>
<BR>&nbsp;else
<BR>&nbsp; while(1) send_message(); <FONT COLOR="#FF0000">//send messages
forever</FONT>
<BR>&nbsp;return 0; <FONT COLOR="#FF0000">// keep the compiler happy.</FONT>
<BR>}
<BR>&nbsp;
<BR>int handle_input(ACE_HANDLE){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t) handle_input "));
<BR>&nbsp;char* data= new char[13];
<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; ACE_OS::printf("Peer probably aborted connection");
<BR>&nbsp; return -1; <FONT COLOR="#FF0000">//de-register from the Reactor.</FONT>
<BR>&nbsp; }
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Show what you got..</FONT>
<BR>&nbsp;ACE_OS::printf("&lt;&lt; %s\n",data);
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//keep yourself registered</FONT>
<BR>&nbsp;return 0;
<BR>&nbsp;
<BR>&nbsp;}
<BR>};

<P>int main(int argc, char* argv[]){
<BR>&nbsp;ACE_INET_Addr addr(10101);
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Thread: (%t) main\n"));

<P>&nbsp;<FONT COLOR="#FF0000">//Prepare to accept connections</FONT>
<BR>&nbsp;Acceptor myacceptor(addr,Reactor::instance());

<P><FONT COLOR="#FF0000">&nbsp;// wait for something to happen.</FONT>
<BR>&nbsp;while(1)
<BR>&nbsp; Reactor::instance()->handle_events();
<BR>&nbsp;
<BR>&nbsp;return 0;
<BR>}

<P>&nbsp;<A HREF="ex08.html">Next Example</A>
</BODY>
</HTML>