<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Detecting a doi collision]]></title><description><![CDATA[<p dir="auto">hi,<br />
when the bacnet4j app monitors other bacnet devices (notifications and cov) then detecting a doi collision gets important.<br />
when two davices do have the same doi then one device will miss event data.</p>
<p dir="auto">the iamrequest.java has to be modified as follows:</p>
<pre><code>
   /**
     * see if the address is my own local echo
     * 
     * @param from
     * @return
     */
    private boolean localEcho(Address from, Address[] localAddresses)
    {
    	for ( Address addr : localAddresses)
    	{
    		boolean equal = addr.equals(from);
    		if (equal)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    
   // modified
    @Override
    public void handle(LocalDevice localDevice, Address from, Network network) {
        // Make sure we're not hearing from ourselves.
    	
    	int myDoi = localDevice.getConfiguration().getInstanceId();
    	int remoteDoi = iAmDeviceIdentifier.getInstanceNumber();
        if (remoteDoi == myDoi)
        {
        	// get my bacnet address and compare the addresses
        	Address[] myAddresses = localDevice.getAllLocalAddresses();
        	boolean lEcho = localEcho(from, myAddresses);
        	if ( lEcho)
        	{
        		return;
         	}
        	else
        	{
        		System.out.println("another instance with my doi found!");
        	}
        }

        // Register the device in the list of known devices.
        RemoteDevice d = localDevice.getRemoteDeviceCreate(remoteDoi, from, network);
        d.setMaxAPDULengthAccepted(maxAPDULengthAccepted.intValue());
        d.setSegmentationSupported(segmentationSupported);
        d.setVendorId(vendorId.intValue());

        // Fire the appropriate event.
        localDevice.getEventHandler().fireIAmReceived(d);
    }

</code></pre>
<p dir="auto">in localdevice.java is the function getLocalIPAdress, but we have multihomed linux boxes - a pain for java developers  :wink:<br />
so we need to add a new function:</p>
<pre><code>
    public Address[] getAllLocalAddresses()
    {
        try 
        {
	    	ArrayList&lt;Address&gt; lAddr = new ArrayList&lt;Address&gt;();
	        for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
	            for (InetAddress addr : Collections.list(iface.getInetAddresses())) 
	            {
	                if (!addr.isLoopbackAddress() &amp;&amp; addr.isSiteLocalAddress())
	                {
	                	byte[] addrBytes = addr.getAddress();
	                	Address address = new Address(addrBytes, messageControl.getPort());
	                	lAddr.add(address);
	                }
	             }
	        }
	  	    	
	    	int elems = lAddr.size();
	    	Address[] retVal = new Address[elems];
	    	lAddr.toArray(retVal);
	    	return retVal;
        }
        catch (Exception e) 
        {
            // Should never happen, so just wrap in a RuntimeException
            throw new RuntimeException(e);
        }
   }

</code></pre>
<p dir="auto">now you can easily detect if there is another device wioth the same doi:</p>
<pre><code>
	public boolean isUniqueInstanceNumber()
	{
		int deviceID = getConfiguration().getInstanceId();
		RemoteDevice rd = getRemoteDevice(deviceID);
		if (null != rd)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

</code></pre>
<p dir="auto">some functionality which is needed in our daily work.</p>
<p dir="auto">robert</p>
]]></description><link>https://forum.mango-os.com/topic/666/detecting-a-doi-collision</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 07:14:47 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/666.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Dec 2010 11:01:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Detecting a doi collision on Mon, 06 Dec 2010 15:49:38 GMT]]></title><description><![CDATA[<p dir="auto">Thanks Robert. This is been added in similar form to the code base. It imposed a difference to the public interface in LocalDevice though, in that the getAddress method now take an InetAddress parameter. The equivalent code is now:</p>
<pre><code>d.getAddress(d.getDefaultLocalInetAddress())
</code></pre>
<p dir="auto">All code changes have been checked into CVS.</p>
]]></description><link>https://forum.mango-os.com/post/5056</link><guid isPermaLink="true">https://forum.mango-os.com/post/5056</guid><dc:creator><![CDATA[mlohbihler]]></dc:creator><pubDate>Mon, 06 Dec 2010 15:49:38 GMT</pubDate></item></channel></rss>