Thomas Vachuska
Committed by Gerrit Code Review

Changed default borrow behaviour to simply get current cell definition if a rese…

…rvation exists already.

Change-Id: I365233a78be6033d176e33c3c3b3ad33f791d85e
......@@ -113,7 +113,7 @@ function cell {
case "$cell" in
"borrow")
aux="/tmp/cell-$$"
curl -sS -X POST "http://$CELL_WARDEN:4321/?user=$(id -un)&duration=${2:-60}" \
curl -sS -X POST "http://$CELL_WARDEN:4321/?user=$(id -un)&duration=${2:-0}" \
-d "$(cat ~/.ssh/id_rsa.pub)" > $aux
. $aux
rm -f $aux
......
......@@ -49,12 +49,13 @@ class Warden {
private static final String USER_NOT_NULL = "User name cannot be null";
private static final String KEY_NOT_NULL = "User key cannot be null";
private static final String UTF_8 = "UTF-8";
private static final long TIMEOUT = 3;
private static final String AUTHORIZED_KEYS = "authorized_keys";
private static final long TIMEOUT = 10; // 10 seconds
private static final int MAX_MINUTES = 240; // 4 hours max
private static final int MINUTE = 60_000; // 1 minute
private static final int DEFAULT_MINUTES = 60;
private final File log = new File("warden.log");
......@@ -153,7 +154,6 @@ class Warden {
synchronized String borrowCell(String userName, String sshKey, int minutes) {
checkNotNull(userName, USER_NOT_NULL);
checkNotNull(sshKey, KEY_NOT_NULL);
checkArgument(minutes > 0, "Number of minutes must be positive");
checkArgument(minutes < MAX_MINUTES, "Number of minutes must be less than %d", MAX_MINUTES);
long now = System.currentTimeMillis();
Reservation reservation = currentUserReservation(userName);
......@@ -161,7 +161,10 @@ class Warden {
Set<String> cells = getAvailableCells();
checkState(!cells.isEmpty(), "No cells are presently available");
String cellName = ImmutableList.copyOf(cells).get(random.nextInt(cells.size()));
reservation = new Reservation(cellName, userName, now, minutes);
reservation = new Reservation(cellName, userName, now, minutes == 0 ? DEFAULT_MINUTES : minutes);
} else if (minutes == 0) {
// If minutes are 0, simply return the cell definition
return getCellDefinition(reservation.cellName);
} else {
reservation = new Reservation(reservation.cellName, userName, now, minutes);
}
......
......@@ -70,7 +70,7 @@ public class WardenServlet extends HttpServlet {
String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8");
String userName = req.getParameter("user");
String sd = req.getParameter("duration");
int duration = isNullOrEmpty(sd) ? 60 : Integer.parseInt(sd);
int duration = isNullOrEmpty(sd) ? 0 : Integer.parseInt(sd);
String cellDefinition = warden.borrowCell(userName, sshKey, duration);
out.println(cellDefinition);
} catch (Exception e) {
......