1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import javax
.net
.ssl
.SSLException
;
9 * Base class used for the server basic handling.
11 * It represents a single action: a server is expected to execute one action for
16 abstract class ConnectActionServer
{
17 private boolean closing
;
20 * The underlying {@link ConnectAction}.
24 protected ConnectAction action
;
27 * Create a new {@link ConnectActionServer}.
30 * the socket to bind to
32 * an optional key to encrypt all the communications (if NULL,
33 * everything will be sent in clear text)
35 public ConnectActionServer(Socket s
, String key
) {
36 action
= new ConnectAction(s
, true, key
) {
38 protected void action() throws Exception
{
39 ConnectActionServer
.this.serverHello();
40 ConnectActionServer
.this.action();
44 protected void onError(Exception e
) {
45 ConnectActionServer
.this.onError(e
);
51 * Send the HELLO message (check that the client sends a String "HELLO" and
52 * send it back, to check I/O and encryption modes).
55 * in case of I/O error
56 * @throws SSLException
57 * in case of encryption error
59 protected void serverHello() throws IOException
, SSLException
{
60 String HELLO
= action
.recString();
61 if (!"HELLO".equals(HELLO
)) {
62 throw new SSLException("Server did not accept the encryption key");
64 action
.sendString(HELLO
);
68 * Actually start the process and call the action (synchronous).
70 public void connect() {
75 * Actually start the process and call the action (asynchronous).
77 public void connectAsync() {
78 new Thread(new Runnable() {
87 * Stop the client/server connection on behalf of the server (usually, the
88 * client connects then is allowed to send as many requests as it wants; in
89 * some cases, though, the server may wish to forcefully close the
90 * connection and can do via this value, when it is set to TRUE).
92 * Example of usage: the client failed an authentication check, cut the
93 * connection here and now.
95 * @return TRUE when it is
97 public boolean isClosing() {
102 * Can be called to stop the client/server connection on behalf of the
103 * server (usually, the client connects then is allowed to send as many
104 * requests as it wants; in some cases, though, the server may wish to
105 * forcefully close the connection and can do so by calling this method).
107 * Example of usage: the client failed an authentication check, cut the
108 * connection here and now.
110 public void close() {
115 * The total amount of bytes received.
117 * @return the amount of bytes received
119 public long getBytesReceived() {
120 return action
.getBytesReceived();
124 * The total amount of bytes sent.
126 * @return the amount of bytes sent
128 public long getBytesSent() {
129 return action
.getBytesWritten();
133 * Method that will be called when an action is performed on the server.
136 * in case of I/O error
138 @SuppressWarnings("unused")
139 public void action() throws Exception
{
143 * Handler called when an unexpected error occurs in the code.
145 * Will just ignore the error by default.
148 * the exception that occurred
150 protected void onError(@SuppressWarnings("unused") Exception e
) {