source: java/main/src/main/java/com/framsticks/hosting/Cli.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1package com.framsticks.hosting;
2
3import java.util.Map;
4import java.util.TreeMap;
5
6import org.apache.logging.log4j.Logger;
7import org.apache.logging.log4j.LogManager;
8
9import com.framsticks.communication.Response;
10import com.framsticks.communication.ServerSideResponseFuture;
11import com.framsticks.params.EventListener;
12import com.framsticks.params.annotations.FramsClassAnnotation;
13import com.framsticks.params.annotations.ParamAnnotation;
14import com.framsticks.params.types.EventParam;
15import com.framsticks.structure.Path;
16import com.framsticks.structure.Tree;
17import com.framsticks.structure.TreeOperations;
18import com.framsticks.util.dispatching.Future;
19import com.framsticks.util.dispatching.ThrowExceptionHandler;
20
21@FramsClassAnnotation
22public 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}
Note: See TracBrowser for help on using the repository browser.