2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.io.IOException;
22 import java.net.Socket;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.SelectionKey;
25 import java.nio.channels.SocketChannel;
26 import java.util.List;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30 import org.eclipse.jetty.util.thread.Scheduler;
32 public class NetworkTrafficSelectChannelEndPoint extends SelectChannelEndPoint
34 private static final Logger LOG = Log.getLogger(NetworkTrafficSelectChannelEndPoint.class);
36 private final List<NetworkTrafficListener> listeners;
38 public NetworkTrafficSelectChannelEndPoint(SocketChannel channel, SelectorManager.ManagedSelector selectSet, SelectionKey key, Scheduler scheduler, long idleTimeout, List<NetworkTrafficListener> listeners) throws IOException
40 super(channel, selectSet, key, scheduler, idleTimeout);
41 this.listeners = listeners;
45 public int fill(ByteBuffer buffer) throws IOException
47 int read = super.fill(buffer);
48 notifyIncoming(buffer, read);
53 public boolean flush(ByteBuffer... buffers) throws IOException
56 for (ByteBuffer b : buffers)
60 int position = b.position();
61 ByteBuffer view=b.slice();
62 flushed&=super.flush(b);
63 int l=b.position()-position;
64 view.limit(view.position()+l);
79 if (listeners != null && !listeners.isEmpty())
81 for (NetworkTrafficListener listener : listeners)
85 listener.opened(getSocket());
99 if (listeners != null && !listeners.isEmpty())
101 for (NetworkTrafficListener listener : listeners)
105 listener.closed(getSocket());
116 public void notifyIncoming(ByteBuffer buffer, int read)
118 if (listeners != null && !listeners.isEmpty() && read > 0)
120 for (NetworkTrafficListener listener : listeners)
124 ByteBuffer view = buffer.asReadOnlyBuffer();
125 listener.incoming(getSocket(), view);
135 public void notifyOutgoing(ByteBuffer view)
137 if (listeners != null && !listeners.isEmpty() && view.hasRemaining())
139 Socket socket=getSocket();
140 for (NetworkTrafficListener listener : listeners)
144 listener.outgoing(socket, view);