/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This class represents the client side application which allows the
 * user to locate others who are logged onto the system.  The user
 * specifies the id's of all those whom he wishes to locate on the
 * command line, and the server will return their locations if they
 * are presently logged onto the system.
 *
 * @author Bill Nagy
 * @version 1.0
 */
public class jlocate
{
  static Properties props = new Properties();

  public static void main(String[] argv)
  {
    FileInputStream fin;
    int localport;

    try
      {
	fin = new FileInputStream(".sipnote");
	props.load(fin);
      }
    catch (FileNotFoundException e)
      {
      }
    catch (IOException e)
      {
	System.err.println("I/O Error loading .sipnote file --> "+e);
	return;
      }

    localport = (new Integer(props.getProperty("SNClient.jport",
					       "5062")).intValue());

    if (argv.length < 1)
      {
	System.out.println("Usage: jlocate <userid[s]>");
	return;
      }

    //Want to check to see if all arguments are names
    int numto = argv.length;

    String[] to = new String[numto];
    
    System.arraycopy(argv, 0, to, 0, numto); /*Copy the names to the to list*/

    JMessage message = new JMessage(new String[] {"LOCATE"},
				    System.getProperty("user.name"), to);

    message.setCommandMessage(true);

    try
      {
	Socket server = new Socket("127.0.0.1", localport);
	ObjectOutputStream out =
	  new ObjectOutputStream(server.getOutputStream());
	ObjectInputStream in =
	  new ObjectInputStream(server.getInputStream());
	Object incoming;
	JMessage response;
	String[] msglines;
	int cnt, loop;
	boolean found;

	out.writeObject(message);
	out.flush();

	incoming = in.readObject();
	if (incoming instanceof JMessage)
	  {
	    response = (JMessage)incoming;
	    msglines = response.getMessage();
	    
	    loop = 0;
	    cnt = 0;
	    found = false;
	    
	    while((cnt < argv.length) && (loop < msglines.length))
	      {
		StringTokenizer st = new StringTokenizer(msglines[loop], "@:");
		String token;
		
		try
		  {
		    token = st.nextToken();
		    if (argv[cnt].equalsIgnoreCase(token))
		      {
			found = true;
			token = st.nextToken();
			System.out.println(argv[cnt]+": "+token);
			loop++;
		      }
		    else
		      {
			if (!found)
			  {
			    System.out.println(argv[cnt]+" not logged in, or not subscribing to messages.");
			    cnt++;
			  }
			else
			  {
			    found = false;
			    cnt++;
			  }
		      }
		  }
		catch(NoSuchElementException e)
		  {
		    //Should never get here
		  }
	      }
	    if (found)
	      cnt++;

	    for(;cnt<argv.length; cnt++)
	      {
		System.out.println(argv[cnt]+" not logged in, or not subscribing to messages.");
	      }
	  }
	server.close();
      }
    catch (ClassNotFoundException e)
      {
	System.err.println("Missing class for returned object --> "+e);
      }
    catch (IOException e)
      {
	System.err.println("I/O Error --> " + e); //IO Error
      }
  }
}
