Revert Server behaviour to what it was :
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectActionClient.java
1 package be.nikiroo.utils.serial.server;
2
3 import java.io.IOException;
4 import java.net.Socket;
5
6 import be.nikiroo.utils.Version;
7
8 /**
9 * Base class used for the client basic handling.
10 * <p>
11 * It represents a single action: a client is expected to only execute one
12 * action.
13 *
14 * @author niki
15 */
16 abstract class ConnectActionClient {
17 /**
18 * The underlying {@link ConnectAction}.
19 * <p>
20 * Cannot be NULL.
21 */
22 protected ConnectAction action;
23
24 /**
25 * Create a new {@link ConnectActionClient} with the current application
26 * version (see {@link Version#getCurrentVersion()}) as the client version.
27 *
28 * @param s
29 * the socket to bind to
30 */
31 public ConnectActionClient(Socket s) {
32 this(s, Version.getCurrentVersion());
33 }
34
35 /**
36 * Create a new {@link ConnectActionClient} with the current application
37 * version (see {@link Version#getCurrentVersion()}) as the client version.
38 *
39 * @param host
40 * the host to bind to
41 * @param port
42 * the port to bind to
43 * @param ssl
44 * TRUE for an SSL connection, FALSE for plain text
45 *
46 * @throws IOException
47 * in case of I/O error when creating the socket
48 */
49 public ConnectActionClient(String host, int port, boolean ssl)
50 throws IOException {
51 this(Server.createSocket(host, port, ssl), Version.getCurrentVersion());
52 }
53
54 /**
55 * Create a new {@link ConnectActionClient}.
56 *
57 * @param host
58 * the host to bind to
59 * @param port
60 * the port to bind to
61 * @param ssl
62 * TRUE for an SSL connection, FALSE for plain text
63 * @param version
64 * the client version
65 *
66 * @throws IOException
67 * in case of I/O error when creating the socket
68 */
69 public ConnectActionClient(String host, int port, boolean ssl,
70 Version version) throws IOException {
71 this(Server.createSocket(host, port, ssl), version);
72 }
73
74 /**
75 * Create a new {@link ConnectActionClient}.
76 *
77 * @param s
78 * the socket to bind to
79 * @param version
80 * the client version
81 */
82 public ConnectActionClient(Socket s, Version version) {
83 action = new ConnectAction(s, false, version) {
84 @Override
85 protected void action(Version serverVersion) throws Exception {
86 ConnectActionClient.this.action(serverVersion);
87 }
88
89 @Override
90 protected void onError(Exception e) {
91 ConnectActionClient.this.onError(e);
92 }
93
94 @Override
95 protected Version negotiateVersion(Version clientVersion) {
96 new Exception("Should never be called on a client")
97 .printStackTrace();
98 return null;
99 }
100 };
101 }
102
103 /**
104 * Actually start the process and call the action (synchronous).
105 */
106 public void connect() {
107 action.connect();
108 }
109
110 /**
111 * Actually start the process and call the action (asynchronous).
112 */
113 public void connectAsync() {
114 new Thread(new Runnable() {
115 @Override
116 public void run() {
117 connect();
118 }
119 }).start();
120 }
121
122 /**
123 * Method that will be called when an action is performed on the client.
124 *
125 * @param serverVersion
126 * the server version
127 *
128 * @throws Exception
129 * in case of I/O error
130 */
131 @SuppressWarnings("unused")
132 public void action(Version serverVersion) throws Exception {
133 }
134
135 /**
136 * Handler called when an unexpected error occurs in the code.
137 * <p>
138 * Will just ignore the error by default.
139 *
140 * @param e
141 * the exception that occurred
142 */
143 protected void onError(@SuppressWarnings("unused") Exception e) {
144 }
145 }