summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_6/ex06.html
blob: 7bfc3b91beb774fd187cf1912b41af804930d12f (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
<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 6</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 6</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>

<P>class MyServiceHandler; //forward declaration
<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 two thread names are kept 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"));
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">//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 remote machine.</FONT>
<BR>&nbsp;activate(THR_NEW_LWP,
<BR>&nbsp;&nbsp; 2, <FONT COLOR="#FF0000">//2 new threads</FONT>
<BR>&nbsp;&nbsp; 0, <FONT COLOR="#FF0000">//force active false, if already
created dont try again.</FONT>
<BR>&nbsp;&nbsp; ACE_DEFAULT_THREAD_PRIORITY,<FONT COLOR="#FF0000">//Use
default thread priority</FONT>
<BR>&nbsp;&nbsp; -1,
<BR>&nbsp;&nbsp; this,<FONT COLOR="#FF0000">//Which ACE_Task object to
create? In this case this one.</FONT>
<BR>&nbsp;&nbsp; 0,<FONT COLOR="#FF0000">// dont care about thread handles
used</FONT>
<BR>&nbsp;&nbsp; 0,<FONT COLOR="#FF0000">// dont care about where stacks
are created</FONT>
<BR>&nbsp;&nbsp; 0,<FONT COLOR="#FF0000">//dont care about stack sizes</FONT>
<BR>&nbsp;&nbsp; thread_names); <FONT COLOR="#FF0000">// keep identifiers
in thread_names</FONT>
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//keep the service handler registered with
the acceptor.</FONT>
<BR>&nbsp;return 0;
<BR>&nbsp;}

<P>void send_message1(void){
<BR>&nbsp;<FONT COLOR="#FF0000">//Send message type 1</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t)Sending message >>"));

<P>&nbsp;<FONT COLOR="#FF0000">//Send the data to the remote peer</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Sent message1"));
<BR>&nbsp;peer().send_n("Message1",LENGTH_MSG_1);
<BR>&nbsp;} <FONT COLOR="#FF0000">//end send_message1</FONT>

<P>int send_message2(void){
<BR>&nbsp;<FONT COLOR="#FF0000">//Send message type 1</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t)Sending message >>"));

<P>&nbsp;<FONT COLOR="#FF0000">//Send the data to the remote peer</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Sent Message2"));
<BR>&nbsp;peer().send_n("Message2",LENGTH_MSG_2);
<BR>&nbsp;}<FONT COLOR="#FF0000">//end send_message_2</FONT>
<BR>&nbsp;
<BR>int svc(void){
<BR>&nbsp;ACE_DEBUG( (LM_DEBUG,?(%t) Svc thread \n?));
<BR>&nbsp;
<BR>&nbsp;if(ACE_Thread::self()== thread_names[0])
<BR>&nbsp; while(1) send_message1(); <FONT COLOR="#FF0000">//send message
1s forever</FONT>
<BR>&nbsp;else
<BR>&nbsp; while(1) send_message2(); <FONT COLOR="#FF0000">//send message
2s 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\n");
<BR>&nbsp; return -1; <FONT COLOR="#FF0000">//de-register from the Reactor.</FONT>
<BR>&nbsp; }
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">//Show what you got..</FONT>
<BR>&nbsp;ACE_OS::printf("&lt;&lt; %s\n",data);
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">//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="ex07.html">Next Example</A>
</BODY>
</HTML>