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