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