| 1 | package be.nikiroo.utils.serial; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.Socket; |
| 5 | |
| 6 | import be.nikiroo.utils.Version; |
| 7 | |
| 8 | /** |
| 9 | * Base class used for the client basic handling. |
| 10 | * <p> |
| 11 | * It represents a single action: a client is expected to only execute one |
| 12 | * action. |
| 13 | * |
| 14 | * @author niki |
| 15 | */ |
| 16 | public class ConnectActionClient { |
| 17 | private ConnectAction action; |
| 18 | |
| 19 | /** |
| 20 | * Create a new {@link ConnectActionClient} with the current application |
| 21 | * version (see {@link Version#getCurrentVersion()}) as the client version. |
| 22 | * |
| 23 | * @param s |
| 24 | * the socket to bind to |
| 25 | */ |
| 26 | public ConnectActionClient(Socket s) { |
| 27 | this(s, Version.getCurrentVersion()); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Create a new {@link ConnectActionClient} with the current application |
| 32 | * version (see {@link Version#getCurrentVersion()}) as the client version. |
| 33 | * |
| 34 | * @param host |
| 35 | * the host to bind to |
| 36 | * @param port |
| 37 | * the port to bind to |
| 38 | * @param ssl |
| 39 | * TRUE for an SSL connection, FALSE for plain text |
| 40 | * |
| 41 | * @throws IOException |
| 42 | * in case of I/O error when creating the socket |
| 43 | */ |
| 44 | public ConnectActionClient(String host, int port, boolean ssl) |
| 45 | throws IOException { |
| 46 | this(Server.createSocket(host, port, ssl), Version.getCurrentVersion()); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Create a new {@link ConnectActionClient}. |
| 51 | * |
| 52 | * @param host |
| 53 | * the host to bind to |
| 54 | * @param port |
| 55 | * the port to bind to |
| 56 | * @param ssl |
| 57 | * TRUE for an SSL connection, FALSE for plain text |
| 58 | * @param version |
| 59 | * the client version |
| 60 | * |
| 61 | * @throws IOException |
| 62 | * in case of I/O error when creating the socket |
| 63 | */ |
| 64 | public ConnectActionClient(String host, int port, boolean ssl, |
| 65 | Version version) throws IOException { |
| 66 | this(Server.createSocket(host, port, ssl), version); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Create a new {@link ConnectActionClient}. |
| 71 | * |
| 72 | * @param s |
| 73 | * the socket to bind to |
| 74 | * @param version |
| 75 | * the client version |
| 76 | */ |
| 77 | public ConnectActionClient(Socket s, Version version) { |
| 78 | action = new ConnectAction(s, false, version) { |
| 79 | @Override |
| 80 | protected void action(Version serverVersion) throws Exception { |
| 81 | ConnectActionClient.this.action(serverVersion); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onError(Exception e) { |
| 86 | ConnectActionClient.this.onError(e); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected Version negotiateVersion(Version clientVersion) { |
| 91 | new Exception("Should never be called on a client") |
| 92 | .printStackTrace(); |
| 93 | return null; |
| 94 | } |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Actually start the process and call the action (synchronous). |
| 100 | */ |
| 101 | public void connect() { |
| 102 | action.connect(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Actually start the process and call the action (asynchronous). |
| 107 | */ |
| 108 | public void connectAsync() { |
| 109 | new Thread(new Runnable() { |
| 110 | @Override |
| 111 | public void run() { |
| 112 | connect(); |
| 113 | } |
| 114 | }).start(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Method that will be called when an action is performed on the client. |
| 119 | * |
| 120 | * @param serverVersion |
| 121 | * the server version |
| 122 | * |
| 123 | * @throws Exception |
| 124 | * in case of I/O error |
| 125 | */ |
| 126 | @SuppressWarnings("unused") |
| 127 | public void action(Version serverVersion) throws Exception { |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Serialise and send the given object to the server (and return the |
| 132 | * deserialised answer). |
| 133 | * |
| 134 | * @param data |
| 135 | * the data to send |
| 136 | * |
| 137 | * @return the answer, which can be NULL |
| 138 | * |
| 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 |
| 149 | */ |
| 150 | public Object send(Object data) throws IOException, NoSuchFieldException, |
| 151 | NoSuchMethodException, ClassNotFoundException { |
| 152 | return action.send(data); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Handler called when an unexpected error occurs in the code. |
| 157 | * <p> |
| 158 | * Will just ignore the error by default. |
| 159 | * |
| 160 | * @param e |
| 161 | * the exception that occurred |
| 162 | */ |
| 163 | protected void onError(@SuppressWarnings("unused") Exception e) { |
| 164 | } |
| 165 | |
| 166 | // old stuff: |
| 167 | |
| 168 | /** |
| 169 | * Do not use. Will never be called. |
| 170 | */ |
| 171 | @SuppressWarnings({ "unused", "javadoc" }) |
| 172 | @Deprecated |
| 173 | protected void onClientVersionReceived(Version clientVersion) { |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Do not use, it is not supposed to be called from the outside. |
| 178 | */ |
| 179 | @SuppressWarnings({ "unused", "javadoc" }) |
| 180 | @Deprecated |
| 181 | public Object flush() throws NoSuchFieldException, NoSuchMethodException, |
| 182 | ClassNotFoundException, IOException, java.lang.NullPointerException { |
| 183 | return null; |
| 184 | } |
| 185 | } |