Minor Registry changes and Controller now registers itself on startup using the registry
diff --git a/src/main/java/net/floodlightcontroller/core/internal/Controller.java b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
index 78b93ed..f288ae4 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/Controller.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
@@ -22,8 +22,9 @@
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
-import java.util.ArrayList;
+import java.net.UnknownHostException;
 import java.nio.channels.ClosedChannelException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -52,13 +53,13 @@
 import net.floodlightcontroller.core.IFloodlightProviderService;
 import net.floodlightcontroller.core.IHAListener;
 import net.floodlightcontroller.core.IInfoProvider;
-import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
-import net.floodlightcontroller.core.IOFMessageListener;
 import net.floodlightcontroller.core.IListener.Command;
+import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
+import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
+import net.floodlightcontroller.core.IOFMessageListener;
 import net.floodlightcontroller.core.IOFSwitch;
 import net.floodlightcontroller.core.IOFSwitchFilter;
 import net.floodlightcontroller.core.IOFSwitchListener;
-import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
 import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
 import net.floodlightcontroller.core.annotations.LogMessageDoc;
 import net.floodlightcontroller.core.annotations.LogMessageDocs;
@@ -77,6 +78,7 @@
 import net.floodlightcontroller.storage.StorageException;
 import net.floodlightcontroller.threadpool.IThreadPoolService;
 import net.onrc.onos.registry.controller.IControllerRegistryService;
+import net.onrc.onos.registry.controller.RegistryException;
 
 import org.jboss.netty.bootstrap.ServerBootstrap;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -109,9 +111,9 @@
 import org.openflow.protocol.OFMessage;
 import org.openflow.protocol.OFPacketIn;
 import org.openflow.protocol.OFPhysicalPort;
-import org.openflow.protocol.OFPortStatus;
 import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
 import org.openflow.protocol.OFPhysicalPort.OFPortState;
+import org.openflow.protocol.OFPortStatus;
 import org.openflow.protocol.OFPortStatus.OFPortReason;
 import org.openflow.protocol.OFSetConfig;
 import org.openflow.protocol.OFStatisticsRequest;
@@ -193,7 +195,7 @@
     protected IThreadPoolService threadPool;
     protected IFlowService flowService;
     protected ITopoRouteService topoRouteService;
-    protected IControllerRegistryService masterHelper;
+    protected IControllerRegistryService registryService;
     
     // Configuration options
     protected int openFlowPort = 6633;
@@ -405,7 +407,7 @@
     }
 
 	public void setMastershipService(IControllerRegistryService serviceImpl) {
-		this.masterHelper = serviceImpl;		
+		this.registryService = serviceImpl;		
 	}
 	
     @Override
@@ -2057,6 +2059,16 @@
         if (controllerId != null) {
             this.controllerId = controllerId;
         }
+        else {
+        	//Try to get the hostname of the machine and use that for controller ID
+        	try {
+    			String hostname = java.net.InetAddress.getLocalHost().getHostName();
+    			this.controllerId = hostname;
+    		} catch (UnknownHostException e) {
+    			// Can't get hostname, we'll just use the default
+    		}
+        }
+        
         log.debug("ControllerId set to {}", this.controllerId);
     }
 
