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 public ConnectActionServer(Socket s
) {
33 this(s
, Version
.getCurrentVersion());
37 * Create a new {@link ConnectActionServer}.
40 * the socket to bind to
44 public ConnectActionServer(Socket s
, Version version
) {
45 action
= new ConnectAction(s
, true, version
) {
47 protected void action(Version clientVersion
) throws Exception
{
48 ConnectActionServer
.this.action(clientVersion
);
52 protected void onError(Exception e
) {
53 ConnectActionServer
.this.onError(e
);
57 protected Version
negotiateVersion(Version clientVersion
) {
58 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
64 * Actually start the process and call the action (synchronous).
66 public void connect() {
71 * Actually start the process and call the action (asynchronous).
73 public void connectAsync() {
74 new Thread(new Runnable() {
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).
88 * Example of usage: the client failed an authentication check, cut the
89 * connection here and now.
91 public boolean isClosing() {
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).
101 * Example of usage: the client failed an authentication check, cut the
102 * connection here and now.
104 public void close() {
109 * The total amount of bytes received.
111 * @return the amount of bytes received
113 public long getBytesReceived() {
114 return action
.getBytesReceived();
118 * The total amount of bytes sent.
120 * @return the amount of bytes sent
122 public long getBytesSent() {
123 return action
.getBytesSent();
127 * Method that will be called when an action is performed on the server.
129 * @param clientVersion
133 * in case of I/O error
135 @SuppressWarnings("unused")
136 public void action(Version clientVersion
) throws Exception
{
140 * Handler called when an unexpected error occurs in the code.
142 * Will just ignore the error by default.
145 * the exception that occurred
147 protected void onError(@SuppressWarnings("unused") Exception e
) {
151 * Method called when we negotiate the version with the client.
153 * Will return the actual server version by default.
155 * @param clientVersion
158 * @return the version to send to the client
160 protected Version
negotiateVersion(
161 @SuppressWarnings("unused") Version clientVersion
) {
162 return action
.getVersion();