| 1 | package be.nikiroo.utils.serial.server; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.Socket; |
| 5 | import java.net.UnknownHostException; |
| 6 | |
| 7 | import be.nikiroo.utils.Version; |
| 8 | |
| 9 | /** |
| 10 | * Base class used for the client basic handling. |
| 11 | * <p> |
| 12 | * It represents a single action: a client is expected to only execute one |
| 13 | * action. |
| 14 | * |
| 15 | * @author niki |
| 16 | */ |
| 17 | abstract class ConnectActionClient { |
| 18 | /** |
| 19 | * The underlying {@link ConnectAction}. |
| 20 | * <p> |
| 21 | * Cannot be NULL. |
| 22 | */ |
| 23 | protected ConnectAction action; |
| 24 | |
| 25 | /** |
| 26 | * Create a new {@link ConnectActionClient} with the current application |
| 27 | * version (see {@link Version#getCurrentVersion()}) as the client version. |
| 28 | * |
| 29 | * @param s |
| 30 | * the socket to bind to |
| 31 | */ |
| 32 | public ConnectActionClient(Socket s) { |
| 33 | this(s, Version.getCurrentVersion()); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Create a new {@link ConnectActionClient} with the current application |
| 38 | * version (see {@link Version#getCurrentVersion()}) as the client version. |
| 39 | * |
| 40 | * @param host |
| 41 | * the host to bind to |
| 42 | * @param port |
| 43 | * the port to bind to |
| 44 | * @param ssl |
| 45 | * TRUE for an SSL connection, FALSE for plain text |
| 46 | * |
| 47 | * @throws IOException |
| 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 |
| 54 | */ |
| 55 | public ConnectActionClient(String host, int port, boolean ssl) |
| 56 | throws IOException { |
| 57 | this(Server.createSocket(host, port, ssl), Version.getCurrentVersion()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create a new {@link ConnectActionClient}. |
| 62 | * |
| 63 | * @param host |
| 64 | * the host to bind to |
| 65 | * @param port |
| 66 | * the port to bind to |
| 67 | * @param ssl |
| 68 | * TRUE for an SSL connection, FALSE for plain text |
| 69 | * @param version |
| 70 | * the client version |
| 71 | * |
| 72 | * @throws IOException |
| 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 |
| 79 | */ |
| 80 | public ConnectActionClient(String host, int port, boolean ssl, |
| 81 | Version version) throws IOException { |
| 82 | this(Server.createSocket(host, port, ssl), version); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Create a new {@link ConnectActionClient}. |
| 87 | * |
| 88 | * @param s |
| 89 | * the socket to bind to |
| 90 | * @param version |
| 91 | * the client version |
| 92 | */ |
| 93 | public ConnectActionClient(Socket s, Version version) { |
| 94 | action = new ConnectAction(s, false, version) { |
| 95 | @Override |
| 96 | protected void action(Version serverVersion) throws Exception { |
| 97 | ConnectActionClient.this.action(serverVersion); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void onError(Exception e) { |
| 102 | ConnectActionClient.this.onError(e); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected Version negotiateVersion(Version clientVersion) { |
| 107 | new Exception("Should never be called on a client") |
| 108 | .printStackTrace(); |
| 109 | return null; |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Actually start the process and call the action (synchronous). |
| 116 | */ |
| 117 | public void connect() { |
| 118 | action.connect(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Actually start the process and call the action (asynchronous). |
| 123 | */ |
| 124 | public void connectAsync() { |
| 125 | new Thread(new Runnable() { |
| 126 | @Override |
| 127 | public void run() { |
| 128 | connect(); |
| 129 | } |
| 130 | }).start(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Method that will be called when an action is performed on the client. |
| 135 | * |
| 136 | * @param serverVersion |
| 137 | * the server version |
| 138 | * |
| 139 | * @throws Exception |
| 140 | * in case of I/O error |
| 141 | */ |
| 142 | @SuppressWarnings("unused") |
| 143 | public void action(Version serverVersion) throws Exception { |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Handler called when an unexpected error occurs in the code. |
| 148 | * <p> |
| 149 | * Will just ignore the error by default. |
| 150 | * |
| 151 | * @param e |
| 152 | * the exception that occurred |
| 153 | */ |
| 154 | protected void onError(@SuppressWarnings("unused") Exception e) { |
| 155 | } |
| 156 | } |