static const char* version_id =
  "$Id: query_name_server.cc,v 1.3 1999/09/16 21:06:23 v2kea318 Exp $";

// $Log: query_name_server.cc,v $
// Revision 1.3  1999/09/16  21:06:23  v2kea318
// Implement the data reading in a functor used in a for_each algorithm
// in the main routine.
//
// Revision 1.2  1999/08/06  16:24:30  v2kea318
// Use the read_int routine to read the integer values from the socket.
//
// Revision 1.1  1999/08/05  22:40:48  v2kea318
// Initial revision
//
//***************************************************************************

#include "query_name_server.h"
#include "Socket.h"
#include "server_name.h"
#include "msg_constants.h"
#include "msg_codes.h"
#include "action_transfer.h"
#include "action_evaluation.h"
#include "string-transfer.h"
#include <algorithm>

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

struct DataReader 
{
    Socket& sock;
    DataReader(Socket& s) : sock(s) {}
    void operator() (UserInfoVector::value_type::value_type& info);
};

void DataReader::operator () (UserInfoVector::value_type::value_type& info)
{
    // get the user id.
    string uid;
    read_string(sock, uid);
    
    // read the machine name.
    string mach;
    read_string(sock, mach);
    
    // read the port;
    size_t port;
    read_int(sock, port);
    
    // insert into the container.
    new (&info) UserInformation(uid, mach, port);
}

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

UserInfoVector query_name_server(const string& user)
{
  // get the server host name.
  string server_name = get_server_name();

  // Open a connection to the server.
  Socket server(server_name, msg_port);

  // Send the correct action code.
  send_action(server, QUERY_REG_LIST);

  // send the desired user id.
  write_string(server, user); eval_response(server);

  // get the number of entries in the response.
  size_t nentries; read_int(server, nentries);

  // Build a vector for the output.
  UserInfoVector info(new UserInfoVector::value_type(nentries));

  // read the data.
  for_each(info->begin(), info->end(), DataReader(server));
  eval_response(server);

  // return.
  return info;
}
