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