4 import java.util.Arrays;
16 CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
17 this.baseurl=_baseurl;
18 this.encryptcipher = _encrypt;
19 this.decryptcipher = _decrypt;
23 private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
24 String reqstring=isput?"req=putslot":"req=getslot";
25 String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
27 urlstr += "&max="+maxentries;
28 return new URL(urlstr);
31 public Slot[] putSlot(Slot slot, int max) {
33 long sequencenumber=slot.getSequenceNumber();
34 byte[] bytes=slot.encode(mac);
36 URL url=buildRequest(true, sequencenumber, max);
37 URLConnection con=url.openConnection();
38 HttpURLConnection http = (HttpURLConnection) con;
39 http.setRequestMethod("POST");
40 http.setFixedLengthStreamingMode(bytes.length);
41 http.setDoOutput(true);
43 OutputStream os=http.getOutputStream();
45 System.out.println(http.getResponseMessage());
47 InputStream is=http.getInputStream();
48 DataInputStream dis=new DataInputStream(is);
49 byte[] resptype=new byte[7];
50 dis.readFully(resptype);
51 if (Arrays.equals(resptype, "getslot".getBytes()))
52 return processSlots(dis);
53 else if (Arrays.equals(resptype, "putslot".getBytes()))
56 throw new Error("Bad response to putslot");
57 } catch (Exception e) {
58 throw new Error("putSlot failed");
62 public Slot[] getSlots(long sequencenumber) {
64 URL url=buildRequest(false, sequencenumber, 0);
65 URLConnection con=url.openConnection();
66 HttpURLConnection http = (HttpURLConnection) con;
67 http.setRequestMethod("POST");
69 System.out.println(http.getResponseMessage());
70 InputStream is=http.getInputStream();
72 DataInputStream dis=new DataInputStream(is);
73 byte[] resptype=new byte[7];
74 dis.readFully(resptype);
75 if (!Arrays.equals(resptype, "getslot".getBytes()))
76 throw new Error("Bad Response: "+new String(resptype));
78 return processSlots(dis);
79 } catch (Exception e) {
80 throw new Error("getSlots failed");
84 Slot[] processSlots(DataInputStream dis) throws IOException {
85 int numberofslots=dis.readInt();
86 int[] sizesofslots=new int[numberofslots];
87 Slot[] slots=new Slot[numberofslots];
88 for(int i=0; i<numberofslots; i++)
89 sizesofslots[i]=dis.readInt();
91 for(int i=0; i<numberofslots; i++) {
92 byte[] data=new byte[sizesofslots[i]];
94 slots[i]=Slot.decode(data, mac);