db06a9f247544cef1d210531d7db45429af1a78b
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 public ConnectActionClient(Socket s
) {
33 this(s
, Version
.getCurrentVersion());
37 * Create a new {@link ConnectActionClient} with the current application
38 * version (see {@link Version#getCurrentVersion()}) as the client version.
45 * TRUE for an SSL connection, FALSE for plain text
48 * in case of I/O error
49 * @throws UnknownHostException
50 * if the host is not known
51 * @throws IllegalArgumentException
52 * if the port parameter is outside the specified range of valid
53 * port values, which is between 0 and 65535, inclusive
55 public ConnectActionClient(String host
, int port
, boolean ssl
)
57 this(Server
.createSocket(host
, port
, ssl
), Version
.getCurrentVersion());
61 * Create a new {@link ConnectActionClient}.
68 * TRUE for an SSL connection, FALSE for plain text
73 * in case of I/O error
74 * @throws UnknownHostException
75 * if the host is not known
76 * @throws IllegalArgumentException
77 * if the port parameter is outside the specified range of valid
78 * port values, which is between 0 and 65535, inclusive
80 public ConnectActionClient(String host
, int port
, boolean ssl
,
81 Version version
) throws IOException
{
82 this(Server
.createSocket(host
, port
, ssl
), version
);
86 * Create a new {@link ConnectActionClient}.
89 * the socket to bind to
93 public ConnectActionClient(Socket s
, Version version
) {
94 action
= new ConnectAction(s
, false, version
) {
96 protected void action(Version serverVersion
) throws Exception
{
97 ConnectActionClient
.this.action(serverVersion
);
101 protected void onError(Exception e
) {
102 ConnectActionClient
.this.onError(e
);
106 protected Version
negotiateVersion(Version clientVersion
) {
107 new Exception("Should never be called on a client")
115 * Actually start the process and call the action (synchronous).
117 public void connect() {
122 * Actually start the process and call the action (asynchronous).
124 public void connectAsync() {
125 new Thread(new Runnable() {
134 * Method that will be called when an action is performed on the client.
136 * @param serverVersion
140 * in case of I/O error
142 @SuppressWarnings("unused")
143 public void action(Version serverVersion
) throws Exception
{
147 * Handler called when an unexpected error occurs in the code.
149 * Will just ignore the error by default.
152 * the exception that occurred
154 protected void onError(@SuppressWarnings("unused") Exception e
) {