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 * Method that will be called when an action is performed on the server.
83 * @param clientVersion
87 * in case of I/O error
89 @SuppressWarnings("unused")
90 public void action(Version clientVersion
) throws Exception
{
94 * Handler called when an unexpected error occurs in the code.
96 * Will just ignore the error by default.
99 * the exception that occurred
101 protected void onError(@SuppressWarnings("unused") Exception e
) {
105 * Method called when we negotiate the version with the client.
107 * Will return the actual server version by default.
109 * @param clientVersion
112 * @return the version to send to the client
114 protected Version
negotiateVersion(
115 @SuppressWarnings("unused") Version clientVersion
) {
116 return action
.getVersion();