2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.nio.ByteBuffer;
24 import java.util.concurrent.TimeoutException;
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;
31 public abstract class AbstractEndPoint extends IdleTimeout implements EndPoint
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;
39 private final FillInterest _fillInterest = new FillInterest()
42 protected boolean needsFill() throws IOException
44 return AbstractEndPoint.this.needsFill();
48 private final WriteFlusher _writeFlusher = new WriteFlusher(this)
51 protected void onIncompleteFlushed()
53 AbstractEndPoint.this.onIncompleteFlush();
57 protected AbstractEndPoint(Scheduler scheduler,InetSocketAddress local,InetSocketAddress remote)
65 public long getCreatedTimeStamp()
71 public InetSocketAddress getLocalAddress()
77 public InetSocketAddress getRemoteAddress()
83 public Connection getConnection()
89 public void setConnection(Connection connection)
91 _connection = connection;
97 LOG.debug("onOpen {}",this);
102 public void onClose()
105 LOG.debug("onClose {}",this);
106 _writeFlusher.onClose();
107 _fillInterest.onClose();
117 public void fillInterested(Callback callback) throws IllegalStateException
120 _fillInterest.register(callback);
124 public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException
126 _writeFlusher.write(callback, buffers);
129 protected abstract void onIncompleteFlush();
131 protected abstract boolean needsFill() throws IOException;
133 protected FillInterest getFillInterest()
135 return _fillInterest;
138 protected WriteFlusher getWriteFlusher()
140 return _writeFlusher;
144 protected void onIdleExpired(TimeoutException timeout)
146 boolean output_shutdown=isOutputShutdown();
147 boolean input_shutdown=isInputShutdown();
148 boolean fillFailed = _fillInterest.onFail(timeout);
149 boolean writeFailed = _writeFlusher.onFail(timeout);
151 // If the endpoint is half closed and there was no onFail handling, the close here
152 // This handles the situation where the connection has completed its close handling
153 // and the endpoint is half closed, but the other party does not complete the close.
154 // This perhaps should not check for half closed, however the servlet spec case allows
155 // for a dispatched servlet or suspended request to extend beyond the connections idle
156 // time. So if this test would always close an idle endpoint that is not handled, then
157 // we would need a mode to ignore timeouts for some HTTP states
158 if (isOpen() && (output_shutdown || input_shutdown) && !(fillFailed || writeFailed))
161 LOG.debug("Ignored idle endpoint {}",this);
165 public String toString()
167 return String.format("%s@%x{%s<->%d,%s,%s,%s,%s,%s,%d,%s}",
168 getClass().getSimpleName(),
171 getLocalAddress().getPort(),
172 isOpen()?"Open":"CLOSED",
173 isInputShutdown()?"ISHUT":"in",
174 isOutputShutdown()?"OSHUT":"out",
175 _fillInterest.isInterested()?"R":"-",
176 _writeFlusher.isInProgress()?"W":"-",
178 getConnection()==null?null:getConnection().getClass().getSimpleName());