/*****************************************************************************/
/* File name: TestDump.java                                                  */
/* Purpose: to test the dump procedure of the NFS mount server               */
/*                                                                           */
/* Last modified: 30.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*                                                                           */
/* This program is distributed in the hope that it will be useful, but       */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General */
/* Public License for more details.                                          */
/*                                                                           */
/* This software is distributed under the terms                              */
/* of the GNU General Public License as published                            */
/* by the Free Software Foundation; either version 2, or (at your option)    */
/* any later version.                                                        */
/* See file COPYING for your rights (GNU GPL)                                */
/*****************************************************************************/

package mount;


import java.net.*;
import java.io.*;
import rpc.*;


public
class

TestDump
        extends rpc.ParserResponder
                                    implements Runnable
{

  private final static String RCSID = "$Header: /d/NFS/mount/RCS/TestDump.java,v 1.4 1999/08/02 16:49:06 vklimov Exp $";

  private InetAddress localInetAddress = null;


  public

  TestDump( java.lang.String propertiesFilePath )

                                                   throws  SocketException
/*****************************************************************************/
/* Function name: TestDump                                                   */
/* File name: TestDump.java                                                  */
/* Purpose: constructor for the TestDump class                               */
/* Throws: SocketException                                                   */
/* Parameters: propertiesFilePath                                            */
/* Returns:                                                                  */
/* Last modified: 11.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      super( Constants.TEST_MOUNT_PORT,
             propertiesFilePath         );
                                                       /* TestDump */
/*---------------------------------------------------------------------------*/

      try
        {
          localInetAddress = InetAddress.getLocalHost();

          System.out.println( "localInetAddress=" +
                              localInetAddress.toString() );

        }
      catch( UnknownHostException e )
        {
          try
            {
              System.out.println( "error in getLocalHost\n" +
                                  e.getMessage() );

              localInetAddress =
                InetAddress.getByName(
                  rpc.Constants.LOCAL_LOOP_IP_ADDRESS );
            }
          catch( UnknownHostException ee )
            {
              System.out.println( "error in getByName\n" +
                                  ee.getMessage() );
            }
        /*end of try*/
        }
    /*end of try-catch*/

    }
/*end of TestDump*/


  public
  void

  sendDumpRequestRPCMessageToMountServer( long transactionId )
/*****************************************************************************/
/* Function name: sendDumpRequestRPCMessageToMountServer                     */
/* File name: TestDump.java                                                  */
/* Purpose: to create dump request RPC message and send it                   */
/*          to mount server                                                  */
/* Parameters: transactionId                                                 */
/* Returns:                                                                  */
/* Last modified: 30.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      byte[] ibuf = null;
      int ilength = 0;
      InetAddress ipAddressWhereToSend = null;
      int portToSendTo = 0;
      DatagramPacket udpPacket = null;
      Message rpcMessage = null;

                                /* sendDumpRequestRPCMessageToMountServer */
/*---------------------------------------------------------------------------*/

      ibuf = new byte[
              Constants.
                MOUNT_DUMP_REQUEST_UDP_PACKET_DATA_LENGTH ];

      rpcMessage =
        new Message( ibuf );

      rpcMessage.
        putUnsignedInteger( transactionId );
      rpcMessage.
        putUnsignedInteger( rpc.Constants.RPC_CALL );
      rpcMessage.
        putUnsignedInteger(
          rpc.Constants.HIGHEST_RPC_PROTOCOL_VERSION_SUPPORTED );
      rpcMessage.
        putUnsignedInteger( (long) mount.Constants.MOUNTPROG );
      rpcMessage.
        putUnsignedInteger( mount.Constants.MOUNT_VERS_HIGH );
      rpcMessage.
        putUnsignedInteger( (long) mount.Constants.MOUNTPROC_DUMP );
      rpcMessage.
        putEmptyOpaqueAuthentication();
      rpcMessage.
        putEmptyOpaqueAuthentication();

      portToSendTo = mount.Constants.MOUNT_PORT;

      try
        {
          ipAddressWhereToSend =
            InetAddress.getByName(
              rpc.Constants.LOCAL_LOOP_IP_ADDRESS );
        }
      catch( UnknownHostException e )
        {
          System.out.println( e.getMessage() );
          return;                                      /* return ! */
        }
    /*end of try*/

      udpPacket = new
        DatagramPacket( ibuf, ibuf.length,
                        ipAddressWhereToSend,
                        portToSendTo          );

      try
        {
          udpSocket.send( udpPacket,
                          transactionId,
                          "sendDumpRequestRPCMessageToMountServer" );
        }
      catch( IOException e )
        {
          System.out.println(
            "NFS mount TestDump, " +
            "sendDumpRequestRPCMessageToMountServer: " +
            "IOException in sending datagram\n" +
            e.getMessage()                              );
        }
    /*end of try-catch*/

    }
