00001 // 00002 // messager - This is a super-class/interface describing things that send 00003 // messages (and/or data). It holds a list of recipients (Messagables), 00004 // and the necessary functionality to deal with it. 00005 // 00006 00007 #include "consmgr.h" 00008 #include "messager.h" 00009 #include "guard.h" 00010 00011 Messager::Messager() 00012 { 00013 int ret; 00014 00015 ret = pthread_mutex_init(&recip_mutex, NULL); 00016 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(recip_mutex)"); 00017 00018 return; 00019 } 00020 00021 Messager::~Messager(void) 00022 { 00023 int ret; 00024 00025 ret = pthread_mutex_destroy(&recip_mutex); 00026 PTHREAD_CHECK_AND_THROW(ret, "mutex_destroy(recip_mutex)"); 00027 00028 return; 00029 } 00030 00031 // Add a recipient to the list of recipients for this Messager's data 00032 void 00033 Messager::add_recipient(const Messageable *outlet) 00034 { 00035 if (!outlet) 00036 throw(CMInvalidParameter("Tried to add a NULL Messageble* to recipient list")); 00037 00038 Guard locker(&recip_mutex); 00039 00040 this->recipients.push_back(const_cast<Messageable*>(outlet)); 00041 00042 return; 00043 } 00044 00045 // Delete a recipient from the list (set) of recipients for this Messager 00046 void 00047 Messager::del_recipient(const Messageable *messageable) 00048 { 00049 Guard locker(&recip_mutex); 00050 00051 this->recipients.remove(const_cast<Messageable*>(messageable)); 00052 00053 return; 00054 } 00055 00059 void 00060 Messager::send_message(MessageP &message) const 00061 { 00062 // XXX - Is there something I need to do to add to message's ref-count? 00063 00064 Guard locker(&recip_mutex); 00065 00066 for (recipient_const_iterator ri = recipients.begin() ; 00067 ri != recipients.end() ; ri++) { 00068 clog << "Sending message " << message->name(); 00069 if (message->type() == Message::M_Data) { 00070 clog << " (" 00071 << dynamic_cast<Data*>(MessageP::GetPointer(message))->len() 00072 << " bytes)"; 00073 } 00074 clog << " to recipient " << (*ri)->get_id() << endl; 00075 (*ri)->queue_message(message); 00076 clog << "Message " << message << " sent." << endl; 00077 } 00078 00079 return; 00080 00081 } 00082