1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.net
.Socket
;
5 import be
.nikiroo
.utils
.Version
;
8 * Base class used for the server basic handling.
10 * It represents a single action: a server is expected to execute one action for
15 abstract class ConnectActionServer
{
16 private boolean closing
;
19 * The underlying {@link ConnectAction}.
23 protected ConnectAction action
;
26 * Create a new {@link ConnectActionServer} with the current application
27 * version (see {@link Version#getCurrentVersion()}) as the server version.
30 * the socket to bind to
32 * an optional key to encrypt all the communications (if NULL,
33 * everything will be sent in clear text)
35 public ConnectActionServer(Socket s
, String key
) {
36 this(s
, key
, Version
.getCurrentVersion());
40 * Create a new {@link ConnectActionServer}.
43 * the socket to bind to
45 * an optional key to encrypt all the communications (if NULL,
46 * everything will be sent in clear text)
50 public ConnectActionServer(Socket s
, String key
, Version version
) {
51 action
= new ConnectAction(s
, true, key
, version
) {
53 protected void action(Version clientVersion
) throws Exception
{
54 ConnectActionServer
.this.action(clientVersion
);
58 protected void onError(Exception e
) {
59 ConnectActionServer
.this.onError(e
);
63 protected Version
negotiateVersion(Version clientVersion
) {
64 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
70 * Actually start the process and call the action (synchronous).
72 public void connect() {
77 * Actually start the process and call the action (asynchronous).
79 public void connectAsync() {
80 new Thread(new Runnable() {
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).
94 * Example of usage: the client failed an authentication check, cut the
95 * connection here and now.
97 * @return TRUE when it is
99 public boolean isClosing() {
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).
109 * Example of usage: the client failed an authentication check, cut the
110 * connection here and now.
112 public void close() {
117 * The total amount of bytes received.
119 * @return the amount of bytes received
121 public long getBytesReceived() {
122 return action
.getBytesReceived();
126 * The total amount of bytes sent.
128 * @return the amount of bytes sent
130 public long getBytesSent() {
131 return action
.getBytesSent();
135 * Method that will be called when an action is performed on the server.
137 * @param clientVersion
141 * in case of I/O error
143 @SuppressWarnings("unused")
144 public void action(Version clientVersion
) throws Exception
{
148 * Handler called when an unexpected error occurs in the code.
150 * Will just ignore the error by default.
153 * the exception that occurred
155 protected void onError(@SuppressWarnings("unused") Exception e
) {
159 * Method called when we negotiate the version with the client.
161 * Will return the actual server version by default.
163 * @param clientVersion
166 * @return the version to send to the client
168 protected Version
negotiateVersion(
169 @SuppressWarnings("unused") Version clientVersion
) {
170 return action
.getVersion();