return reservations;
}
public static void main(String[] args) throws Exception {
final ConsoleReader reader = new ConsoleReader();
ARS ars = null;
while (true) {
String line = reader.readLine(">");
if (line == null)
break;
final String[] tokens = line.split("\\s+");
if (tokens[0].equals("reserve") && tokens.length >= 4 && ars != null) {
// start up multiple threads all trying to reserve the same resource, no more than one should succeed
final ARS fars = ars;
ArrayList<Thread> threads = new ArrayList<Thread>();
for (int i = 3; i < tokens.length; i++) {
final int whoIndex = i;
Runnable reservationTask = new Runnable() {
@Override
public void run() {
try {
reader.println(" " + String.format("%20s", tokens[whoIndex]) + " : " + fars.reserve(tokens[1], tokens[2], tokens[whoIndex]));
} catch (Exception e) {
e.printStackTrace();
}
}
};
threads.add(new Thread(reservationTask));
}
for (Thread thread : threads)
thread.start();
for (Thread thread : threads)
thread.join();
} else if (tokens[0].equals("cancel") && tokens.length == 4 && ars != null) {
ars.cancel(tokens[1], tokens[2], tokens[3]);
} else if (tokens[0].equals("list") && tokens.length == 3 && ars != null) {
List<String> reservations = ars.list(tokens[1], tokens[2]);
if (reservations.size() > 0) {
reader.println(" Reservation holder : " + reservations.get(0));
if (reservations.size() > 1)
reader.println(" Wait list : " + reservations.subList(1, reservations.size()));
}
} else if (tokens[0].equals("quit") && tokens.length == 1) {
break;
} else if (tokens[0].equals("connect") && tokens.length == 6 && ars == null) {
ZooKeeperInstance zki = new ZooKeeperInstance(new ClientConfiguration().withInstance(tokens[1]).withZkHosts(tokens[2]));
Connector conn = zki.getConnector(tokens[3], new PasswordToken(tokens[4]));
if (conn.tableOperations().exists(tokens[5])) {
ars = new ARS(conn, tokens[5]);
reader.println(" connected");
} else
reader.println(" No Such Table");
} else {
System.out.println(" Commands : ");
if (ars == null) {
reader.println(" connect <instance> <zookeepers> <user> <pass> <table>");
} else {
reader.println(" reserve <what> <when> <who> {who}");
reader.println(" cancel <what> <when> <who>");
reader.println(" list <what> <when>");
}
}
}
}