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