1b92b42235a91e471cb650b2e8dc48608a1d6dc3
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
;
10 * Base class used for the client basic handling.
12 * It represents a single action: a client is expected to only execute one
17 abstract class ConnectActionClient
{
19 * The underlying {@link ConnectAction}.
23 protected ConnectAction action
;
26 * Create a new {@link ConnectActionClient}.
33 * an optional key to encrypt all the communications (if NULL,
34 * everything will be sent in clear text)
37 * in case of I/O error
38 * @throws UnknownHostException
39 * if the host is not known
40 * @throws IllegalArgumentException
41 * if the port parameter is outside the specified range of valid
42 * port values, which is between 0 and 65535, inclusive
44 public ConnectActionClient(String host
, int port
, String key
)
46 this(new Socket(host
, port
), key
);
50 * Create a new {@link ConnectActionClient}.
53 * the socket to bind to
55 * an optional key to encrypt all the communications (if NULL,
56 * everything will be sent in clear text)
58 public ConnectActionClient(Socket s
, String key
) {
59 action
= new ConnectAction(s
, false, key
) {
61 protected void action() throws Exception
{
62 ConnectActionClient
.this.clientHello();
63 ConnectActionClient
.this.action();
67 protected void onError(Exception e
) {
68 ConnectActionClient
.this.onError(e
);
74 * Send the HELLO message (send a String "HELLO" to the server, to check I/O
75 * and encryption modes).
77 * Will automatically handle the answer (the server must answer "HELLO" in
81 * in case of I/O error
82 * @throws SSLException
83 * in case of encryption error
85 protected void clientHello() throws IOException
{
86 String HELLO
= action
.sendString("HELLO");
87 if (!"HELLO".equals(HELLO
)) {
88 throw new SSLException("Server did not accept the encryption key");
93 * Actually start the process and call the action (synchronous).
95 public void connect() {
100 * Actually start the process and call the action (asynchronous).
102 public void connectAsync() {
103 new Thread(new Runnable() {
112 * Method that will be called when an action is performed on the client.
115 * in case of I/O error
117 @SuppressWarnings("unused")
118 public void action() throws Exception
{
122 * Handler called when an unexpected error occurs in the code.
124 * Will just ignore the error by default.
127 * the exception that occurred
129 protected void onError(@SuppressWarnings("unused") Exception e
) {