source: java/main/src/main/java/com/framsticks/core/AbstractInstance.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.6 KB
Line 
1package com.framsticks.core;
2import java.util.HashSet;
3import java.util.Set;
4
5import javax.annotation.Nonnull;
6
7import org.apache.log4j.Logger;
8
9import com.framsticks.params.AccessInterface;
10import com.framsticks.params.CompositeParam;
11import com.framsticks.params.FramsClass;
12import com.framsticks.params.Param;
13import com.framsticks.params.ParamsPackage;
14import com.framsticks.params.Registry;
15import com.framsticks.params.annotations.AutoAppendAnnotation;
16import com.framsticks.params.annotations.FramsClassAnnotation;
17import com.framsticks.util.dispatching.Dispatching;
18import com.framsticks.util.dispatching.RunAt;
19import com.framsticks.util.dispatching.Thread;
20// import static com.framsticks.core.InstanceUtils.*;
21
22/**
23 * @author Piotr Sniegowski
24 */
25@FramsClassAnnotation
26public abstract class AbstractInstance extends Thread<Instance> implements Instance {
27
28        private static final Logger log = Logger.getLogger(Instance.class);
29
30        private Node root;
31
32        @Override
33        public void setRoot(Node node) {
34                root = node;
35        }
36
37        @Override
38        public @Nonnull Node getRoot() {
39                assert root != null;
40                return root;
41        }
42
43        public boolean isRootAssigned() {
44                // assert isActive();
45                return root != null;
46        }
47
48        protected Set<InstanceListener> listeners = new HashSet<InstanceListener>();
49
50        public AbstractInstance() {
51                setName("entity");
52        }
53
54        protected void tryRegisterOnChangeEvents(Path path) {
55
56        }
57
58
59        protected void fireRun(Exception e) {
60                for (InstanceListener l : this.listeners) {
61                        l.onRun(e);
62                }
63        }
64
65        protected void fireStop(Exception e) {
66                for (InstanceListener l : this.listeners) {
67                        l.onStop(e);
68                }
69        }
70
71        @Override
72        public void addListener(final InstanceListener listener) {
73                assert Dispatching.isThreadSafe();
74                Dispatching.dispatchIfNotActive(this, new RunAt<Instance>() {
75                        @Override
76                        public void run() {
77                                listeners.add(listener);
78                        }
79                });
80        }
81
82        @Override
83        public void removeListener(final InstanceListener listener) {
84                assert Dispatching.isThreadSafe();
85                Dispatching.dispatchIfNotActive(this, new RunAt<Instance>() {
86                        @Override
87                        public void run() {
88                                listeners.remove(listener);
89                        }
90                });
91        }
92
93        protected void fireListChange(Path path, ListChange change) {
94                assert isActive();
95                for (InstanceListener l : this.listeners) {
96                        l.onListChange(path, change);
97                }
98        }
99
100        @Override
101        public void notifyOfFetch(Path path) {
102                fireFetch(path);
103        }
104
105        protected void fireFetch(Path path) {
106                assert isActive();
107                for (InstanceListener l : this.listeners) {
108                        l.onFetch(path);
109                }
110        }
111
112
113        @Override
114        public final FramsClass getInfoFromCache(String id) {
115                assert isActive();
116                return registry.getFramsClass(id);
117        }
118
119        protected Registry registry = new Registry();
120
121
122        @Override
123        public @Nonnull AccessInterface prepareAccess(CompositeParam param) {
124                return registry.prepareAccess(param);
125        }
126
127        @Override
128        public void takeAllFrom(Registry source) {
129                registry.takeAllFrom(source);
130        }
131
132        @AutoAppendAnnotation
133        public void usePackage(ParamsPackage paramsPackage) {
134                log.debug("using package " + paramsPackage + " in instance " + this);
135                paramsPackage.register(registry);
136        }
137
138        @AutoAppendAnnotation
139        public void takeFromRegistry(Registry registry) {
140                log.debug("taking from registry " + registry + " in instance " + this);
141                this.registry.takeAllFrom(registry);
142        }
143
144        @Override
145        protected void joinableStart() {
146                dispatch(new RunAt<Instance>() {
147                        @Override
148                        public void run() {
149                                if (!isRootAssigned()) {
150                                        setRoot(new Node(Param.build().name("Instance").id(getName()).type("o").finish(CompositeParam.class), null));
151                                }
152                        }
153                });
154                super.joinableStart();
155        }
156
157        @Override
158        public void putInfoIntoCache(FramsClass framclass) {
159                registry.putFramsClass(framclass);
160        }
161}
162
Note: See TracBrowser for help on using the repository browser.