X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FConnectActionClient.java;h=83537025d00e103adc127ebad31b096129609f85;hp=5b982021fe60bc9c2633ed3283b41ad6b4629288;hb=f157aed840bdd5b8ef04902d2326d916f71139da;hpb=08a58812f12617289463b00161c98d7c59490bf2 diff --git a/src/be/nikiroo/utils/serial/ConnectActionClient.java b/src/be/nikiroo/utils/serial/ConnectActionClient.java index 5b98202..8353702 100644 --- a/src/be/nikiroo/utils/serial/ConnectActionClient.java +++ b/src/be/nikiroo/utils/serial/ConnectActionClient.java @@ -5,27 +5,181 @@ import java.net.Socket; import be.nikiroo.utils.Version; -public class ConnectActionClient extends ConnectAction { - protected ConnectActionClient(Socket s) { - super(s, false, Version.getCurrentVersion()); - } +/** + * Base class used for the client basic handling. + *

+ * It represents a single action: a client is expected to only execute one + * action. + * + * @author niki + */ +public class ConnectActionClient { + private ConnectAction action; - protected ConnectActionClient(Socket s, Version version) { - super(s, false, version); + /** + * Create a new {@link ConnectActionClient} with the current application + * version (see {@link Version#getCurrentVersion()}) as the client version. + * + * @param s + * the socket to bind to + */ + public ConnectActionClient(Socket s) { + this(s, Version.getCurrentVersion()); } - protected ConnectActionClient(String host, int port, boolean ssl) + /** + * Create a new {@link ConnectActionClient} with the current application + * version (see {@link Version#getCurrentVersion()}) as the client version. + * + * @param host + * the host to bind to + * @param port + * the port to bind to + * @param ssl + * TRUE for an SSL connection, FALSE for plain text + * + * @throws IOException + * in case of I/O error when creating the socket + */ + public ConnectActionClient(String host, int port, boolean ssl) throws IOException { - super(Server.createSocket(host, port, ssl), false, Version - .getCurrentVersion()); + this(Server.createSocket(host, port, ssl), Version.getCurrentVersion()); } - protected ConnectActionClient(String host, int port, boolean ssl, + /** + * Create a new {@link ConnectActionClient}. + * + * @param host + * the host to bind to + * @param port + * the port to bind to + * @param ssl + * TRUE for an SSL connection, FALSE for plain text + * @param version + * the client version + * + * @throws IOException + * in case of I/O error when creating the socket + */ + public ConnectActionClient(String host, int port, boolean ssl, Version version) throws IOException { - super(Server.createSocket(host, port, ssl), false, version); + this(Server.createSocket(host, port, ssl), version); + } + + /** + * Create a new {@link ConnectActionClient}. + * + * @param s + * the socket to bind to + * @param version + * the client version + */ + public ConnectActionClient(Socket s, Version version) { + action = new ConnectAction(s, false, version) { + @Override + protected void action(Version serverVersion) throws Exception { + ConnectActionClient.this.action(serverVersion); + } + + @Override + protected void onError(Exception e) { + ConnectActionClient.this.onError(e); + } + + @Override + protected Version negotiateVersion(Version clientVersion) { + new Exception("Should never be called on a client") + .printStackTrace(); + return null; + } + }; + } + + /** + * Actually start the process and call the action (synchronous). + */ + public void connect() { + action.connect(); + } + + /** + * Actually start the process and call the action (asynchronous). + */ + public void connectAsync() { + new Thread(new Runnable() { + @Override + public void run() { + connect(); + } + }).start(); } - @Override + /** + * Method that will be called when an action is performed on the client. + * + * @param serverVersion + * the server version + * + * @throws Exception + * in case of I/O error + */ + @SuppressWarnings("unused") public void action(Version serverVersion) throws Exception { } + + /** + * Serialise and send the given object to the server (and return the + * deserialised answer). + * + * @param data + * the data to send + * + * @return the answer, which can be NULL + * + * @throws IOException + * in case of I/O error + * @throws NoSuchFieldException + * if the serialised data contains information about a field + * which does actually not exist in the class we know of + * @throws NoSuchMethodException + * if a class described in the serialised data cannot be created + * because it is not compatible with this code + * @throws ClassNotFoundException + * if a class described in the serialised data cannot be found + */ + public Object send(Object data) throws IOException, NoSuchFieldException, + NoSuchMethodException, ClassNotFoundException { + return action.send(data); + } + + /** + * Handler called when an unexpected error occurs in the code. + *

+ * Will just ignore the error by default. + * + * @param e + * the exception that occurred + */ + protected void onError(@SuppressWarnings("unused") Exception e) { + } + + // old stuff: + + /** + * Do not use. Will never be called. + */ + @SuppressWarnings({ "unused", "javadoc" }) + @Deprecated + protected void onClientVersionReceived(Version clientVersion) { + } + + /** + * Do not use, it is not supposed to be called from the outside. + */ + @SuppressWarnings({ "unused", "javadoc" }) + @Deprecated + public Object flush() throws NoSuchFieldException, NoSuchMethodException, + ClassNotFoundException, IOException, java.lang.NullPointerException { + return null; + } } \ No newline at end of file