source: java/main/src/main/java/com/framsticks/remote/SimulatorInstance.java @ 96

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

File size: 3.3 KB
Line 
1package com.framsticks.remote;
2
3import java.util.List;
4
5import org.apache.log4j.Logger;
6
7import static com.framsticks.core.InstanceUtils.*;
8import com.framsticks.communication.EventCallback;
9import com.framsticks.communication.File;
10import com.framsticks.communication.util.LoggingSubscriptionCallback;
11import com.framsticks.core.Instance;
12import com.framsticks.core.InstanceUtils;
13import com.framsticks.core.Path;
14import com.framsticks.params.annotations.FramsClassAnnotation;
15import com.framsticks.params.types.EventParam;
16import com.framsticks.util.Logging;
17import com.framsticks.util.PeriodicTask;
18import com.framsticks.util.UnaryFunctor;
19import com.framsticks.util.UnaryListenersSet;
20import com.framsticks.util.dispatching.Future;
21import com.framsticks.util.dispatching.RunAt;
22
23@FramsClassAnnotation
24public class SimulatorInstance extends RemoteInstance {
25
26        private final static Logger log = Logger.getLogger(SimulatorInstance.class);
27
28        protected Path simulator;
29
30        /**
31         *
32         */
33        public SimulatorInstance() {
34                super();
35        }
36
37        public void setRunning(final boolean running) {
38                assert isActive();
39                //simulator.call(simulator.getParam(running ? "start" : "stop", ProcedureParam.class), new LoggingStateCallback(log, (running ? "starting" : "stopping") + " server"));
40        }
41
42        protected final UnaryListenersSet<Boolean> simulationRunningListeners = new UnaryListenersSet<Boolean>();
43
44        protected void updateSimulationRunning() {
45                assert isActive();
46                /*
47                fetchValue(simulator, getParam(simulator, "running", Param.class), new StateFunctor() {
48                        @Override
49                        public void call(Exception e) {
50                                if (e != null) {
51                                        log.fatal("failed to query simulator running status: " + e);
52                                        return;
53                                }
54
55                                invokeLater(new Runnable() {
56                                        @Override
57                                        public void run() {
58                                                boolean value = bindAccess(simulator).get("running", Boolean.class);
59                                                log.trace("server running: " + value);
60                                                simulationRunningListeners.call(value);
61                                        }
62                                });
63
64                        }
65                });
66                 */
67        }
68
69        public void addRunningStateListener(UnaryFunctor<Boolean, Boolean> listener) {
70                assert isActive();
71                simulationRunningListeners.add(listener);
72        }
73        protected void onProtocolVersionNegotiated() {
74                dispatch(new RunAt<Instance>() {
75                        @Override
76                        public void run() {
77                                resolveAndFetch(SimulatorInstance.this, "/simulator", new Future<Path>(Logging.logger(log, "failed to resolve simulator node", SimulatorInstance.this)) {
78                                        @Override
79                                        protected void result(Path path) {
80                                                assert isActive();
81                                                simulator = path;
82                                                fireRun(null);
83                                                log.info("resolved simulator node");
84
85                                                EventParam param = InstanceUtils.getInfoFromCache(simulator).getParamEntry("running_changed", EventParam.class);
86
87                                                assert param != null;
88                                                connection.subscribe(simulator.getTextual() + "/" + param.getId(), SimulatorInstance.this, new LoggingSubscriptionCallback<Instance>(log, "server running state change", new EventCallback() {
89                                                        @Override
90                                                        public void call(List<File> files) {
91                                                                dispatch(new RunAt<Instance>() {
92                                                                        @Override
93                                                                        public void run() {
94                                                                                updateSimulationRunning();
95                                                                        }
96                                                                });
97                                                        }
98                                                }));
99                                                new PeriodicTask<Instance>(SimulatorInstance.this, 1000) {
100                                                        @Override
101                                                        public void run() {
102                                                                updateSimulationRunning();
103                                                                again();
104                                                        }
105                                                };
106                                        }
107                                });
108                        }
109                });
110        }
111
112}
Note: See TracBrowser for help on using the repository browser.