001package edu.pdx.cs410J.family;
002
003import java.io.*;
004import java.rmi.*;
005import java.text.*;
006import java.util.*;
007
008/**
009 * This program is an RMI client that queries a remote family tree
010 */
011public class GetLiving {
012  private static PrintStream err = System.err;
013  private static PrintStream out = System.out;
014
015  /**
016   * Prints usage information about this program
017   */
018  private static void usage(String s) {
019    err.println("\n** " + s + "\n");
020    err.println("usage: java GetLiving familyName host port");
021    err.println("  -date Date");
022    err.println("");
023    err.println("This program queries a remote family tree to " +
024                "determine which people are alive.");
025    err.println("");
026    err.println("Dates should be in the form MM/DD/YYYY");
027    err.println("");
028    System.exit(1);
029  }
030
031  public static void main(String[] args) {
032    String familyName = null;
033    String host = null;
034    int port = -1;
035    Date date = null;
036
037    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
038
039    for (int i = 0; i < args.length; i++) {
040      if (args[i].equals("-date")) {
041        if (++i >= args.length) {
042          usage("Missing date");
043        }
044
045        try {
046          date = df.parse(args[i]);
047          
048        } catch (ParseException ex) {
049          usage("Malformed date: " + args[i]);
050        }
051
052      } else if (familyName == null) {
053        familyName = args[i];
054
055      } else if (host == null) {
056        host = args[i];
057
058      } else if (port == -1) {
059        try {
060          port = Integer.parseInt(args[i]);
061
062        } catch (NumberFormatException ex) {
063          usage("Invalid port: " + args[i]);
064        }
065
066      } else {
067        usage("Spurious command line: " + args[i]);
068      }
069    }
070
071    if (familyName == null) {
072      usage("Missing family name");
073
074    } else if (host == null) {
075      usage("Missing host");
076
077    } else if (port == -1) {
078      usage("Missing port");
079    }
080
081    // Install an RMISecurityManager, if there is not a
082    // SecurityManager already installed
083    if (System.getSecurityManager() == null) {
084      System.setSecurityManager(new RMISecurityManager());
085    }
086
087    // Look up the remote family tree object
088    String name = "rmi://" + host + ":" + port + "/" + familyName;
089
090    try {
091      RemoteFamilyTree tree = 
092        (RemoteFamilyTree) Naming.lookup(name);
093
094      if (date != null) {
095        Collection living = tree.getLiving(date);
096        out.println("\n" + living.size() + " people were alive on " +
097                    df.format(date) + "\n");
098        Iterator iter = living.iterator();
099        while (iter.hasNext()) {
100          RemotePerson person = (RemotePerson) iter.next();
101          out.println(person.getDescription());
102          out.println("");
103        }
104
105      } else {
106        Collection living = tree.getLiving();
107        out.println("\n" + living.size() + " people are alive\n");
108        Iterator iter = living.iterator();
109        while (iter.hasNext()) {
110          RemotePerson person = (RemotePerson) iter.next();
111          out.println(person.getDescription());
112          out.println("");
113        }
114      }
115
116    } catch (Exception ex) {
117      ex.printStackTrace(err);
118    }
119
120  }
121
122}