/*end of sendDumpRequestRPCMessageToMountServer*/


  public
  void

  sendDumpExportListRequestRPCMessageToMountServer( long transactionId )
/*****************************************************************************/
/* Function name: sendDumpExportListRequestRPCMessageToMountServer           */
/* File name: TestDump.java                                                  */
/* Purpose: to create dump export list request RPC message and send it       */
/*          to mount server                                                  */
/* Parameters: transactionId                                                 */
/* Returns:                                                                  */
/* Last modified: 30.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      byte[] ibuf = null;
      int ilength = 0;
      InetAddress ipAddressWhereToSend = null;
      int portToSendTo = 0;
      DatagramPacket udpPacket = null;
      Message rpcMessage = null;

                      /* sendDumpExportListRequestRPCMessageToMountServer */
/*---------------------------------------------------------------------------*/

      ibuf = new byte[
              Constants.
                MOUNT_DUMP_REQUEST_UDP_PACKET_DATA_LENGTH ];

      rpcMessage =
        new Message( ibuf );

      rpcMessage.
        putUnsignedInteger( transactionId );
      rpcMessage.
        putUnsignedInteger( rpc.Constants.RPC_CALL );
      rpcMessage.
        putUnsignedInteger(
          rpc.Constants.HIGHEST_RPC_PROTOCOL_VERSION_SUPPORTED );
      rpcMessage.
        putUnsignedInteger( (long) mount.Constants.MOUNTPROG );
      rpcMessage.
        putUnsignedInteger( mount.Constants.MOUNT_VERS_HIGH );
      rpcMessage.
        putUnsignedInteger( (long) mount.Constants.MOUNTPROC_EXPORT );
      rpcMessage.
        putEmptyOpaqueAuthentication();
      rpcMessage.
        putEmptyOpaqueAuthentication();

      portToSendTo = mount.Constants.MOUNT_PORT;

      try
        {
          ipAddressWhereToSend =
            InetAddress.getByName(
              rpc.Constants.LOCAL_LOOP_IP_ADDRESS );
        }
      catch( UnknownHostException e )
        {
          System.out.println( e.getMessage() );
          return;                                      /* return ! */
        }
    /*end of try*/

      udpPacket = new
        DatagramPacket( ibuf, ibuf.length,
                        ipAddressWhereToSend,
                        portToSendTo          );

      try
        {
          udpSocket.send( udpPacket,
                          transactionId,
                          "sendDumpExportListRequestRPCMessageToMountServer" );
        }
      catch( IOException e )
        {
          System.out.println(
            "NFS mount TestDump, " +
            "sendDumpExportListRequestRPCMessageToMountServer: " +
            "IOException in sending datagram\n" +
            e.getMessage()                              );
        }
    /*end of try-catch*/

    }
/*end of sendDumpExportListRequestRPCMessageToMountServer*/




  public
  void

  analyzeDumpExportListRPCReplyMessage(
    ParserResponderReturnArguments
      parserResponderReturnArguments,
    int nMaxMountEntriesDumpExportListToGet     )
/*****************************************************************************/
/* Function name: analyzeDumpExportListRPCReplyMessage                       */
/* File name: TestDump.java                                                  */
/* Purpose: to analyze the reply of the mount server on export list dump     */
/*          request                                                          */
/* Parameters: parserResponderReturnArguments                                */
/*             nMaxMountEntriesDumpExportListToGet                           */
/* Returns:                                                                  */
/* Last modified: 17.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      String groupName = null;
      String dirPath = null;
      Message rpcMessageWithParametersToRemoteProcedure = null;

                                  /* analyzeDumpExportListRPCReplyMessage */
/*---------------------------------------------------------------------------*/

      rpcMessageWithParametersToRemoteProcedure = new
        Message(
          parserResponderReturnArguments.
            byteArrayContainingParametersToRemoteProcedure );

      dirPath =
        rpcMessageWithParametersToRemoteProcedure.
          getString();
      groupName =
        rpcMessageWithParametersToRemoteProcedure.
          getString();

      System.out.println( "dirPath=" + dirPath +
                          "\ngroupName=" + groupName );

    }
