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 | |
3087aeb5 NR |
7 | import 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 |
17 | abstract class ConnectActionClient { |
18 | /** | |
19 | * The underlying {@link ConnectAction}. | |
20 | * <p> | |
21 | * Cannot be NULL. | |
22 | */ | |
23 | protected ConnectAction action; | |
ce0974c4 | 24 | |
f157aed8 | 25 | /** |
3087aeb5 NR |
26 | * Create a new {@link ConnectActionClient}, using the current version of |
27 | * the program. | |
f157aed8 NR |
28 | * |
29 | * @param host | |
30 | * the host to bind to | |
31 | * @param port | |
32 | * the port to bind to | |
8468bb79 NR |
33 | * @param key |
34 | * an optional key to encrypt all the communications (if NULL, | |
35 | * everything will be sent in clear text) | |
f157aed8 | 36 | * |
3087aeb5 | 37 | * |
f157aed8 | 38 | * @throws IOException |
f4053377 NR |
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 | |
f157aed8 | 45 | */ |
8468bb79 | 46 | public ConnectActionClient(String host, int port, String key) |
ce0974c4 | 47 | throws IOException { |
3087aeb5 | 48 | this(host, port, key, Version.getCurrentVersion()); |
f157aed8 NR |
49 | } |
50 | ||
51 | /** | |
52 | * Create a new {@link ConnectActionClient}. | |
53 | * | |
3087aeb5 NR |
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 | * | |
f157aed8 NR |
82 | * @param s |
83 | * the socket to bind to | |
8468bb79 NR |
84 | * @param key |
85 | * an optional key to encrypt all the communications (if NULL, | |
86 | * everything will be sent in clear text) | |
f157aed8 | 87 | */ |
08f80ac5 | 88 | public ConnectActionClient(Socket s, String key) { |
3087aeb5 NR |
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) { | |
f157aed8 | 105 | @Override |
3087aeb5 | 106 | protected void action(Version serverVersion) throws Exception { |
3087aeb5 | 107 | ConnectActionClient.this.action(serverVersion); |
f157aed8 NR |
108 | } |
109 | ||
110 | @Override | |
111 | protected void onError(Exception e) { | |
112 | ConnectActionClient.this.onError(e); | |
113 | } | |
3087aeb5 NR |
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 | } | |
f157aed8 NR |
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(); | |
ce0974c4 NR |
141 | } |
142 | ||
f157aed8 NR |
143 | /** |
144 | * Method that will be called when an action is performed on the client. | |
145 | * | |
3087aeb5 NR |
146 | * @param serverVersion |
147 | * the version of the server connected to this client | |
148 | * | |
f157aed8 NR |
149 | * @throws Exception |
150 | * in case of I/O error | |
151 | */ | |
152 | @SuppressWarnings("unused") | |
3087aeb5 | 153 | public void action(Version serverVersion) throws Exception { |
ce0974c4 | 154 | } |
f157aed8 | 155 | |
f157aed8 NR |
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 | } | |
ce0974c4 | 166 | } |