import java.rmi.*; import java.io.*; import java.util.*; public class RMIClient { public static void main(String[] args) { if (args.length < 5) { System.err.println("Usage: at least 5 args\n" + "Please read \"README.doc\" first."); return; } String servername = args[0]; int port = Integer.parseInt(args[1]); String operation = args[2]; String username = args[3]; String password = args[4]; try { String registration = "rmi://" + servername + ":" + port + "/RMIInterface"; Remote remoteService = Naming.lookup(registration); RMIInterface Service = (RMIInterface) remoteService; BufferedReader input = new BufferedReader(new InputStreamReader( System.in)); // operate the args String[] cmd = args; String otherusername = ""; String title = ""; int meetingid = 0; Date start = null; Date end = null; if (!operation.equals("register") && !Service.login(username, password)) { System.out.println("Error!"); return; } while (!operation.equals("quit")) { if (operation.equals("help")) { System.out .println("1.If you input any operation else, " + "the program will do nothing and " + "return to the main menu.\n" + "2.The \"start\" and \"end\" time " + "should be in this format: " + "YYYY_MM_DD_H_M_S"); } if (operation.equals("register")) { if (Service.register(username, password)) System.out.println("Successful!"); else { System.out.println("Failed!"); return; } } if (operation.equals("add")) { if (cmd.length < 9) { System.err.println("Usage: at least 9 args\n" + "Please read \"README.doc\" first."); return; } otherusername = cmd[5]; start = toDate(cmd[6]); end = toDate(cmd[7]); title = cmd[8]; if (start == null || end == null) { System.out.println("Failed!"); } else { if (Service.add(username, password, otherusername, title, start, end)) { System.out.println("Successful!"); } else { System.out.println("Failed!"); } } } if (operation.equals("delete")) { if (cmd.length < 6) { System.err.println("Usage: at least 6 args\n" + "Please read \"README.doc\" first."); return; } meetingid = Integer.parseInt(cmd[5]); if (Service.delete(username, password, meetingid)) { System.out.println("Successful!"); } else { System.out.println("Failed!"); } } if (operation.equals("clear")) { if (Service.clear(username, password)) { System.out.println("Successful!"); } else { System.out.println("Failed!"); } } if (operation.equals("query")) { if (cmd.length < 7) { System.err.println("Usage: at least 7 args"); return; } start = toDate(cmd[5]); end = toDate(cmd[6]); if (start == null || end == null) { System.out.println("Failed!"); } else { Vector v = Service.query(username, password, start, end); if (v == null) System.out.println("Failed!"); else if (v.isEmpty()) System.out.println("No meetings found."); else System.out.println(v); } } displayMenu(); System.out.println("Input an operation: "); operation = input.readLine(); int sumArgs = 0; if (operation.equals("add")) sumArgs = 4; if (operation.equals("delete")) sumArgs = 1; if (operation.equals("query")) sumArgs = 2; cmd = new String[5 + sumArgs]; for (int i = 0; i < sumArgs; i++) { System.out.println("Input argument " + (i + 1) + ":"); cmd[5 + i] = input.readLine(); } } } catch (Exception e) { System.err.println(e.toString()); } } public static void displayMenu() { System.out.println("RMI Menu:"); System.out.println("\t1. add\n" + "\t\targuments: "); System.out.println("\t2. delete\n" + "\t\targuments: <meetingid>"); System.out.println("\t3. clear\n" + "\t\targuments: no args"); System.out.println("\t4. query\n" + "\t\targuments: <start> <end>"); System.out.println("\t5. help\n" + "\t\targuments: no args"); System.out.println("\t6. quit\n" + "\t\targuments: no args\n"); } public static Date toDate(String date) { StringTokenizer token = new StringTokenizer(date, "_"); if (token.countTokens() != 6) { System.err.println("Error"); return null; } int year = Integer.parseInt(token.nextToken()); int month = Integer.parseInt(token.nextToken()); int day = Integer.parseInt(token.nextToken()); int hour = Integer.parseInt(token.nextToken()); int minute = Integer.parseInt(token.nextToken()); int second = Integer.parseInt(token.nextToken()); Date d = new Date(year, month, day, hour, minute, second); return d; } }