static const char* version_id =
  "$Id: msg_locate.cc,v 1.9 1999/10/18 18:38:16 v2kea318 Exp $";

// $Log: msg_locate.cc,v $
// Revision 1.9  1999/10/18  18:38:16  v2kea318
// More than one userid may now be entered on the command line.  The error
// message for a bad_user_id error action failure is now more descriptive.
//
// Revision 1.8  1999/08/12  19:25:17  v2kea318
// Do not embed C++ (//) style comments inside C (/* */) style comments.
//
// Revision 1.7  1999/08/06  16:23:44  v2kea318
// Moved the bulk of the code to the query_name_server library routine.
//
// Revision 1.6  1999/07/30  17:55:48  v2kea318
// Reformat the header file names for Standard C++ compliance.
//
// Revision 1.5  1999/07/07  19:00:53  v2kea318
// Use the read_force function to read the number of entries in the list.
//
// Revision 1.4  1999/05/07  19:40:20  v2kea318
// Use the cfailure.h file to define an << operator for ConnectFailure
// exceptions.
//
// Revision 1.3  1999/05/07  18:48:14  v2kea318
// *** empty log message ***
//
// Revision 1.2  1999/05/07  16:49:31  v2kea318
// *** empty log message ***
//
// Revision 1.1  1999/05/07  16:15:40  v2kea318
// Initial revision
//
/***************************************************************************

This program will query the msg server for a user id and return information
about all active connections by that user.

***************************************************************************/

#include <iostream>
#include <cstdlib>
#include "msg_user_table.h"
#include "query_name_server.h"
#include "cfailure.h"
#include "action_evaluation.h"

// define an output operator for UserInformation objects.
typedef MsgUserTable::UserInformation UserInformation;
ostream& operator << (ostream& os, const UserInformation& info)
{
  os << "user " << info.userid << " connected from " << info.machine
     << " on port " << info.port;
  return os;
}

//***************************************************************************

int main(int argc, char* argv[])
{
  // check command line arguments.
  if (argc == 1) {
    cerr << "usage:  " << argv[0] << " user_id|-all [...]" << endl;
    return EXIT_FAILURE;
  }

  try {
    while (--argc) {
      // get the user id.
      string userid = *++argv;

      // get the information.
      try {
	// query the server.
	UserInfoVector info = query_name_server(userid);
	
	// dump the output.
	copy(info->begin(), info->end(),
	     ostream_iterator<UserInformation>(cout, "\n"));
      } catch (const action_failed& af) {
	if (af.failure_code() == bad_user_id) 
	  cerr << "user " << userid << " not registered." << endl;
	else
	  cerr << af << endl;
      }
    }
    
    // that's it.
    return EXIT_SUCCESS;
  } catch (const SocketBase::ConnectFailure& cf) {
    cerr << cf << endl;
  } catch (const SocketBase::SocketException& se) {
    cerr << "socket exception --> " << se.what() << endl;
  } catch (const exception& e) {
    cerr << e.what() << endl;
  } catch (...) {
    cerr << "unhandled exception" << endl;
    throw;
  }

  // if we're here, we took an exception.
  return EXIT_FAILURE;
}
