source: java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.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: 7.2 KB
Line 
1package com.framsticks.params;
2
3import java.io.IOException;
4import java.util.Collection;
5
6import static com.framsticks.util.lang.Containers.filterInstanceof;
7
8import org.apache.log4j.Logger;
9
10import com.framsticks.util.UnimplementedException;
11
12/**
13 * The Class SimpleAbstractAccess implements all the methods of AccessInterface
14 * which actions can be implemented with usage of {@link AccessInterface} methods
15 * or concern schema, which is stored in {@link #framsClass}
16 *
17 * Based on c++ class SimpleAbstractParam located in: cpp/gdk/param.*
18 *
19 * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please
20 *         replace name and surname with my personal data)
21 *
22 * @author Piotr Sniegowski
23 */
24public abstract class SimpleAbstractAccess implements AccessInterface {
25
26        private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName());
27
28        protected final FramsClass framsClass;
29
30        /**
31         * @param framsClass
32         */
33        public SimpleAbstractAccess(FramsClass framsClass) {
34                this.framsClass = framsClass;
35        }
36
37        @Override
38        public final FramsClass getFramsClass() {
39                return framsClass;
40        }
41
42        /**
43         * Simple String key, value class.
44         */
45        public static class Entry {
46
47                public final String key;
48                public final String value;
49
50                public Entry(String key, String value) {
51                        this.key = key;
52                        this.value = value;
53                }
54
55                @Override
56                public String toString() {
57                        return key + " = " + value;
58                }
59        }
60
61
62        @Override
63        public String getId() {
64                return framsClass.getId();
65        }
66
67        @Override
68        public int getParamCount() {
69                return framsClass.getParamCount();
70        }
71
72        @Override
73        public Param getParam(int i) {
74                return framsClass.getParam(i);
75        }
76
77        @Override
78        public Param getParam(String id) {
79                return framsClass.getParam(id);
80        }
81
82        // @Override
83        // public Param getGroupMember(int gi, int n) {
84        //      return framsClass.getGroupMember(gi, n);
85        // }
86
87        @Override
88        public <T> T get(int i, Class<T> type) {
89                return get(framsClass.getParamEntry(i, ValueParam.class), type);
90        }
91
92        @Override
93        public <T> T get(String id, Class<T> type) {
94                return get(framsClass.getParamEntry(id, ValueParam.class), type);
95        }
96
97        @Override
98        public <T> int set(int i, T value) {
99                return set(framsClass.getParamEntry(i, ValueParam.class), value);
100        }
101
102        @Override
103        public <T> int set(String id, T value) {
104                return set(framsClass.getParamEntry(id, ValueParam.class), value);
105        }
106
107        @Override
108        public <T> int set(ValueParam param, T value) {
109                int flags = 0;
110
111                //String id = param.getEffectiveId();
112                try {
113                        Object oldValue = get(param, param.getStorageType());
114                        ReassignResult<?> result = param.reassign(value, oldValue);
115                        Object casted = result.getValue();
116                        if (!casted.equals(oldValue)) {
117                                internalSet(param, casted);
118                        }
119                        flags = result.getFlags();
120                } catch (CastFailure e) {
121                        log.error("casting failure while set: ", e);
122                }
123                return flags;
124        }
125
126        @Override
127        public void setDefault(boolean numericOnly) {
128                for (int i = 0; i < framsClass.getParamCount(); i++) {
129                        setDefault(i, numericOnly);
130                }
131        }
132
133        @Override
134        public void setDefault(int i, boolean numericOnly) {
135                ValueParam entry = framsClass.getParamEntry(i, ValueParam.class);
136                if ((entry != null)     && (!numericOnly || entry.isNumeric())) {
137                        set(i, entry.getDef(entry.getStorageType()));
138                }
139        }
140
141        @Override
142        public void setMin() {
143                for (int i = 0; i < framsClass.getParamCount(); i++) {
144                        setMin(i);
145                }
146        }
147
148        @Override
149        public void setMin(int i) {
150                PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class);
151                if (entry == null) {
152                        return;
153                }
154                Object min = entry.getMin(entry.getStorageType());
155                if (min != null) {
156                        set(i, min);
157                }
158        }
159
160        @Override
161        public void setMax() {
162                for (int i = 0; i < framsClass.getParamCount(); i++) {
163                        setMax(i);
164                }
165        }
166
167        @Override
168        public void setMax(int i) {
169                PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class);
170                if (entry == null) {
171                        return;
172                }
173                Object max = entry.getMax(entry.getStorageType());
174                if (max != null) {
175                        set(i, max);
176                }
177        }
178
179        @Override
180        public void copyFrom(AccessInterface src) {
181                throw new UnimplementedException();
182                // clearValues();
183                //TODO: iterate over self, and pull from src
184                /*
185                for (int i = 0; i < src.getFramsClass().size(); i++) {
186                        this.set(i, src.get(i, Object.class));
187                }
188                */
189        }
190
191        @Override
192        public void save(SinkInterface sink) {
193                assert framsClass != null;
194                sink.print(framsClass.getId()).print(":").breakLine();
195                for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {
196                        Object value = get(p, Object.class);
197                        if ((value == null) || value.equals(p.getDef(Object.class))) {
198                                continue;
199                        }
200                        sink.print(p.getId()).print(":");
201                        p.save(sink, value);
202                        sink.breakLine();
203                }
204                sink.breakLine();
205        }
206
207        private Entry readEntry(SourceInterface source)
208                        throws IOException {
209
210                String line;
211                String key = null;
212                StringBuilder value = null;
213                while ((line = source.readLine()) != null)
214                {
215                        if (key == null) {
216                                int colonIndex = line.indexOf(':');
217                                if (colonIndex == -1) {
218                                        return null;
219                                }
220                                key = line.substring(0, colonIndex);
221                                String inlineValue = line.substring(colonIndex + 1);
222
223
224                                if (!inlineValue.startsWith("~")) {
225                                        return new Entry(key, inlineValue);
226                                }
227                                value = new StringBuilder();
228                                value.append(inlineValue.substring(1));
229                                continue;
230                        }
231                        if (value.length() != 0) {
232                                value.append(System.getProperty("line.separator"));
233                        }
234                        if (line.contains("~")) {
235                                value.append(line.substring(0, line.indexOf("~")));
236                                return new Entry(key, value.toString());
237                        }
238                        value.append(line);
239                        /*
240                        if (line.contains("~")) {
241                                String lastLine = line.substring(0, line.indexOf("~"));
242                                if (lastLine.length() > 0) {
243                                        appendToValue(value, lastLine);
244                                }
245                                return new Entry(key, value.toString());
246                        }
247                        appendToValue(value, line);
248                        */
249                }
250                return null;
251        }
252
253        @Override
254        public void load(SourceInterface source) throws Exception {
255                //TODO not clearing values, because get from manager gives only fields, not children
256                //this.clearValues();
257
258                Entry entry;
259                while ((entry = readEntry(source)) != null) {
260                        Param param = getParam(entry.key);
261                        if (param == null) {
262                                continue;
263                        }
264                        if (!(param instanceof ValueParam)) {
265                                log.warn("param " + param + " is not a ValueParam");
266                                continue;
267                        }
268                        if ((param.getFlags() & Flags.DONTLOAD) != 0) {
269                                log.debug("DontLoad flag was set - not loading...");
270                        } else {
271                                int retFlags = this.set((ValueParam) param, entry.value);
272                                if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
273                                        String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big";
274                                        log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted");
275                                }
276                        }
277                }
278        }
279
280        protected abstract <T> void internalSet(ValueParam param, T value);
281
282        @Override
283        public Collection<Param> getParams() {
284                return framsClass.getParamEntries();
285        }
286
287        /*
288        protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) {
289                int flags = 0;
290                T val = type.cast(value);
291                T min = param.getMin(type);
292                T max = param.getMax(type);
293                if (min != null && val.compareTo(min) < 0) {
294                        val = min;
295                        flags |= Flags.PSET_HITMIN;
296                }
297                if (max != null && val.compareTo(max) > 0) {
298                        val = max;
299                        flags |= Flags.PSET_HITMAX;
300                }
301                internalSet(param, val);
302                return flags;
303        }*/
304
305
306}
Note: See TracBrowser for help on using the repository browser.