1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | import java.util.Map; |
---|
4 | import java.util.TreeMap; |
---|
5 | |
---|
6 | import org.apache.logging.log4j.Logger; |
---|
7 | import org.apache.logging.log4j.LogManager; |
---|
8 | |
---|
9 | import com.framsticks.communication.Response; |
---|
10 | import com.framsticks.communication.ServerSideResponseFuture; |
---|
11 | import com.framsticks.params.EventListener; |
---|
12 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
13 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
14 | import com.framsticks.params.types.EventParam; |
---|
15 | import com.framsticks.structure.Path; |
---|
16 | import com.framsticks.structure.Tree; |
---|
17 | import com.framsticks.structure.TreeOperations; |
---|
18 | import com.framsticks.util.dispatching.Future; |
---|
19 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
20 | |
---|
21 | @FramsClassAnnotation |
---|
22 | public class Cli { |
---|
23 | private static final Logger log = |
---|
24 | LogManager.getLogger(Cli.class); |
---|
25 | |
---|
26 | |
---|
27 | protected int eventCounter = 0; |
---|
28 | protected final Tree tree; |
---|
29 | protected final ClientAtServer client; |
---|
30 | |
---|
31 | protected final String address; |
---|
32 | |
---|
33 | @ParamAnnotation |
---|
34 | public final Map<String, CliEvent> events = new TreeMap<>(); |
---|
35 | |
---|
36 | /** |
---|
37 | * @param tree |
---|
38 | */ |
---|
39 | public Cli(ClientAtServer client) { |
---|
40 | this.client = client; |
---|
41 | this.tree = client.getTree(); |
---|
42 | this.address = client.connection.getAddress(); |
---|
43 | } |
---|
44 | |
---|
45 | public void addListener(Path path, EventParam param, String pathPrefix, ServerSideResponseFuture responseFuture) { |
---|
46 | log.debug("adding listener for {}: {}", path, param); |
---|
47 | |
---|
48 | final CliEvent event = new CliEvent(); |
---|
49 | event.cli = this; |
---|
50 | event.id = "e" + (eventCounter++); |
---|
51 | event.path = path; |
---|
52 | event.param = param; |
---|
53 | event.name = pathPrefix + Path.appendString(path.getTextual(), param.getId()); |
---|
54 | event.pathToEvent = "/cli/events/" + event.id; |
---|
55 | |
---|
56 | event.listener = new EventListener<Object>() { |
---|
57 | |
---|
58 | @Override |
---|
59 | public void action(Object argument) { |
---|
60 | log.debug("registered event {} happened with {}", event, argument); |
---|
61 | |
---|
62 | client.connection.sendFile( |
---|
63 | "event " + event.pathToEvent + " " + event.name, |
---|
64 | ClientAtServer.printToFile("", tree.getRegistry().createAccess(argument.getClass()).select(argument)), |
---|
65 | null, |
---|
66 | client |
---|
67 | ); |
---|
68 | } |
---|
69 | }; |
---|
70 | |
---|
71 | // SyncedFuture handler = new SyncedFuture<Void>(ThrowExceptionHandler.getInstance()); |
---|
72 | TreeOperations.addListener(path, param, event.listener, Object.class, Future.doNothing(Void.class, ThrowExceptionHandler.getInstance())); |
---|
73 | |
---|
74 | events.put(event.id, event); |
---|
75 | responseFuture.pass(new Response(true, event.pathToEvent)); |
---|
76 | // handler |
---|
77 | } |
---|
78 | |
---|
79 | @Override |
---|
80 | public String toString() { |
---|
81 | return "CLI for " + tree; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * @return the address |
---|
86 | */ |
---|
87 | @ParamAnnotation |
---|
88 | public String getAddress() { |
---|
89 | return address; |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|