Commit | Line | Data |
---|---|---|
79ce1a49 | 1 | package be.nikiroo.utils.serial.server; |
ce0974c4 NR |
2 | |
3 | import java.io.IOException; | |
4 | import java.net.Socket; | |
f4053377 | 5 | import java.net.UnknownHostException; |
ce0974c4 | 6 | |
340e6065 NR |
7 | import javax.net.ssl.SSLException; |
8 | ||
3087aeb5 NR |
9 | import be.nikiroo.utils.Version; |
10 | ||
f157aed8 NR |
11 | /** |
12 | * Base class used for the client basic handling. | |
13 | * <p> | |
14 | * It represents a single action: a client is expected to only execute one | |
15 | * action. | |
16 | * | |
17 | * @author niki | |
18 | */ | |
79ce1a49 NR |
19 | abstract class ConnectActionClient { |
20 | /** | |
21 | * The underlying {@link ConnectAction}. | |
22 | * <p> | |
23 | * Cannot be NULL. | |
24 | */ | |
25 | protected ConnectAction action; | |
ce0974c4 | 26 | |
f157aed8 | 27 | /** |
3087aeb5 NR |
28 | * Create a new {@link ConnectActionClient}, using the current version of |
29 | * the program. | |
f157aed8 NR |
30 | * |
31 | * @param host | |
32 | * the host to bind to | |
33 | * @param port | |
34 | * the port to bind to | |
8468bb79 NR |
35 | * @param key |
36 | * an optional key to encrypt all the communications (if NULL, | |
37 | * everything will be sent in clear text) | |
f157aed8 | 38 | * |
3087aeb5 | 39 | * |
f157aed8 | 40 | * @throws IOException |
f4053377 NR |
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 | |
f157aed8 | 47 | */ |
8468bb79 | 48 | public ConnectActionClient(String host, int port, String key) |
ce0974c4 | 49 | throws IOException { |
3087aeb5 | 50 | this(host, port, key, Version.getCurrentVersion()); |
f157aed8 NR |
51 | } |
52 | ||
53 | /** | |
54 | * Create a new {@link ConnectActionClient}. | |
55 | * | |
3087aeb5 NR |
56 | * @param host |
57 | * the host to bind to | |
58 | * @param port | |
59 | * the port to bind to | |
60 | * @param key | |
61 | * an optional key to encrypt all the communications (if NULL, | |
62 | * everything will be sent in clear text) | |
63 | * @param clientVersion | |
64 | * the client version | |
65 | * | |
66 | * | |
67 | * @throws IOException | |
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 | |
74 | */ | |
75 | public ConnectActionClient(String host, int port, String key, | |
76 | Version clientVersion) throws IOException { | |
77 | this(new Socket(host, port), key, clientVersion); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Create a new {@link ConnectActionClient}, using the current version of | |
82 | * the program. | |
83 | * | |
f157aed8 NR |
84 | * @param s |
85 | * the socket to bind to | |
8468bb79 NR |
86 | * @param key |
87 | * an optional key to encrypt all the communications (if NULL, | |
88 | * everything will be sent in clear text) | |
f157aed8 | 89 | */ |
08f80ac5 | 90 | public ConnectActionClient(Socket s, String key) { |
3087aeb5 NR |
91 | this(s, key, Version.getCurrentVersion()); |
92 | } | |
93 | ||
94 | /** | |
95 | * Create a new {@link ConnectActionClient}. | |
96 | * | |
97 | * @param s | |
98 | * the socket to bind to | |
99 | * @param key | |
100 | * an optional key to encrypt all the communications (if NULL, | |
101 | * everything will be sent in clear text) | |
102 | * @param clientVersion | |
103 | * the client version | |
104 | */ | |
105 | public ConnectActionClient(Socket s, String key, Version clientVersion) { | |
106 | action = new ConnectAction(s, false, key, clientVersion) { | |
f157aed8 | 107 | @Override |
3087aeb5 | 108 | protected void action(Version serverVersion) throws Exception { |
340e6065 | 109 | ConnectActionClient.this.clientHello(); |
3087aeb5 | 110 | ConnectActionClient.this.action(serverVersion); |
f157aed8 NR |
111 | } |
112 | ||
113 | @Override | |
114 | protected void onError(Exception e) { | |
115 | ConnectActionClient.this.onError(e); | |
116 | } | |
3087aeb5 NR |
117 | |
118 | @Override | |
119 | protected Version negotiateVersion(Version clientVersion) { | |
120 | new Exception("Should never be called on a client") | |
121 | .printStackTrace(); | |
122 | return null; | |
123 | } | |
f157aed8 NR |
124 | }; |
125 | } | |
126 | ||
340e6065 NR |
127 | /** |
128 | * Send the HELLO message (send a String "HELLO" to the server, to check I/O | |
129 | * and encryption modes). | |
130 | * <p> | |
131 | * Will automatically handle the answer (the server must answer "HELLO" in | |
132 | * kind). | |
133 | * | |
134 | * @throws IOException | |
135 | * in case of I/O error | |
136 | * @throws SSLException | |
137 | * in case of encryption error | |
138 | */ | |
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"); | |
143 | } | |
144 | } | |
145 | ||
f157aed8 NR |
146 | /** |
147 | * Actually start the process and call the action (synchronous). | |
148 | */ | |
149 | public void connect() { | |
150 | action.connect(); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Actually start the process and call the action (asynchronous). | |
155 | */ | |
156 | public void connectAsync() { | |
157 | new Thread(new Runnable() { | |
158 | @Override | |
159 | public void run() { | |
160 | connect(); | |
161 | } | |
162 | }).start(); | |
ce0974c4 NR |
163 | } |
164 | ||
f157aed8 NR |
165 | /** |
166 | * Method that will be called when an action is performed on the client. | |
167 | * | |
3087aeb5 NR |
168 | * @param serverVersion |
169 | * the version of the server connected to this client | |
170 | * | |
f157aed8 NR |
171 | * @throws Exception |
172 | * in case of I/O error | |
173 | */ | |
174 | @SuppressWarnings("unused") | |
3087aeb5 | 175 | public void action(Version serverVersion) throws Exception { |
ce0974c4 | 176 | } |
f157aed8 | 177 | |
f157aed8 NR |
178 | /** |
179 | * Handler called when an unexpected error occurs in the code. | |
180 | * <p> | |
181 | * Will just ignore the error by default. | |
182 | * | |
183 | * @param e | |
184 | * the exception that occurred | |
185 | */ | |
186 | protected void onError(@SuppressWarnings("unused") Exception e) { | |
187 | } | |
ce0974c4 | 188 | } |