Local communication working
[iotcloud.git] / version2 / src / java / iotcloud / CloudComm.java
index 5741019513afcad9027b1d866963bce07d6e77fe..b8c296ba3c0fb3ebeaf8cf482c816414721e3c00 100644 (file)
@@ -74,6 +74,19 @@ class CloudComm {
                }
        }
 
+       /**
+        * Inits all the security stuff
+        */
+       public void initSecurity() throws ServerException {
+               // try to get the salt and if one does not exist set one
+               if (!getSalt()) {
+                       //Set the salt
+                       setSalt();
+               }
+
+               initCrypt();
+       }
+
        /**
         * Inits the HMAC generator.
         */
@@ -109,30 +122,31 @@ class CloudComm {
                return new URL(urlstr);
        }
 
-       public void setSalt() throws ServerException {
+       private void setSalt() throws ServerException {
 
                if (salt != null) {
                        // Salt already sent to server so dont set it again
                        return;
                }
-               byte[] saltTmp = new byte[SALT_SIZE];
-               random.nextBytes(saltTmp);
-
-               URL url = null;
-               URLConnection con = null;
-               HttpURLConnection http = null;
 
                try {
-                       url = new URL(baseurl + "?req=setsalt");
-                       con = url.openConnection();
-                       http = (HttpURLConnection) con;
+                       byte[] saltTmp = new byte[SALT_SIZE];
+                       random.nextBytes(saltTmp);
+
+                       URL url = new URL(baseurl + "?req=setsalt");
+                       URLConnection con = url.openConnection();
+                       HttpURLConnection http = (HttpURLConnection) con;
+
                        http.setRequestMethod("POST");
                        http.setFixedLengthStreamingMode(saltTmp.length);
                        http.setDoOutput(true);
                        http.setConnectTimeout(TIMEOUT_MILLIS);
                        http.connect();
+
                        OutputStream os = http.getOutputStream();
                        os.write(saltTmp);
+                       os.flush();
+
                        int responsecode = http.getResponseCode();
                        if (responsecode != HttpURLConnection.HTTP_OK) {
                                // TODO: Remove this print
@@ -140,33 +154,14 @@ class CloudComm {
                                throw new Error("Invalid response");
                        }
 
+                       salt = saltTmp;
                } catch (Exception e) {
+                       // e.printStackTrace();
                        throw new ServerException("Failed setting salt", ServerException.TypeConnectTimeout);
                }
-
-
-               try {
-                       InputStream is = http.getInputStream();
-                       DataInputStream dis = new DataInputStream(is);
-                       // byte [] tmp = new byte[1];
-                       byte tmp = dis.readByte();
-
-                       if (tmp == 0) {
-                               salt = saltTmp;
-                               initCrypt();
-                       } else {
-                               getSalt(); // there was already a salt so we need to get it
-                       }
-
-               } catch (SocketTimeoutException e) {
-                       throw new ServerException("setSalt failed", ServerException.TypeInputTimeout);
-               } catch (Exception e) {
-                       e.printStackTrace();
-                       throw new Error("setSlot failed");
-               }
        }
 
-       private void getSalt() throws ServerException {
+       private boolean getSalt() throws ServerException {
                URL url = null;
                URLConnection con = null;
                HttpURLConnection http = null;
@@ -174,7 +169,7 @@ class CloudComm {
                try {
                        url = new URL(baseurl + "?req=getsalt");
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("getSlot failed");
                }
                try {
@@ -188,21 +183,34 @@ class CloudComm {
                } catch (SocketTimeoutException e) {
                        throw new ServerException("getSalt failed", ServerException.TypeConnectTimeout);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("getSlot failed");
                }
 
                try {
+
+                       int responsecode = http.getResponseCode();
+                       if (responsecode != HttpURLConnection.HTTP_OK) {
+                               // TODO: Remove this print
+                               // System.out.println(responsecode);
+                               throw new Error("Invalid response");
+                       }
+
                        InputStream is = http.getInputStream();
-                       DataInputStream dis = new DataInputStream(is);
-                       int salt_length = dis.readInt();
-                       byte [] tmp = new byte[salt_length];
-                       dis.readFully(tmp);
-                       salt = tmp;
+                       if (is.available() > 0) {
+                               DataInputStream dis = new DataInputStream(is);
+                               int salt_length = dis.readInt();
+                               byte [] tmp = new byte[salt_length];
+                               dis.readFully(tmp);
+                               salt = tmp;
+                               return true;
+                       } else {
+                               return false;
+                       }
                } catch (SocketTimeoutException e) {
                        throw new ServerException("getSalt failed", ServerException.TypeInputTimeout);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("getSlot failed");
                }
        }
@@ -212,14 +220,16 @@ class CloudComm {
         * On failure, the server will send slots with newer sequence
         * numbers.
         */
-       Slot[] putSlot(Slot slot, int max) throws ServerException {
+       public Slot[] putSlot(Slot slot, int max) throws ServerException {
                URL url = null;
                URLConnection con = null;
                HttpURLConnection http = null;
 
                try {
                        if (salt == null) {
-                               getSalt();
+                               if (!getSalt()) {
+                                       throw new ServerException("putSlot failed", ServerException.TypeSalt);
+                               }
                                initCrypt();
                        }
 
@@ -243,10 +253,12 @@ class CloudComm {
                        os.flush();
 
                        // System.out.println("Bytes Sent: " + bytes.length);
+               } catch (ServerException e) {
+                       throw e;
                } catch (SocketTimeoutException e) {
                        throw new ServerException("putSlot failed", ServerException.TypeConnectTimeout);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("putSlot failed");
                }
 
@@ -268,7 +280,7 @@ class CloudComm {
                } catch (SocketTimeoutException e) {
                        throw new ServerException("putSlot failed", ServerException.TypeInputTimeout);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("putSlot failed");
                }
        }
@@ -277,14 +289,16 @@ class CloudComm {
         * Request the server to send all slots with the given
         * sequencenumber or newer.
         */
-       Slot[] getSlots(long sequencenumber) throws ServerException {
+       public Slot[] getSlots(long sequencenumber) throws ServerException {
                URL url = null;
                URLConnection con = null;
                HttpURLConnection http = null;
 
                try {
                        if (salt == null) {
-                               getSalt();
+                               if (!getSalt()) {
+                                       throw new ServerException("getSlots failed", ServerException.TypeSalt);
+                               }
                                initCrypt();
                        }
 
@@ -300,7 +314,7 @@ class CloudComm {
                } catch (ServerException e) {
                        throw e;
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("getSlots failed");
                }
 
@@ -316,7 +330,7 @@ class CloudComm {
                } catch (SocketTimeoutException e) {
                        throw new ServerException("getSlots failed", ServerException.TypeInputTimeout);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       // e.printStackTrace();
                        throw new Error("getSlots failed");
                }
        }
@@ -329,19 +343,12 @@ class CloudComm {
                int numberofslots = dis.readInt();
                int[] sizesofslots = new int[numberofslots];
 
-
-               // System.out.println("number of slots: " + numberofslots);
-
-
-
                Slot[] slots = new Slot[numberofslots];
                for (int i = 0; i < numberofslots; i++)
                        sizesofslots[i] = dis.readInt();
 
                for (int i = 0; i < numberofslots; i++) {
 
-                       // System.out.println("Size of slot: " + sizesofslots[i]);
-
                        byte[] data = new byte[sizesofslots[i]];
                        dis.readFully(data);
 
@@ -467,14 +474,16 @@ class CloudComm {
        public void close() {
                doEnd = true;
 
-               try {
-                       localServerThread.join();
-               } catch (Exception e) {
-                       e.printStackTrace();
-                       throw new Error("Local Server thread join issue...");
+               if (localServerThread != null) {
+                       try {
+                               localServerThread.join();
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                               throw new Error("Local Server thread join issue...");
+                       }
                }
 
-               System.out.println("Done Closing");
+               // System.out.println("Done Closing Cloud Comm");
        }
 
        protected void finalize() throws Throwable {