I3ISU_Opgave11

=Exercise 1 The Message Distribution System=

=Exercise 1.1 Why a template function?= notify is a template function because the sender can then send different kinds of messages to the subscribers. A copy-constructor has to be used because all subscribers gets a unique message. This means the messages has to be copied. This means a general solution that can handle all different kinds of messages.

We have added new comments to the code below. This is to explain the code: code format="cpp" template  void notify(const std::string& msgId, M* m) const {  osapi::ScopedLock lock (m_); SubscriberIdMap::const_iterator iter = sm_.find(msgId); if(iter != sm_.end) // Found entries {     SubscriberIdContainer& subList = iter->second; // Finds subscribers and sends to SubscriberIdContainer.

for(SubscriberIdContainer::const_iterator iterSubs = subList.begin; iterSubs != subList.end ; ++iterSubs ) {        M * tmp = new M(*m); // Message* has a virtual constructor, this is not useable. This way is more general. // Calls the messages copy-constructor. This general way means we can handle all different // kinds of messages iterSubs -> send (tmp ); //Sends copy of message using the send function in subscriber. }  } delete m; // RAII would be better. Automatically frees allocated memory. } code

=Exercise 1.2 API Implementation=

=Exercise 2 RAII is important so lets use it here!= The SubscriberHelper class needs these functions: code format="cpp" MessageDistributionSystem::getInstance.subscribe(msgId, mq_, id_); // For SubscriberHelper MessageDistributionSystem::getInstance.unSubscribemsgId, mq_, id_); // For unSubscribe unSubscribe; // For destructor code

=Exercise 3 Design considerations=