1 #include "ace/FILE_Addr.h"
2 #include "ace/FILE_Connector.h"
3 #include "ace/FILE_IO.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 (),
17 // Listing 012 code/ch18
18 AnswerCallDownstreamTask::AnswerCallDownstreamTask ()
19 : CommandTask(Command::CMD_ANSWER_CALL
)
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 ());
37 return Command::RESULT_SUCCESS
;
40 // Listing 014 code/ch18
41 AnswerCallUpstreamTask::AnswerCallUpstreamTask ()
42 : CommandTask(Command::CMD_ANSWER_CALL
)
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
;
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 (),
63 // Listing 022 code/ch18
64 RetrieveCallerIdDownstreamTask::RetrieveCallerIdDownstreamTask
66 : CommandTask(Command::CMD_RETRIEVE_CALLER_ID
)
69 int RetrieveCallerIdDownstreamTask::process (Command
*)
72 ACE_TEXT ("Retrieving Caller ID data\n")));
74 return Command::RESULT_SUCCESS
;
77 // Listing 023 code/ch18
78 RetrieveCallerIdUpstreamTask::RetrieveCallerIdUpstreamTask
80 : CommandTask(Command::CMD_RETRIEVE_CALLER_ID
)
83 int RetrieveCallerIdUpstreamTask::process (Command
*command
)
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
;
102 PlayMessageModule::PlayMessageModule (ACE_SOCK_Stream
*peer
)
103 : CommandModule (ACE_TEXT ("PlayMessage Module"),
104 new PlayMessageDownstreamTask (),
105 new PlayMessageUpstreamTask (),
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
;
121 ACE_FILE_Addr
*addr
=
122 (ACE_FILE_Addr
*)command
->extra_data_
;
124 if (connector
.connect (file
, *addr
) == -1)
126 command
->numeric_result_
= -1;
130 command
->numeric_result_
= 0;
132 CommandModule
*module
=
133 (CommandModule
*)this->module ();
137 while ((rwbytes
= file
.recv (rwbuf
, 512)) > 0)
139 module
->peer ().send_n (rwbuf
, rwbytes
);
143 return Command::RESULT_SUCCESS
;
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 (),
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
186 ACE_FILE_Connector connector
;
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
,
195 ACE_TEXT ("create file")),
196 Command::RESULT_FAILURE
);
199 CommandModule
*module
=
200 (CommandModule
*)this->module ();
202 ssize_t total_bytes
= 0;
205 while ((rwbytes
= module
->peer ().recv (rwbuf
, 512)) > 0)
207 total_bytes
+= file
.send_n (rwbuf
, rwbytes
);
213 ACE_TEXT ("RecordMessageUpstreamTask ")
214 ACE_TEXT ("- recorded %d byte message\n"),
217 return Command::RESULT_SUCCESS
;