| 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 javax.net.ssl.SSLException; |
| 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}. |
| 27 | * |
| 28 | * @param host |
| 29 | * the host to bind to |
| 30 | * @param port |
| 31 | * the port to bind to |
| 32 | * @param key |
| 33 | * an optional key to encrypt all the communications (if NULL, |
| 34 | * everything will be sent in clear text) |
| 35 | * |
| 36 | * @throws IOException |
| 37 | * in case of I/O error |
| 38 | * @throws UnknownHostException |
| 39 | * if the host is not known |
| 40 | * @throws IllegalArgumentException |
| 41 | * if the port parameter is outside the specified range of valid |
| 42 | * port values, which is between 0 and 65535, inclusive |
| 43 | */ |
| 44 | public ConnectActionClient(String host, int port, String key) |
| 45 | throws IOException { |
| 46 | this(new Socket(host, port), key); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Create a new {@link ConnectActionClient}. |
| 51 | * |
| 52 | * @param s |
| 53 | * the socket to bind to |
| 54 | * @param key |
| 55 | * an optional key to encrypt all the communications (if NULL, |
| 56 | * everything will be sent in clear text) |
| 57 | */ |
| 58 | public ConnectActionClient(Socket s, String key) { |
| 59 | action = new ConnectAction(s, false, key) { |
| 60 | @Override |
| 61 | protected void action() throws Exception { |
| 62 | ConnectActionClient.this.clientHello(); |
| 63 | ConnectActionClient.this.action(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onError(Exception e) { |
| 68 | ConnectActionClient.this.onError(e); |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Send the HELLO message (send a String "HELLO" to the server, to check I/O |
| 75 | * and encryption modes). |
| 76 | * <p> |
| 77 | * Will automatically handle the answer (the server must answer "HELLO" in |
| 78 | * kind). |
| 79 | * |
| 80 | * @throws IOException |
| 81 | * in case of I/O error |
| 82 | * @throws SSLException |
| 83 | * in case of encryption error |
| 84 | */ |
| 85 | protected void clientHello() throws IOException { |
| 86 | String HELLO = action.sendString("HELLO"); |
| 87 | if (!"HELLO".equals(HELLO)) { |
| 88 | throw new SSLException("Server did not accept the encryption key"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Actually start the process and call the action (synchronous). |
| 94 | */ |
| 95 | public void connect() { |
| 96 | action.connect(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Actually start the process and call the action (asynchronous). |
| 101 | */ |
| 102 | public void connectAsync() { |
| 103 | new Thread(new Runnable() { |
| 104 | @Override |
| 105 | public void run() { |
| 106 | connect(); |
| 107 | } |
| 108 | }).start(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Method that will be called when an action is performed on the client. |
| 113 | * |
| 114 | * @throws Exception |
| 115 | * in case of I/O error |
| 116 | */ |
| 117 | @SuppressWarnings("unused") |
| 118 | public void action() throws Exception { |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Handler called when an unexpected error occurs in the code. |
| 123 | * <p> |
| 124 | * Will just ignore the error by default. |
| 125 | * |
| 126 | * @param e |
| 127 | * the exception that occurred |
| 128 | */ |
| 129 | protected void onError(@SuppressWarnings("unused") Exception e) { |
| 130 | } |
| 131 | } |