2 // ========================================================================
3 // Copyright (c) 1995-2016 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.nio.channels.ClosedChannelException;
23 import java.nio.channels.ReadPendingException;
24 import java.util.concurrent.atomic.AtomicReference;
26 import org.eclipse.jetty.util.Callback;
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
31 /* ------------------------------------------------------------ */
33 * A Utility class to help implement {@link EndPoint#fillInterested(Callback)}
34 * by keeping state and calling the context and callback objects.
37 public abstract class FillInterest
39 private final static Logger LOG = Log.getLogger(FillInterest.class);
40 private final AtomicReference<Callback> _interested = new AtomicReference<>(null);
42 /* ------------------------------------------------------------ */
43 protected FillInterest()
47 /* ------------------------------------------------------------ */
48 /** Call to register interest in a callback when a read is possible.
49 * The callback will be called either immediately if {@link #needsFill()}
50 * returns true or eventually once {@link #fillable()} is called.
52 * @throws ReadPendingException
54 public <C> void register(Callback callback) throws ReadPendingException
57 throw new IllegalArgumentException();
59 if (!_interested.compareAndSet(null,callback))
61 LOG.warn("Read pending for "+_interested.get()+" prevented "+callback);
62 throw new ReadPendingException();
75 /* ------------------------------------------------------------ */
76 /** Call to signal that a read is now possible.
78 public void fillable()
80 Callback callback=_interested.get();
81 if (callback!=null && _interested.compareAndSet(callback,null))
85 /* ------------------------------------------------------------ */
87 * @return True if a read callback has been registered
89 public boolean isInterested()
91 return _interested.get()!=null;
94 /* ------------------------------------------------------------ */
95 /** Call to signal a failure to a registered interest
96 * @return true if the cause was passed to a {@link Callback} instance
98 public boolean onFail(Throwable cause)
100 Callback callback=_interested.get();
101 if (callback!=null && _interested.compareAndSet(callback,null))
103 callback.failed(cause);
109 /* ------------------------------------------------------------ */
110 public void onClose()
112 Callback callback=_interested.get();
113 if (callback!=null && _interested.compareAndSet(callback,null))
114 callback.failed(new ClosedChannelException());
117 /* ------------------------------------------------------------ */
119 public String toString()
121 return String.format("FillInterest@%x{%b,%s}",hashCode(),_interested.get(),_interested.get());
124 /* ------------------------------------------------------------ */
125 /** Register the read interest
126 * Abstract method to be implemented by the Specific ReadInterest to
127 * enquire if a read is immediately possible and if not to schedule a future
128 * call to {@link #fillable()} or {@link #onFail(Throwable)}
129 * @return true if a read is possible
130 * @throws IOException
132 abstract protected boolean needsFill() throws IOException;