1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import javax
.net
.ssl
.SSLException
;
8 import be
.nikiroo
.utils
.Version
;
11 * Base class used for the server basic handling.
13 * It represents a single action: a server is expected to execute one action for
18 abstract class ConnectActionServer
{
19 private boolean closing
;
22 * The underlying {@link ConnectAction}.
26 protected ConnectAction action
;
29 * Create a new {@link ConnectActionServer}, using the current version.
32 * the socket to bind to
34 * an optional key to encrypt all the communications (if NULL,
35 * everything will be sent in clear text)
37 public ConnectActionServer(Socket s
, String key
) {
38 this(s
, key
, Version
.getCurrentVersion());
42 * Create a new {@link ConnectActionServer}.
45 * the socket to bind to
47 * an optional key to encrypt all the communications (if NULL,
48 * everything will be sent in clear text)
49 * @param serverVersion
50 * the version of this server,that will be sent to the client
52 public ConnectActionServer(Socket s
, String key
, Version serverVersion
) {
53 action
= new ConnectAction(s
, true, key
, serverVersion
) {
55 protected void action(Version clientVersion
) throws Exception
{
56 ConnectActionServer
.this.serverHello();
57 ConnectActionServer
.this.action(clientVersion
);
61 protected void onError(Exception e
) {
62 ConnectActionServer
.this.onError(e
);
66 protected Version
negotiateVersion(Version clientVersion
) {
67 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
73 * Send the HELLO message (check that the client sends a String "HELLO" and
74 * send it back, to check I/O and encryption modes).
77 * in case of I/O error
78 * @throws SSLException
79 * in case of encryption error
81 protected void serverHello() throws IOException
, SSLException
{
82 String HELLO
= action
.recString();
83 if (!"HELLO".equals(HELLO
)) {
84 throw new SSLException("Server did not accept the encryption key");
86 action
.sendString(HELLO
);
90 * Actually start the process and call the action (synchronous).
92 public void connect() {
97 * Actually start the process and call the action (asynchronous).
99 public void connectAsync() {
100 new Thread(new Runnable() {
109 * Stop the client/server connection on behalf of the server (usually, the
110 * client connects then is allowed to send as many requests as it wants; in
111 * some cases, though, the server may wish to forcefully close the
112 * connection and can do via this value, when it is set to TRUE).
114 * Example of usage: the client failed an authentication check, cut the
115 * connection here and now.
117 * @return TRUE when it is
119 public boolean isClosing() {
124 * Can be called to stop the client/server connection on behalf of the
125 * server (usually, the client connects then is allowed to send as many
126 * requests as it wants; in some cases, though, the server may wish to
127 * forcefully close the connection and can do so by calling this method).
129 * Example of usage: the client failed an authentication check, cut the
130 * connection here and now.
132 public void close() {
137 * The total amount of bytes received.
139 * @return the amount of bytes received
141 public long getBytesReceived() {
142 return action
.getBytesReceived();
146 * The total amount of bytes sent.
148 * @return the amount of bytes sent
150 public long getBytesSent() {
151 return action
.getBytesWritten();
155 * Method that will be called when an action is performed on the server.
157 * @param clientVersion
158 * the version of the client connected to this server
161 * in case of I/O error
163 @SuppressWarnings("unused")
164 public void action(Version clientVersion
) throws Exception
{
168 * Handler called when an unexpected error occurs in the code.
170 * Will just ignore the error by default.
173 * the exception that occurred
175 protected void onError(@SuppressWarnings("unused") Exception e
) {
179 * Method called when we negotiate the version with the client.
181 * Will return the actual server version by default.
183 * @param clientVersion
186 * @return the version to send to the client
188 protected Version
negotiateVersion(
189 @SuppressWarnings("unused") Version clientVersion
) {
190 return action
.getVersion();