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