1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | import static com.framsticks.util.lang.Strings.assureNotEmpty; |
---|
4 | |
---|
5 | import com.framsticks.communication.*; |
---|
6 | import com.framsticks.communication.queries.ApplicationRequest; |
---|
7 | import com.framsticks.communication.queries.CallRequest; |
---|
8 | import com.framsticks.communication.queries.GetRequest; |
---|
9 | import com.framsticks.communication.queries.InfoRequest; |
---|
10 | import com.framsticks.communication.queries.SetRequest; |
---|
11 | import com.framsticks.core.Mode; |
---|
12 | import com.framsticks.core.Tree; |
---|
13 | import com.framsticks.core.Path; |
---|
14 | import com.framsticks.params.*; |
---|
15 | import com.framsticks.params.types.ProcedureParam; |
---|
16 | import com.framsticks.parsers.Savers; |
---|
17 | import com.framsticks.util.FramsticksException; |
---|
18 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
19 | import com.framsticks.util.dispatching.Dispatching; |
---|
20 | import com.framsticks.util.dispatching.FutureHandler; |
---|
21 | import com.framsticks.util.dispatching.Joinable; |
---|
22 | import com.framsticks.util.dispatching.JoinableParent; |
---|
23 | import com.framsticks.util.dispatching.JoinableState; |
---|
24 | import static com.framsticks.core.TreeOperations.*; |
---|
25 | |
---|
26 | import java.net.Socket; |
---|
27 | import java.util.LinkedList; |
---|
28 | import java.util.List; |
---|
29 | |
---|
30 | /** |
---|
31 | * @author Piotr Sniegowski |
---|
32 | */ |
---|
33 | public class ClientAtServer extends AbstractJoinable implements RequestHandler, JoinableParent { |
---|
34 | |
---|
35 | protected final Server server; |
---|
36 | protected final Tree tree; |
---|
37 | protected final ServerSideManagedConnection connection; |
---|
38 | |
---|
39 | public ClientAtServer(Server server, Socket socket) { |
---|
40 | this.server = server; |
---|
41 | this.tree = server.hosted; |
---|
42 | this.connection = new ServerSideManagedConnection(socket, this); |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | public String getName() { |
---|
47 | return connection + " to " + server; |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public void handle(final ApplicationRequest request, final ServerSideResponseFuture responseCallback) { |
---|
52 | assureNotEmpty(request.getPath()); |
---|
53 | |
---|
54 | resolve(tree, request.getPath(), new FutureHandler<Path>(responseCallback) { |
---|
55 | @Override |
---|
56 | protected void result(final Path path) { |
---|
57 | |
---|
58 | // final AccessInterface access = tree.prepareAccess(path); |
---|
59 | final AccessInterface access = bindAccess(path); |
---|
60 | |
---|
61 | if (request instanceof SetRequest) { |
---|
62 | SetRequest setRequest = (SetRequest) request; |
---|
63 | //TODO Primitive Param? |
---|
64 | tree.set(path, access.getFramsClass().getParamEntry(setRequest.getField(), PrimitiveParam.class), setRequest.getValue(), new FutureHandler<Integer>(responseCallback) { |
---|
65 | @Override |
---|
66 | protected void result(Integer flag) { |
---|
67 | responseCallback.pass(new Response(true, Flags.write(flag, null), null)); |
---|
68 | |
---|
69 | } |
---|
70 | }); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | if (request instanceof GetRequest) { |
---|
75 | tree.get(path, Mode.FETCH, new FutureHandler<Object>(responseCallback) { |
---|
76 | |
---|
77 | @Override |
---|
78 | protected void result(Object result) { |
---|
79 | if (result != access.getSelected()) { |
---|
80 | throw new FramsticksException().msg("mismatch objects during fetch").arg("path", path); |
---|
81 | } |
---|
82 | // TODO Auto-generated method stub |
---|
83 | List<File> files = new LinkedList<File>(); |
---|
84 | ListSink sink = new ListSink(); |
---|
85 | access.save(sink); |
---|
86 | files.add(new File(path.getTextual(), new ListSource(sink.getOut()))); |
---|
87 | responseCallback.pass(new Response(true, "", files)); |
---|
88 | } |
---|
89 | }); |
---|
90 | |
---|
91 | return; |
---|
92 | } |
---|
93 | if (request instanceof CallRequest) { |
---|
94 | final CallRequest callRequest = (CallRequest) request; |
---|
95 | tree.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), new FutureHandler<Object>(responseCallback) { |
---|
96 | @Override |
---|
97 | protected void result(Object result) { |
---|
98 | ListSink sink = new ListSink(); |
---|
99 | sink.print("Result:").breakLine(); |
---|
100 | sink.print("value:").print("["); |
---|
101 | if (result != null) { |
---|
102 | sink.print(result); |
---|
103 | } |
---|
104 | sink.print("]"); |
---|
105 | |
---|
106 | responseCallback.pass(new Response(true, "", File.single(new File("", new ListSource(sink.getOut()))))); |
---|
107 | } |
---|
108 | }); |
---|
109 | return; |
---|
110 | } |
---|
111 | if (request instanceof InfoRequest) { |
---|
112 | FramsClass framsClass = getInfo(path); |
---|
113 | if (framsClass == null) { |
---|
114 | throw new FramsticksException().msg("info should be available"); |
---|
115 | } |
---|
116 | responseCallback.pass(new Response(true, null, File.single(new File(path.getTextual(), new ListSource(Savers.saveFramsClass(new ListSink(), framsClass).getOut()))))); |
---|
117 | return; |
---|
118 | } |
---|
119 | |
---|
120 | throw new FramsticksException().msg("invalid request type: " + request.getCommand()); |
---|
121 | } |
---|
122 | }); |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | @Override |
---|
127 | protected void joinableStart() { |
---|
128 | Dispatching.use(connection, this); |
---|
129 | } |
---|
130 | |
---|
131 | @Override |
---|
132 | protected void joinableInterrupt() { |
---|
133 | Dispatching.drop(connection, this); |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | protected void joinableFinish() { |
---|
138 | } |
---|
139 | |
---|
140 | @Override |
---|
141 | protected void joinableJoin() throws InterruptedException { |
---|
142 | Dispatching.join(connection); |
---|
143 | } |
---|
144 | |
---|
145 | @Override |
---|
146 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
147 | proceedToState(state); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | } |
---|