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