View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.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;
27  
28  import org.eclipse.jetty.util.BufferUtil;
29  import org.eclipse.jetty.util.log.Log;
30  import org.eclipse.jetty.util.log.Logger;
31  import org.eclipse.jetty.util.thread.Scheduler;
32  
33  public class NetworkTrafficSelectChannelEndPoint extends SelectChannelEndPoint
34  {
35      private static final Logger LOG = Log.getLogger(NetworkTrafficSelectChannelEndPoint.class);
36  
37      private final List<NetworkTrafficListener> listeners;
38  
39      public NetworkTrafficSelectChannelEndPoint(SocketChannel channel, SelectorManager.ManagedSelector selectSet, SelectionKey key, Scheduler scheduler, long idleTimeout, List<NetworkTrafficListener> listeners) throws IOException
40      {
41          super(channel, selectSet, key, scheduler, idleTimeout);
42          this.listeners = listeners;
43      }
44  
45      @Override
46      public int fill(ByteBuffer buffer) throws IOException
47      {
48          int read = super.fill(buffer);
49          notifyIncoming(buffer, read);
50          return read;
51      }
52  
53      @Override
54      public boolean flush(ByteBuffer... buffers) throws IOException
55      {
56          boolean flushed=true;
57          for (ByteBuffer b : buffers)
58          {
59              if (b.hasRemaining())
60              {
61                  int position = b.position();
62                  ByteBuffer view=b.slice();
63                  flushed&=super.flush(b);
64                  int l=b.position()-position;
65                  view.limit(view.position()+l);
66                  notifyOutgoing(view);
67                  if (!flushed)
68                      break;
69              }
70          }
71          return flushed;
72      }
73  
74      
75  
76      @Override
77      public void onOpen()
78      {
79          super.onOpen();
80          if (listeners != null && !listeners.isEmpty())
81          {
82              for (NetworkTrafficListener listener : listeners)
83              {
84                  try
85                  {
86                      listener.opened(getSocket());
87                  }
88                  catch (Exception x)
89                  {
90                      LOG.warn(x);
91                  }
92              }
93          }
94      }
95  
96      @Override
97      public void onClose()
98      {
99          super.onClose();
100         if (listeners != null && !listeners.isEmpty())
101         {
102             for (NetworkTrafficListener listener : listeners)
103             {
104                 try
105                 {
106                     listener.closed(getSocket());
107                 }
108                 catch (Exception x)
109                 {
110                     LOG.warn(x);
111                 }
112             }
113         }
114     }
115 
116 
117     public void notifyIncoming(ByteBuffer buffer, int read)
118     {
119         if (listeners != null && !listeners.isEmpty() && read > 0)
120         {
121             for (NetworkTrafficListener listener : listeners)
122             {
123                 try
124                 {
125                     ByteBuffer view = buffer.asReadOnlyBuffer();
126                     listener.incoming(getSocket(), view);
127                 }
128                 catch (Exception x)
129                 {
130                     LOG.warn(x);
131                 }
132             }
133         }
134     }
135 
136     public void notifyOutgoing(ByteBuffer view)
137     {
138         if (listeners != null && !listeners.isEmpty() && view.hasRemaining())
139         {
140             Socket socket=getSocket();
141             for (NetworkTrafficListener listener : listeners)
142             {
143                 try
144                 {
145                     listener.outgoing(socket, view);   
146                 }
147                 catch (Exception x)
148                 {
149                     LOG.warn(x);
150                 }
151             }
152         }
153     }
154 
155 }