1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.net
.Socket
;
6 * Base class used for the server basic handling.
8 * It represents a single action: a server is expected to execute one action for
13 abstract class ConnectActionServer
{
14 private boolean closing
;
17 * The underlying {@link ConnectAction}.
21 protected ConnectAction action
;
24 * Create a new {@link ConnectActionServer}.
27 * the socket to bind to
29 * an optional key to encrypt all the communications (if NULL,
30 * everything will be sent in clear text)
32 public ConnectActionServer(Socket s
, String key
) {
33 action
= new ConnectAction(s
, true, key
) {
35 protected void action() throws Exception
{
36 ConnectActionServer
.this.action();
40 protected void onError(Exception e
) {
41 ConnectActionServer
.this.onError(e
);
47 * Actually start the process and call the action (synchronous).
49 public void connect() {
54 * Actually start the process and call the action (asynchronous).
56 public void connectAsync() {
57 new Thread(new Runnable() {
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).
71 * Example of usage: the client failed an authentication check, cut the
72 * connection here and now.
74 * @return TRUE when it is
76 public boolean isClosing() {
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).
86 * Example of usage: the client failed an authentication check, cut the
87 * connection here and now.
94 * The total amount of bytes received.
96 * @return the amount of bytes received
98 public long getBytesReceived() {
99 return action
.getBytesReceived();
103 * The total amount of bytes sent.
105 * @return the amount of bytes sent
107 public long getBytesSent() {
108 return action
.getBytesWritten();
112 * Method that will be called when an action is performed on the server.
115 * in case of I/O error
117 @SuppressWarnings("unused")
118 public void action() throws Exception
{
122 * Handler called when an unexpected error occurs in the code.
124 * Will just ignore the error by default.
127 * the exception that occurred
129 protected void onError(@SuppressWarnings("unused") Exception e
) {