}
}
+ /**
+ * 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.
*/
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
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;
try {
url = new URL(baseurl + "?req=getsalt");
} catch (Exception e) {
- e.printStackTrace();
+ // e.printStackTrace();
throw new Error("getSlot failed");
}
try {
} 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");
}
}
* 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();
}
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");
}
} catch (SocketTimeoutException e) {
throw new ServerException("putSlot failed", ServerException.TypeInputTimeout);
} catch (Exception e) {
- e.printStackTrace();
+ // e.printStackTrace();
throw new Error("putSlot failed");
}
}
* 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();
}
} catch (ServerException e) {
throw e;
} catch (Exception e) {
- e.printStackTrace();
+ // e.printStackTrace();
throw new Error("getSlots failed");
}
} catch (SocketTimeoutException e) {
throw new ServerException("getSlots failed", ServerException.TypeInputTimeout);
} catch (Exception e) {
- e.printStackTrace();
+ // e.printStackTrace();
throw new Error("getSlots failed");
}
}
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);
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 {