| 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 | * This class implements a simple server that can listen for connections and |
| 11 | * send/receive objects. |
| 12 | * <p> |
| 13 | * Note: this {@link ServerObject} has to be discarded after use (cannot be |
| 14 | * started twice). |
| 15 | * |
| 16 | * @author niki |
| 17 | */ |
| 18 | abstract public class ServerObject extends Server { |
| 19 | /** |
| 20 | * Create a new server that will start listening on the network when |
| 21 | * {@link ServerObject#start()} is called. |
| 22 | * |
| 23 | * @param port |
| 24 | * the port to listen on, or 0 to assign any unallocated port |
| 25 | * found (which can later on be queried via |
| 26 | * {@link ServerObject#getPort()} |
| 27 | * @param key |
| 28 | * an optional key to encrypt all the communications (if NULL, |
| 29 | * everything will be sent in clear text) |
| 30 | * |
| 31 | * @throws IOException |
| 32 | * in case of I/O error |
| 33 | * @throws UnknownHostException |
| 34 | * if the IP address of the host could not be determined |
| 35 | * @throws IllegalArgumentException |
| 36 | * if the port parameter is outside the specified range of valid |
| 37 | * port values, which is between 0 and 65535, inclusive |
| 38 | */ |
| 39 | public ServerObject(int port, String key) throws IOException { |
| 40 | super(port, key); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Create a new server that will start listening on the network when |
| 45 | * {@link ServerObject#start()} is called. |
| 46 | * |
| 47 | * @param name |
| 48 | * the server name (only used for debug info and traces) |
| 49 | * @param port |
| 50 | * the port to listen on |
| 51 | * @param key |
| 52 | * an optional key to encrypt all the communications (if NULL, |
| 53 | * everything will be sent in clear text) |
| 54 | * |
| 55 | * @throws IOException |
| 56 | * in case of I/O error |
| 57 | * @throws UnknownHostException |
| 58 | * if the IP address of the host could not be determined |
| 59 | * @throws IllegalArgumentException |
| 60 | * if the port parameter is outside the specified range of valid |
| 61 | * port values, which is between 0 and 65535, inclusive |
| 62 | */ |
| 63 | public ServerObject(String name, int port, String key) throws IOException { |
| 64 | super(name, port, key); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected ConnectActionServer createConnectActionServer(Socket s) { |
| 69 | return new ConnectActionServerObject(s, key) { |
| 70 | @Override |
| 71 | public void action(Version clientVersion) throws Exception { |
| 72 | try { |
| 73 | for (Object data = rec(); true; data = rec()) { |
| 74 | Object rep = null; |
| 75 | try { |
| 76 | rep = onRequest(this, clientVersion, data); |
| 77 | if (isClosing()) { |
| 78 | return; |
| 79 | } |
| 80 | } catch (Exception e) { |
| 81 | onError(e); |
| 82 | } |
| 83 | send(rep); |
| 84 | } |
| 85 | } catch (NullPointerException e) { |
| 86 | // Client has no data any more, we quit |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void onError(Exception e) { |
| 92 | ServerObject.this.onError(e); |
| 93 | } |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * This is the method that is called on each client request. |
| 99 | * <p> |
| 100 | * You are expected to react to it and return an answer (which can be NULL). |
| 101 | * |
| 102 | * @param action |
| 103 | * the client action |
| 104 | * @param clientVersion |
| 105 | * the client version |
| 106 | * @param data |
| 107 | * the data sent by the client (which can be NULL) |
| 108 | * |
| 109 | * @return the answer to return to the client (which can be NULL) |
| 110 | * |
| 111 | * @throws Exception |
| 112 | * in case of an exception, the error will only be logged |
| 113 | */ |
| 114 | abstract protected Object onRequest(ConnectActionServerObject action, |
| 115 | Version clientVersion, Object data) throws Exception; |
| 116 | } |