source: java/main/src/main/java/com/framsticks/communication/ServerSideManagedConnection.java @ 99

Last change on this file since 99 was 99, checked in by psniegowski, 11 years ago

HIGHLIGTS:

  • complete events implementation
  • add CLI in Java Framsticks server
  • add automatic registration for events in GUI
  • improve objects fetching (object are never overwritten with new instances)
  • properly react for ListChange? events
  • add ListPanel? with table view
    • columns to be shown may be statically specified in configuration
    • currently modyfying data through tables is not available
  • improve maven configuration
    • configuration file may be specified without touching pom.xml

CHANGELOG:
Extract constants from Flags into ParamFlags? and SetStateFlags?.

Extract flags I/O to FlagsUtils? class.

Configured maven to exec given resource configuration.

For example:
mvn exec:exec -Dframsticks.config=/configs/managed-console.xml

Cleanup pom.xml

Rename ObjectTree? to LocalTree? (also make LocalTree? and RemoteTree? final).

Minor change.

Add maximum number of columns in ListPanelProvider?.

Improve ColumnsConfig? interpretation.

Automatically fill FramsClass?.name if trying to construct empty.

Improve identitifer case mangling in XmlLoader?.

Introduce configurable ColumnsConfig?.

Draft working version of ListPanel?.

Table is being shown (although empty).

More improvements to table building.

Move some functionality from Frame to TreeModel?.

Move tree classes in gui to separate package.

Remove old table related classes.

Add draft implementation of TableModel?.

Redirect ParamBuilder?.forAccess to AccessInterface?.

Optimize ParamBuilder?.forAccess()

Do not clear list when loading.

Do not load fetched values directly.

Implement different AccessInterface? copying policy.

Optimize fetching values routine.

Remove Mode enum (work out get semantics).

Some improvements to ListChange? handling.

Improve UniqueListAccess?.

Add reaction for ListChanges? in the TreeNode?.

EventListeners? are being added in the TreeNode?.

Listeners for ListParams? are now very naive (they download
whole list).

Automatially register on events in GUI.

Events are working in RemoteTree? and Server.

Move listeners to the ClientSideManagedConnection?.

Remove old classes responsible for event subscriptions.

Improve event reading.

Improve events handling at server side.

Add register attribute in FramsClassAnnotation?
to automatically also register other classes.

Registering events works.

Setup for remote listeners registration.

More improvements.

Minor changes.

Add rootTree to the ClientAtServer?.

Moving CLI to the ClientAtServer?.

Fix bug: use Void.TYPE instead of Void.class

More development around CLI.

  • Improve Path resolving.

Add synthetic root to ObjectTree?.

It is needed to allow sybling for the original root
that would containg CLI.

Some work with registering events in RemoteTree?.

Draft implementation of listener registering in RemoteTree?.

Support events registration in the ObjectTree?.

Add events support to ReflectionAccess?.

EventParam? is recognized by ParamCandidate?.

Prepare interface for Events across project.

Add EventListener? and API for listeners in Tree.

File size: 3.9 KB
Line 
1package com.framsticks.communication;
2
3import com.framsticks.communication.queries.*;
4import com.framsticks.params.SourceInterface;
5import com.framsticks.util.FramsticksException;
6import com.framsticks.util.lang.Holder;
7import com.framsticks.util.lang.Pair;
8import com.framsticks.util.lang.Strings;
9
10import org.apache.log4j.Logger;
11
12import java.net.Socket;
13import com.framsticks.util.dispatching.RunAt;
14
15/**
16 * @author Piotr Sniegowski
17 */
18public class ServerSideManagedConnection extends ManagedConnection {
19
20        private final static Logger log = Logger.getLogger(ServerSideManagedConnection.class);
21
22        protected final RequestHandler requestHandler;
23
24        public ServerSideManagedConnection(Socket socket, RequestHandler requestHandler) {
25                setAddress(new Address(socket.getInetAddress().getHostAddress(), socket.getPort()));
26                setDescription("server connection");
27                this.socket = socket;
28                this.requestHandler = requestHandler;
29                // socket.setSoTimeout(500);
30                setupStreams();
31        }
32
33
34        protected void processNextInputBatch() {
35                processNextRequest();
36        }
37
38        @Override
39        protected void receiverThreadRoutine() {
40
41                processInputBatchesUntilClosed();
42        }
43
44        protected void handleRequest(Request request, ServerSideResponseFuture responseCallback) {
45                if (request instanceof ApplicationRequest) {
46                        requestHandler.handle((ApplicationRequest) request, responseCallback);
47                        return;
48                }
49                if (request instanceof ProtocolRequest) {
50                        if (request instanceof VersionRequest) {
51                                responseCallback.pass(new Response(true, null, null));
52                                return;
53                        }
54                        if (request instanceof UseRequest) {
55                                String feature = ((UseRequest)request).getFeature();
56                                if (feature.equals("request_id")) {
57                                        requestIdEnabled = true;
58                                        responseCallback.pass(new Response(true, null, null));
59                                        return;
60                                }
61                                responseCallback.pass(new Response(false, "unknown feature: " + feature, null));
62                                return;
63                        }
64
65                }
66                log.error("unhandled request: " + request);
67                responseCallback.pass(new Response(false, "unhandled", null));
68        }
69
70        protected final void putFile(File file, String outId) {
71                putLine("file" + outId/* + " " + f.getPath()*/);
72                SourceInterface content = file.getContent();
73                String line;
74                while ((line = content.readLine()) != null) {
75                        putLine(line);
76                }
77                putLine("eof");
78        }
79
80        public final void sendFile(final String header, final File file) {
81                senderThread.dispatch(new RunAt<Connection>(requestHandler) {
82                        @Override
83                        protected void runAt() {
84                                putLine(header);
85                                putFile(file, "");
86                                flushOut();
87                        }
88                });
89        }
90
91        protected final void respond(final Response response, final Integer id) {
92                senderThread.dispatch(new RunAt<Connection>(requestHandler) {
93                        @Override
94                        protected void runAt() {
95                                String outId = id != null ? " " + id : "";
96                                if (response.getFiles() != null) {
97                                        for (File f : response.getFiles()) {
98                                                putFile(f, outId);
99                                        }
100                                }
101                                StringBuilder statusLine = new StringBuilder();
102                                statusLine.append(response.getOk() ? "ok" : "error").append(outId);
103                                if (Strings.notEmpty(response.getComment())) {
104                                        Request.quoteValue(statusLine.append(" "), response.getComment());
105                                }
106                                putLine(statusLine.toString());
107                                flushOut();
108                        }
109                });
110
111        }
112
113
114        protected void processNextRequest() {
115                final Holder<Integer> id = new Holder<>();
116                final String line = getLine();
117                try {
118                        Pair<CharSequence, CharSequence> command = Request.takeIdentifier(line);
119                        final Pair<Integer, CharSequence> rest = takeRequestId(command.second);
120                        id.set(rest.first);
121
122                        final Request request = Request.parse(command.first, rest.second);
123
124                        if (log.isTraceEnabled()) {
125                                log.trace("read request: " + request);
126                        }
127
128                        //TODO what to do here?
129                        handleRequest(request, new ServerSideResponseFuture() {
130                                @Override
131                                protected void result(Response response) {
132                                        respond(response, rest.first);
133                                }
134                        });
135                } catch (FramsticksException e) {
136                        e.arg("id", id.get()).arg("line", line);
137                        log.error("error: ", e);
138                        respond(new Response(false, "invalid input: " + e.getMsg(), null), id.get());
139                        return;
140                }
141
142        }
143}
Note: See TracBrowser for help on using the repository browser.