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