LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / net / TelnetServerSocket.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2016 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.net;
30
31 import java.io.IOException;
32 import java.net.InetAddress;
33 import java.net.ServerSocket;
34 import java.net.Socket;
35 import java.net.SocketException;
36
37 /**
38 * This class provides a ServerSocket that return TelnetSocket's in accept().
39 */
40 public final class TelnetServerSocket extends ServerSocket {
41
42 // ServerSocket interface -------------------------------------------------
43
44 /**
45 * Creates an unbound server socket.
46 *
47 * @throws IOException if an I/O error occurs
48 */
49 public TelnetServerSocket() throws IOException {
50 super();
51 }
52
53 /**
54 * Creates a server socket, bound to the specified port.
55 *
56 * @param port the port number, or 0 to use a port number that is
57 * automatically allocated.
58 * @throws IOException if an I/O error occurs
59 */
60 public TelnetServerSocket(final int port) throws IOException {
61 super(port);
62 }
63
64 /**
65 * Creates a server socket and binds it to the specified local port
66 * number, with the specified backlog.
67 *
68 * @param port the port number, or 0 to use a port number that is
69 * automatically allocated.
70 * @param backlog requested maximum length of the queue of incoming
71 * connections.
72 * @throws IOException if an I/O error occurs
73 */
74 public TelnetServerSocket(final int port,
75 final int backlog) throws IOException {
76
77 super(port, backlog);
78 }
79
80 /**
81 * Create a server with the specified port, listen backlog, and local IP
82 * address to bind to.
83 *
84 * @param port the port number, or 0 to use a port number that is
85 * automatically allocated.
86 * @param backlog requested maximum length of the queue of incoming
87 * connections.
88 * @param bindAddr the local InetAddress the server will bind to
89 * @throws IOException if an I/O error occurs
90 */
91 public TelnetServerSocket(final int port, final int backlog,
92 final InetAddress bindAddr) throws IOException {
93
94 super(port, backlog, bindAddr);
95 }
96
97 /**
98 * Listens for a connection to be made to this socket and accepts it. The
99 * method blocks until a connection is made.
100 *
101 * @return the new Socket
102 * @throws IOException if an I/O error occurs
103 */
104 @Override
105 public Socket accept() throws IOException {
106 if (isClosed()) {
107 throw new SocketException("Socket is closed");
108 }
109 if (!isBound()) {
110 throw new SocketException("Socket is not bound");
111 }
112
113 Socket socket = new TelnetSocket();
114 implAccept(socket);
115 return socket;
116 }
117
118 }