| 1 | package be.nikiroo.utils.serial.server; |
| 2 | |
| 3 | import java.net.Socket; |
| 4 | |
| 5 | /** |
| 6 | * Base class used for the server basic handling. |
| 7 | * <p> |
| 8 | * It represents a single action: a server is expected to execute one action for |
| 9 | * each client action. |
| 10 | * |
| 11 | * @author niki |
| 12 | */ |
| 13 | abstract class ConnectActionServer { |
| 14 | private boolean closing; |
| 15 | |
| 16 | /** |
| 17 | * The underlying {@link ConnectAction}. |
| 18 | * <p> |
| 19 | * Cannot be NULL. |
| 20 | */ |
| 21 | protected ConnectAction action; |
| 22 | |
| 23 | /** |
| 24 | * Create a new {@link ConnectActionServer}. |
| 25 | * |
| 26 | * @param s |
| 27 | * the socket to bind to |
| 28 | * @param key |
| 29 | * an optional key to encrypt all the communications (if NULL, |
| 30 | * everything will be sent in clear text) |
| 31 | */ |
| 32 | public ConnectActionServer(Socket s, String key) { |
| 33 | action = new ConnectAction(s, true, key) { |
| 34 | @Override |
| 35 | protected void action() throws Exception { |
| 36 | ConnectActionServer.this.action(); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected void onError(Exception e) { |
| 41 | ConnectActionServer.this.onError(e); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Actually start the process and call the action (synchronous). |
| 48 | */ |
| 49 | public void connect() { |
| 50 | action.connect(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Actually start the process and call the action (asynchronous). |
| 55 | */ |
| 56 | public void connectAsync() { |
| 57 | new Thread(new Runnable() { |
| 58 | @Override |
| 59 | public void run() { |
| 60 | connect(); |
| 61 | } |
| 62 | }).start(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Stop the client/server connection on behalf of the server (usually, the |
| 67 | * client connects then is allowed to send as many requests as it wants; in |
| 68 | * some cases, though, the server may wish to forcefully close the |
| 69 | * connection and can do via this value, when it is set to TRUE). |
| 70 | * <p> |
| 71 | * Example of usage: the client failed an authentication check, cut the |
| 72 | * connection here and now. |
| 73 | * |
| 74 | * @return TRUE when it is |
| 75 | */ |
| 76 | public boolean isClosing() { |
| 77 | return closing; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Can be called to stop the client/server connection on behalf of the |
| 82 | * server (usually, the client connects then is allowed to send as many |
| 83 | * requests as it wants; in some cases, though, the server may wish to |
| 84 | * forcefully close the connection and can do so by calling this method). |
| 85 | * <p> |
| 86 | * Example of usage: the client failed an authentication check, cut the |
| 87 | * connection here and now. |
| 88 | */ |
| 89 | public void close() { |
| 90 | closing = true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * The total amount of bytes received. |
| 95 | * |
| 96 | * @return the amount of bytes received |
| 97 | */ |
| 98 | public long getBytesReceived() { |
| 99 | return action.getBytesReceived(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * The total amount of bytes sent. |
| 104 | * |
| 105 | * @return the amount of bytes sent |
| 106 | */ |
| 107 | public long getBytesSent() { |
| 108 | return action.getBytesWritten(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Method that will be called when an action is performed on the server. |
| 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 | } |