Adding Fidelius manual.
[iotcloud.git] / version1 / src / java / iotcloud / CloudComm.java
index ac906b145321606ba80b9246f1e051deebc10894..5a55f587e3e21f2476b71d8759ec5864dc6a3adb 100644 (file)
@@ -24,7 +24,7 @@ class CloudComm {
        static final int SALT_SIZE = 8;
        byte salt[];
        Table table;
-       
+
        /**
         * Empty Constructor needed for child class.
         */
@@ -37,8 +37,8 @@ class CloudComm {
         */
 
        CloudComm(Table _table, String _baseurl, String _password) {
-               this.table=_table;
-               this.baseurl=_baseurl;
+               this.table = _table;
+               this.baseurl = _baseurl;
                this.password = _password;
                this.random = new SecureRandom();
        }
@@ -64,13 +64,13 @@ class CloudComm {
 
        private void initCrypt() {
                try {
-                       SecretKeySpec key=initKey();
+                       SecretKeySpec key = initKey();
                        password = null; // drop password
                        mac = Mac.getInstance("HmacSHA256");
                        mac.init(key);
-                       encryptCipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
+                       encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
-                       decryptCipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
+                       decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                        decryptCipher.init(Cipher.DECRYPT_MODE, key);
                } catch (Exception e) {
                        e.printStackTrace();
@@ -83,27 +83,27 @@ class CloudComm {
         */
 
        private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
-               String reqstring=isput?"req=putslot":"req=getslot";
-               String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
+               String reqstring = isput ? "req=putslot" : "req=getslot";
+               String urlstr = baseurl + "?" + reqstring + "&seq=" + sequencenumber;
                if (maxentries != 0)
-                       urlstr += "&max="+maxentries;
+                       urlstr += "&max=" + maxentries;
                return new URL(urlstr);
        }
-       
+
        public void setSalt() {
                try {
                        salt = new byte[SALT_SIZE];
                        random.nextBytes(salt);
-                       URL url=new URL(baseurl+"?req=setsalt");
-                       URLConnection con=url.openConnection();
+                       URL url = new URL(baseurl + "?req=setsalt");
+                       URLConnection con = url.openConnection();
                        HttpURLConnection http = (HttpURLConnection) con;
                        http.setRequestMethod("POST");
                        http.setFixedLengthStreamingMode(salt.length);
                        http.setDoOutput(true);
                        http.connect();
-                       OutputStream os=http.getOutputStream();
+                       OutputStream os = http.getOutputStream();
                        os.write(salt);
-                       int responsecode=http.getResponseCode();
+                       int responsecode = http.getResponseCode();
                        if (responsecode != HttpURLConnection.HTTP_OK)
                                throw new Error("Invalid response");
                } catch (Exception e) {
@@ -114,20 +114,20 @@ class CloudComm {
        }
 
        private void getSalt() throws Exception {
-               URL url=new URL(baseurl+"?req=getsalt");
-               URLConnection con=url.openConnection();
+               URL url = new URL(baseurl + "?req=getsalt");
+               URLConnection con = url.openConnection();
                HttpURLConnection http = (HttpURLConnection) con;
                http.setRequestMethod("POST");
                http.connect();
-               
-               InputStream is=http.getInputStream();
-               DataInputStream dis=new DataInputStream(is);
-               int salt_length=dis.readInt();
-               byte [] tmp=new byte[salt_length];
+
+               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;
+               salt = tmp;
        }
-       
+
        /*
         * API for putting a slot into the queue.  Returns null on success.
         * On failure, the server will send slots with newer sequence
@@ -140,13 +140,13 @@ class CloudComm {
                                getSalt();
                                initCrypt();
                        }
-                       
-                       long sequencenumber=slot.getSequenceNumber();
-                       byte[] bytes=slot.encode(mac);
+
+                       long sequencenumber = slot.getSequenceNumber();
+                       byte[] bytes = slot.encode(mac);
                        bytes = encryptCipher.doFinal(bytes);
 
-                       URL url=buildRequest(true, sequencenumber, max);
-                       URLConnection con=url.openConnection();
+                       URL url = buildRequest(true, sequencenumber, max);
+                       URLConnection con = url.openConnection();
                        HttpURLConnection http = (HttpURLConnection) con;
 
                        http.setRequestMethod("POST");
@@ -154,12 +154,12 @@ class CloudComm {
                        http.setDoOutput(true);
                        http.connect();
 
-                       OutputStream os=http.getOutputStream();
+                       OutputStream os = http.getOutputStream();
                        os.write(bytes);
 
-                       InputStream is=http.getInputStream();
-                       DataInputStream dis=new DataInputStream(is);
-                       byte[] resptype=new byte[7];
+                       InputStream is = http.getInputStream();
+                       DataInputStream dis = new DataInputStream(is);
+                       byte[] resptype = new byte[7];
                        dis.readFully(resptype);
                        if (Arrays.equals(resptype, "getslot".getBytes()))
                                return processSlots(dis);
@@ -184,20 +184,20 @@ class CloudComm {
                                getSalt();
                                initCrypt();
                        }
-                       
-                       URL url=buildRequest(false, sequencenumber, 0);
-                       URLConnection con=url.openConnection();
+
+                       URL url = buildRequest(false, sequencenumber, 0);
+                       URLConnection con = url.openConnection();
                        HttpURLConnection http = (HttpURLConnection) con;
                        http.setRequestMethod("POST");
                        http.connect();
-                       InputStream is=http.getInputStream();
+                       InputStream is = http.getInputStream();
+
+                       DataInputStream dis = new DataInputStream(is);
 
-                       DataInputStream dis=new DataInputStream(is);
-                       
-                       byte[] resptype=new byte[7];
+                       byte[] resptype = new byte[7];
                        dis.readFully(resptype);
                        if (!Arrays.equals(resptype, "getslot".getBytes()))
-                               throw new Error("Bad Response: "+new String(resptype));
+                               throw new Error("Bad Response: " + new String(resptype));
                        else
                                return processSlots(dis);
                } catch (Exception e) {
@@ -212,19 +212,19 @@ class CloudComm {
         */
 
        private Slot[] processSlots(DataInputStream dis) throws Exception {
-               int numberofslots=dis.readInt();
-               int[] sizesofslots=new int[numberofslots];
-               Slot[] slots=new Slot[numberofslots];
-               for(int i=0; i<numberofslots; i++)
-                       sizesofslots[i]=dis.readInt();
-
-               for(int i=0; i<numberofslots; i++) {
-                       byte[] data=new byte[sizesofslots[i]];
+               int numberofslots = dis.readInt();
+               int[] sizesofslots = new int[numberofslots];
+               Slot[] slots = new Slot[numberofslots];
+               for (int i = 0; i < numberofslots; i++)
+                       sizesofslots[i] = dis.readInt();
+
+               for (int i = 0; i < numberofslots; i++) {
+                       byte[] data = new byte[sizesofslots[i]];
                        dis.readFully(data);
 
                        data = decryptCipher.doFinal(data);
 
-                       slots[i]=Slot.decode(table, data, mac);
+                       slots[i] = Slot.decode(table, data, mac);
                }
                dis.close();
                return slots;