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}, using the current version of
34 * an optional key to encrypt all the communications (if NULL,
35 * everything will be sent in clear text)
39 * in case of I/O error
40 * @throws UnknownHostException
41 * if the host is not known
42 * @throws IllegalArgumentException
43 * if the port parameter is outside the specified range of valid
44 * port values, which is between 0 and 65535, inclusive
46 public ConnectActionClient(String host
, int port
, String key
)
48 this(host
, port
, key
, Version
.getCurrentVersion());
52 * Create a new {@link ConnectActionClient}.
59 * an optional key to encrypt all the communications (if NULL,
60 * everything will be sent in clear text)
61 * @param clientVersion
66 * in case of I/O error
67 * @throws UnknownHostException
68 * if the host is not known
69 * @throws IllegalArgumentException
70 * if the port parameter is outside the specified range of valid
71 * port values, which is between 0 and 65535, inclusive
73 public ConnectActionClient(String host
, int port
, String key
,
74 Version clientVersion
) throws IOException
{
75 this(new Socket(host
, port
), key
, clientVersion
);
79 * Create a new {@link ConnectActionClient}, using the current version of
83 * the socket to bind to
85 * an optional key to encrypt all the communications (if NULL,
86 * everything will be sent in clear text)
88 public ConnectActionClient(Socket s
, String key
) {
89 this(s
, key
, Version
.getCurrentVersion());
93 * Create a new {@link ConnectActionClient}.
96 * the socket to bind to
98 * an optional key to encrypt all the communications (if NULL,
99 * everything will be sent in clear text)
100 * @param clientVersion
103 public ConnectActionClient(Socket s
, String key
, Version clientVersion
) {
104 action
= new ConnectAction(s
, false, key
, clientVersion
) {
106 protected void action(Version serverVersion
) throws Exception
{
107 ConnectActionClient
.this.action(serverVersion
);
111 protected void onError(Exception e
) {
112 ConnectActionClient
.this.onError(e
);
116 protected Version
negotiateVersion(Version clientVersion
) {
117 new Exception("Should never be called on a client")
125 * Actually start the process and call the action (synchronous).
127 public void connect() {
132 * Actually start the process and call the action (asynchronous).
134 public void connectAsync() {
135 new Thread(new Runnable() {
144 * Method that will be called when an action is performed on the client.
146 * @param serverVersion
147 * the version of the server connected to this client
150 * in case of I/O error
152 @SuppressWarnings("unused")
153 public void action(Version serverVersion
) throws Exception
{
157 * Handler called when an unexpected error occurs in the code.
159 * Will just ignore the error by default.
162 * the exception that occurred
164 protected void onError(@SuppressWarnings("unused") Exception e
) {