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

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

HIGHLIGHTS:

  • add auto loading and saving algorithms between

frams files format and Java classes

  • respect ValueChange? events in GUI (do not reload object)
  • support results of procedures in Java server
  • make Experiment automatically convert between frams file and NetFile? object
  • add MessageLogger? (compatible with original frams server messages)
  • WorkPackageLogic? now validates results, is able to discard them, reschedule

whole package, or only uncomputed remainder

CHANGELOG:
Show just a short description in PrimeExperiment?.

Add primes_changed event to the PrimeExperiment?.

Make WorkPackageLogic? robust to frams server returning invalid results.

Add MessageLogger? to logics.

Add NetFile? interface. Support Messages from server.

Minor changes to connections.

Merge results in the PrimeExperiment?.

More netload class->file conversion to Simulator.

Move netsave parsing to Simulator.

Fix bug with inverted ordering of events firing in Experiment.

Minor changes.

Minor logging changes.

Use AccessOperations?.convert in NetLoadSaveLogic?

NetLoadSaveLogic? now encloses the conversion.

Use more generic AccessOperations? saveAll and loadAll in PrimePackage?.

Add Result class for enclosing of call invocations' results.

Improve feature request handling in Connections.

Use AccessOperations?.convert in RemoteTree? events parsing.

Minor change.

Add some information params to Java server root and CLI objects.

A draft implementation of loadAll algorithm.

That algorithm tries to load objects into a tree structure.

Add AccessOperationsTest? test.

Develop WorkPackageLogic?.

  • add state tracking fields
  • add work package generation

Add utility class SimplePrimitive?.

Meant for Java backend classes, enclose a single primitive value
and set of listeners.

Improve primitive value refresh in GUI.

When ValueChange? found in called event, do not reload whole
object, but only update GUI (no communication is performed).

Use ValueChange? in the TestClass? test.

Minor changes.

Sending all packages in PrimeExperiment? to the frams servers.

Develop AccessOperations?.loadComposites().

Remove addAccess from MultiParamLoader? interface.

There is now no default AccessProvider? in MultiParamLoader?.
User must explicitely set AccessStash? or Registry.

Improve saving algorithms in AccessOperations?.

File size: 3.6 KB
Line 
1package com.framsticks.communication;
2
3import com.framsticks.communication.queries.*;
4import com.framsticks.util.FramsticksException;
5import com.framsticks.util.lang.Holder;
6import com.framsticks.util.lang.Pair;
7import com.framsticks.util.lang.Strings;
8
9import org.apache.logging.log4j.Logger;
10import org.apache.logging.log4j.LogManager;
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 = LogManager.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
35        @Override
36        protected void receiverThreadRoutine() {
37
38                processInputBatchesUntilClosed();
39        }
40
41        protected void handleRequest(Request request, ServerSideResponseFuture responseCallback) {
42                if (request instanceof ApplicationRequest) {
43                        requestHandler.handle((ApplicationRequest) request, responseCallback);
44                        return;
45                }
46                if (request instanceof ProtocolRequest) {
47                        if (request instanceof VersionRequest) {
48                                responseCallback.pass(new Response(true, null, null));
49                                return;
50                        }
51                        if (request instanceof UseRequest) {
52                                String feature = ((UseRequest)request).getFeature();
53                                if (feature.equals("request_id")) {
54                                        requestIdEnabled = true;
55                                        responseCallback.pass(new Response(true, null, null));
56                                        return;
57                                }
58                                if (feature.equals("call_empty_result")) {
59                                        responseCallback.pass(new Response(true, null, null));
60                                        return;
61                                }
62                                if (feature.equals("needfile_id")) {
63                                        responseCallback.pass(new Response(true, null, null));
64                                        return;
65                                }
66                                responseCallback.pass(new Response(false, "unknown feature: " + feature, null));
67                                return;
68                        }
69
70                }
71                log.error("unhandled request: {}", request);
72                responseCallback.pass(new Response(false, "unhandled", null));
73        }
74
75
76
77        protected final void respond(final Response response, final Integer id) {
78                senderThread.dispatch(new RunAt<Connection>(requestHandler) {
79                        @Override
80                        protected void runAt() {
81                                if (response.getFiles() != null) {
82                                        for (File f : response.getFiles()) {
83                                                putFile(f, id);
84                                        }
85                                }
86                                StringBuilder statusLine = new StringBuilder();
87                                statusLine.append(response.getOk() ? "ok" : "error").append(idToString(id));
88                                if (Strings.notEmpty(response.getComment())) {
89                                        Request.quoteValue(statusLine.append(" "), response.getComment());
90                                }
91                                putLine(statusLine.toString());
92                                flushOut();
93                        }
94                });
95
96        }
97
98
99        protected void processNextInputBatch() {
100                final Holder<Integer> id = new Holder<>();
101                final String line = getLine();
102                try {
103                        Pair<CharSequence, CharSequence> command = Request.takeIdentifier(line);
104                        final Pair<Integer, CharSequence> rest = takeRequestId(command.second);
105                        id.set(rest.first);
106
107                        final Request request = Request.parse(command.first, rest.second);
108
109                        if (log.isTraceEnabled()) {
110                                log.trace("read request: {}", request);
111                        }
112
113                        handleRequest(request, new ServerSideResponseFuture() {
114                                @Override
115                                protected void result(Response response) {
116                                        respond(response, rest.first);
117                                }
118                        });
119                } catch (FramsticksException e) {
120                        e.arg("id", id.get()).arg("line", line);
121                        log.error("error: ", e);
122                        respond(new Response(false, "invalid input: " + e.getMsg(), null), id.get());
123                        return;
124                }
125
126        }
127}
Note: See TracBrowser for help on using the repository browser.