Commit | Line | Data |
---|---|---|
79ce1a49 | 1 | package be.nikiroo.utils.serial.server; |
ce0974c4 NR |
2 | |
3 | import java.net.Socket; | |
4 | ||
3087aeb5 NR |
5 | import be.nikiroo.utils.Version; |
6 | ||
f157aed8 NR |
7 | /** |
8 | * Base class used for the server basic handling. | |
9 | * <p> | |
10 | * It represents a single action: a server is expected to execute one action for | |
11 | * each client action. | |
12 | * | |
13 | * @author niki | |
14 | */ | |
79ce1a49 | 15 | abstract class ConnectActionServer { |
a72ea8a7 NR |
16 | private boolean closing; |
17 | ||
79ce1a49 NR |
18 | /** |
19 | * The underlying {@link ConnectAction}. | |
20 | * <p> | |
21 | * Cannot be NULL. | |
22 | */ | |
23 | protected ConnectAction action; | |
f157aed8 | 24 | |
f157aed8 | 25 | /** |
3087aeb5 | 26 | * Create a new {@link ConnectActionServer}, using the current version. |
f157aed8 NR |
27 | * |
28 | * @param s | |
29 | * the socket 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 | 33 | */ |
08f80ac5 | 34 | public ConnectActionServer(Socket s, String key) { |
3087aeb5 NR |
35 | this(s, key, Version.getCurrentVersion()); |
36 | } | |
37 | ||
38 | /** | |
39 | * Create a new {@link ConnectActionServer}. | |
40 | * | |
41 | * @param s | |
42 | * the socket to bind to | |
43 | * @param key | |
44 | * an optional key to encrypt all the communications (if NULL, | |
45 | * everything will be sent in clear text) | |
46 | * @param serverVersion | |
47 | * the version of this server,that will be sent to the client | |
48 | */ | |
49 | public ConnectActionServer(Socket s, String key, Version serverVersion) { | |
50 | action = new ConnectAction(s, true, key, serverVersion) { | |
f157aed8 | 51 | @Override |
3087aeb5 | 52 | protected void action(Version clientVersion) throws Exception { |
3087aeb5 | 53 | ConnectActionServer.this.action(clientVersion); |
f157aed8 NR |
54 | } |
55 | ||
56 | @Override | |
57 | protected void onError(Exception e) { | |
58 | ConnectActionServer.this.onError(e); | |
59 | } | |
3087aeb5 NR |
60 | |
61 | @Override | |
62 | protected Version negotiateVersion(Version clientVersion) { | |
63 | return ConnectActionServer.this.negotiateVersion(clientVersion); | |
64 | } | |
f157aed8 NR |
65 | }; |
66 | } | |
67 | ||
68 | /** | |
69 | * Actually start the process and call the action (synchronous). | |
70 | */ | |
71 | public void connect() { | |
72 | action.connect(); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Actually start the process and call the action (asynchronous). | |
77 | */ | |
78 | public void connectAsync() { | |
79 | new Thread(new Runnable() { | |
80 | @Override | |
81 | public void run() { | |
82 | connect(); | |
83 | } | |
84 | }).start(); | |
85 | } | |
86 | ||
a72ea8a7 NR |
87 | /** |
88 | * Stop the client/server connection on behalf of the server (usually, the | |
89 | * client connects then is allowed to send as many requests as it wants; in | |
90 | * some cases, though, the server may wish to forcefully close the | |
91 | * connection and can do via this value, when it is set to TRUE). | |
92 | * <p> | |
93 | * Example of usage: the client failed an authentication check, cut the | |
94 | * connection here and now. | |
8468bb79 NR |
95 | * |
96 | * @return TRUE when it is | |
a72ea8a7 NR |
97 | */ |
98 | public boolean isClosing() { | |
99 | return closing; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Can be called to stop the client/server connection on behalf of the | |
104 | * server (usually, the client connects then is allowed to send as many | |
105 | * requests as it wants; in some cases, though, the server may wish to | |
106 | * forcefully close the connection and can do so by calling this method). | |
107 | * <p> | |
108 | * Example of usage: the client failed an authentication check, cut the | |
109 | * connection here and now. | |
110 | */ | |
111 | public void close() { | |
112 | closing = true; | |
113 | } | |
114 | ||
4bb7e88e NR |
115 | /** |
116 | * The total amount of bytes received. | |
117 | * | |
118 | * @return the amount of bytes received | |
119 | */ | |
120 | public long getBytesReceived() { | |
121 | return action.getBytesReceived(); | |
122 | } | |
123 | ||
124 | /** | |
125 | * The total amount of bytes sent. | |
126 | * | |
127 | * @return the amount of bytes sent | |
128 | */ | |
129 | public long getBytesSent() { | |
08f80ac5 | 130 | return action.getBytesWritten(); |
4bb7e88e NR |
131 | } |
132 | ||
f157aed8 NR |
133 | /** |
134 | * Method that will be called when an action is performed on the server. | |
135 | * | |
3087aeb5 NR |
136 | * @param clientVersion |
137 | * the version of the client connected to this server | |
138 | * | |
f157aed8 NR |
139 | * @throws Exception |
140 | * in case of I/O error | |
141 | */ | |
142 | @SuppressWarnings("unused") | |
3087aeb5 | 143 | public void action(Version clientVersion) throws Exception { |
f157aed8 NR |
144 | } |
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 | } | |
3087aeb5 NR |
156 | |
157 | /** | |
158 | * Method called when we negotiate the version with the client. | |
159 | * <p> | |
160 | * Will return the actual server version by default. | |
161 | * | |
162 | * @param clientVersion | |
163 | * the client version | |
164 | * | |
165 | * @return the version to send to the client | |
166 | */ | |
167 | protected Version negotiateVersion( | |
168 | @SuppressWarnings("unused") Version clientVersion) { | |
169 | return action.getVersion(); | |
170 | } | |
ce0974c4 | 171 | } |