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
{
17 * The underlying {@link ConnectAction}.
21 protected ConnectAction action
;
24 * Create a new {@link ConnectActionServer} with the current application
25 * version (see {@link Version#getCurrentVersion()}) as the server version.
28 * the socket to bind to
30 public ConnectActionServer(Socket s
) {
31 this(s
, Version
.getCurrentVersion());
35 * Create a new {@link ConnectActionServer}.
38 * the socket to bind to
42 public ConnectActionServer(Socket s
, Version version
) {
43 action
= new ConnectAction(s
, true, version
) {
45 protected void action(Version clientVersion
) throws Exception
{
46 ConnectActionServer
.this.action(clientVersion
);
50 protected void onError(Exception e
) {
51 ConnectActionServer
.this.onError(e
);
55 protected Version
negotiateVersion(Version clientVersion
) {
56 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
62 * Actually start the process and call the action (synchronous).
64 public void connect() {
69 * Actually start the process and call the action (asynchronous).
71 public void connectAsync() {
72 new Thread(new Runnable() {
81 * The total amount of bytes received.
83 * @return the amount of bytes received
85 public long getBytesReceived() {
86 return action
.getBytesReceived();
90 * The total amount of bytes sent.
92 * @return the amount of bytes sent
94 public long getBytesSent() {
95 return action
.getBytesSent();
99 * Method that will be called when an action is performed on the server.
101 * @param clientVersion
105 * in case of I/O error
107 @SuppressWarnings("unused")
108 public void action(Version clientVersion
) throws Exception
{
112 * Handler called when an unexpected error occurs in the code.
114 * Will just ignore the error by default.
117 * the exception that occurred
119 protected void onError(@SuppressWarnings("unused") Exception e
) {
123 * Method called when we negotiate the version with the client.
125 * Will return the actual server version by default.
127 * @param clientVersion
130 * @return the version to send to the client
132 protected Version
negotiateVersion(
133 @SuppressWarnings("unused") Version clientVersion
) {
134 return action
.getVersion();