set fcp connection to null if connection failed
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelClient.java
index 5de3aa2..93378d3 100644 (file)
@@ -106,7 +106,7 @@ public class HighLevelClient {
        private final String clientName;
 
        /** The FCP connection to the node. */
-       private FcpConnection fcpConnection;
+       private FcpConnection fcpConnection = null;
 
        /** Listeners for high-level client events. */
        private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
@@ -226,7 +226,7 @@ public class HighLevelClient {
         */
        private void fireProgressReceived(String identifier, HighLevelProgress highLevelProgress) {
                for (HighLevelProgressListener highLevelProgressListener: highLevelProgressListeners) {
-                       highLevelProgressListener.progressReceived(identifier, highLevelProgress);
+                       highLevelProgressListener.progressReceived(this, identifier, highLevelProgress);
                }
        }
 
@@ -303,13 +303,18 @@ public class HighLevelClient {
         *             if an I/O error occurs communicating with the node
         */
        public HighLevelCallback<ConnectResult> connect(InetAddress address, int port) throws IOException {
-               fcpConnection = new FcpConnection(address, port);
-               fcpConnection.addFcpListener(highLevelClientFcpListener);
-               fcpConnection.connect();
-               ClientHello clientHello = new ClientHello(clientName);
-               connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
-               fcpConnection.sendMessage(clientHello);
-               return connectCallback;
+               try {
+                       fcpConnection = new FcpConnection(address, port);
+                       fcpConnection.addFcpListener(highLevelClientFcpListener);
+                       fcpConnection.connect();
+                       ClientHello clientHello = new ClientHello(clientName);
+                       connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
+                       fcpConnection.sendMessage(clientHello);
+                       return connectCallback;
+               } catch (IOException ioe1) {
+                       fcpConnection = null;
+                       throw ioe1;
+               }
        }
 
        /**
@@ -518,7 +523,6 @@ public class HighLevelClient {
                        fcpConnection.close();
                }
                fcpConnection = null;
-               fireClientDisconnected(throwable);
        }
 
        /**
@@ -711,6 +715,7 @@ public class HighLevelClient {
                        }
                        cancelIdentifier(null);
                        disconnect(throwable);
+                       fireClientDisconnected(throwable);
                }
 
                /**