
package org.greenstone.p2p;

import java.net.URL;
import java.util.*;

/**
 * This is the class that provides a set of functions for a Greenstone server to call
 * in order to send information/messages into a GDS network.
 */

public class GreenstoneInterface
{ private GreenstoneClient client;
  private URL              server;

  // temporary variables which will eventually disapper - ignore!
  private static GreenstoneClient lastClient;
  private static NameServer       nameServer = null;
  // end of temporary variables

  /**
   * Make a new interface with a client that will receive the responses
   * to any messages sent.
   *
   * @param: the client to receive responses to messages sent through this
   *         interface.
   */
  public GreenstoneInterface(GreenstoneClient client)
  { this.client = client;
    lastClient = this.client;
    //    /*this.*/nameServer = null;
  }

  /**
   *  Set up a registration for this machine with a GDS server.
   *
   *  The Greenstone machine should take care to store the URL of the
   *  server for later use - don't continually reregister the same
   *  server!
   *
   *  @param: <code>URL</code> the location of the GDS server to register with
   *
   *  @return: <code>boolean</code> whether the registration was successful or not.
   */
  public boolean registerWithServer(URL server)
  { // connect to server
    // 
    if (/*this.*/nameServer == null) {
      /*this.*/nameServer = new NameServer(server.toString());
    }
    this.server = server;
    /*this.*/nameServer.registerServer(this.client.getURL());

    return true;
  }

  /**
   *  Method to unregister from the current GDS server for this Greenstone
   *  machine.
   *
   *  @return: <code>boolean</code> whether the registration was successful or not.
   */
  public boolean unregisterFromServer()
  { /*this.*/nameServer = null;
    return true;
  }

  /**
   *  Send a message into the GDS system through the current GDS server.
   *
   *  Success does not guarantee that you will receive a response.
   *
   *  @param: <code>String</code> the identifier for this type of message
   *  @param: <code>Map</code> a <code>Vector</code> item is associated with
   *          each key in this map.
   *
   *  @return: <code>boolean</code> whether the sending was successful or not.
   */
  public boolean sendMessage(String message, Map parameters)
  { if (/*this.*/nameServer != null)
      { /*this.*/nameServer.sendMessage(this.client.getURL(), message, parameters);
    }
    return false;
  }

  /**
   *  Receive a response to a message sent from this Greenstone machine and
   *  forward it to the current client - won't be needed in the long run, it
   *  is here at present to provide a route for returning responses.
   */
  static public void receiveResponse(URL greenstoneURL, ClientResponse response)
  { lastClient.receiveResponse(null, response);
  }
}