1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.io.nio;
15
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.channels.SelectionKey;
19 import java.nio.channels.SocketChannel;
20 import java.util.List;
21
22 import org.eclipse.jetty.io.Buffer;
23 import org.eclipse.jetty.io.NetworkTrafficListener;
24 import org.eclipse.jetty.util.log.Log;
25
26 public class NetworkTrafficSelectChannelEndPoint extends SelectChannelEndPoint
27 {
28 private final List<NetworkTrafficListener> listeners;
29
30 public NetworkTrafficSelectChannelEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey key, int maxIdleTime, List<NetworkTrafficListener> listeners) throws IOException
31 {
32 super(channel, selectSet, key, maxIdleTime);
33 this.listeners = listeners;
34 }
35
36 @Override
37 public int fill(Buffer buffer) throws IOException
38 {
39 int read = super.fill(buffer);
40 notifyIncoming(buffer, read);
41 return read;
42 }
43
44 @Override
45 public int flush(Buffer buffer) throws IOException
46 {
47 int position = buffer.getIndex();
48 int written = super.flush(buffer);
49 notifyOutgoing(buffer, position, written);
50 return written;
51 }
52
53 @Override
54 protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
55 {
56 int headerPosition = header.getIndex();
57 int headerLength = header.length();
58 int bufferPosition = buffer.getIndex();
59 int written = super.gatheringFlush(header, bbuf0, buffer,bbuf1);
60 notifyOutgoing(header, headerPosition, written > headerLength ? headerLength : written);
61 notifyOutgoing(buffer, bufferPosition, written > headerLength ? written - headerLength : 0);
62 return written;
63 }
64
65 public void notifyOpened()
66 {
67 if (listeners != null && !listeners.isEmpty())
68 {
69 for (NetworkTrafficListener listener : listeners)
70 {
71 try
72 {
73 listener.opened(_socket);
74 }
75 catch (Exception x)
76 {
77 Log.warn(x);
78 }
79 }
80 }
81 }
82
83 public void notifyIncoming(Buffer buffer, int read)
84 {
85 if (listeners != null && !listeners.isEmpty() && read > 0)
86 {
87 for (NetworkTrafficListener listener : listeners)
88 {
89 try
90 {
91 Buffer view = buffer.asReadOnlyBuffer();
92 listener.incoming(_socket, view);
93 }
94 catch (Exception x)
95 {
96 Log.warn(x);
97 }
98 }
99 }
100 }
101
102 public void notifyOutgoing(Buffer buffer, int position, int written)
103 {
104 if (listeners != null && !listeners.isEmpty() && written > 0)
105 {
106 for (NetworkTrafficListener listener : listeners)
107 {
108 try
109 {
110 Buffer view = buffer.asReadOnlyBuffer();
111 view.setGetIndex(position);
112 view.setPutIndex(position + written);
113 listener.outgoing(_socket, view);
114 }
115 catch (Exception x)
116 {
117 Log.warn(x);
118 }
119 }
120 }
121 }
122
123 public void notifyClosed()
124 {
125 if (listeners != null && !listeners.isEmpty())
126 {
127 for (NetworkTrafficListener listener : listeners)
128 {
129 try
130 {
131 listener.closed(_socket);
132 }
133 catch (Exception x)
134 {
135 Log.warn(x);
136 }
137 }
138 }
139 }
140 }