NextableInputStream: separate base behaviour from nextable
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionServer.java
CommitLineData
79ce1a49 1package be.nikiroo.utils.serial.server;
ce0974c4
NR
2
3import java.net.Socket;
4
5import be.nikiroo.utils.Version;
6
f157aed8
NR
7/**
8 * Base class used for the server basic handling.
9 * <p>
10 * It represents a single action: a server is expected to execute one action for
11 * each client action.
12 *
13 * @author niki
14 */
79ce1a49 15abstract class ConnectActionServer {
a72ea8a7
NR
16 private boolean closing;
17
79ce1a49
NR
18 /**
19 * The underlying {@link ConnectAction}.
20 * <p>
21 * Cannot be NULL.
22 */
23 protected ConnectAction action;
f157aed8
NR
24
25 /**
26 * Create a new {@link ConnectActionServer} with the current application
27 * version (see {@link Version#getCurrentVersion()}) as the server version.
28 *
29 * @param s
30 * the socket to bind to
8468bb79
NR
31 * @param key
32 * an optional key to encrypt all the communications (if NULL,
33 * everything will be sent in clear text)
f157aed8 34 */
8468bb79
NR
35 public ConnectActionServer(Socket s, String key) {
36 this(s, key, Version.getCurrentVersion());
f157aed8
NR
37 }
38
39 /**
40 * Create a new {@link ConnectActionServer}.
41 *
42 * @param s
43 * the socket to bind to
8468bb79
NR
44 * @param key
45 * an optional key to encrypt all the communications (if NULL,
46 * everything will be sent in clear text)
f157aed8
NR
47 * @param version
48 * the server version
49 */
8468bb79
NR
50 public ConnectActionServer(Socket s, String key, Version version) {
51 action = new ConnectAction(s, true, key, version) {
f157aed8
NR
52 @Override
53 protected void action(Version clientVersion) throws Exception {
54 ConnectActionServer.this.action(clientVersion);
55 }
56
57 @Override
58 protected void onError(Exception e) {
59 ConnectActionServer.this.onError(e);
60 }
61
62 @Override
63 protected Version negotiateVersion(Version clientVersion) {
64 return ConnectActionServer.this.negotiateVersion(clientVersion);
65 }
66 };
67 }
68
69 /**
70 * Actually start the process and call the action (synchronous).
71 */
72 public void connect() {
73 action.connect();
74 }
75
76 /**
77 * Actually start the process and call the action (asynchronous).
78 */
79 public void connectAsync() {
80 new Thread(new Runnable() {
81 @Override
82 public void run() {
83 connect();
84 }
85 }).start();
86 }
87
a72ea8a7
NR
88 /**
89 * Stop the client/server connection on behalf of the server (usually, the
90 * client connects then is allowed to send as many requests as it wants; in
91 * some cases, though, the server may wish to forcefully close the
92 * connection and can do via this value, when it is set to TRUE).
93 * <p>
94 * Example of usage: the client failed an authentication check, cut the
95 * connection here and now.
8468bb79
NR
96 *
97 * @return TRUE when it is
a72ea8a7
NR
98 */
99 public boolean isClosing() {
100 return closing;
101 }
102
103 /**
104 * Can be called to stop the client/server connection on behalf of the
105 * server (usually, the client connects then is allowed to send as many
106 * requests as it wants; in some cases, though, the server may wish to
107 * forcefully close the connection and can do so by calling this method).
108 * <p>
109 * Example of usage: the client failed an authentication check, cut the
110 * connection here and now.
111 */
112 public void close() {
113 closing = true;
114 }
115
4bb7e88e
NR
116 /**
117 * The total amount of bytes received.
118 *
119 * @return the amount of bytes received
120 */
121 public long getBytesReceived() {
122 return action.getBytesReceived();
123 }
124
125 /**
126 * The total amount of bytes sent.
127 *
128 * @return the amount of bytes sent
129 */
130 public long getBytesSent() {
131 return action.getBytesSent();
132 }
133
f157aed8
NR
134 /**
135 * Method that will be called when an action is performed on the server.
136 *
137 * @param clientVersion
138 * the client version
139 *
140 * @throws Exception
141 * in case of I/O error
142 */
143 @SuppressWarnings("unused")
144 public void action(Version clientVersion) throws Exception {
145 }
146
f157aed8
NR
147 /**
148 * Handler called when an unexpected error occurs in the code.
149 * <p>
150 * Will just ignore the error by default.
151 *
152 * @param e
153 * the exception that occurred
154 */
155 protected void onError(@SuppressWarnings("unused") Exception e) {
156 }
157
158 /**
159 * Method called when we negotiate the version with the client.
160 * <p>
161 * Will return the actual server version by default.
162 *
163 * @param clientVersion
164 * the client version
165 *
166 * @return the version to send to the client
167 */
168 protected Version negotiateVersion(
169 @SuppressWarnings("unused") Version clientVersion) {
170 return action.getVersion();
171 }
ce0974c4 172}