2d85c79c86f41b2d7807bb3fcc3ad6b508a3cddc
1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import be
.nikiroo
.utils
.Version
;
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 public class ConnectActionServer
{
17 private ConnectAction action
;
20 * Create a new {@link ConnectActionServer} with the current application
21 * version (see {@link Version#getCurrentVersion()}) as the server version.
24 * the socket to bind to
26 public ConnectActionServer(Socket s
) {
27 this(s
, Version
.getCurrentVersion());
31 * Create a new {@link ConnectActionServer}.
34 * the socket to bind to
38 public ConnectActionServer(Socket s
, Version version
) {
39 action
= new ConnectAction(s
, true, version
) {
41 protected void action(Version clientVersion
) throws Exception
{
42 ConnectActionServer
.this.action(clientVersion
);
46 protected void onError(Exception e
) {
47 ConnectActionServer
.this.onError(e
);
51 protected Version
negotiateVersion(Version clientVersion
) {
52 return ConnectActionServer
.this.negotiateVersion(clientVersion
);
58 * Actually start the process and call the action (synchronous).
60 public void connect() {
65 * Actually start the process and call the action (asynchronous).
67 public void connectAsync() {
68 new Thread(new Runnable() {
77 * Method that will be called when an action is performed on the server.
79 * @param clientVersion
83 * in case of I/O error
85 @SuppressWarnings("unused")
86 public void action(Version clientVersion
) throws Exception
{
90 * Serialise and send the given object to the client.
96 * in case of I/O error
97 * @throws NoSuchFieldException
98 * if the serialised data contains information about a field
99 * which does actually not exist in the class we know of
100 * @throws NoSuchMethodException
101 * if a class described in the serialised data cannot be created
102 * because it is not compatible with this code
103 * @throws ClassNotFoundException
104 * if a class described in the serialised data cannot be found
106 public void send(Object data
) throws IOException
, NoSuchFieldException
,
107 NoSuchMethodException
, ClassNotFoundException
{
112 * (Flush the data to the client if needed and) retrieve its answer.
114 * @return the deserialised answer (which can actually be NULL)
116 * @throws IOException
117 * in case of I/O error
118 * @throws NoSuchFieldException
119 * if the serialised data contains information about a field
120 * which does actually not exist in the class we know of
121 * @throws NoSuchMethodException
122 * if a class described in the serialised data cannot be created
123 * because it is not compatible with this code
124 * @throws ClassNotFoundException
125 * if a class described in the serialised data cannot be found
126 * @throws java.lang.NullPointerException
127 * if the counter part has no data to send
129 public Object
rec() throws NoSuchFieldException
, NoSuchMethodException
,
130 ClassNotFoundException
, IOException
, java
.lang
.NullPointerException
{
135 * Handler called when an unexpected error occurs in the code.
137 * Will just ignore the error by default.
140 * the exception that occurred
142 protected void onError(@SuppressWarnings("unused") Exception e
) {
146 * Method called when we negotiate the version with the client.
148 * Will return the actual server version by default.
150 * @param clientVersion
153 * @return the version to send to the client
155 protected Version
negotiateVersion(
156 @SuppressWarnings("unused") Version clientVersion
) {
157 return action
.getVersion();
163 * Not used anymore. See {@link ConnectActionServer#rec()}.
165 @SuppressWarnings("javadoc")
167 public Object
flush() throws NoSuchFieldException
, NoSuchMethodException
,
168 ClassNotFoundException
, IOException
, java
.lang
.NullPointerException
{
173 * Not used anymore. See
174 * {@link ConnectActionServer#negotiateVersion(Version)}.
176 @SuppressWarnings({ "unused", "javadoc" })
178 protected void onClientVersionReceived(Version clientVersion
) {