more code
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Sat, 23 Jul 2016 17:34:20 +0000 (10:34 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Sat, 23 Jul 2016 17:34:20 +0000 (10:34 -0700)
src/java/iotcloud/CloudComm.java
src/java/iotcloud/IoTString.java [new file with mode: 0644]
src/java/iotcloud/Slot.java
src/java/iotcloud/Table.java

index 4bc223b..4f64036 100644 (file)
@@ -8,11 +8,13 @@ class CloudComm {
        String baseurl;
        Cipher encryptcipher;
        Cipher decryptcipher;
+       Mac mac;
        
-       CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt) {
+       CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
                this.baseurl=_baseurl;
                this.encryptcipher = _encrypt;
                this.decryptcipher = _decrypt;
+               this.mac = _mac;
        }
        
        private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
@@ -25,15 +27,17 @@ class CloudComm {
 
        public Slot[] putSlot(Slot slot, int max) throws IOException{
                long sequencenumber=slot.getSequenceNumber();
+               byte[] bytes=slot.encode(mac);
+               
                URL url=buildRequest(true, sequencenumber, max);
                URLConnection con=url.openConnection();
                HttpURLConnection http = (HttpURLConnection) con;
                http.setRequestMethod("POST");
-               http.setFixedLengthStreamingMode(slot.getBytes().length);
+               http.setFixedLengthStreamingMode(bytes.length);
                http.setDoOutput(true);
                http.connect();
                OutputStream os=http.getOutputStream();
-               os.write(slot.getBytes());
+               os.write(bytes);
                System.out.println(http.getResponseMessage());
 
                InputStream is=http.getInputStream();
@@ -41,7 +45,7 @@ class CloudComm {
                byte[] resptype=new byte[7];
                dis.readFully(resptype);
                if (Arrays.equals(resptype, "getslot".getBytes()))
-                       return processSlots(dis, sequencenumber);
+                       return processSlots(dis);
                else if (Arrays.equals(resptype, "putslot".getBytes()))
                        return null;
                else
@@ -63,10 +67,10 @@ class CloudComm {
                if (!Arrays.equals(resptype, "getslot".getBytes()))
                        throw new Error("Bad Response: "+new String(resptype));
                else
-                       return processSlots(dis, sequencenumber);
+                       return processSlots(dis);
        }
                
-       Slot[] processSlots(DataInputStream dis, long sequencenumber) throws IOException {
+       Slot[] processSlots(DataInputStream dis) throws IOException {
                int numberofslots=dis.readInt();
                int[] sizesofslots=new int[numberofslots];
                Slot[] slots=new Slot[numberofslots];
@@ -77,7 +81,7 @@ class CloudComm {
                for(int i=0;i<numberofslots;i++) {
                        byte[] data=new byte[sizesofslots[i]];
                        dis.readFully(data);
-                       slots[i]=new Slot(sequencenumber+i, data);
+                       slots[i]=Slot.decode(data, mac);
                }
                dis.close();
                return slots;
diff --git a/src/java/iotcloud/IoTString.java b/src/java/iotcloud/IoTString.java
new file mode 100644 (file)
index 0000000..1fe7d0e
--- /dev/null
@@ -0,0 +1,38 @@
+package IoTCloud;
+
+import util.Arrays;
+
+public class IoTString {
+       byte[] array;
+       int hashcode;
+
+       public IoTString(byte[] _array) {
+               array=(byte[]) _array.clone();
+               hashcode=Arrays.hashCode(array);
+       }
+
+       public IoTString(String str) {
+               array=str.getBytes();
+               hashcode=Arrays.hashCode(array);
+       }
+
+       public int hashCode() {
+               return hashcode;
+       }
+
+       public String toString() {
+               return new String(array);
+       }
+
+       public byte[] getBytes() {
+               return (byte[]) array.clone();
+       }
+       
+       public boolean equals(Object o) {
+               if (o instanceof IoTString) {
+                       IoTString i=(IoTString)o;
+                       return Arrays.equals(array, i.array);
+               }
+               return false;
+       }
+}
index 841e818..b0002b0 100644 (file)
@@ -41,7 +41,7 @@ class Slot {
                long seqnum=bb.getLong();
                long machineid=bb.getLong();
                int numentries=bb.getInt();
-               Vector<Entry> entries=new Vector();
+               Vector<Entry> entries=new Vector<Entry>();
                for(int i=0;i<numentries;i++) {
                        entries.add(Entry.decode(bb));
                }
index f1087ae..5efe9b2 100644 (file)
@@ -22,7 +22,7 @@ public class Table {
                        decryptCipher.init(Cipher.DECRYPT_MODE, secret);
                        hmac = Mac.getInstance("HmacSHA256");
                        hmac.init(secret);
-                       cloud=new CloudComm(baseurl, encryptCipher, decryptCipher);
+                       cloud=new CloudComm(baseurl, encryptCipher, decryptCipher, hmac);
                } catch (Exception e) {
                        throw new Error("Failed To Initialize Ciphers");
                }
@@ -38,4 +38,7 @@ public class Table {
                        throw new Error("Failed generating key.");
                }
        }
+
+
+
 }