1 //
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.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.io;
20
21 import java.io.Closeable;
22 import java.nio.ByteBuffer;
23
24 import org.eclipse.jetty.util.component.Container;
25
26 /**
27 * <p>A {@link Connection} is associated to an {@link EndPoint} so that I/O events
28 * happening on the {@link EndPoint} can be processed by the {@link Connection}.</p>
29 * <p>A typical implementation of {@link Connection} overrides {@link #onOpen()} to
30 * {@link EndPoint#fillInterested(org.eclipse.jetty.util.Callback) set read interest} on the {@link EndPoint},
31 * and when the {@link EndPoint} signals read readyness, this {@link Connection} can
32 * read bytes from the network and interpret them.</p>
33 */
34 public interface Connection extends Closeable
35 {
36 /**
37 * <p>Adds a listener of connection events.</p>
38 *
39 * @param listener the listener to add
40 */
41 public void addListener(Listener listener);
42
43 /**
44 * <p>Removes a listener of connection events.</p>
45 *
46 * @param listener the listener to remove
47 */
48 public void removeListener(Listener listener);
49
50 /**
51 * <p>Callback method invoked when this connection is opened.</p>
52 * <p>Creators of the connection implementation are responsible for calling this method.</p>
53 */
54 public void onOpen();
55
56 /**
57 * <p>Callback method invoked when this connection is closed.</p>
58 * <p>Creators of the connection implementation are responsible for calling this method.</p>
59 */
60 public void onClose();
61
62 /**
63 * @return the {@link EndPoint} associated with this {@link Connection}
64 */
65 public EndPoint getEndPoint();
66
67 /**
68 * <p>Performs a logical close of this connection.</p>
69 * <p>For simple connections, this may just mean to delegate the close to the associated
70 * {@link EndPoint} but, for example, SSL connections should write the SSL close message
71 * before closing the associated {@link EndPoint}.</p>
72 */
73 @Override
74 public void close();
75
76 /**
77 * <p>Callback method invoked upon an idle timeout event.</p>
78 * <p>Implementations of this method may return true to indicate that the idle timeout
79 * handling should proceed normally, typically failing the EndPoint and causing it to
80 * be closed.</p>
81 * <p>When false is returned, the handling of the idle timeout event is halted
82 * immediately and the EndPoint left in the state it was before the idle timeout event.</p>
83 *
84 * @return true to let the EndPoint handle the idle timeout,
85 * false to tell the EndPoint to halt the handling of the idle timeout.
86 */
87 public boolean onIdleExpired();
88
89 public int getMessagesIn();
90 public int getMessagesOut();
91 public long getBytesIn();
92 public long getBytesOut();
93 public long getCreatedTimeStamp();
94
95 public interface UpgradeFrom extends Connection
96 {
97 /**
98 * <p>Takes the input buffer from the connection on upgrade.</p>
99 * <p>This method is used to take any unconsumed input from
100 * a connection during an upgrade.</p>
101 *
102 * @return A buffer of unconsumed input. The caller must return the buffer
103 * to the bufferpool when consumed and this connection must not.
104 */
105 ByteBuffer onUpgradeFrom();
106 }
107
108 public interface UpgradeTo extends Connection
109 {
110 /**
111 * <p>Callback method invoked when this connection is upgraded.</p>
112 * <p>This must be called before {@link #onOpen()}.</p>
113 * @param prefilled An optional buffer that can contain prefilled data. Typically this
114 * results from an upgrade of one protocol to the other where the old connection has buffered
115 * data destined for the new connection. The new connection must take ownership of the buffer
116 * and is responsible for returning it to the buffer pool
117 */
118 void onUpgradeTo(ByteBuffer prefilled);
119 }
120
121 /**
122 * <p>A Listener for connection events.</p>
123 * <p>Listeners can be added to a {@link Connection} to get open and close events.
124 * The AbstractConnectionFactory implements a pattern where objects implement
125 * this interface that have been added via {@link Container#addBean(Object)} to
126 * the Connector or ConnectionFactory are added as listeners to all new connections
127 * </p>
128 */
129 public interface Listener
130 {
131 public void onOpened(Connection connection);
132
133 public void onClosed(Connection connection);
134
135 public static class Adapter implements Listener
136 {
137 @Override
138 public void onOpened(Connection connection)
139 {
140 }
141
142 @Override
143 public void onClosed(Connection connection)
144 {
145 }
146 }
147 }
148 }