Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Streams / CommandTasks.cpp
bloba96ca9541bf9af33a55861e80145d11bd2650cb0
1 #include "ace/FILE_Addr.h"
2 #include "ace/FILE_Connector.h"
3 #include "ace/FILE_IO.h"
5 #include "Command.h"
6 #include "CommandTasks.h"
7 #include "RecordingDevice_Text.h"
9 // Listing 011 code/ch18
10 AnswerCallModule::AnswerCallModule (ACE_SOCK_Stream *peer)
11 : CommandModule (ACE_TEXT ("AnswerCall Module"),
12 new AnswerCallDownstreamTask (),
13 new AnswerCallUpstreamTask (),
14 peer)
15 { }
16 // Listing 011
17 // Listing 012 code/ch18
18 AnswerCallDownstreamTask::AnswerCallDownstreamTask ()
19 : CommandTask(Command::CMD_ANSWER_CALL)
20 { }
21 // Listing 012
22 // Listing 013 code/ch18
23 int AnswerCallDownstreamTask::process (Command *command)
25 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Answer Call (downstream)\n")));
27 TextListenerAcceptor *acceptor =
28 (TextListenerAcceptor *)command->extra_data_;
30 CommandModule *module =
31 (CommandModule*)this->module ();
33 command->numeric_result_ =
34 acceptor->accept (module->peer ());
36 acceptor->release ();
37 return Command::RESULT_SUCCESS;
39 // Listing 013
40 // Listing 014 code/ch18
41 AnswerCallUpstreamTask::AnswerCallUpstreamTask ()
42 : CommandTask(Command::CMD_ANSWER_CALL)
43 { }
44 // Listing 014
45 // Listing 015 code/ch18
46 int AnswerCallUpstreamTask::process (Command *)
48 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Answer Call (upstream)\n")));
50 return Command::RESULT_SUCCESS;
52 // Listing 015
54 // Listing 021 code/ch18
55 RetrieveCallerIdModule::RetrieveCallerIdModule
56 (ACE_SOCK_Stream *peer)
57 : CommandModule (ACE_TEXT ("RetrieveCallerId Module"),
58 new RetrieveCallerIdDownstreamTask (),
59 new RetrieveCallerIdUpstreamTask (),
60 peer)
61 { }
62 // Listing 021
63 // Listing 022 code/ch18
64 RetrieveCallerIdDownstreamTask::RetrieveCallerIdDownstreamTask
66 : CommandTask(Command::CMD_RETRIEVE_CALLER_ID)
67 { }
69 int RetrieveCallerIdDownstreamTask::process (Command *)
71 ACE_DEBUG ((LM_DEBUG,
72 ACE_TEXT ("Retrieving Caller ID data\n")));
74 return Command::RESULT_SUCCESS;
76 // Listing 022
77 // Listing 023 code/ch18
78 RetrieveCallerIdUpstreamTask::RetrieveCallerIdUpstreamTask
80 : CommandTask(Command::CMD_RETRIEVE_CALLER_ID)
81 { }
83 int RetrieveCallerIdUpstreamTask::process (Command *command)
85 ACE_DEBUG ((LM_DEBUG,
86 ACE_TEXT ("Returning Caller ID data\n")));
88 ACE_INET_Addr remote_addr;
90 CommandModule *module =
91 (CommandModule*)this->module ();
93 module->peer ().get_remote_addr (remote_addr);
94 ACE_TCHAR remote_addr_str[256];
95 remote_addr.addr_to_string (remote_addr_str, 256);
96 command->result_ = ACE_TString (remote_addr_str);
98 return Command::RESULT_SUCCESS;
100 // Listing 023
102 PlayMessageModule::PlayMessageModule (ACE_SOCK_Stream *peer)
103 : CommandModule (ACE_TEXT ("PlayMessage Module"),
104 new PlayMessageDownstreamTask (),
105 new PlayMessageUpstreamTask (),
106 peer)
109 PlayMessageDownstreamTask::PlayMessageDownstreamTask ()
110 : CommandTask(Command::CMD_PLAY_MESSAGE)
112 // Listing 032 code/ch18
113 int PlayMessageDownstreamTask::process (Command *command)
115 ACE_DEBUG ((LM_DEBUG,
116 ACE_TEXT ("Play Outgoing Message\n")));
118 ACE_FILE_Connector connector;
119 ACE_FILE_IO file;
121 ACE_FILE_Addr *addr =
122 (ACE_FILE_Addr *)command->extra_data_;
124 if (connector.connect (file, *addr) == -1)
126 command->numeric_result_ = -1;
128 else
130 command->numeric_result_ = 0;
132 CommandModule *module =
133 (CommandModule*)this->module ();
135 char rwbuf[512];
136 ssize_t rwbytes;
137 while ((rwbytes = file.recv (rwbuf, 512)) > 0)
139 module->peer ().send_n (rwbuf, rwbytes);
143 return Command::RESULT_SUCCESS;
145 // Listing 032
146 PlayMessageUpstreamTask::PlayMessageUpstreamTask ()
147 : CommandTask(Command::CMD_PLAY_MESSAGE)
150 int PlayMessageUpstreamTask::process (Command *command)
152 ACE_FILE_Addr * addr =
153 (ACE_FILE_Addr *)command->extra_data_;
155 ACE_DEBUG ((LM_DEBUG,
156 ACE_TEXT ("Outgoing message (%s) sent\n"),
157 addr->get_path_name ()));
159 return Command::RESULT_SUCCESS;
162 RecordMessageModule::RecordMessageModule (ACE_SOCK_Stream *peer)
163 : CommandModule (ACE_TEXT ("RecordMessage Module"),
164 new RecordMessageDownstreamTask (),
165 new RecordMessageUpstreamTask (),
166 peer)
169 RecordMessageDownstreamTask::RecordMessageDownstreamTask ()
170 : CommandTask(Command::CMD_RECORD_MESSAGE)
173 int RecordMessageDownstreamTask::process (Command *)
175 return Command::RESULT_SUCCESS;
178 RecordMessageUpstreamTask::RecordMessageUpstreamTask ()
179 : CommandTask(Command::CMD_RECORD_MESSAGE)
181 // Listing 033 code/ch18
182 int RecordMessageUpstreamTask::process (Command *command)
184 // Collect whatever the peer sends and write into the
185 // specified file.
186 ACE_FILE_Connector connector;
187 ACE_FILE_IO file;
189 ACE_FILE_Addr *addr =
190 (ACE_FILE_Addr *)command->extra_data_;
192 if (connector.connect (file, *addr) == -1)
193 ACE_ERROR_RETURN ((LM_ERROR,
194 ACE_TEXT ("%p\n"),
195 ACE_TEXT ("create file")),
196 Command::RESULT_FAILURE);
197 file.truncate (0);
199 CommandModule *module =
200 (CommandModule*)this->module ();
202 ssize_t total_bytes = 0;
203 char rwbuf[512];
204 ssize_t rwbytes;
205 while ((rwbytes = module->peer ().recv (rwbuf, 512)) > 0)
207 total_bytes += file.send_n (rwbuf, rwbytes);
210 file.close ();
212 ACE_DEBUG ((LM_INFO,
213 ACE_TEXT ("RecordMessageUpstreamTask ")
214 ACE_TEXT ("- recorded %d byte message\n"),
215 total_bytes));
217 return Command::RESULT_SUCCESS;
219 // Listing 033