/*end of analyzeDumpExportListRPCReplyMessage*/


  public
  void

  analyzeDumpRPCReplyMessage(
    ParserResponderReturnArguments
      parserResponderReturnArguments,
    int nMaxMountEntriesDumpToGet     )
/*****************************************************************************/
/* Function name: analyzeDumpRPCReplyMessage                                 */
/* File name: TestDump.java                                                  */
/* Purpose: to analyze the reply of the mount server on mount entries dump   */
/*          request                                                          */
/* Parameters: parserResponderReturnArguments                                */
/*             nMaxMountEntriesDumpToGet                                     */
/* Returns:                                                                  */
/* Last modified: 19.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      int i;
      String hostName = null;
      String dirPath = null;
      boolean continueToLoop = true;
      Message rpcMessageWithParametersToRemoteProcedure = null;

                                            /* analyzeDumpRPCReplyMessage */
/*---------------------------------------------------------------------------*/

      rpcMessageWithParametersToRemoteProcedure = new
        Message(
          parserResponderReturnArguments.
            byteArrayContainingParametersToRemoteProcedure );

      for (i=0;
           ( continueToLoop &&
             (i<nMaxMountEntriesDumpToGet)
           );                          i++)
        {
          hostName =
            rpcMessageWithParametersToRemoteProcedure.
              getString();
          dirPath =
            rpcMessageWithParametersToRemoteProcedure.
              getString();

          if ( (hostName.length() == 0) &&
               (dirPath.length() == 0)
             )
            {
              if (i==0)
                System.out.println(
                   "nothing mounted on the server" );
              else
                System.out.println(
                   "end of dump"    );
            /*end of if*/
              continueToLoop = false;
            }
          else
            {
              System.out.println( "i=" + i +
                                  "\n\t\thostName=" + hostName +
                                  "\n\t\tdirPath=" + dirPath    );
            }
        /*end of if on null lengths*/

        }
    /*end of for-loop*/
    }
/*end of analyzeDumpRPCReplyMessage*/


  public
  void

  receiveDumpRPCMessageFromMountServerAndPrintOut(
    int nMaxMountEntriesDumpToGet                   )
/*****************************************************************************/
/* Function name: receiveDumpRPCMessageFromMountServerAndPrintOut            */
/* File name: TestDump.java                                                  */
/* Purpose: to get dump of all mount entries from the mount server and       */
/*          print it out                                                     */
/* Parameters: nMaxMountEntriesDumpToGet                                     */
/* Returns:                                                                  */
/* Last modified: 19.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      ParserResponderReturnArguments
        parserResponderReturnArguments;
      boolean nowMayProcessTheParametersToRemoteProcedure;
      byte[] byteBuffer = null;
      DatagramPacket udpPacket = null;

                       /* receiveDumpRPCMessageFromMountServerAndPrintOut */
/*---------------------------------------------------------------------------*/


System.out.println( "entered " +
   "receiveDumpRPCMessageFromMountServerAndPrintOut" );

      byteBuffer = new
        byte[ Constants.
                MOUNTPROC_DUMP_UDP_DATA_PREFIX_LENGTH +
              (nMaxMountEntriesDumpToGet *
               Constants.N_BYTES_PER_MOUNT_ENTRY ) +
              Constants.N_BYTES_PER_NULL_MOUNT_ENTRY  ];

System.out.println( "byteBuffer.length=" +
                    byteBuffer.length      );

      udpPacket = new
        DatagramPacket( byteBuffer,
                        byteBuffer.length );

      try
        {

System.out.println( "going to receive " +
                    "from port " + udpSocket.getLocalPort() +
                    "\non address " +
                    udpSocket.getLocalAddress().toString() +
                    "\nready to get " +
                    udpPacket.getLength() +
                    " bytes"                 );

          udpSocket.receive( udpPacket );

System.out.println( "received a UDP packet" );

        }
      catch( IOException e )
        {
          System.out.println(
            "udpSocket.receive: " +
            e.getMessage()          );
        }
    /*end of try-catch*/

      parserResponderReturnArguments = new
        ParserResponderReturnArguments();

      nowMayProcessTheParametersToRemoteProcedure =
        processUDPPacket(
          udpPacket,
          parserResponderReturnArguments      );

      if ( nowMayProcessTheParametersToRemoteProcedure )
        {
          analyzeDumpRPCReplyMessage(
            parserResponderReturnArguments,
            nMaxMountEntriesDumpToGet           );
        }
      else
        ;
    /*end of if on nowMayProcessTheParametersToRemoteProcedure*/

    }
