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