83537025d00e103adc127ebad31b096129609f85
1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import be
.nikiroo
.utils
.Version
;
9 * Base class used for the client basic handling.
11 * It represents a single action: a client is expected to only execute one
16 public class ConnectActionClient
{
17 private ConnectAction action
;
20 * Create a new {@link ConnectActionClient} with the current application
21 * version (see {@link Version#getCurrentVersion()}) as the client version.
24 * the socket to bind to
26 public ConnectActionClient(Socket s
) {
27 this(s
, Version
.getCurrentVersion());
31 * Create a new {@link ConnectActionClient} with the current application
32 * version (see {@link Version#getCurrentVersion()}) as the client version.
39 * TRUE for an SSL connection, FALSE for plain text
42 * in case of I/O error when creating the socket
44 public ConnectActionClient(String host
, int port
, boolean ssl
)
46 this(Server
.createSocket(host
, port
, ssl
), Version
.getCurrentVersion());
50 * Create a new {@link ConnectActionClient}.
57 * TRUE for an SSL connection, FALSE for plain text
62 * in case of I/O error when creating the socket
64 public ConnectActionClient(String host
, int port
, boolean ssl
,
65 Version version
) throws IOException
{
66 this(Server
.createSocket(host
, port
, ssl
), version
);
70 * Create a new {@link ConnectActionClient}.
73 * the socket to bind to
77 public ConnectActionClient(Socket s
, Version version
) {
78 action
= new ConnectAction(s
, false, version
) {
80 protected void action(Version serverVersion
) throws Exception
{
81 ConnectActionClient
.this.action(serverVersion
);
85 protected void onError(Exception e
) {
86 ConnectActionClient
.this.onError(e
);
90 protected Version
negotiateVersion(Version clientVersion
) {
91 new Exception("Should never be called on a client")
99 * Actually start the process and call the action (synchronous).
101 public void connect() {
106 * Actually start the process and call the action (asynchronous).
108 public void connectAsync() {
109 new Thread(new Runnable() {
118 * Method that will be called when an action is performed on the client.
120 * @param serverVersion
124 * in case of I/O error
126 @SuppressWarnings("unused")
127 public void action(Version serverVersion
) throws Exception
{
131 * Serialise and send the given object to the server (and return the
132 * deserialised answer).
137 * @return the answer, which can be NULL
139 * @throws IOException
140 * in case of I/O error
141 * @throws NoSuchFieldException
142 * if the serialised data contains information about a field
143 * which does actually not exist in the class we know of
144 * @throws NoSuchMethodException
145 * if a class described in the serialised data cannot be created
146 * because it is not compatible with this code
147 * @throws ClassNotFoundException
148 * if a class described in the serialised data cannot be found
150 public Object
send(Object data
) throws IOException
, NoSuchFieldException
,
151 NoSuchMethodException
, ClassNotFoundException
{
152 return action
.send(data
);
156 * Handler called when an unexpected error occurs in the code.
158 * Will just ignore the error by default.
161 * the exception that occurred
163 protected void onError(@SuppressWarnings("unused") Exception e
) {
169 * Do not use. Will never be called.
171 @SuppressWarnings({ "unused", "javadoc" })
173 protected void onClientVersionReceived(Version clientVersion
) {
177 * Do not use, it is not supposed to be called from the outside.
179 @SuppressWarnings({ "unused", "javadoc" })
181 public Object
flush() throws NoSuchFieldException
, NoSuchMethodException
,
182 ClassNotFoundException
, IOException
, java
.lang
.NullPointerException
{