1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
5 import java
.net
.UnknownHostException
;
7 import be
.nikiroo
.utils
.Version
;
10 * Base class used for the client basic handling.
12 * It represents a single action: a client is expected to only execute one
17 abstract class ConnectActionClient
{
19 * The underlying {@link ConnectAction}.
23 protected ConnectAction action
;
26 * Create a new {@link ConnectActionClient} with the current application
27 * version (see {@link Version#getCurrentVersion()}) as the client 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 ConnectActionClient(Socket s
, String key
) {
36 this(s
, key
, Version
.getCurrentVersion());
40 * Create a new {@link ConnectActionClient} with the current application
41 * version (see {@link Version#getCurrentVersion()}) as the client version.
48 * an optional key to encrypt all the communications (if NULL,
49 * everything will be sent in clear text)
52 * in case of I/O error
53 * @throws UnknownHostException
54 * if the host is not known
55 * @throws IllegalArgumentException
56 * if the port parameter is outside the specified range of valid
57 * port values, which is between 0 and 65535, inclusive
59 public ConnectActionClient(String host
, int port
, String key
)
61 this(new Socket(host
, port
), key
, Version
.getCurrentVersion());
65 * Create a new {@link ConnectActionClient}.
72 * an optional key to encrypt all the communications (if NULL,
73 * everything will be sent in clear text)
78 * in case of I/O error
79 * @throws UnknownHostException
80 * if the host is not known
81 * @throws IllegalArgumentException
82 * if the port parameter is outside the specified range of valid
83 * port values, which is between 0 and 65535, inclusive
85 public ConnectActionClient(String host
, int port
, String key
,
86 Version version
) throws IOException
{
87 this(new Socket(host
, port
), key
, version
);
91 * Create a new {@link ConnectActionClient}.
94 * the socket to bind to
96 * an optional key to encrypt all the communications (if NULL,
97 * everything will be sent in clear text)
101 public ConnectActionClient(Socket s
, String key
, Version version
) {
102 action
= new ConnectAction(s
, false, key
, version
) {
104 protected void action(Version serverVersion
) throws Exception
{
105 ConnectActionClient
.this.action(serverVersion
);
109 protected void onError(Exception e
) {
110 ConnectActionClient
.this.onError(e
);
114 protected Version
negotiateVersion(Version clientVersion
) {
115 new Exception("Should never be called on a client")
123 * Actually start the process and call the action (synchronous).
125 public void connect() {
130 * Actually start the process and call the action (asynchronous).
132 public void connectAsync() {
133 new Thread(new Runnable() {
142 * Method that will be called when an action is performed on the client.
144 * @param serverVersion
148 * in case of I/O error
150 @SuppressWarnings("unused")
151 public void action(Version serverVersion
) throws Exception
{
155 * Handler called when an unexpected error occurs in the code.
157 * Will just ignore the error by default.
160 * the exception that occurred
162 protected void onError(@SuppressWarnings("unused") Exception e
) {