1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | import com.framsticks.communication.*; |
---|
4 | import com.framsticks.communication.queries.ApplicationRequest; |
---|
5 | import com.framsticks.communication.queries.GetRequest; |
---|
6 | import com.framsticks.communication.queries.InfoRequest; |
---|
7 | import com.framsticks.core.Instance; |
---|
8 | import com.framsticks.core.Path; |
---|
9 | import com.framsticks.params.*; |
---|
10 | import com.framsticks.parsers.Savers; |
---|
11 | import com.framsticks.core.LocalInstance; |
---|
12 | import com.framsticks.util.dispatching.Future; |
---|
13 | |
---|
14 | import java.net.Socket; |
---|
15 | import java.util.ArrayList; |
---|
16 | import java.util.LinkedList; |
---|
17 | import java.util.List; |
---|
18 | import com.framsticks.util.dispatching.RunAt; |
---|
19 | |
---|
20 | /** |
---|
21 | * @author Piotr Sniegowski |
---|
22 | */ |
---|
23 | public class InstanceClient implements RequestHandler { |
---|
24 | |
---|
25 | protected final LocalInstance instance; |
---|
26 | protected final ServerConnection connection; |
---|
27 | |
---|
28 | public InstanceClient(LocalInstance instance, Socket socket) { |
---|
29 | this.instance = instance; |
---|
30 | connection = new ServerConnection(socket, this); |
---|
31 | connection.start(); |
---|
32 | } |
---|
33 | |
---|
34 | @Override |
---|
35 | public String toString() { |
---|
36 | return instance + "|" + connection.toString(); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | @Override |
---|
41 | public void handle(final ApplicationRequest request, final ResponseCallback<?> responseCallback) { |
---|
42 | instance.invokeLater(new RunAt<Instance>() { |
---|
43 | @Override |
---|
44 | public void run() { |
---|
45 | final Path path = instance.getPath(request.getPath()); |
---|
46 | if (!path.isResolved(request.getPath())) { |
---|
47 | responseCallback.process(new Response(false, "\"invalid path\"", null)); |
---|
48 | return; |
---|
49 | } |
---|
50 | if (request instanceof GetRequest) { |
---|
51 | instance.findInfo(path, new Future<FramsClass>() { |
---|
52 | @Override |
---|
53 | public void result(FramsClass result, Exception e) { |
---|
54 | if (result == null) { |
---|
55 | responseCallback.process(new Response(false, "\"failed to find info for access bind\"", null)); |
---|
56 | return; |
---|
57 | } |
---|
58 | List<File> files = new LinkedList<File>(); |
---|
59 | AccessInterface access = instance.bindAccess(path); |
---|
60 | |
---|
61 | if (access == null) { |
---|
62 | |
---|
63 | responseCallback.process(new Response(false, "\"failed to bind access\"", null)); |
---|
64 | return; |
---|
65 | } |
---|
66 | ListSink sink = new ListSink(); |
---|
67 | access.save(sink); |
---|
68 | files.add(new File(path.getTextual(), new ListSource(sink.getOut()))); |
---|
69 | responseCallback.process(new Response(true, "", files)); |
---|
70 | } |
---|
71 | }); |
---|
72 | return; |
---|
73 | } |
---|
74 | if (request instanceof InfoRequest) { |
---|
75 | instance.findInfo(path, new Future<FramsClass>() { |
---|
76 | @Override |
---|
77 | public void result(FramsClass result, Exception e) { |
---|
78 | if (result == null) { |
---|
79 | responseCallback.process(new Response(false, "\"info not found\"", null)); |
---|
80 | return; |
---|
81 | } |
---|
82 | ListSink sink = new ListSink(); |
---|
83 | Savers.saveFramsClass(sink, result); |
---|
84 | List<File> files = new ArrayList<File>(); |
---|
85 | files.add(new File(path.getTextual(), new ListSource(sink.getOut()))); |
---|
86 | responseCallback.process(new Response(true, null, files)); |
---|
87 | } |
---|
88 | }); |
---|
89 | return; |
---|
90 | } |
---|
91 | responseCallback.process(new Response(false, "invalid", null)); |
---|
92 | } |
---|
93 | }); |
---|
94 | |
---|
95 | } |
---|
96 | } |
---|