Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Streams / RecordingDevice_Text.cpp
blobd06d2c131dab4e1d22c0a76750a6cc3b548de46f
1 /*
2 * A RecordingDevice that listens to a socket and collects text.
3 */
5 #include "MessageInfo.h"
6 #include "RecordingDevice.h"
7 #include "RecordingDevice_Text.h"
8 #include "Util.h"
10 TextListenerAcceptor::TextListenerAcceptor ()
11 : ACE_Event_Handler(), RecordingDevice()
12 { }
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,
20 ACE_TEXT ("%p\n"),
21 ACE_TEXT ("acceptor open")),
22 -1);
23 return 0;
26 ACE_HANDLE TextListenerAcceptor::get_handle () const
28 return this->acceptor_.get_handle ();
31 int TextListenerAcceptor::handle_input (ACE_HANDLE)
33 ACE_DEBUG ((LM_INFO,
34 ACE_TEXT ("TextListenerAcceptor - connection request\n" )));
35 return 0;
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[])
48 ACE_UNUSED_ARG(argc);
50 ACE_INET_Addr addr (argv[1]);
52 if (this->open (addr) < 0)
53 ACE_ERROR_RETURN ((LM_ERROR,
54 ACE_TEXT ("%p\n"),
55 ACE_TEXT ("TextListener - open")),
56 -1);
57 return 0;
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 ()
73 return -1;
76 CallerId *TextListenerAcceptor::retrieve_callerId ()
78 return 0;
81 int TextListenerAcceptor::play_message (ACE_FILE_Addr &addr)
83 ACE_UNUSED_ARG(addr);
84 return 0;
87 MessageType *TextListenerAcceptor::record_message (ACE_FILE_Addr &addr)
89 ACE_UNUSED_ARG(addr);
90 return 0;
94 // Listing 01 code/ch18
95 TextListener::TextListener (TextListenerAcceptor *acceptor)
96 : acceptor_(acceptor)
98 ACE_TRACE ("TextListener ctor");
100 ACE_NEW (this->command_stream_, CommandStream (&(this->peer_)));
101 this->command_stream_->open (0);
103 // Listing 01
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_;
129 // Listing 02
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_);
143 return caller_id;
145 // Listing 03
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"));
161 ACE_FILE_IO *file;
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);
166 else
167 ACE_ERROR_RETURN
168 ((LM_ERROR, ACE_TEXT ("Invalid message type %d\n"),
169 type->get_codec ()), -1);
170 int rval = this->play_message (temp);
171 file->remove ();
172 return rval;
174 // Listing 04
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)
184 return 0;
186 return new MessageType (MessageType::RAWTEXT, addr);
188 // Listing 05
190 // Listing 06 code/ch18
191 void TextListener::release ()
193 delete this;
195 // Listing 06