Commit | Line | Data |
---|---|---|
79ce1a49 | 1 | package be.nikiroo.utils.serial.server; |
ce0974c4 | 2 | |
340e6065 | 3 | import java.io.IOException; |
ce0974c4 NR |
4 | import java.net.Socket; |
5 | ||
340e6065 NR |
6 | import javax.net.ssl.SSLException; |
7 | ||
3087aeb5 NR |
8 | import be.nikiroo.utils.Version; |
9 | ||
f157aed8 NR |
10 | /** |
11 | * Base class used for the server basic handling. | |
12 | * <p> | |
13 | * It represents a single action: a server is expected to execute one action for | |
14 | * each client action. | |
15 | * | |
16 | * @author niki | |
17 | */ | |
79ce1a49 | 18 | abstract class ConnectActionServer { |
a72ea8a7 NR |
19 | private boolean closing; |
20 | ||
79ce1a49 NR |
21 | /** |
22 | * The underlying {@link ConnectAction}. | |
23 | * <p> | |
24 | * Cannot be NULL. | |
25 | */ | |
26 | protected ConnectAction action; | |
f157aed8 | 27 | |
f157aed8 | 28 | /** |
3087aeb5 | 29 | * Create a new {@link ConnectActionServer}, using the current version. |
f157aed8 NR |
30 | * |
31 | * @param s | |
32 | * the socket 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 | */ |
08f80ac5 | 37 | public ConnectActionServer(Socket s, String key) { |
3087aeb5 NR |
38 | this(s, key, Version.getCurrentVersion()); |
39 | } | |
40 | ||
41 | /** | |
42 | * Create a new {@link ConnectActionServer}. | |
43 | * | |
44 | * @param s | |
45 | * the socket to bind to | |
46 | * @param key | |
47 | * an optional key to encrypt all the communications (if NULL, | |
48 | * everything will be sent in clear text) | |
49 | * @param serverVersion | |
50 | * the version of this server,that will be sent to the client | |
51 | */ | |
52 | public ConnectActionServer(Socket s, String key, Version serverVersion) { | |
53 | action = new ConnectAction(s, true, key, serverVersion) { | |
f157aed8 | 54 | @Override |
3087aeb5 | 55 | protected void action(Version clientVersion) throws Exception { |
340e6065 | 56 | ConnectActionServer.this.serverHello(); |
3087aeb5 | 57 | ConnectActionServer.this.action(clientVersion); |
f157aed8 NR |
58 | } |
59 | ||
60 | @Override | |
61 | protected void onError(Exception e) { | |
62 | ConnectActionServer.this.onError(e); | |
63 | } | |
3087aeb5 NR |
64 | |
65 | @Override | |
66 | protected Version negotiateVersion(Version clientVersion) { | |
67 | return ConnectActionServer.this.negotiateVersion(clientVersion); | |
68 | } | |
f157aed8 NR |
69 | }; |
70 | } | |
71 | ||
340e6065 NR |
72 | /** |
73 | * Send the HELLO message (check that the client sends a String "HELLO" and | |
74 | * send it back, to check I/O and encryption modes). | |
75 | * | |
76 | * @throws IOException | |
77 | * in case of I/O error | |
78 | * @throws SSLException | |
79 | * in case of encryption error | |
80 | */ | |
81 | protected void serverHello() throws IOException, SSLException { | |
82 | String HELLO = action.recString(); | |
83 | if (!"HELLO".equals(HELLO)) { | |
84 | throw new SSLException("Server did not accept the encryption key"); | |
85 | } | |
86 | action.sendString(HELLO); | |
87 | } | |
88 | ||
f157aed8 NR |
89 | /** |
90 | * Actually start the process and call the action (synchronous). | |
91 | */ | |
92 | public void connect() { | |
93 | action.connect(); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Actually start the process and call the action (asynchronous). | |
98 | */ | |
99 | public void connectAsync() { | |
100 | new Thread(new Runnable() { | |
101 | @Override | |
102 | public void run() { | |
103 | connect(); | |
104 | } | |
105 | }).start(); | |
106 | } | |
107 | ||
a72ea8a7 NR |
108 | /** |
109 | * Stop the client/server connection on behalf of the server (usually, the | |
110 | * client connects then is allowed to send as many requests as it wants; in | |
111 | * some cases, though, the server may wish to forcefully close the | |
112 | * connection and can do via this value, when it is set to TRUE). | |
113 | * <p> | |
114 | * Example of usage: the client failed an authentication check, cut the | |
115 | * connection here and now. | |
8468bb79 NR |
116 | * |
117 | * @return TRUE when it is | |
a72ea8a7 NR |
118 | */ |
119 | public boolean isClosing() { | |
120 | return closing; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Can be called to stop the client/server connection on behalf of the | |
125 | * server (usually, the client connects then is allowed to send as many | |
126 | * requests as it wants; in some cases, though, the server may wish to | |
127 | * forcefully close the connection and can do so by calling this method). | |
128 | * <p> | |
129 | * Example of usage: the client failed an authentication check, cut the | |
130 | * connection here and now. | |
131 | */ | |
132 | public void close() { | |
133 | closing = true; | |
134 | } | |
135 | ||
4bb7e88e NR |
136 | /** |
137 | * The total amount of bytes received. | |
138 | * | |
139 | * @return the amount of bytes received | |
140 | */ | |
141 | public long getBytesReceived() { | |
142 | return action.getBytesReceived(); | |
143 | } | |
144 | ||
145 | /** | |
146 | * The total amount of bytes sent. | |
147 | * | |
148 | * @return the amount of bytes sent | |
149 | */ | |
150 | public long getBytesSent() { | |
08f80ac5 | 151 | return action.getBytesWritten(); |
4bb7e88e NR |
152 | } |
153 | ||
f157aed8 NR |
154 | /** |
155 | * Method that will be called when an action is performed on the server. | |
156 | * | |
3087aeb5 NR |
157 | * @param clientVersion |
158 | * the version of the client connected to this server | |
159 | * | |
f157aed8 NR |
160 | * @throws Exception |
161 | * in case of I/O error | |
162 | */ | |
163 | @SuppressWarnings("unused") | |
3087aeb5 | 164 | public void action(Version clientVersion) throws Exception { |
f157aed8 NR |
165 | } |
166 | ||
f157aed8 NR |
167 | /** |
168 | * Handler called when an unexpected error occurs in the code. | |
169 | * <p> | |
170 | * Will just ignore the error by default. | |
171 | * | |
172 | * @param e | |
173 | * the exception that occurred | |
174 | */ | |
175 | protected void onError(@SuppressWarnings("unused") Exception e) { | |
176 | } | |
3087aeb5 NR |
177 | |
178 | /** | |
179 | * Method called when we negotiate the version with the client. | |
180 | * <p> | |
181 | * Will return the actual server version by default. | |
182 | * | |
183 | * @param clientVersion | |
184 | * the client version | |
185 | * | |
186 | * @return the version to send to the client | |
187 | */ | |
188 | protected Version negotiateVersion( | |
189 | @SuppressWarnings("unused") Version clientVersion) { | |
190 | return action.getVersion(); | |
191 | } | |
ce0974c4 | 192 | } |