/*****************************************************************************/
/* File name: UdpSocket.java                                                 */
/* Purpose: to extend DatagramSocket to add functionality                    */
/*                                                                           */
/* Last modified: 31.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 io;


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


public
class

UdpSocket extends DatagramSocket
{

  private final static java.lang.String RCSID =
"$Header: /d/NFS/io/RCS/UdpSocket.java,v 1.3 1999/08/02 22:44:34 vklimov Exp $";


  private Logable log = null;
  private boolean logMessages = false;
  private boolean logTransactions = false;


  public
  UdpSocket( int port ) throws SocketException
/*****************************************************************************/
/* Function name: UdpSocket                                                  */
/* File name: UdpSocket.java                                                 */
/* Purpose: constructor for the UdpSocket class                              */
/* Parameters: port                                                          */
/* Returns:                                                                  */
/* Last modified: 09.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      super( port );
                                                       /* UdpSocket */
/*---------------------------------------------------------------------------*/

    }
/*end of UdpSocket*/


  public

  UdpSocket( int port,
             Logable log,
             boolean logMessages,
             boolean logTransactions ) throws SocketException
/*****************************************************************************/
/* Function name: UdpSocket                                                  */
/* File name: UdpSocket.java                                                 */
/* Purpose: constructor for the UdpSocket class                              */
/* Parameters: port                                                          */
/*             log                                                           */
/*             logMessages                                                   */
/*             logTransactions                                               */
/* Throws: SocketException                                                   */
/* Returns:                                                                  */
/* Last modified: 30.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      super( port );

                                                       /* UdpSocket */
/*---------------------------------------------------------------------------*/

      this.log = log;
      this.logMessages = logMessages;
      this.logTransactions = logTransactions;

    }
/*end of UdpSocket*/


  public
  void

  send( DatagramPacket udpPacket,
        long transactionId,
        java.lang.String transactionDecription )
                                                   throws IOException
/*****************************************************************************/
/* Function name: send                                                       */
/* File name: UdpSocket.java                                                 */
/* Purpose: to send a UDP packet and to log it if possible                   */
/* Parameters: udpPacket                                                     */
/*             transactionId                                                 */
/*             transactionDecription                                         */
/* Throws: IOException                                                       */
/* Returns:                                                                  */
/* Last modified: 11.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      super.send( udpPacket );

      String stringToPrintOut = "";
                                                              /* send */
/*---------------------------------------------------------------------------*/

      if (log == null) return;             /* return ! */

      if (logMessages)
        {
          stringToPrintOut +=
            "UdpSocket, send\n";
          stringToPrintOut +=
            "going to send to port " +
            udpPacket.getPort() +
            " to address " +
            udpPacket.getAddress() +
            "\nudpPacket: " +
            "length=" + udpPacket.getLength() +
            "\n" +
            util.Array.
              byteArrayToStringOfHex(
                udpPacket.getData()   ) +
            "\n";
          log.write( stringToPrintOut );
        }
      else
        ;
    /*end of if*/

      if (logTransactions)
        {
          stringToPrintOut +=
            "UdpSocket, send\n";
          stringToPrintOut +=
            "going to send reply on transaction ID " +
            transactionId +
            " " + transactionDecription +
            "\n";
          log.write( stringToPrintOut );
        }
      else
        ;
    /*end of if*/


    }
/*end of send*/


  public
  void

  receive( DatagramPacket udpPacket )
                                        throws IOException
/*****************************************************************************/
/* Function name: receive                                                    */
/* File name: UdpSocket.java                                                 */
/* Purpose: to receive a UDP packet and to log it if possible                */
/* Parameters: udpPacket                                                     */
/* Throws: IOException                                                       */
/* Returns:                                                                  */
/* Last modified: 31.07.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*****************************************************************************/
    {
      super.receive( udpPacket );

      int length = 0;
      String stringToPrintOut = "";
                                                              /* receive */
/*---------------------------------------------------------------------------*/

      if (log == null) return;             /* return ! */


      if (logMessages)
        {
          length = udpPacket.getLength();
          stringToPrintOut +=
            " UdpSocket, receive\n";
          stringToPrintOut +=
            "received from port " +
            udpPacket.getPort() +
            ", address " +
            udpPacket.getAddress() +
            "\nudpPacket: " +
            "length=" + length +
            "\n" +
            util.Array.
              byteArrayToStringOfHex(
                udpPacket.getData(),
                0, /* from the beginning */
                length ) +
            "\n";
          log.write( stringToPrintOut );
        }
      else
        ;
    /*end of if*/

    }
/*end of receive*/


} /*end of UdpSocket */

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