Commit | Line | Data |
---|---|---|
79ce1a49 | 1 | package be.nikiroo.utils.serial.server; |
ce0974c4 NR |
2 | |
3 | import java.io.IOException; | |
4 | import java.net.Socket; | |
5 | ||
6 | import be.nikiroo.utils.Version; | |
7 | ||
f157aed8 NR |
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 | */ | |
79ce1a49 NR |
16 | abstract class ConnectActionClient { |
17 | /** | |
18 | * The underlying {@link ConnectAction}. | |
19 | * <p> | |
20 | * Cannot be NULL. | |
21 | */ | |
22 | protected ConnectAction action; | |
ce0974c4 | 23 | |
f157aed8 NR |
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()); | |
ce0974c4 NR |
33 | } |
34 | ||
f157aed8 NR |
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) | |
ce0974c4 | 50 | throws IOException { |
f157aed8 | 51 | this(Server.createSocket(host, port, ssl), Version.getCurrentVersion()); |
ce0974c4 NR |
52 | } |
53 | ||
f157aed8 NR |
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, | |
ce0974c4 | 70 | Version version) throws IOException { |
f157aed8 NR |
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(); | |
ce0974c4 NR |
120 | } |
121 | ||
f157aed8 NR |
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") | |
ce0974c4 NR |
132 | public void action(Version serverVersion) throws Exception { |
133 | } | |
f157aed8 | 134 | |
f157aed8 NR |
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 | } | |
ce0974c4 | 145 | } |