|
24 | 24 | /** |
25 | 25 | * A NetworkModule provides access to a specific transport to the broker. This may be a plain socket, a TLS secured |
26 | 26 | * connection, a serial line, etc. |
| 27 | + * |
| 28 | + * <h3>Lifecycle</h3> |
| 29 | + * Each NetworkModule instance has a lifecycle with the following logical states: |
| 30 | + * <ul> |
| 31 | + * <li><b>CREATED</b> - the instance holds no connection to its broker, no network resources are allocated and the |
| 32 | + * {@code get...Stream}-methods may return {@code null} or throw an {@link IOException}. |
| 33 | + * <li><b>CONNECTED</b> - the instance has an active connection to its broker, underlaying network ressources (e.g. |
| 34 | + * socket) |
| 35 | + * are allocated, the {@code get...Stream}-methods return open streams for reading and writing.</li> |
| 36 | + * <li><b>DISCONNECTED</b> - the instance holds no connection to its broker, underlaying network resources are closed |
| 37 | + * and |
| 38 | + * released, the {@code get...Stream}-methods will always throw an {@link IOException}.</li> |
| 39 | + * </ul> |
27 | 40 | * <p> |
28 | | - * TODO: Provide description about the reusability of the NetworkModule. |
| 41 | + * The following transitions may occure: |
| 42 | + * <ul> |
| 43 | + * <li>{@code CREATED -> CONNECTED} - if {@link #start()} succeeded</li> |
| 44 | + * <li>{@code CREATED -> CREATED} - if {@link #start()} failed</li> |
| 45 | + * <li>{@code CONNECTED -> DISCONNECTED} - when {@link #stop()} is called; If an {@link IOException} occures on one of |
| 46 | + * the input / |
| 47 | + * output streams the {@code stop()}-method will be called.</li> |
| 48 | + * <li>{@code DISCONNECTED -> CONNECTED} - if {@link #start()} succeeded during a re-connect attempt</li> |
| 49 | + * </ul> |
| 50 | + * The {@link #start()}-method of a NetworkModule instance may only be called multiple times if either the previous call |
| 51 | + * resulted in an exception or {@link #stop()} has been called in between. The objects returned by |
| 52 | + * {@link #getInputStream()} und {@link #getOutputStream()} are most likely different instances each time |
| 53 | + * {@link #start()} had been called. |
29 | 54 | */ |
30 | 55 | public interface NetworkModule { |
| 56 | + |
31 | 57 | /** |
32 | | - * Open the transport to the broker. |
| 58 | + * Open the transport to the broker. The streams provided by {@link #getInputStream()} and |
| 59 | + * {@link #getOutputStream()} are expected to be open for read / write operations after this method succeed. |
33 | 60 | * |
34 | | - * @throws IOException |
35 | | - * @throws MqttException |
| 61 | + * @throws IOException of any underlaying transport |
| 62 | + * @throws MqttException if the server connection cannot be established, e.g. the connection is being refused |
36 | 63 | */ |
37 | 64 | public void start() throws IOException, MqttException; |
38 | 65 |
|
| 66 | + /** |
| 67 | + * Returns the input stream to be used for receiving messages. This method is usually called once directly after |
| 68 | + * {@link #start()}. The returned input stream will be used in the CommsReceiver of the client connection. |
| 69 | + * |
| 70 | + * @return the input stream to be used for receiving messages ({@code null} may be returned if {@link #start()} had |
| 71 | + * never been sucessfully called on this instance) |
| 72 | + * @throws IOException if an I/O error occurs when creating the input stream or the broker connection is not |
| 73 | + * established |
| 74 | + */ |
39 | 75 | public InputStream getInputStream() throws IOException; |
40 | 76 |
|
| 77 | + /** |
| 78 | + * Returns the output stream to be used for sending messages. This method is usually called once directly after |
| 79 | + * {@link #start()}. The returned output stream will be used in the CommsSender of the client connection. |
| 80 | + * |
| 81 | + * @return output stream to be used for sending messages ({@code null} may be returned if {@link #start()} had never |
| 82 | + * been sucessfully called on this instance) |
| 83 | + * @throws IOException if an I/O error occurs when creating the output stream or the broker connection is not |
| 84 | + * established |
| 85 | + */ |
41 | 86 | public OutputStream getOutputStream() throws IOException; |
42 | 87 |
|
43 | 88 | /** |
44 | | - * Close the transport to the broker. |
| 89 | + * Close the transport to the broker. The streams provided by {@link #getInputStream()} and |
| 90 | + * {@link #getOutputStream()} should be closed after this method returns. |
45 | 91 | * <p> |
46 | 92 | * TODO: Check whether the input and output streams are to be closed as well? |
47 | 93 | * |
48 | | - * @throws IOException |
| 94 | + * @throws IOException if an I/O error occurs when closing the transport |
49 | 95 | */ |
50 | 96 | public void stop() throws IOException; |
51 | 97 |
|
|
0 commit comments