Merge commit 'b459e462a5c3447d0693674253c40bc7385a4f66'
[fanfix.git] / serial / server / ConnectActionClient.java
1 package be.nikiroo.utils.serial.server;
2
3 import java.io.IOException;
4 import java.net.Socket;
5 import java.net.UnknownHostException;
6
7 import be.nikiroo.utils.Version;
8
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 */
17 abstract class ConnectActionClient {
18 /**
19 * The underlying {@link ConnectAction}.
20 * <p>
21 * Cannot be NULL.
22 */
23 protected ConnectAction action;
24
25 /**
26 * Create a new {@link ConnectActionClient}, using the current version of
27 * the program.
28 *
29 * @param host
30 * the host to bind to
31 * @param port
32 * the port to bind to
33 * @param key
34 * an optional key to encrypt all the communications (if NULL,
35 * everything will be sent in clear text)
36 *
37 *
38 * @throws IOException
39 * in case of I/O error
40 * @throws UnknownHostException
41 * if the host is not known
42 * @throws IllegalArgumentException
43 * if the port parameter is outside the specified range of valid
44 * port values, which is between 0 and 65535, inclusive
45 */
46 public ConnectActionClient(String host, int port, String key)
47 throws IOException {
48 this(host, port, key, Version.getCurrentVersion());
49 }
50
51 /**
52 * Create a new {@link ConnectActionClient}.
53 *
54 * @param host
55 * the host to bind to
56 * @param port
57 * the port to bind to
58 * @param key
59 * an optional key to encrypt all the communications (if NULL,
60 * everything will be sent in clear text)
61 * @param clientVersion
62 * the client version
63 *
64 *
65 * @throws IOException
66 * in case of I/O error
67 * @throws UnknownHostException
68 * if the host is not known
69 * @throws IllegalArgumentException
70 * if the port parameter is outside the specified range of valid
71 * port values, which is between 0 and 65535, inclusive
72 */
73 public ConnectActionClient(String host, int port, String key,
74 Version clientVersion) throws IOException {
75 this(new Socket(host, port), key, clientVersion);
76 }
77
78 /**
79 * Create a new {@link ConnectActionClient}, using the current version of
80 * the program.
81 *
82 * @param s
83 * the socket to bind to
84 * @param key
85 * an optional key to encrypt all the communications (if NULL,
86 * everything will be sent in clear text)
87 */
88 public ConnectActionClient(Socket s, String key) {
89 this(s, key, Version.getCurrentVersion());
90 }
91
92 /**
93 * Create a new {@link ConnectActionClient}.
94 *
95 * @param s
96 * the socket to bind to
97 * @param key
98 * an optional key to encrypt all the communications (if NULL,
99 * everything will be sent in clear text)
100 * @param clientVersion
101 * the client version
102 */
103 public ConnectActionClient(Socket s, String key, Version clientVersion) {
104 action = new ConnectAction(s, false, key, clientVersion) {
105 @Override
106 protected void action(Version serverVersion) throws Exception {
107 ConnectActionClient.this.action(serverVersion);
108 }
109
110 @Override
111 protected void onError(Exception e) {
112 ConnectActionClient.this.onError(e);
113 }
114
115 @Override
116 protected Version negotiateVersion(Version clientVersion) {
117 new Exception("Should never be called on a client")
118 .printStackTrace();
119 return null;
120 }
121 };
122 }
123
124 /**
125 * Actually start the process and call the action (synchronous).
126 */
127 public void connect() {
128 action.connect();
129 }
130
131 /**
132 * Actually start the process and call the action (asynchronous).
133 */
134 public void connectAsync() {
135 new Thread(new Runnable() {
136 @Override
137 public void run() {
138 connect();
139 }
140 }).start();
141 }
142
143 /**
144 * Method that will be called when an action is performed on the client.
145 *
146 * @param serverVersion
147 * the version of the server connected to this client
148 *
149 * @throws Exception
150 * in case of I/O error
151 */
152 @SuppressWarnings("unused")
153 public void action(Version serverVersion) throws Exception {
154 }
155
156 /**
157 * Handler called when an unexpected error occurs in the code.
158 * <p>
159 * Will just ignore the error by default.
160 *
161 * @param e
162 * the exception that occurred
163 */
164 protected void onError(@SuppressWarnings("unused") Exception e) {
165 }
166 }