@@ -2112,6 +2124,12 @@
                         "that the system database has failed to start. " +
                         LogMessageDoc.CHECK_CONTROLLER)
     public void startupComponents() {
+    	try {
+			registryService.registerController(controllerId);
+		} catch (RegistryException e2) {
+			log.warn("Registry service error: {}", e2.getMessage());
+		}
+    	
         // Create the table names we use
         storageSource.createTable(CONTROLLER_TABLE_NAME, null);
         storageSource.createTable(SWITCH_TABLE_NAME, null);
diff --git a/src/main/java/net/onrc/onos/registry/controller/IControllerRegistryService.java b/src/main/java/net/onrc/onos/registry/controller/IControllerRegistryService.java
index e924f6a..0971f40 100644
--- a/src/main/java/net/onrc/onos/registry/controller/IControllerRegistryService.java
+++ b/src/main/java/net/onrc/onos/registry/controller/IControllerRegistryService.java
@@ -29,8 +29,9 @@
 	public String getMastershipId ();
 	
 	/**
-	 * Register a controller to the ONOS cluster
+	 * Register a controller to the ONOS cluster. 
 	 * @param controller A string identifying the controller
+	 * @throws errors connecting to registry service, controllerId already registered
 	 */
 	public void registerController(String controllerId) throws RegistryException;
 	
diff --git a/src/main/java/net/onrc/onos/registry/controller/RegistryException.java b/src/main/java/net/onrc/onos/registry/controller/RegistryException.java
index 3b237c2..fbc68a44 100644
--- a/src/main/java/net/onrc/onos/registry/controller/RegistryException.java
+++ b/src/main/java/net/onrc/onos/registry/controller/RegistryException.java
@@ -9,18 +9,16 @@
 		// TODO Auto-generated constructor stub
 	}
 
-	
-	public RegistryException(String message) {
-		super(message);
-		// TODO Auto-generated constructor stub
-	}
-
 	public RegistryException(Throwable cause) {
 		super(cause);
 		// TODO Auto-generated constructor stub
 	}
 	*/
 	
+	public RegistryException(String message) {
+		super(message);
+	}
+	
 	public RegistryException(String message, Throwable cause) {
 		super(message, cause);
 	}
diff --git a/src/main/java/net/onrc/onos/registry/controller/StandaloneRegistry.java b/src/main/java/net/onrc/onos/registry/controller/StandaloneRegistry.java
index e3181fe..8a468bc 100644
--- a/src/main/java/net/onrc/onos/registry/controller/StandaloneRegistry.java
+++ b/src/main/java/net/onrc/onos/registry/controller/StandaloneRegistry.java
@@ -32,7 +32,8 @@
 	public void requestControl(long dpid, ControlChangeCallback cb)
 			throws RegistryException {
 		if (controllerId == null) {
-			throw new RuntimeException("Must register a controller before calling requestControl");
+			throw new RuntimeException(
+					"Must register a controller before calling requestControl");
 		}
 		
 		switchCallbacks.put(HexString.toHexString(dpid), cb);
@@ -75,6 +76,10 @@
 	@Override
 	public void registerController(String controllerId)
 			throws RegistryException {
+		if (this.controllerId != null) {
+			throw new RegistryException(
+					"Controller already registered with id " + this.controllerId);
+		}
 		this.controllerId = controllerId;
 	}
 
diff --git a/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java b/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
index 58b8bc7..1695451 100644
--- a/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
+++ b/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
@@ -22,6 +22,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Charsets;
 import com.netflix.curator.RetryPolicy;
 import com.netflix.curator.framework.CuratorFramework;
 import com.netflix.curator.framework.CuratorFrameworkFactory;
@@ -286,15 +287,20 @@
 	@Override
 	public void registerController(String id) throws RegistryException {
 		//if (!zookeeperEnabled) return;
+		if (controllerId != null) {
+			throw new RegistryException(
+					"Controller already registered with id " + controllerId);
+		}
 		
 		controllerId = id;
 		
-		byte bytes[] = null;
+		byte bytes[] = id.getBytes(Charsets.UTF_8);
+		/*byte bytes[] = null;
 		try {
 			bytes = id.getBytes("UTF-8");
 		} catch (UnsupportedEncodingException e1) {
 			throw new RegistryException("Error encoding string", e1);
-		}
+		}*/
 		
 		String path = controllerPath + "/" + id;
 		
@@ -364,12 +370,13 @@
 				}
 				*/
 				
-				String controllerId = null;
+				/*String controllerId = null;
 				try {
 					controllerId = new String(d.getData(), "UTF-8");
 				} catch (UnsupportedEncodingException e) {
 					log.warn("Encoding exception: {}", e.getMessage());
-				}
+				}*/
+				String controllerId = new String(d.getData(), Charsets.UTF_8);
 				
 				String[] splitted = d.getPath().split("-");
 				int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]);