| 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 server basic handling. |
| 10 | * <p> |
| 11 | * It represents a single action: a server is expected to execute one action for |
| 12 | * each client action. |
| 13 | * |
| 14 | * @author niki |
| 15 | */ |
| 16 | public class ConnectActionServer { |
| 17 | private ConnectAction action; |
| 18 | |
| 19 | /** |
| 20 | * Create a new {@link ConnectActionServer} with the current application |
| 21 | * version (see {@link Version#getCurrentVersion()}) as the server version. |
| 22 | * |
| 23 | * @param s |
| 24 | * the socket to bind to |
| 25 | */ |
| 26 | public ConnectActionServer(Socket s) { |
| 27 | this(s, Version.getCurrentVersion()); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Create a new {@link ConnectActionServer}. |
| 32 | * |
| 33 | * @param s |
| 34 | * the socket to bind to |
| 35 | * @param version |
| 36 | * the server version |
| 37 | */ |
| 38 | public ConnectActionServer(Socket s, Version version) { |
| 39 | action = new ConnectAction(s, true, version) { |
| 40 | @Override |
| 41 | protected void action(Version clientVersion) throws Exception { |
| 42 | ConnectActionServer.this.action(clientVersion); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected void onError(Exception e) { |
| 47 | ConnectActionServer.this.onError(e); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected Version negotiateVersion(Version clientVersion) { |
| 52 | return ConnectActionServer.this.negotiateVersion(clientVersion); |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Actually start the process and call the action (synchronous). |
| 59 | */ |
| 60 | public void connect() { |
| 61 | action.connect(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Actually start the process and call the action (asynchronous). |
| 66 | */ |
| 67 | public void connectAsync() { |
| 68 | new Thread(new Runnable() { |
| 69 | @Override |
| 70 | public void run() { |
| 71 | connect(); |
| 72 | } |
| 73 | }).start(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Method that will be called when an action is performed on the server. |
| 78 | * |
| 79 | * @param clientVersion |
| 80 | * the client version |
| 81 | * |
| 82 | * @throws Exception |
| 83 | * in case of I/O error |
| 84 | */ |
| 85 | @SuppressWarnings("unused") |
| 86 | public void action(Version clientVersion) throws Exception { |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Serialise and send the given object to the client. |
| 91 | * |
| 92 | * @param data |
| 93 | * the data to send |
| 94 | * |
| 95 | * @throws IOException |
| 96 | * in case of I/O error |
| 97 | * @throws NoSuchFieldException |
| 98 | * if the serialised data contains information about a field |
| 99 | * which does actually not exist in the class we know of |
| 100 | * @throws NoSuchMethodException |
| 101 | * if a class described in the serialised data cannot be created |
| 102 | * because it is not compatible with this code |
| 103 | * @throws ClassNotFoundException |
| 104 | * if a class described in the serialised data cannot be found |
| 105 | */ |
| 106 | public void send(Object data) throws IOException, NoSuchFieldException, |
| 107 | NoSuchMethodException, ClassNotFoundException { |
| 108 | action.send(data); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * (Flush the data to the client if needed and) retrieve its answer. |
| 113 | * |
| 114 | * @return the deserialised answer (which can actually be NULL) |
| 115 | * |
| 116 | * @throws IOException |
| 117 | * in case of I/O error |
| 118 | * @throws NoSuchFieldException |
| 119 | * if the serialised data contains information about a field |
| 120 | * which does actually not exist in the class we know of |
| 121 | * @throws NoSuchMethodException |
| 122 | * if a class described in the serialised data cannot be created |
| 123 | * because it is not compatible with this code |
| 124 | * @throws ClassNotFoundException |
| 125 | * if a class described in the serialised data cannot be found |
| 126 | * @throws java.lang.NullPointerException |
| 127 | * if the counter part has no data to send |
| 128 | */ |
| 129 | public Object rec() throws NoSuchFieldException, NoSuchMethodException, |
| 130 | ClassNotFoundException, IOException, java.lang.NullPointerException { |
| 131 | return action.rec(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Handler called when an unexpected error occurs in the code. |
| 136 | * <p> |
| 137 | * Will just ignore the error by default. |
| 138 | * |
| 139 | * @param e |
| 140 | * the exception that occurred |
| 141 | */ |
| 142 | protected void onError(@SuppressWarnings("unused") Exception e) { |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Method called when we negotiate the version with the client. |
| 147 | * <p> |
| 148 | * Will return the actual server version by default. |
| 149 | * |
| 150 | * @param clientVersion |
| 151 | * the client version |
| 152 | * |
| 153 | * @return the version to send to the client |
| 154 | */ |
| 155 | protected Version negotiateVersion( |
| 156 | @SuppressWarnings("unused") Version clientVersion) { |
| 157 | return action.getVersion(); |
| 158 | } |
| 159 | |
| 160 | // old stuff: |
| 161 | |
| 162 | /** |
| 163 | * Not used anymore. See {@link ConnectActionServer#rec()}. |
| 164 | */ |
| 165 | @SuppressWarnings("javadoc") |
| 166 | @Deprecated |
| 167 | public Object flush() throws NoSuchFieldException, NoSuchMethodException, |
| 168 | ClassNotFoundException, IOException, java.lang.NullPointerException { |
| 169 | return rec(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Not used anymore. See |
| 174 | * {@link ConnectActionServer#negotiateVersion(Version)}. |
| 175 | */ |
| 176 | @SuppressWarnings({ "unused", "javadoc" }) |
| 177 | @Deprecated |
| 178 | protected void onClientVersionReceived(Version clientVersion) { |
| 179 | } |
| 180 | } |