import net.jini.core.entry.*;
import net.jini.core.lookup.*; 
import net.jini.core.discovery.*;    
import net.jini.lookup.entry.*; 
import com.sun.jini.lookup.*;
import java.rmi.*;
import java.util.*;
import java.io.*;

/***************************************************************************

    JSortClient -- JSort Example Client

 ***************************************************************************/

    class JSortClient
      {
      int iRecLength;
      JSortInterface jSort;                // interface to the service.



/*    M A I N
 */
/**
 */
public static void main (String[] args)
      {
      String s;
      JSortClient client;

      client = new JSortClient ();
      client.findService ("JSort");

      s = client.releaseRecordsToSort (false);
      client.returnRecordsFromSort (s, false);

      s = client.releaseRecordsToSort (true);
      client.returnRecordsFromSort (s, true);
      }



/*=========================================================================
              The following methods are in alphabetical order
  =========================================================================*/



/*    F I N D   S E R V I C E
 */
/**   This finds the JSort service using Jini's lookup service.
 *
 */
void findService (String sServiceName)
      {
      int iPort;
      String sHost;
      Entry[] aeAttributes;
      LookupLocator lookup;  
      ServiceID id;       
      ServiceRegistrar registrar;
      ServiceTemplate template;

      try 
        {
        System.setSecurityManager (new RMISecurityManager ());


/*      Get the lookup service.
        =======================  */

        lookup = new LookupLocator ("jini://localhost");
        sHost = lookup.getHost ();
        iPort = lookup.getPort ();
        System.out.println ();
        System.out.println ("client: LookupLocator      = " + lookup);
        System.out.println ("client: LookupLocator.host = " + sHost);
        System.out.println ("client: LookupLocator.port = " + iPort);

        registrar  = lookup.getRegistrar ();
        id  = registrar.getServiceID ();
        System.out.println ("client: ServiceRegistrar   = " + registrar);
        System.out.println ("client: ServiceID          = " + id);


/*      Get JSortInterface.
        ===================  */
          
        aeAttributes = new Entry[1];
        aeAttributes[0] = new Name (sServiceName);
        template = new ServiceTemplate (null, null, aeAttributes);
        jSort = (JSortInterface) registrar.lookup (template);
        System.out.println ("client: ServiceTemplate    = " + template);
        System.out.println ("client: Service object     = " + jSort);
        if (!(jSort instanceof JSortInterface))
          {
          System.out.println ("client: Received ->" + jSort
            + "<- instead of JSortInterface");
          System.exit (0);
          }
        } 

      catch (Exception e) 
        {
        System.out.println ("findService() exception: " + e);
        }
      }



/*    R E L E A S E   R E C O R D S   T O   S O R T
 */
/**   This creates records then releases them to sort and invokes the sort.
 *    It returns the name of the sort job that is received from the jsort
 *    service.
 *
 */
private String releaseRecordsToSort (boolean bUseBlocksIn)
      {
      int i, j, k, m;
      String s;
      Hashtable ht;
      Hashtable htRet;
      String sJobName;
      ByteArrayOutputStream baos;

      ht = new Hashtable ();
      iRecLength = 15;

      try
        {


/*      Setup sort parameters then open the sort job.
        =============================================  */

        ht.put (JSortInterface.FIELD_TAG + "1", "0-4");
        ht.put (JSortInterface.ORDER_TAG + "1", 
          JSortInterface.ASCENDING_VALUE);

        ht.put (JSortInterface.FIELD_TAG + "2", "5-9");
        ht.put (JSortInterface.ORDER_TAG + "2", 
          JSortInterface.DESCENDING_VALUE);

        ht.put (JSortInterface.FIELD_TAG + "3", "10-14");
        ht.put (JSortInterface.ORDER_TAG + "3", 
          JSortInterface.DESCENDING_VALUE);

        ht.put (JSortInterface.REC_LENGTH_TAG,  String.valueOf (iRecLength));
        ht.put (JSortInterface.DEBUG_TAG, "0");

        htRet = jSort.open (ht);
        sJobName = (String) htRet.get (JSortInterface.JOB_NAME_TAG);
        System.out.println ("\nReturn from open: " + htRet + "\n");

        baos = new ByteArrayOutputStream ();
        ht = new Hashtable ();
        ht.put (JSortInterface.JOB_NAME_TAG, sJobName);


/*      Each cycle of this loop outputs one sort record.
        ================================================  */

        for (i = 0; i < 20; i++)
          {
          for (j = 0; j < 3; j++)
            {
            for (k = 0; k < 3; k++)
              {
              s = "";
              for (m = 0; m < 5; m++)
                {
                s += String.valueOf ((char) (i + 'a'));
                }
              for (m = 0; m < 5; m++)
                {
                s += String.valueOf ((char) (j + 'a'));
                }
              for (m = 0; m < 5; m++)
                {
                s += String.valueOf ((char) (k + 'a'));
                }
              if (bUseBlocksIn)
                {
                baos.write (s.getBytes ());                   // by blocks
                }
              else   
                {
                ht.put (JSortInterface.DATA_TAG, s);
                htRet = jSort.releaseRecord (ht);             // by records
                }
              System.out.println ("Released -->" + s + "<-- ret=" + htRet);
              }
            }
          }
        if (bUseBlocksIn)               
          {
          jSort.releaseBlock (sJobName, baos.toByteArray ()); // by blocks
          }


/*      Sort the records.
        =================  */

        System.out.print ("\nSorting...");
        htRet = jSort.sort (ht);
        System.out.println (" return=" + htRet + "\n");

        return ((String) htRet.get (JSortInterface.JOB_NAME_TAG));
        }
      catch (Exception e)
        {
        System.out.println ("releaseRecordsToSort(): exception " + e);
        }

      return (null);
      }



/*    R E T U R N   R E C O R D S   F R O M   S O R T
 */
/**   This returns all the records from the sort service.
 *
 */
void returnRecordsFromSort (String sJobNameIn, boolean bUseBlocksIn)
      {
      int i;
      String s;
      byte[] ab;
      Hashtable ht;
      Hashtable htRet;
      int iNumItems;

      try
        {
        ht = new Hashtable ();
        ht.put (JSortInterface.JOB_NAME_TAG, sJobNameIn);

        if (bUseBlocksIn)
          {
          iNumItems = 20;
          ab = new byte[iNumItems * iRecLength];

          while ((iNumItems * iRecLength) == ab.length)
            {
            ab = jSort.returnBlock (sJobNameIn, iNumItems);  // by blocks
            for (i = 0; i < ab.length; i += iRecLength)
              {
              s = new String (ab, i, iRecLength);
              System.out.println ("Returned -->" + s + "<--");
              }
            }
          }
        else      
          {
          htRet = jSort.returnRecord (ht);                   // by records
          while (htRet.get (JSortInterface.EOF_TAG) == null)
            {
            System.out.println ("Returned -->" 
              + htRet.get (JSortInterface.DATA_TAG) + "<--");
            htRet = jSort.returnRecord (ht);
            }
          }

        htRet = jSort.close (ht);
        System.out.println ("\nClose return: " + htRet + "\n");
        }
      catch (Exception e)
        {
        System.out.println ("returnRecordsFromSort(): exception " + e);
        }
      }

      }

/*==========================(  End of Source  )============================*/