/*end of receiveDumpRPCMessageFromMountServerAndPrintOut*/


  public
  void

  receiveDumpExportListRPCMessageFromMountServerAndPrintOut(
    int nMaxMountEntriesDumpExportListToGet                   )
/*****************************************************************************/
/* Function name: receiveDumpExportListRPCMessageFromMountServerAndPrintOut  */
/* File name: TestDump.java                                                */
/* Purpose: to get dump of all mount entries from the mount server and       */
/*          print it out                                                     */
/* Parameters: nMaxMountEntriesDumpExportListToGet                           */
/* Returns:                                                                  */
/* Last modified: 17.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      ParserResponderReturnArguments
        parserResponderReturnArguments;
      boolean nowMayProcessTheParametersToRemoteProcedure;
      byte[] byteBuffer = null;
      DatagramPacket udpPacket = null;

              /* receiveDumpExportListRPCMessageFromMountServerAndPrintOut */
/*---------------------------------------------------------------------------*/

      byteBuffer = new
        byte[ Constants.
                MOUNTPROC_DUMP_UDP_DATA_PREFIX_LENGTH +
              Constants.N_BYTES_PER_MOUNT_ENTRY       +
              Constants.N_BYTES_PER_NULL_MOUNT_ENTRY    ];

      udpPacket = new
        DatagramPacket( byteBuffer,
                        byteBuffer.length );

      try
        {
          udpSocket.receive( udpPacket );
        }
      catch( IOException e )
        {
          System.out.println(
            "udpSocket.receive: " +
            e.getMessage()          );
        }
    /*end of try-catch*/

      parserResponderReturnArguments = new
        ParserResponderReturnArguments();

      nowMayProcessTheParametersToRemoteProcedure =
        processUDPPacket(
          udpPacket,
          parserResponderReturnArguments      );

      if ( nowMayProcessTheParametersToRemoteProcedure )
        {
          analyzeDumpExportListRPCReplyMessage(
            parserResponderReturnArguments,
            nMaxMountEntriesDumpExportListToGet           );
        }
      else
        ;
    /*end of if on nowMayProcessTheParametersToRemoteProcedure*/

    }
/*end of receiveDumpExportListRPCMessageFromMountServerAndPrintOut*/


  public
  void

  run()
/*****************************************************************************/
/* Function name: run                                                        */
/* File name: TestDump.java                                                  */
/* Purpose: to test the mount server                                         */
/* Parameters:                                                               */
/* Returns:                                                                  */
/* Last modified: 19.06.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      FhStatus fhStatus = new FhStatus();
      boolean mountedOK;
      boolean unmountedOK;
      long portNumber = 0;
                                                                 /* run */
/*---------------------------------------------------------------------------*/

System.out.println( "\nsending DUMP mount entries request" );

      sendDumpRequestRPCMessageToMountServer(
        nfs.Constants.INITIAL_TRANSACTION_ID   );

      receiveDumpRPCMessageFromMountServerAndPrintOut(
        Constants.N_ENTRIES_MAX_DUMP_TO_GET            );

System.out.println( "\ntesting DUMP Export List procedure" );

      sendDumpExportListRequestRPCMessageToMountServer(
        nfs.Constants.INITIAL_TRANSACTION_ID           );

      receiveDumpExportListRPCMessageFromMountServerAndPrintOut(
        Constants.N_ENTRIES_MAX_DUMP_TO_GET                      );


      udpSocket.close();

  }
/*end of run*/


  public
  static
  void

  main( String[] args )
/*****************************************************************************/
/* Function name: main                                                       */
/* File name: TestDump.java                                                  */
/* Purpose: main method for the TestDump class                               */
/* Parameters: agrument strings array                                        */
/* Returns:                                                                  */
/* Last modified: 11.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      TestDump test = null;
                                                          /* main */
/*---------------------------------------------------------------------------*/

      try
        {
          test = new TestDump( Constants.PROPERTIES_FILE_PATH );
        }
      catch( SocketException e )
        {
          System.out.println(
            "can\'t create io.UdpSocket: " +
            e.getMessage()                      );
          System.exit( 1 );
        }
    /*end of try-catch*/

      test.run();
      System.exit( 0 );

    }
/*end of main*/


} /* end of TestDump class */

/*****************************************************************************/
/* End of file: TestDump.java                                                */
/*****************************************************************************/
