1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.io; 20 21 import java.net.Socket; 22 23 /** 24 * <p>A listener for raw network traffic within Jetty.</p> 25 * <p>{@link NetworkTrafficListener}s can be installed in a 26 * <code>org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector</code>, 27 * and are notified of the following network traffic events:</p> 28 * <ul> 29 * <li>Connection opened, when the server has accepted the connection from a remote client</li> 30 * <li>Incoming bytes, when the server receives bytes sent from a remote client</li> 31 * <li>Outgoing bytes, when the server sends bytes to a remote client</li> 32 * <li>Connection closed, when the server has closed the connection to a remote client</li> 33 * </ul> 34 * <p>{@link NetworkTrafficListener}s can be used to log the network traffic viewed by 35 * a Jetty server (for example logging to filesystem) for activities such as debugging 36 * or request/response cycles or for replaying request/response cycles to other servers.</p> 37 */ 38 public interface NetworkTrafficListener 39 { 40 /** 41 * <p>Callback method invoked when a connection from a remote client has been accepted.</p> 42 * <p>The {@code socket} parameter can be used to extract socket address information of 43 * the remote client.</p> 44 * 45 * @param socket the socket associated with the remote client 46 */ 47 public void opened(Socket socket); 48 49 /** 50 * <p>Callback method invoked when bytes sent by a remote client arrived on the server.</p> 51 * 52 * @param socket the socket associated with the remote client 53 * @param bytes the read-only buffer containing the incoming bytes 54 */ 55 public void incoming(Socket socket, Buffer bytes); 56 57 /** 58 * <p>Callback method invoked when bytes are sent to a remote client from the server.</p> 59 * <p>This method is invoked after the bytes have been actually written to the remote client.</p> 60 * 61 * @param socket the socket associated with the remote client 62 * @param bytes the read-only buffer containing the outgoing bytes 63 */ 64 public void outgoing(Socket socket, Buffer bytes); 65 66 /** 67 * <p>Callback method invoked when a connection to a remote client has been closed.</p> 68 * <p>The {@code socket} parameter is already closed when this method is called, so it 69 * cannot be queried for socket address information of the remote client.<br /> 70 * However, the {@code socket} parameter is the same object passed to {@link #opened(Socket)}, 71 * so it is possible to map socket information in {@link #opened(Socket)} and retrieve it 72 * in this method. 73 * 74 * @param socket the (closed) socket associated with the remote client 75 */ 76 public void closed(Socket socket); 77 78 /** 79 * <p>A commodity class that implements {@link NetworkTrafficListener} with empty methods.</p> 80 */ 81 public static class Empty implements NetworkTrafficListener 82 { 83 public void opened(Socket socket) 84 { 85 } 86 87 public void incoming(Socket socket, Buffer bytes) 88 { 89 } 90 91 public void outgoing(Socket socket, Buffer bytes) 92 { 93 } 94 95 public void closed(Socket socket) 96 { 97 } 98 } 99 }