1 //
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.
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.websocket.api;
20
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24
25 /**
26 * Session represents an active link of communications with a Remote WebSocket Endpoint.
27 * <p>
28 */
29 public interface Session extends Closeable
30 {
31 /**
32 * Request a close of the current conversation with a normal status code and no reason phrase.
33 * <p>
34 * This will enqueue a graceful close to the remote endpoint.
35 *
36 * @see #close(CloseStatus)
37 * @see #close(int, String)
38 * @see #disconnect()
39 */
40 @Override
41 void close();
42
43 /**
44 * Request Close the current conversation, giving a reason for the closure. Note the websocket spec defines the acceptable uses of status codes and reason
45 * phrases.
46 * <p>
47 * This will enqueue a graceful close to the remote endpoint.
48 *
49 * @param closeStatus
50 * the reason for the closure
51 *
52 * @see #close()
53 * @see #close(int, String)
54 * @see #disconnect()
55 */
56 void close(CloseStatus closeStatus);
57
58 /**
59 * Send a websocket Close frame, with status code.
60 * <p>
61 * This will enqueue a graceful close to the remote endpoint.
62 *
63 * @param statusCode
64 * the status code
65 * @param reason
66 * the (optional) reason. (can be null for no reason)
67 * @see StatusCode
68 *
69 * @see #close()
70 * @see #close(CloseStatus)
71 * @see #disconnect()
72 */
73 void close(int statusCode, String reason);
74
75 /**
76 * Issue a harsh disconnect of the underlying connection.
77 * <p>
78 * This will terminate the connection, without sending a websocket close frame.
79 * <p>
80 * Once called, any read/write activity on the websocket from this point will be indeterminate.
81 * <p>
82 * Once the underlying connection has been determined to be closed, the various onClose() events (either
83 * {@link WebSocketListener#onWebSocketClose(int, String)} or {@link OnWebSocketClose}) will be called on your websocket.
84 *
85 * @see #close()
86 * @see #close(CloseStatus)
87 * @see #close(int, String)
88 * @see #disconnect()
89 */
90 void disconnect() throws IOException;
91
92 /**
93 * Return the number of milliseconds before this conversation will be closed by the container if it is inactive, ie no messages are either sent or received
94 * in that time.
95 *
96 * @return the timeout in milliseconds.
97 */
98 long getIdleTimeout();
99
100 /**
101 * Get the address of the local side.
102 *
103 * @return the local side address
104 */
105 public InetSocketAddress getLocalAddress();
106
107 /**
108 * Access the (now read-only) {@link WebSocketPolicy} in use for this connection.
109 *
110 * @return the policy in use
111 */
112 WebSocketPolicy getPolicy();
113
114 /**
115 * Returns the version of the websocket protocol currently being used. This is taken as the value of the Sec-WebSocket-Version header used in the opening
116 * handshake. i.e. "13".
117 *
118 * @return the protocol version
119 */
120 String getProtocolVersion();
121
122 /**
123 * Return a reference to the RemoteEndpoint object representing the other end of this conversation.
124 *
125 * @return the remote endpoint
126 */
127 RemoteEndpoint getRemote();
128
129 /**
130 * Get the address of the remote side.
131 *
132 * @return the remote side address
133 */
134 public InetSocketAddress getRemoteAddress();
135
136 /**
137 * Get the UpgradeRequest used to create this session
138 *
139 * @return the UpgradeRequest used to create this session
140 */
141 UpgradeRequest getUpgradeRequest();
142
143 /**
144 * Get the UpgradeResponse used to create this session
145 *
146 * @return the UpgradeResponse used to create this session
147 */
148 UpgradeResponse getUpgradeResponse();
149
150 /**
151 * Return true if and only if the underlying socket is open.
152 *
153 * @return whether the session is open
154 */
155 abstract boolean isOpen();
156
157 /**
158 * Return true if and only if the underlying socket is using a secure transport.
159 *
160 * @return whether its using a secure transport
161 */
162 boolean isSecure();
163
164 /**
165 * Set the number of milliseconds before this conversation will be closed by the container if it is inactive, ie no messages are either sent or received.
166 *
167 * @param ms
168 * the number of milliseconds.
169 */
170 void setIdleTimeout(long ms);
171
172 /**
173 * Suspend a the incoming read events on the connection.
174 *
175 * @return the suspend token suitable for resuming the reading of data on the connection.
176 */
177 SuspendToken suspend();
178 }