2 * A RecordingDevice that listens to a socket and collects text.
5 #include "MessageInfo.h"
6 #include "RecordingDevice.h"
7 #include "RecordingDevice_Text.h"
10 TextListenerAcceptor::TextListenerAcceptor ()
11 : ACE_Event_Handler(), RecordingDevice()
14 // ACE_Event_Handler interface
16 int TextListenerAcceptor::open (ACE_INET_Addr
&addr
)
18 if (this->acceptor_
.open (addr
, 1) == -1)
19 ACE_ERROR_RETURN ((LM_ERROR
,
21 ACE_TEXT ("acceptor open")),
26 ACE_HANDLE
TextListenerAcceptor::get_handle () const
28 return this->acceptor_
.get_handle ();
31 int TextListenerAcceptor::handle_input (ACE_HANDLE
)
34 ACE_TEXT ("TextListenerAcceptor - connection request\n" )));
38 int TextListenerAcceptor::accept (ACE_SOCK_Stream
&peer
)
40 return this->acceptor_
.accept (peer
);
43 // RecordingDevice interface
45 // Open a listening socket on the port specified by argv.
46 int TextListenerAcceptor::init (int argc
, ACE_TCHAR
*argv
[])
50 ACE_INET_Addr
addr (argv
[1]);
52 if (this->open (addr
) < 0)
53 ACE_ERROR_RETURN ((LM_ERROR
,
55 ACE_TEXT ("TextListener - open")),
60 ACE_Event_Handler
*TextListenerAcceptor::get_handler () const
62 return (ACE_Event_Handler
*)this;
65 RecordingDevice
*TextListenerAcceptor::wait_for_activity ()
67 this->RecordingDevice::wait_for_activity ();
68 return new TextListener (this);
71 int TextListenerAcceptor::answer_call ()
76 CallerId
*TextListenerAcceptor::retrieve_callerId ()
81 int TextListenerAcceptor::play_message (ACE_FILE_Addr
&addr
)
87 MessageType
*TextListenerAcceptor::record_message (ACE_FILE_Addr
&addr
)
94 // Listing 01 code/ch18
95 TextListener::TextListener (TextListenerAcceptor
*acceptor
)
98 ACE_TRACE ("TextListener ctor");
100 ACE_NEW (this->command_stream_
, CommandStream (&(this->peer_
)));
101 this->command_stream_
->open (0);
105 const ACE_TCHAR
*TextListener::get_name () const
107 return ACE_TEXT ("TextListener");
110 // Listing 02 code/ch18
111 int TextListener::answer_call ()
113 ACE_DEBUG ((LM_DEBUG
,
114 ACE_TEXT ("TextListener::answer_call()\n")));
116 Command
*c
= new Command ();
117 c
->command_
= Command::CMD_ANSWER_CALL
;
118 c
->extra_data_
= this->acceptor_
;
120 c
= this->command_stream_
->execute (c
);
122 ACE_DEBUG ((LM_DEBUG
,
123 ACE_TEXT ("TextListener::answer_call() ")
124 ACE_TEXT ("result is %d\n"),
125 c
->numeric_result_
));
127 return c
->numeric_result_
;
131 // Listing 03 code/ch18
132 CallerId
*TextListener::retrieve_callerId ()
134 ACE_DEBUG ((LM_DEBUG
,
135 ACE_TEXT ("TextListener::retrieve_callerId()\n")));
137 Command
*c
= new Command ();
138 c
->command_
= Command::CMD_RETRIEVE_CALLER_ID
;
140 c
= this->command_stream_
->execute (c
);
142 CallerId
*caller_id
= new CallerId (c
->result_
);
147 // Listing 04 code/ch18
148 int TextListener::play_message (ACE_FILE_Addr
&addr
)
150 MessageType
*type
= Util::identify_message (addr
);
151 if (type
->is_text ())
153 Command
*c
= new Command ();
154 c
->command_
= Command::CMD_PLAY_MESSAGE
;
155 c
->extra_data_
= &addr
;
156 c
= this->command_stream_
->execute (c
);
157 return c
->numeric_result_
;
160 ACE_FILE_Addr
temp (ACE_TEXT ("/tmp/outgoing_message.text"));
162 if (type
->is_audio ())
163 file
= Util::audio_to_text (addr
, temp
);
164 else if (type
->is_video ())
165 file
= Util::video_to_text (addr
, temp
);
168 ((LM_ERROR
, ACE_TEXT ("Invalid message type %d\n"),
169 type
->get_codec ()), -1);
170 int rval
= this->play_message (temp
);
176 // Listing 05 code/ch18
177 MessageType
*TextListener::record_message (ACE_FILE_Addr
&addr
)
179 Command
*c
= new Command ();
180 c
->command_
= Command::CMD_RECORD_MESSAGE
;
181 c
->extra_data_
= &addr
;
182 c
= this->command_stream_
->execute (c
);
183 if (c
->numeric_result_
== -1)
186 return new MessageType (MessageType::RAWTEXT
, addr
);
190 // Listing 06 code/ch18
191 void TextListener::release ()