1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
5 import java
.net
.UnknownHostException
;
7 import javax
.net
.ssl
.SSLException
;
9 import be
.nikiroo
.utils
.Version
;
12 * Base class used for the client basic handling.
14 * It represents a single action: a client is expected to only execute one
19 abstract class ConnectActionClient
{
21 * The underlying {@link ConnectAction}.
25 protected ConnectAction action
;
28 * Create a new {@link ConnectActionClient}, using the current version of
36 * an optional key to encrypt all the communications (if NULL,
37 * everything will be sent in clear text)
41 * in case of I/O error
42 * @throws UnknownHostException
43 * if the host is not known
44 * @throws IllegalArgumentException
45 * if the port parameter is outside the specified range of valid
46 * port values, which is between 0 and 65535, inclusive
48 public ConnectActionClient(String host
, int port
, String key
)
50 this(host
, port
, key
, Version
.getCurrentVersion());
54 * Create a new {@link ConnectActionClient}.
61 * an optional key to encrypt all the communications (if NULL,
62 * everything will be sent in clear text)
63 * @param clientVersion
68 * in case of I/O error
69 * @throws UnknownHostException
70 * if the host is not known
71 * @throws IllegalArgumentException
72 * if the port parameter is outside the specified range of valid
73 * port values, which is between 0 and 65535, inclusive
75 public ConnectActionClient(String host
, int port
, String key
,
76 Version clientVersion
) throws IOException
{
77 this(new Socket(host
, port
), key
, clientVersion
);
81 * Create a new {@link ConnectActionClient}, using the current version of
85 * the socket to bind to
87 * an optional key to encrypt all the communications (if NULL,
88 * everything will be sent in clear text)
90 public ConnectActionClient(Socket s
, String key
) {
91 this(s
, key
, Version
.getCurrentVersion());
95 * Create a new {@link ConnectActionClient}.
98 * the socket to bind to
100 * an optional key to encrypt all the communications (if NULL,
101 * everything will be sent in clear text)
102 * @param clientVersion
105 public ConnectActionClient(Socket s
, String key
, Version clientVersion
) {
106 action
= new ConnectAction(s
, false, key
, clientVersion
) {
108 protected void action(Version serverVersion
) throws Exception
{
109 ConnectActionClient
.this.clientHello();
110 ConnectActionClient
.this.action(serverVersion
);
114 protected void onError(Exception e
) {
115 ConnectActionClient
.this.onError(e
);
119 protected Version
negotiateVersion(Version clientVersion
) {
120 new Exception("Should never be called on a client")
128 * Send the HELLO message (send a String "HELLO" to the server, to check I/O
129 * and encryption modes).
131 * Will automatically handle the answer (the server must answer "HELLO" in
134 * @throws IOException
135 * in case of I/O error
136 * @throws SSLException
137 * in case of encryption error
139 protected void clientHello() throws IOException
{
140 String HELLO
= action
.sendString("HELLO");
141 if (!"HELLO".equals(HELLO
)) {
142 throw new SSLException("Server did not accept the encryption key");
147 * Actually start the process and call the action (synchronous).
149 public void connect() {
154 * Actually start the process and call the action (asynchronous).
156 public void connectAsync() {
157 new Thread(new Runnable() {
166 * Method that will be called when an action is performed on the client.
168 * @param serverVersion
169 * the version of the server connected to this client
172 * in case of I/O error
174 @SuppressWarnings("unused")
175 public void action(Version serverVersion
) throws Exception
{
179 * Handler called when an unexpected error occurs in the code.
181 * Will just ignore the error by default.
184 * the exception that occurred
186 protected void onError(@SuppressWarnings("unused") Exception e
) {