From 70ead9ecdf13fe341b2438ef07954e2e497679c2 Mon Sep 17 00:00:00 2001 From: Brian Demsky Date: Sat, 23 Jul 2016 11:18:51 -0700 Subject: [PATCH] More Java Code --- src/.gitignore | 3 - src/java/iotcloud/CloudComm.java | 89 ++++++++++++++------------ src/java/iotcloud/IoTString.java | 4 +- src/java/iotcloud/RejectedMessage.java | 18 ++++-- src/java/iotcloud/Slot.java | 8 +++ src/java/iotcloud/SlotBuffer.java | 72 +++++++++++++++++++++ src/java/iotcloud/Table.java | 48 +++++++++++++- 7 files changed, 188 insertions(+), 54 deletions(-) delete mode 100644 src/.gitignore create mode 100644 src/java/iotcloud/SlotBuffer.java diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index 0cf5f64..0000000 --- a/src/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.o -*~ -*.class diff --git a/src/java/iotcloud/CloudComm.java b/src/java/iotcloud/CloudComm.java index 4f64036..eb061a4 100644 --- a/src/java/iotcloud/CloudComm.java +++ b/src/java/iotcloud/CloudComm.java @@ -26,55 +26,62 @@ 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(bytes.length); - http.setDoOutput(true); - http.connect(); - OutputStream os=http.getOutputStream(); - os.write(bytes); - System.out.println(http.getResponseMessage()); - - 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); - else if (Arrays.equals(resptype, "putslot".getBytes())) - return null; - else - throw new Error("Bad response to putslot"); + try { + 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(bytes.length); + http.setDoOutput(true); + http.connect(); + OutputStream os=http.getOutputStream(); + os.write(bytes); + System.out.println(http.getResponseMessage()); + + 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); + else if (Arrays.equals(resptype, "putslot".getBytes())) + return null; + else + throw new Error("Bad response to putslot"); + } catch (Exception e) { + throw new Error("putSlot failed"); + } } - public Slot[] getSlots(long sequencenumber) throws IOException { - URL url=buildRequest(false, sequencenumber, 0); - URLConnection con=url.openConnection(); - HttpURLConnection http = (HttpURLConnection) con; - http.setRequestMethod("POST"); - http.connect(); - System.out.println(http.getResponseMessage()); - InputStream is=http.getInputStream(); - - DataInputStream dis=new DataInputStream(is); - byte[] resptype=new byte[7]; - dis.readFully(resptype); - if (!Arrays.equals(resptype, "getslot".getBytes())) - throw new Error("Bad Response: "+new String(resptype)); - else - return processSlots(dis); + public Slot[] getSlots(long sequencenumber) { + try { + URL url=buildRequest(false, sequencenumber, 0); + URLConnection con=url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.connect(); + System.out.println(http.getResponseMessage()); + InputStream is=http.getInputStream(); + + DataInputStream dis=new DataInputStream(is); + byte[] resptype=new byte[7]; + dis.readFully(resptype); + if (!Arrays.equals(resptype, "getslot".getBytes())) + throw new Error("Bad Response: "+new String(resptype)); + else + return processSlots(dis); + } catch (Exception e) { + throw new Error("getSlots failed"); + } } Slot[] processSlots(DataInputStream dis) throws IOException { int numberofslots=dis.readInt(); int[] sizesofslots=new int[numberofslots]; Slot[] slots=new Slot[numberofslots]; - System.out.println(numberofslots); for(int i=0;i= tail) + return head - tail; + return (array.length + head) - tail; + } + + void resize(int newsize) { + if (newsize == (array.length-1)) + return; + Slot[] newarray = new Slot[newsize+1]; + int currsize = size(); + int index = tail; + for(int i=0; i < currsize; i++) { + newarray[i] = array[index]; + if ((++index) == array.length) + index = 0; + } + array = newarray; + tail = currsize; + head = 0; + } + + void putSlot(Slot s) { + array[head]=s; + head++; + if (oldestseqn==0) + oldestseqn = s.getSequenceNumber(); + + if (head==tail) { + tail++; + oldestseqn++; + } + } + + Slot getSlot(long seqnum) { + int diff=(int) (seqnum-oldestseqn); + int index=diff + tail; + if (index > array.length) { + if (head >= tail) + return null; + index-= array.length; + } + + if (index >= array.length || + index >= head) + return null; + + return array[index]; + } + + long getOldestSeqNum() { + return oldestseqn; + } + + long getNewestSeqNum() { + return oldestseqn + size(); + } +} diff --git a/src/java/iotcloud/Table.java b/src/java/iotcloud/Table.java index 5efe9b2..5c6ec6b 100644 --- a/src/java/iotcloud/Table.java +++ b/src/java/iotcloud/Table.java @@ -1,16 +1,21 @@ package iotcloud; import java.util.HashMap; +import java.util.Arrays; import javax.crypto.spec.*; import javax.crypto.*; public class Table { int numslots; - HashMap table=new HashMap(); + HashMap table=new HashMap(); + SlotBuffer buffer; CloudComm cloud; private Mac hmac; + long sequencenumber; public Table(String baseurl, String password) { initCloud(baseurl, password); + buffer = new SlotBuffer(); + sequencenumber = 1; } private void initCloud(String baseurl, String password) { @@ -39,6 +44,47 @@ public class Table { } } + public void update() { + Slot[] newslots=cloud.getSlots(sequencenumber); + validateandupdate(newslots); + } + + void validateandupdate(Slot[] newslots) { + //The cloud communication layer has checked slot HMACs already + //before decoding + + if (newslots.length==0) + return; + + long firstseqnum=newslots[0].getSequenceNumber(); + if (firstseqnum < sequencenumber) + throw new Error("Server sent older slots!"); + + checkHMACChain(newslots); + + if (firstseqnum == (buffer.getNewestSeqNum()+1)) { + //contiguous update + } else { + //non-contiguous update + } + + } + + void checkHMACChain(Slot[] newslots) { + if (newslots[0].getSequenceNumber() == (buffer.getNewestSeqNum()+1)) { + Slot prevslot=buffer.getSlot(buffer.getNewestSeqNum()); + Slot currslot=newslots[0]; + if (!Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC())) + throw new Error("Error in HMAC Chain"); + } + for(int i=1; i < newslots.length; i++) { + Slot prevslot=newslots[i-1]; + Slot currslot=newslots[i]; + if (!Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC())) + throw new Error("Error in HMAC Chain"); + } + } + } -- 2.34.1