package ActiveMQ;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Map.Entry;

import org.apache.activemq.network.NetworkBridge;
import org.apache.activemq.network.NetworkConnector;

import plugincore.PluginEngine;

class BrokerMonitor implements Runnable 
{
	  private String agentPath;
	  public boolean MonitorActive;
	  public BrokerMonitor(String agentPath)
	  {
	    	this.agentPath = agentPath;
	    	this.MonitorActive = true;
	  }
	  public void shutdown()
	  {
			MonitorActive = false;
	  }
	  public void run() 
	  {
		  try
		  {
			  String brokerAddress = PluginEngine.brokeredAgents.get(agentPath).activeAddress;
			  
			  if((InetAddress.getByName(brokerAddress) instanceof Inet6Address))
        	  {
				  brokerAddress = "[" + brokerAddress + "]";
        	  }
			  NetworkConnector bridge = PluginEngine.broker.AddNetworkConnector(brokerAddress);
			  bridge.start();
			    //PluginEngine.broker.AddNetworkConnector(URI)
	    		while(MonitorActive)
	    		{
	    			System.out.println("Monitoring thread for : " + agentPath);
	    			System.out.println("Started : " + bridge.isStarted());
	    			System.out.println("Static : " + bridge.isStaticBridge());
	    			System.out.println("Broker Name : " + bridge.getBrokerName());
	    			System.out.println("Name : " + bridge.getName());
	    			int count = 0;
	    			for(NetworkBridge b : bridge.activeBridges())
	    			{
	    				System.out.println("Active Bridge:" + count);
	    				System.out.println("local address:" + b.getLocalAddress() + " localbrokername: " + b.getLocalBrokerName() + " remoteaddress: " + b.getRemoteAddress() + " remotebrokerid: " + b.getRemoteBrokerId() +" remotebrokername: "+ b.getRemoteBrokerName());
	    				count++;
	    			}
	    			
	    			Thread.sleep(5000);
	    			//bridge.stop();
	    			//PluginEngine.brokeredAgents.get(agentPath).brokerStatus = BrokerStatusType.FAILED;
	    			//MonitorActive = false;
	    		}
		  }
		  catch(Exception ex)
		  {
	    		
		  }
	  }
}
@@ -1352,16 +1352,54 @@
 nt = 0;%0A
+%09    %09%09%09boolean bridgeActive = false;%0A
 %09    %09%09%09
@@ -1575,21 +1575,48 @@
 ddress()
- + %22 
+);%0A%09    %09%09%09%09System.out.println(%22
 localbro
@@ -1650,21 +1650,48 @@
 erName()
- + %22 
+);%0A%09    %09%09%09%09System.out.println(%22
 remotead
@@ -1725,13 +1725,40 @@
 ss()
- + %22 
+);%0A%09    %09%09%09%09System.out.println(%22
 remo
@@ -1798,12 +1798,40 @@
 Id()
- +%22 
+);%0A%09    %09%09%09%09System.out.println(%22
 remo
@@ -1899,17 +1899,62 @@
 %09    %09%09%09
-%7D
+%09if(b.getRemoteBrokerName().equals(agentPath))
 %0A%09    %09%09
@@ -1954,16 +1954,18 @@
 %09    %09%09%09
+%09
+%7B
 %0A%09    %09%09
@@ -1969,38 +1969,97 @@
  %09%09%09
-Thread.sleep(5000);%0A%09    %09%09%09//
+%09%09bridgeActive = true;%0A%09    %09%09%09%09%7D%0A%09    %09%09%09%7D%0A%09    %09%09%09if(!bridgeActive)%0A%09    %09%09%09%7B%0A%09    %09%09%09%09
 brid
@@ -2069,24 +2069,25 @@
 stop();%0A
+%09
 %09    %09%09%09
 //Plugin
@@ -2078,18 +2078,16 @@
 %09    %09%09%09
-//
 PluginEn
@@ -2161,24 +2161,25 @@
 FAILED;%0A
+%09
 %09    %09%09%09
 //Monito
@@ -2170,18 +2170,16 @@
 %09    %09%09%09
-//
 MonitorA
@@ -2189,24 +2189,71 @@
 ve = false;%0A
+%09    %09%09%09%7D%0A%09    %09%09%09Thread.sleep(5000);%0A%09    %09%09%09%0A
 %09    %09%09%7D%0A%09%09 

Bring it