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