View Javadoc

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.server.nio;
20  
21  import java.io.IOException;
22  import java.nio.channels.SelectionKey;
23  import java.nio.channels.SocketChannel;
24  import java.util.ConcurrentModificationException;
25  import java.util.List;
26  import java.util.concurrent.CopyOnWriteArrayList;
27  
28  import org.eclipse.jetty.io.NetworkTrafficListener;
29  import org.eclipse.jetty.io.nio.NetworkTrafficSelectChannelEndPoint;
30  import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
31  import org.eclipse.jetty.io.nio.SelectorManager;
32  
33  /**
34   * <p>A specialized version of {@link SelectChannelConnector} that supports {@link NetworkTrafficListener}s.</p>
35   * <p>{@link NetworkTrafficListener}s can be added and removed dynamically before and after this connector has
36   * been started without causing {@link ConcurrentModificationException}s.</p>
37   */
38  public class NetworkTrafficSelectChannelConnector extends SelectChannelConnector
39  {
40      private final List<NetworkTrafficListener> listeners = new CopyOnWriteArrayList<NetworkTrafficListener>();
41  
42      /**
43       * @param listener the listener to add
44       */
45      public void addNetworkTrafficListener(NetworkTrafficListener listener)
46      {
47          listeners.add(listener);
48      }
49  
50      /**
51       * @param listener the listener to remove
52       */
53      public void removeNetworkTrafficListener(NetworkTrafficListener listener)
54      {
55          listeners.remove(listener);
56      }
57  
58      @Override
59      protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey key) throws IOException
60      {
61          NetworkTrafficSelectChannelEndPoint endPoint = new NetworkTrafficSelectChannelEndPoint(channel, selectSet, key, _maxIdleTime, listeners);
62          endPoint.setConnection(selectSet.getManager().newConnection(channel,endPoint, key.attachment()));
63          endPoint.notifyOpened();
64          return endPoint;
65      }
66  
67      @Override
68      protected void endPointClosed(SelectChannelEndPoint endpoint)
69      {
70          super.endPointClosed(endpoint);
71          ((NetworkTrafficSelectChannelEndPoint)endpoint).notifyClosed();
72      }
73  }