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 LOG.debug("onClose {}",this);
104 _writeFlusher.onClose();
105 _fillInterest.onClose();
106 }
107
108 @Override
109 public void close()
110 {
111 super.close();
112 }
113
114 @Override
115 public void fillInterested(Callback callback) throws IllegalStateException
116 {
117 notIdle();
118 _fillInterest.register(callback);
119 }
120
121 @Override
122 public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException
123 {
124 _writeFlusher.write(callback, buffers);
125 }
126
127 protected abstract void onIncompleteFlush();
128
129 protected abstract boolean needsFill() throws IOException;
130
131 protected FillInterest getFillInterest()
132 {
133 return _fillInterest;
134 }
135
136 protected WriteFlusher getWriteFlusher()
137 {
138 return _writeFlusher;
139 }
140
141 @Override
142 protected void onIdleExpired(TimeoutException timeout)
143 {
144 if (isOutputShutdown() || _fillInterest.isInterested() || _writeFlusher.isInProgress())
145 {
146 boolean output_shutdown=isOutputShutdown();
147 _fillInterest.onFail(timeout);
148 _writeFlusher.onFail(timeout);
149 if (output_shutdown)
150 close();
151 }
152 }
153
154 @Override
155 public String toString()
156 {
157 return String.format("%s@%x{%s<r-l>%s,o=%b,is=%b,os=%b,fi=%s,wf=%s,it=%d}{%s}",
158 getClass().getSimpleName(),
159 hashCode(),
160 getRemoteAddress(),
161 getLocalAddress(),
162 isOpen(),
163 isInputShutdown(),
164 isOutputShutdown(),
165 _fillInterest,
166 _writeFlusher,
167 getIdleTimeout(),
168 getConnection());
169 }
170 }