Commit | Line | Data |
---|---|---|
79ce1a49 | 1 | package be.nikiroo.utils.serial.server; |
ce0974c4 NR |
2 | |
3 | import java.net.Socket; | |
4 | ||
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 NR |
15 | abstract class ConnectActionServer { |
16 | /** | |
17 | * The underlying {@link ConnectAction}. | |
18 | * <p> | |
19 | * Cannot be NULL. | |
20 | */ | |
21 | protected ConnectAction action; | |
f157aed8 NR |
22 | |
23 | /** | |
24 | * Create a new {@link ConnectActionServer} with the current application | |
25 | * version (see {@link Version#getCurrentVersion()}) as the server version. | |
26 | * | |
27 | * @param s | |
28 | * the socket to bind to | |
29 | */ | |
30 | public ConnectActionServer(Socket s) { | |
31 | this(s, Version.getCurrentVersion()); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Create a new {@link ConnectActionServer}. | |
36 | * | |
37 | * @param s | |
38 | * the socket to bind to | |
39 | * @param version | |
40 | * the server version | |
41 | */ | |
42 | public ConnectActionServer(Socket s, Version version) { | |
43 | action = new ConnectAction(s, true, version) { | |
44 | @Override | |
45 | protected void action(Version clientVersion) throws Exception { | |
46 | ConnectActionServer.this.action(clientVersion); | |
47 | } | |
48 | ||
49 | @Override | |
50 | protected void onError(Exception e) { | |
51 | ConnectActionServer.this.onError(e); | |
52 | } | |
53 | ||
54 | @Override | |
55 | protected Version negotiateVersion(Version clientVersion) { | |
56 | return ConnectActionServer.this.negotiateVersion(clientVersion); | |
57 | } | |
58 | }; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Actually start the process and call the action (synchronous). | |
63 | */ | |
64 | public void connect() { | |
65 | action.connect(); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Actually start the process and call the action (asynchronous). | |
70 | */ | |
71 | public void connectAsync() { | |
72 | new Thread(new Runnable() { | |
73 | @Override | |
74 | public void run() { | |
75 | connect(); | |
76 | } | |
77 | }).start(); | |
78 | } | |
79 | ||
4bb7e88e NR |
80 | /** |
81 | * The total amount of bytes received. | |
82 | * | |
83 | * @return the amount of bytes received | |
84 | */ | |
85 | public long getBytesReceived() { | |
86 | return action.getBytesReceived(); | |
87 | } | |
88 | ||
89 | /** | |
90 | * The total amount of bytes sent. | |
91 | * | |
92 | * @return the amount of bytes sent | |
93 | */ | |
94 | public long getBytesSent() { | |
95 | return action.getBytesSent(); | |
96 | } | |
97 | ||
f157aed8 NR |
98 | /** |
99 | * Method that will be called when an action is performed on the server. | |
100 | * | |
101 | * @param clientVersion | |
102 | * the client version | |
103 | * | |
104 | * @throws Exception | |
105 | * in case of I/O error | |
106 | */ | |
107 | @SuppressWarnings("unused") | |
108 | public void action(Version clientVersion) throws Exception { | |
109 | } | |
110 | ||
f157aed8 NR |
111 | /** |
112 | * Handler called when an unexpected error occurs in the code. | |
113 | * <p> | |
114 | * Will just ignore the error by default. | |
115 | * | |
116 | * @param e | |
117 | * the exception that occurred | |
118 | */ | |
119 | protected void onError(@SuppressWarnings("unused") Exception e) { | |
120 | } | |
121 | ||
122 | /** | |
123 | * Method called when we negotiate the version with the client. | |
124 | * <p> | |
125 | * Will return the actual server version by default. | |
126 | * | |
127 | * @param clientVersion | |
128 | * the client version | |
129 | * | |
130 | * @return the version to send to the client | |
131 | */ | |
132 | protected Version negotiateVersion( | |
133 | @SuppressWarnings("unused") Version clientVersion) { | |
134 | return action.getVersion(); | |
135 | } | |
ce0974c4 | 136 | } |