1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | import org.apache.log4j.Level; |
---|
4 | import org.apache.log4j.Logger; |
---|
5 | |
---|
6 | import com.framsticks.core.Tree; |
---|
7 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
8 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
9 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
10 | import com.framsticks.util.FramsticksException; |
---|
11 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
12 | import com.framsticks.util.dispatching.Dispatching; |
---|
13 | import com.framsticks.util.dispatching.Joinable; |
---|
14 | import com.framsticks.util.dispatching.JoinableCollection; |
---|
15 | import com.framsticks.util.dispatching.JoinableParent; |
---|
16 | import com.framsticks.util.dispatching.JoinableState; |
---|
17 | import com.framsticks.util.dispatching.RunAt; |
---|
18 | import com.framsticks.util.dispatching.Task; |
---|
19 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.net.InetSocketAddress; |
---|
23 | import java.net.ServerSocket; |
---|
24 | import java.net.Socket; |
---|
25 | import com.framsticks.util.dispatching.Thread; |
---|
26 | |
---|
27 | @FramsClassAnnotation |
---|
28 | public class Server extends AbstractJoinable implements JoinableParent { |
---|
29 | |
---|
30 | private final static Logger log = Logger.getLogger(Server.class); |
---|
31 | |
---|
32 | protected int port; |
---|
33 | |
---|
34 | protected ServerSocket acceptSocket; |
---|
35 | protected Tree hosted; |
---|
36 | protected final JoinableCollection<ClientAtServer> clients = new JoinableCollection<ClientAtServer>(); |
---|
37 | |
---|
38 | public static class Accept { |
---|
39 | }; |
---|
40 | |
---|
41 | protected Thread<Accept> acceptThread = new Thread<>(); |
---|
42 | |
---|
43 | /** |
---|
44 | * |
---|
45 | */ |
---|
46 | public Server() { |
---|
47 | log.debug("created server"); |
---|
48 | port = 9009; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * @return the port |
---|
53 | */ |
---|
54 | @ParamAnnotation |
---|
55 | public int getPort() { |
---|
56 | return port; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * @param port the port to set |
---|
61 | */ |
---|
62 | @ParamAnnotation |
---|
63 | public void setPort(int port) { |
---|
64 | this.port = port; |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | protected void joinableInterrupt() { |
---|
69 | Dispatching.drop(acceptThread, this); |
---|
70 | Dispatching.drop(hosted, this); |
---|
71 | Dispatching.drop(clients, this); |
---|
72 | try { |
---|
73 | acceptSocket.close(); |
---|
74 | } catch (IOException e) { |
---|
75 | log.debug("exception caught during socket closing: ", e); |
---|
76 | } |
---|
77 | finish(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * @return the hosted |
---|
82 | */ |
---|
83 | public Tree getHosted() { |
---|
84 | return hosted; |
---|
85 | } |
---|
86 | |
---|
87 | @AutoAppendAnnotation |
---|
88 | public void setHosted(Tree hosted) { |
---|
89 | if (this.hosted != null) { |
---|
90 | throw new FramsticksException().msg("hosted tree is already set").arg("current", this.hosted); |
---|
91 | } |
---|
92 | this.hosted = hosted; |
---|
93 | acceptThread.setName(hosted.getName() + " acceptor"); |
---|
94 | clients.setObservableName(hosted.getName() + " clients"); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
99 | proceedToState(state); |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | protected void joinableStart() { |
---|
104 | Dispatching.use(acceptThread, this); |
---|
105 | Dispatching.use(hosted, this); |
---|
106 | Dispatching.use(clients, this); |
---|
107 | try { |
---|
108 | acceptSocket = new ServerSocket(); |
---|
109 | } catch (IOException e) { |
---|
110 | throw new FramsticksException().msg("failed to create server socket").cause(e); |
---|
111 | } |
---|
112 | tryBind(0); |
---|
113 | } |
---|
114 | |
---|
115 | @Override |
---|
116 | @ParamAnnotation |
---|
117 | public String getName() { |
---|
118 | return hosted != null ? hosted.getName() : "server"; |
---|
119 | } |
---|
120 | |
---|
121 | protected void acceptNext() { |
---|
122 | if (!isRunning()) { |
---|
123 | log.debug("server is not in running state, aborting accepting"); |
---|
124 | return; |
---|
125 | } |
---|
126 | //TODO TEH |
---|
127 | acceptThread.dispatch(new RunAt<Accept>(ThrowExceptionHandler.getInstance()) { |
---|
128 | @Override |
---|
129 | protected void runAt() { |
---|
130 | try { |
---|
131 | log.debug("accepting"); |
---|
132 | final Socket socket = acceptSocket.accept(); |
---|
133 | assert socket != null; |
---|
134 | log.debug("accepted socket: " + socket.getInetAddress().getHostAddress()); |
---|
135 | hosted.dispatch(new RunAt<Tree>(this) { |
---|
136 | @Override |
---|
137 | protected void runAt() { |
---|
138 | ClientAtServer client = new ClientAtServer(Server.this, socket); |
---|
139 | clients.add(client); |
---|
140 | log.info("client connected: " + client); |
---|
141 | } |
---|
142 | }); |
---|
143 | } catch (IOException e) { |
---|
144 | log.log((isRunning() ? Level.ERROR : Level.DEBUG), "failed to accept socket: " + e); |
---|
145 | } |
---|
146 | acceptNext(); |
---|
147 | } |
---|
148 | }); |
---|
149 | } |
---|
150 | |
---|
151 | protected void tryBind(int when) { |
---|
152 | acceptThread.dispatch(new Task<Accept>(ThrowExceptionHandler.getInstance(), when) { |
---|
153 | @Override |
---|
154 | protected void runAt() { |
---|
155 | try { |
---|
156 | acceptSocket.bind(new InetSocketAddress(port)); |
---|
157 | log.debug("started accepting on port " + port); |
---|
158 | acceptNext(); |
---|
159 | return; |
---|
160 | } catch (IOException e) { |
---|
161 | log.warn("failed to accept on port " + port + " (repeating): " + e); |
---|
162 | } |
---|
163 | tryBind(1000); |
---|
164 | } |
---|
165 | }); |
---|
166 | } |
---|
167 | |
---|
168 | @Override |
---|
169 | protected void joinableFinish() { |
---|
170 | |
---|
171 | } |
---|
172 | |
---|
173 | @Override |
---|
174 | protected void joinableJoin() throws InterruptedException { |
---|
175 | Dispatching.join(acceptThread); |
---|
176 | Dispatching.join(hosted); |
---|
177 | Dispatching.join(clients); |
---|
178 | } |
---|
179 | |
---|
180 | // @Override |
---|
181 | // public FramsClass getInfoFromCache(String id) { |
---|
182 | // assert isActive(); |
---|
183 | // if (id == null) { |
---|
184 | // return null; |
---|
185 | // } |
---|
186 | // FramsClass cached = registry.getFramsClass(id); |
---|
187 | // if (cached != null) { |
---|
188 | // return cached; |
---|
189 | // } |
---|
190 | // try { |
---|
191 | // Class<?> nativeClass = Class.forName(id); |
---|
192 | // FramsClass framsClass = FramsClass.build().forClass(nativeClass); |
---|
193 | |
---|
194 | // if (!framsClass.getId().equals(id)) { |
---|
195 | // log.error("no matching id"); |
---|
196 | // return null; |
---|
197 | // } |
---|
198 | |
---|
199 | // registry.registerReflectedClass(null, id, nativeClass); |
---|
200 | // registry.putFramsClass(framsClass); |
---|
201 | // return framsClass; |
---|
202 | // } catch (ClassNotFoundException ignored) { |
---|
203 | // } catch (ConstructionException e) { |
---|
204 | // log.error("failed to use info from cache: " + e); |
---|
205 | // } |
---|
206 | |
---|
207 | // return null; |
---|
208 | // } |
---|
209 | |
---|
210 | // @Override |
---|
211 | // protected void fetchInfo(Path path, Future<FramsClass> future) { |
---|
212 | // assert isActive(); |
---|
213 | |
---|
214 | // FramsClass framsClass = getInfoFromCache(path.getTop().getObject().getClass().getCanonicalName()); |
---|
215 | // if (framsClass == null) { |
---|
216 | // log.error("failed to create frams class for: " + path.getTop().getObject().getClass()); |
---|
217 | // future.result(null, new Exception()); |
---|
218 | // return; |
---|
219 | // } |
---|
220 | // future.result(framsClass, null); |
---|
221 | // } |
---|
222 | } |
---|