//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     This library is free software; you can redistribute it and/or
//     modify it under the terms of the GNU Lesser General Public License 
//     as published by the Free Software Foundation; either version 2.1 
//     of the License, or any later version.
// 
//     This library 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 Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General
//     Public License along with this library; if not, write to the 
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
//     Boston, MA 02111-1307 USA 
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.4 (15127), 2011/06/03 14:52:49
//                        $Revision: 14936 $ 
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.test;

import java.io.IOException;

import de.knipp.rritoolkit.Port;
import de.knipp.rritoolkit.request.Request;
import de.knipp.rritoolkit.response.Response;


/**
 *  This is a helper class for debugging/logging purposes. It is wrapper class 
 *  that prints the sent requests and returned responses to the console.
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class DebugPort implements Port
{
  /** the actual port */
  
  private Port port;
  
  
  /**
   *  constructor
   *
   *  @param thePort               the actual port
   */
  
  public DebugPort (Port thePort)
  {
    port = thePort;
  }
  
  
  //--- implementation of Port -------------------------------------------------
  
  /**
   *  "execute" a request, i.e. send it to the registry and wait for the
   *  response
   *
   *  @param request          the request
   *  @return                 the response received
   *  @throws IOException     if either the transmission or the reception failed
   */

  @Override
  public Response execute (Request request)
    throws IOException
  {
    Thread thread = Thread.currentThread ();
    String name = "thread " + thread.getId () + " (" + thread.getName () + ")";
    
    System.err.println (name + " sending:");
    System.err.println (request);
    System.err.println ();
    
    try
    {
      Response resp = port.execute (request);
      
      System.err.println (name + " received:");
      System.err.println (resp);
      System.err.println ();
      
      return resp;
  
    } catch (IOException e)
    {
      System.err.println (name + " caught an exception:");
      e.printStackTrace (System.err);
      
      throw e;
    }
  }
  
}



