fix StringUtils test
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionClient.java
CommitLineData
79ce1a49 1package be.nikiroo.utils.serial.server;
ce0974c4
NR
2
3import java.io.IOException;
4import java.net.Socket;
f4053377 5import java.net.UnknownHostException;
ce0974c4 6
340e6065
NR
7import javax.net.ssl.SSLException;
8
f157aed8
NR
9/**
10 * Base class used for the client basic handling.
11 * <p>
12 * It represents a single action: a client is expected to only execute one
13 * action.
14 *
15 * @author niki
16 */
79ce1a49
NR
17abstract class ConnectActionClient {
18 /**
19 * The underlying {@link ConnectAction}.
20 * <p>
21 * Cannot be NULL.
22 */
23 protected ConnectAction action;
ce0974c4 24
f157aed8 25 /**
08f80ac5 26 * Create a new {@link ConnectActionClient}.
f157aed8
NR
27 *
28 * @param host
29 * the host to bind to
30 * @param port
31 * the port to bind to
8468bb79
NR
32 * @param key
33 * an optional key to encrypt all the communications (if NULL,
34 * everything will be sent in clear text)
f157aed8
NR
35 *
36 * @throws IOException
f4053377
NR
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
f157aed8 43 */
8468bb79 44 public ConnectActionClient(String host, int port, String key)
ce0974c4 45 throws IOException {
08f80ac5 46 this(new Socket(host, port), key);
f157aed8
NR
47 }
48
49 /**
50 * Create a new {@link ConnectActionClient}.
51 *
52 * @param s
53 * the socket to bind to
8468bb79
NR
54 * @param key
55 * an optional key to encrypt all the communications (if NULL,
56 * everything will be sent in clear text)
f157aed8 57 */
08f80ac5
NR
58 public ConnectActionClient(Socket s, String key) {
59 action = new ConnectAction(s, false, key) {
f157aed8 60 @Override
08f80ac5 61 protected void action() throws Exception {
340e6065 62 ConnectActionClient.this.clientHello();
08f80ac5 63 ConnectActionClient.this.action();
f157aed8
NR
64 }
65
66 @Override
67 protected void onError(Exception e) {
68 ConnectActionClient.this.onError(e);
69 }
f157aed8
NR
70 };
71 }
72
340e6065
NR
73 /**
74 * Send the HELLO message (send a String "HELLO" to the server, to check I/O
75 * and encryption modes).
76 * <p>
77 * Will automatically handle the answer (the server must answer "HELLO" in
78 * kind).
79 *
80 * @throws IOException
81 * in case of I/O error
82 * @throws SSLException
83 * in case of encryption error
84 */
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");
89 }
90 }
91
f157aed8
NR
92 /**
93 * Actually start the process and call the action (synchronous).
94 */
95 public void connect() {
96 action.connect();
97 }
98
99 /**
100 * Actually start the process and call the action (asynchronous).
101 */
102 public void connectAsync() {
103 new Thread(new Runnable() {
104 @Override
105 public void run() {
106 connect();
107 }
108 }).start();
ce0974c4
NR
109 }
110
f157aed8
NR
111 /**
112 * Method that will be called when an action is performed on the client.
113 *
f157aed8
NR
114 * @throws Exception
115 * in case of I/O error
116 */
117 @SuppressWarnings("unused")
08f80ac5 118 public void action() throws Exception {
ce0974c4 119 }
f157aed8 120
f157aed8
NR
121 /**
122 * Handler called when an unexpected error occurs in the code.
123 * <p>
124 * Will just ignore the error by default.
125 *
126 * @param e
127 * the exception that occurred
128 */
129 protected void onError(@SuppressWarnings("unused") Exception e) {
130 }
ce0974c4 131}