...
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectActionClient.java
CommitLineData
79ce1a49 1package be.nikiroo.utils.serial.server;
ce0974c4
NR
2
3import java.io.IOException;
4import java.net.Socket;
f4053377 5import java.net.UnknownHostException;
ce0974c4 6
f157aed8
NR
7/**
8 * Base class used for the client basic handling.
9 * <p>
10 * It represents a single action: a client is expected to only execute one
11 * action.
12 *
13 * @author niki
14 */
79ce1a49
NR
15abstract class ConnectActionClient {
16 /**
17 * The underlying {@link ConnectAction}.
18 * <p>
19 * Cannot be NULL.
20 */
21 protected ConnectAction action;
ce0974c4 22
f157aed8 23 /**
23cf894d 24 * Create a new {@link ConnectActionClient}.
f157aed8
NR
25 *
26 * @param host
27 * the host to bind to
28 * @param port
29 * the port to bind to
8468bb79
NR
30 * @param key
31 * an optional key to encrypt all the communications (if NULL,
32 * everything will be sent in clear text)
f157aed8
NR
33 *
34 * @throws IOException
f4053377
NR
35 * in case of I/O error
36 * @throws UnknownHostException
37 * if the host is not known
38 * @throws IllegalArgumentException
39 * if the port parameter is outside the specified range of valid
40 * port values, which is between 0 and 65535, inclusive
f157aed8 41 */
8468bb79 42 public ConnectActionClient(String host, int port, String key)
ce0974c4 43 throws IOException {
23cf894d 44 this(new Socket(host, port), key);
f157aed8
NR
45 }
46
47 /**
48 * Create a new {@link ConnectActionClient}.
49 *
50 * @param s
51 * the socket to bind to
8468bb79
NR
52 * @param key
53 * an optional key to encrypt all the communications (if NULL,
54 * everything will be sent in clear text)
f157aed8 55 */
23cf894d
NR
56 public ConnectActionClient(Socket s, String key) {
57 action = new ConnectAction(s, false, key) {
f157aed8 58 @Override
23cf894d
NR
59 protected void action() throws Exception {
60 ConnectActionClient.this.action();
f157aed8
NR
61 }
62
63 @Override
64 protected void onError(Exception e) {
65 ConnectActionClient.this.onError(e);
66 }
f157aed8
NR
67 };
68 }
69
70 /**
71 * Actually start the process and call the action (synchronous).
72 */
73 public void connect() {
74 action.connect();
75 }
76
77 /**
78 * Actually start the process and call the action (asynchronous).
79 */
80 public void connectAsync() {
81 new Thread(new Runnable() {
82 @Override
83 public void run() {
84 connect();
85 }
86 }).start();
ce0974c4
NR
87 }
88
f157aed8
NR
89 /**
90 * Method that will be called when an action is performed on the client.
91 *
f157aed8
NR
92 * @throws Exception
93 * in case of I/O error
94 */
95 @SuppressWarnings("unused")
23cf894d 96 public void action() throws Exception {
ce0974c4 97 }
f157aed8 98
f157aed8
NR
99 /**
100 * Handler called when an unexpected error occurs in the code.
101 * <p>
102 * Will just ignore the error by default.
103 *
104 * @param e
105 * the exception that occurred
106 */
107 protected void onError(@SuppressWarnings("unused") Exception e) {
108 }
ce0974c4 109}