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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/**
* $Id: Answerer.cpp 94345 2011-07-24 04:29:17Z mesnier_p $
*
* Streams Listing 01
*
* An answering machine based on a one-way ACE_Stream
*/
#include "ace/OS_NS_string.h"
#include "ace/Stream.h"
#include "ace/Message_Block.h"
#include "ace/FILE_IO.h"
#include "MessageInfo.h"
#include "Message.h"
#include "BasicTask.h"
#include "EndTask.h"
#include "Util.h"
#include "RecordingDevice.h"
// Listing 21 code/ch18
class AnswerIncomingCall : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("AnswerIncomingCall::process()");
if (message->recorder ()->answer_call () < 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("AnswerIncomingCall")),
-1);
return 0;
}
};
// Listing 21
// Listing 22 code/ch18
class GetCallerId : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("GetCallerId::process()");
CallerId *id;
id = message->recorder ()->retrieve_callerId ();
if (!id)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("GetCallerId")),
-1);
message->caller_id (id);
return 0;
}
};
// Listing 22
// Listing 23 code/ch18
class PlayOutgoingMessage : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("PlayOutgoingMessage::process()");
ACE_FILE_Addr outgoing_message =
this->get_outgoing_message (message);
int pmrv =
message->recorder ()->play_message (outgoing_message);
if (pmrv < 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("PlayOutgoingMessage")),
-1);
return 0;
}
ACE_FILE_Addr get_outgoing_message (Message *)
{
// Exclude 23
return ACE_FILE_Addr (ACE_TEXT ("/tmp/outgoing_message"));
// Exclude 23
}
};
// Listing 23
// Listing 24 code/ch18
class RecordIncomingMessage : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("RecordIncomingMessage::process()");
ACE_FILE_Addr incoming_message =
this->get_incoming_message_queue ();
MessageType *type =
message->recorder ()->record_message (incoming_message);
if (!type)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("RecordIncomingMessage")),
-1);
message->incoming_message (incoming_message, type);
return 0;
}
ACE_FILE_Addr get_incoming_message_queue (void)
{
// Exclude 24
return ACE_FILE_Addr (ACE_TEXT ("/tmp/incoming_message"));
// Exclude 24
}
};
// Listing 24
// Listing 25 code/ch18
class ReleaseDevice : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("ReleaseDevice::process()");
message->recorder ()->release ();
return 0;
}
};
// Listing 25
// Listing 26 code/ch18
class EncodeMessage : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("EncodeMessage::process()");
ACE_FILE_Addr &incoming = message->addr ();
ACE_FILE_Addr addr = this->get_message_destination (message);
if (message->is_text ())
Util::convert_to_unicode (incoming, addr);
else if (message->is_audio ())
Util::convert_to_mp3 (incoming, addr);
else if (message->is_video ())
Util::convert_to_mpeg (incoming, addr);
message->addr (addr);
return 0;
}
ACE_FILE_Addr get_message_destination (Message *)
{
// Exclude 26
return ACE_FILE_Addr (ACE_TEXT ("/tmp/encoded_message"));
// Exclude 26
}
};
// Listing 26
// Listing 27 code/ch18
class SaveMetaData : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("SaveMetaData::process()");
ACE_TString path (message->addr ().get_path_name ());
path += ACE_TEXT (".xml");
ACE_FILE_Connector connector;
ACE_FILE_IO file;
ACE_FILE_Addr addr (path.c_str ());
if (connector.connect (file, addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("create meta-data file")),
0);
file.truncate (0);
this->write (file, "<Message>\n");
// ...
this->write (file, "</Message>\n");
file.close ();
return 0;
}
private:
//FUZZ: disable check_for_lack_ACE_OS
int write (ACE_FILE_IO &file, const char *str)
{
//FUZZ: enable check_for_lack_ACE_OS
return file.send (str, ACE_OS::strlen (str));
}
};
// Listing 27
// Listing 28 code/ch18
class NotifySomeone : public BasicTask
{
protected:
virtual int process (Message *message)
{
ACE_TRACE ("NotifySomeone::process()");
// Format an email to tell someone about the
// newly received message.
// ...
// Display message information in the logfile
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("New message from %s ")
ACE_TEXT ("received and stored at %s\n"),
message->caller_id ()->string (),
message->addr ().get_path_name ()));
return 0;
}
};
// Listing 28
// Listing 10 code/ch18
class RecordingStream : public ACE_Stream<ACE_MT_SYNCH>
{
public:
typedef ACE_Stream<ACE_MT_SYNCH> inherited;
typedef ACE_Module<ACE_MT_SYNCH> Module;
RecordingStream () : inherited()
{ }
// Listing 10
//FUZZ: disable check_for_lack_ACE_OS
// Listing 1000 code/ch18
virtual int open (void *arg,
Module *head = 0, Module *tail = 0)
{
//FUZZ: enable check_for_lack_ACE_OS
if (tail == 0)
ACE_NEW_RETURN (tail,
Module (ACE_TEXT ("End Module"), new TheEndTask ()),
-1);
this->inherited::open (arg, head, tail);
// Listing 1000
// Listing 1001 code/ch18
Module *answerIncomingCallModule;
ACE_NEW_RETURN (answerIncomingCallModule,
Module (ACE_TEXT ("Answer Incoming Call"),
new AnswerIncomingCall ()),
-1);
// Listing 11 code/ch18
Module *getCallerIdModule;
ACE_NEW_RETURN (getCallerIdModule,
Module (ACE_TEXT ("Get Caller ID"), new GetCallerId ()),
-1);
// Listing 11
Module *playOGMModule;
ACE_NEW_RETURN (playOGMModule,
Module (ACE_TEXT ("Play Outgoing Message"),
new PlayOutgoingMessage ()),
-1);
Module *recordModule;
ACE_NEW_RETURN (recordModule,
Module (ACE_TEXT ("Record Incoming Message"),
new RecordIncomingMessage ()),
-1);
Module *releaseModule;
ACE_NEW_RETURN (releaseModule,
Module (ACE_TEXT ("Release Device"),
new ReleaseDevice ()),
-1);
Module *conversionModule;
ACE_NEW_RETURN (conversionModule,
Module (ACE_TEXT ("Encode Message"),
new EncodeMessage ()),
-1);
Module *saveMetaDataModule;
ACE_NEW_RETURN (saveMetaDataModule,
Module (ACE_TEXT ("Save Meta-Data"),
new SaveMetaData ()),
-1);
Module *notificationModule;
ACE_NEW_RETURN (notificationModule,
Module (ACE_TEXT ("Notify Someone"),
new NotifySomeone ()),
-1);
// Listing 1001
// Listing 12 code/ch18
if (this->push (notificationModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("notificationModule")),
-1);
if (this->push (saveMetaDataModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("saveMetaDataModule")),
-1);
if (this->push (conversionModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("conversionModule")),
-1);
if (this->push (releaseModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("releaseModule")),
-1);
if (this->push (recordModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("recordModule")),
-1);
if (this->push (playOGMModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("playOGMModule")),
-1);
if (this->push (getCallerIdModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n"),
ACE_TEXT ("getCallerIdModule")),
-1);
if (this->push (answerIncomingCallModule) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Failed to push %p\n")
ACE_TEXT ("answerIncomingCallModule")),
-1);
// Listing 12
return 0;
}
// Listing 13 code/ch18
int record (RecordingDevice *recorder)
{
ACE_Message_Block * mb = 0;
ACE_NEW_RETURN (mb, ACE_Message_Block (sizeof(Message)), -1);
Message *message = (Message *)mb->wr_ptr ();
mb->wr_ptr (sizeof(Message));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("RecordingStream::record() - ")
ACE_TEXT ("message->recorder(recorder)\n")));
message->recorder (recorder);
int rval = this->put (mb);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("RecordingStream::record() - ")
ACE_TEXT ("this->put() returns %d\n"),
rval));
return rval;
}
// Listing 13
};
// Listing 1 code/ch18
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
RecordingDevice *recorder =
RecordingDeviceFactory::instantiate (argc, argv);
// Listing 1
// Listing 2 code/ch18
RecordingStream *recording_stream;
ACE_NEW_RETURN (recording_stream, RecordingStream, -1);
if (recording_stream->open (0) < 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("RecordingStream->open()")),
0);
// Listing 2
// Listing 3 code/ch18
for (;;)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Waiting for incoming message\n")));
RecordingDevice *activeRecorder =
recorder->wait_for_activity ();
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Initiating recording process\n")));
recording_stream->record (activeRecorder);
}
// Listing 3
ACE_NOTREACHED (return 0;)
}
|