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 | ||
80 | /** | |
81 | * Method that will be called when an action is performed on the server. | |
82 | * | |
83 | * @param clientVersion | |
84 | * the client version | |
85 | * | |
86 | * @throws Exception | |
87 | * in case of I/O error | |
88 | */ | |
89 | @SuppressWarnings("unused") | |
90 | public void action(Version clientVersion) throws Exception { | |
91 | } | |
92 | ||
f157aed8 NR |
93 | /** |
94 | * Handler called when an unexpected error occurs in the code. | |
95 | * <p> | |
96 | * Will just ignore the error by default. | |
97 | * | |
98 | * @param e | |
99 | * the exception that occurred | |
100 | */ | |
101 | protected void onError(@SuppressWarnings("unused") Exception e) { | |
102 | } | |
103 | ||
104 | /** | |
105 | * Method called when we negotiate the version with the client. | |
106 | * <p> | |
107 | * Will return the actual server version by default. | |
108 | * | |
109 | * @param clientVersion | |
110 | * the client version | |
111 | * | |
112 | * @return the version to send to the client | |
113 | */ | |
114 | protected Version negotiateVersion( | |
115 | @SuppressWarnings("unused") Version clientVersion) { | |
116 | return action.getVersion(); | |
117 | } | |
ce0974c4 | 118 | } |