FileDocCategorySizeDatePackage
SearchCollector.javaAPI DocExample3595Fri Apr 04 17:10:28 BST 1997dcj.examples.Collaborative

SearchCollector

public class SearchCollector extends RMIMediatorImpl

Fields Summary
protected Vector
unassignedSites
protected Hashtable
siteTable
protected SiteIndex
index
Constructors Summary
public SearchCollector(Vector sites)


     
    Enumeration e = sites.elements();
    while (e.hasMoreElements()) {
      String s = (String)e.nextElement();
      addSite(s);
    }
  
Methods Summary
public booleanaddSite(java.lang.String siteName)

    boolean success = false;
    if (!siteTable.get(siteName)) {
      unassignedSites.addElement(siteName);
      success = true;
    }
    return success;
  
public booleanaddSite(java.lang.String siteName, SiteSearcher searcher)

    siteTable.put(siteName, searcher);
    unassignedSites.removeElement(siteName);
    return success;
  
protected booleanassignSites(SiteSearcher searcher)

    boolean success = false;
    String sHost = searcher.getHostName();
    try {
      byte[] hostIP = InetAddress.getByName(sHost).getAddress();
      Enumeration enum = unassignedSites.elements();
      while (enum.hasMoreElements()) {
        String siteName = (String)enum.nextElement();
        try {
          byte[] siteIP = InetAddress.getByName(siteName).getAddress();
          if (hostIP[0] == siteIP[0] && hostIP[1] == siteIP[1]) {
            searcher.addSite(siteName);
            addSite(siteName, searcher);
            success = true;
          }
        }
        catch (Exception e1) {}
      }
    }
    catch (Exception e2) {}

    if (!success) {
      // Couldn't assign a site by address, so pick one at random
      // and give it to this searcher.
      if (unassignedSites.length() > 0) {
        String siteName = (String)unassignedSites.elementAt(0);
        searcher.addSite(siteName);
        addSite(siteName, searcher);
        success = true;
      }
    }

    return success;
  
public SiteIndexgetIndex()

 return index; 
public booleanregister(Identity i, RMICollaborator c)

    boolean success = super.register(i, c);
    if (success) {
      try {
        SiteSearcher searcher = (SiteSearcher)c;
        Vector sites = searcher.getSites();
        if (sites == null) {
          // This searcher doesn't have any sites yet, so give
          // it some of our unassigned sites.
          assignSites(searcher);
        }
        else {
          // The searcher has some sites that it searches already,
          // so add them to our site table.
          Enumeration enum = sites.elements();
          while (enum.hasMoreElements()) {
            String siteName = (String)enum.nextElement();
            addSite(siteName, searcher);
          }
        }
      }
      catch (Exception e) {
        success = false;
        System.out.println("Failed assigning sites.");
        e.printStackTrace();
      }
    }

    return success;
  
public booleanremoveSite(java.lang.String siteName)

    boolean success = false;
    if (siteTable.remove(siteName) || unassignedSites.remove(siteName)) {
      success = true;
    }
    return success;
  
public booleanupdate()

    Vector searchers = getMembers();
    Enumeration enum = searchers.elements();
    while (enum.hasMoreElements()) {
      SiteSearcher s = (SiteSearcher)enum.nextElement();
      Hashtable results = s.doSearch();
      mergeResults(results);
    }