350d3fe40a1320b68f555458a43c3ceec11e8c25
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}, using the current version.
29 * the socket to bind to
31 * an optional key to encrypt all the communications (if NULL,
32 * everything will be sent in clear text)
34 public ConnectActionServer(Socket s
, String key
) {
35 this(s
, key
, Version
.getCurrentVersion());
39 * Create a new {@link ConnectActionServer}.
42 * the socket to bind to
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
49 public ConnectActionServer(Socket s
, String key
, Version serverVersion
) {
50 action
= new ConnectAction(s
, true, key
, serverVersion
) {
52 protected void action(Version clientVersion
) throws Exception
{
53 ConnectActionServer
.this.action(clientVersion
);
57 protected void onError(Exception e
) {
58 ConnectActionServer
.this.onError(e
);
62 protected Version
negotiateVersion(Version clientVersion
) {
63 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
69 * Actually start the process and call the action (synchronous).
71 public void connect() {
76 * Actually start the process and call the action (asynchronous).
78 public void connectAsync() {
79 new Thread(new Runnable() {
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).
93 * Example of usage: the client failed an authentication check, cut the
94 * connection here and now.
96 * @return TRUE when it is
98 public boolean isClosing() {
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).
108 * Example of usage: the client failed an authentication check, cut the
109 * connection here and now.
111 public void close() {
116 * The total amount of bytes received.
118 * @return the amount of bytes received
120 public long getBytesReceived() {
121 return action
.getBytesReceived();
125 * The total amount of bytes sent.
127 * @return the amount of bytes sent
129 public long getBytesSent() {
130 return action
.getBytesWritten();
134 * Method that will be called when an action is performed on the server.
136 * @param clientVersion
137 * the version of the client connected to this server
140 * in case of I/O error
142 @SuppressWarnings("unused")
143 public void action(Version clientVersion
) throws Exception
{
147 * Handler called when an unexpected error occurs in the code.
149 * Will just ignore the error by default.
152 * the exception that occurred
154 protected void onError(@SuppressWarnings("unused") Exception e
) {
158 * Method called when we negotiate the version with the client.
160 * Will return the actual server version by default.
162 * @param clientVersion
165 * @return the version to send to the client
167 protected Version
negotiateVersion(
168 @SuppressWarnings("unused") Version clientVersion
) {
169 return action
.getVersion();