# LoggingNetSocket.java

    /*
    ** Module   : LoggingNetSocket.java
    ** Abstract : Logging wrapper for different implementation of the NetSocket interface
    **
    ** Copyright (c) 2016-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date--  ---------------------------------------Description---------------------------------------
    ** 001 IAS 20160805  Initial version
    ** 002 IAS 20200805  Improve logging
    ** 003 GBB 20230512  Logging methods replaced by CentralLogger/ConversionStatus.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.net;

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

    import javax.net.ssl.*;

    /**
     * Logging wrapper for different implementation of the NetSocket interface
     */
    public class LoggingNetSocket
    implements NetSocket
    {
       /** Underlying NetSocket implementation */
       private final NetSocket socket;

       /**
        * Constructor
        *
        * @param socket
        *        underlying NetSocket implementation
        */
       public LoggingNetSocket(NetSocket socket)
       {
          super();
          this.socket = socket;
          tracef("Creating %s: %s -> %s",
                                     socket.getClass().getSimpleName(),
                                     socket.getLocalSockAddr(),
                                     socket.getRemoteSockAddr());
       }

       /**
        * Gets the SSL session.
        *
        * @return   The SSL session or <code>null</code> if the socket is not
        *           secure.
        */
       @Override
       public SSLSession getSession()
       {
          return socket.getSession();
       }

       /**
        * Closes the socket.
        */
       @Override
       public void close()
       {
          socket.close();
       }

       /**
        * Reports if the socket is closed (and thus can no longer be used).
        *
        * @return   <code>true</code> if the socket is closed.
        */
       @Override
       public boolean isClosed()
       {
          return socket.isClosed();
       }

       /**
        * Access the address of the local system, through which we are connecting.
        *
        * @return   The local address.
        */
       @Override
       public InetAddress getLocalAddr()
       {
          return socket.getLocalAddr();
       }

       /**
        * Access the address of the local system, through which we are connecting.
        *
        * @return   The local socket address.
        */
       @Override
       public InetSocketAddress getLocalSockAddr()
       {
          return socket.getLocalSockAddr();
       }

       /**
        * Access the address of the remote system, to which we are connecting.
        *
        * @return   The remote address.
        */
       @Override
       public InetAddress getRemoteAddr()
       {
          return socket.getRemoteAddr();
       }

       /**
        * Access the address of the remote system, to which we are connecting.
        *
        * @return   The remote socket address.
        */
       public InetSocketAddress getRemoteSockAddr()
       {
          return socket.getRemoteSockAddr();
       }

       /**
        * Gets the list with added session listeners. May return <code>null</code> if none has
        * been added.
        *
        * @return  The current session listener.
        */
       @Override
       public List<SessionListener> getSessionListeners()
       {
          return socket.getSessionListeners();
       }

       /**
        * Adds a session listener.
        *
        * @param   sessionListener
        *          A session listener to be added.
        */
       @Override
       public void addSessionListener(SessionListener sessionListener)
       {
          socket.addSessionListener(sessionListener);
       }

       /**
        * Flush the output if applicable
        */
       @Override
       public void flush()
       throws IOException
       {
          traceln("flush");
          socket.flush();
       }

       /**
        * Write byte array to the output
        *
        * @param data
        *        byte array
        */
       @Override
       public void write(byte[] data)
       throws IOException
       {
          tracef("write(byte[%s])", data.length);
          socket.write(data);
       }

       /**
        * Read the next portion of raw data (byte array) to the output
        *
        * @return byte array
        */
       @Override
       public byte[] read()
       throws IOException
       {
          byte[] b = socket.read();
          tracef("read(): byte[%s]", b.length);
          return b;
       }

       /**
        * Read the integer from the input
        *
        * @return integer data
        */
       @Override
       public int readInt()
       throws IOException
       {
          int i = socket.readInt();
          tracef("readInt(): %s", i);
          return i;
       }

       /**
        * Write an integer to the output
        *
        * @param val
        *        integer data to be written
        */
       @Override
       public void writeInt(int val)
       throws IOException
       {
          tracef("writeInt(%s)", val);
          socket.writeInt(val);
       }

       /**
        * Write a byte sequence to the output
        *
        * @param bytes
        *        byte sequence to be written
        */
       @Override
       public void writeBytes(byte[] bytes)
       throws IOException
       {
          tracef("writeBytes(byte[%s])", bytes.length);
          socket.writeBytes(bytes);
       }

      /**
       * Read the byte sequence from the input
       *
       * @param bytes
       *        byte array to be filled by the input bytes
       */
       @Override
       public void readBytes(byte[] bytes)
       throws IOException
       {
          tracef("readBytes(): byte[%s]", bytes.length);
          socket.readBytes(bytes);
       }

       /**
        * Write a boolean to the output
        *
        * @param val
        *        boolean data to be written
        */
       @Override
       public void writeBoolean(boolean val)
       throws IOException
       {
          tracef("writeBoolean(%s)", val);
          socket.writeBoolean(val);
       }

       /**
        * Read the boolean from the input
        *
        * @return boolean data
        */
       @Override
       public boolean readBoolean()
       throws IOException
       {
          boolean b = socket.readBoolean();
          tracef("readBoolean(): %s", b);
          return b;
       }

       /**
        * Read the UTF string from the input
        *
        * @return UTF string
        */
       @Override
       public String readUTF()
       throws IOException
       {
          String s = socket.readUTF();
          tracef("readUTF(): '%s'", s);
          return s;
       }

       /**
        * Write an UTF String to the output
        *
        * @param val
        *        UTF String to be written
        */
       @Override
       public void writeUTF(String val)
       throws IOException
       {
          tracef("writeUTF('%s')", val);
          socket.writeUTF(val);
       }
    }