From abc52a83c17dace61cfd7583ced8b758cb31daff Mon Sep 17 00:00:00 2001
From: Reiner Hauser <Reiner.Hauser@cern.ch>
Date: Wed, 4 Nov 2015 14:46:56 +0000
Subject: [PATCH] Publish multicast address to initial partition even if
 statically configured.

In addition, if there is already an entry for this MC address, but with
a different partition name, the configuration will fail.
---
 src/NameService.cxx | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/NameService.cxx b/src/NameService.cxx
index d464fc8..0fb7f43 100644
--- a/src/NameService.cxx
+++ b/src/NameService.cxx
@@ -1,4 +1,6 @@
 
+#include "boost/lexical_cast.hpp"
+
 #include "asyncmsg/NameService.h"
 #include "transport/Interface.h"
 
@@ -351,10 +353,12 @@ namespace daq {
 
         std::string NameService::allocate_multicast_address(const std::string& addr, const std::string& network)
         {
-            MultiCastGroup group;
+	    const std::string    name_prefix("RunParams.MultiCast-");
+	    const std::string    group_prefix("224.100.100.");
 
+            MultiCastGroup group;
 
-            group.Partition = m_partition.name();
+            group.Partition        = m_partition.name();
             group.MulticastAddress = addr;
             group.MulticastNetwork = network;
 
@@ -376,11 +380,10 @@ namespace daq {
                 if(!found) {
                     // allocate a new group.
 
-                    unsigned short    index = 1;
-                    std::string       name_prefix = "RunParams.MultiCast-";
+                    unsigned short index = 1;
 
                     group.Partition = m_partition.name();
-                    group.MulticastAddress = std::string("224.100.100.") + boost::lexical_cast<std::string>(index);
+                    group.MulticastAddress = group_prefix + boost::lexical_cast<std::string>(index);
                     group.MulticastNetwork = network;
 
 		    ISInfoDictionary  dict(IPCPartition("initial"));
@@ -393,7 +396,7 @@ namespace daq {
                         } catch(...) {
                             // try next
                             index++;
-			    group.MulticastAddress = std::string("224.100.100.") + boost::lexical_cast<std::string>(index);
+			    group.MulticastAddress = group_prefix + boost::lexical_cast<std::string>(index);
                         }
                     }
                 }
@@ -402,7 +405,25 @@ namespace daq {
                     // A dynamic address was requested, but we could not allocate one...
                     throw CannotAllocateMulticast(ERS_HERE);
                 }
-            }    
+            } else {
+		// Statically configured address
+		MultiCastGroupNamed checkGroup(IPCPartition("initial"), name_prefix + group.MulticastAddress);
+		if(checkGroup.isExist()) {
+		    checkGroup.checkout();
+
+		    // The partition names must match, or there is the potential that two
+		    // partitions use the same MC group.
+		    if(checkGroup.Partition != group.Partition) {
+			throw CannotAllocateMulticast(ERS_HERE);
+		    }
+		} else {
+		    // publish our new object to say what we are using.
+		    checkGroup.Partition        = group.Partition;
+		    checkGroup.MulticastAddress = group.MulticastAddress;
+		    checkGroup.MulticastNetwork = group.MulticastNetwork;
+		    checkGroup.checkin();
+		}
+	    }
 
             ISInfoDictionary  dict(m_partition);
             ISServerIterator servers(m_partition, m_is_server + ".*");
-- 
GitLab