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