1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.io;
20
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.nio.ByteBuffer;
24 import java.util.concurrent.TimeoutException;
25
26 import org.eclipse.jetty.util.Callback;
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29 import org.eclipse.jetty.util.thread.Scheduler;
30
31 public abstract class AbstractEndPoint extends IdleTimeout implements EndPoint
32 {
33 private static final Logger LOG = Log.getLogger(AbstractEndPoint.class);
34 private final long _created=System.currentTimeMillis();
35 private final InetSocketAddress _local;
36 private final InetSocketAddress _remote;
37 private volatile Connection _connection;
38
39 private final FillInterest _fillInterest = new FillInterest()
40 {
41 @Override
42 protected boolean needsFill() throws IOException
43 {
44 return AbstractEndPoint.this.needsFill();
45 }
46 };
47 private final WriteFlusher _writeFlusher = new WriteFlusher(this)
48 {
49 @Override
50 protected void onIncompleteFlushed()
51 {
52 AbstractEndPoint.this.onIncompleteFlush();
53 }
54 };
55
56 protected AbstractEndPoint(Scheduler scheduler,InetSocketAddress local,InetSocketAddress remote)
57 {
58 super(scheduler);
59 _local=local;
60 _remote=remote;
61 }
62
63 @Override
64 public long getCreatedTimeStamp()
65 {
66 return _created;
67 }
68
69 @Override
70 public InetSocketAddress getLocalAddress()
71 {
72 return _local;
73 }
74
75 @Override
76 public InetSocketAddress getRemoteAddress()
77 {
78 return _remote;
79 }
80
81 @Override
82 public Connection getConnection()
83 {
84 return _connection;
85 }
86
87 @Override
88 public void setConnection(Connection connection)
89 {
90 _connection = connection;
91 }
92
93 @Override
94 public void onOpen()
95 {
96 LOG.debug("onOpen {}",this);
97 super.onOpen();
98 }
99
100 @Override
101 public void onClose()
102 {
103 super.onClose();
104 LOG.debug("onClose {}",this);
105 _writeFlusher.onClose();
106 _fillInterest.onClose();
107 }
108
109 @Override
110 public void close()
111 {
112 super.close();
113 }
114
115 @Override
116 public void fillInterested(Callback callback) throws IllegalStateException
117 {
118 notIdle();
119 _fillInterest.register(callback);
120 }
121
122 @Override
123 public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException
124 {
125 _writeFlusher.write(callback, buffers);
126 }
127
128 protected abstract void onIncompleteFlush();
129
130 protected abstract boolean needsFill() throws IOException;
131
132 protected FillInterest getFillInterest()
133 {
134 return _fillInterest;
135 }
136
137 protected WriteFlusher getWriteFlusher()
138 {
139 return _writeFlusher;
140 }
141
142 @Override
143 protected void onIdleExpired(TimeoutException timeout)
144 {
145 if (isOutputShutdown() || _fillInterest.isInterested() || _writeFlusher.isInProgress())
146 {
147 boolean output_shutdown=isOutputShutdown();
148 _fillInterest.onFail(timeout);
149 _writeFlusher.onFail(timeout);
150 if (output_shutdown)
151 close();
152 }
153 }
154
155 @Override
156 public String toString()
157 {
158 return String.format("%s@%x{%s<r-l>%s,o=%b,is=%b,os=%b,fi=%s,wf=%s,it=%d}{%s}",
159 getClass().getSimpleName(),
160 hashCode(),
161 getRemoteAddress(),
162 getLocalAddress(),
163 isOpen(),
164 isInputShutdown(),
165 isOutputShutdown(),
166 _fillInterest,
167 _writeFlusher,
168 getIdleTimeout(),
169 getConnection());
170 }
171 }