source: java/main/src/main/java/com/framsticks/hosting/ServerInstance.java @ 86

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

HIGHLIGHTS:

  • use java annotations to mark classes and fields to be used when:
    • using java classes with ReflectionAccess? to represent remote objects with FramsClass? description found by "info ..." requests
    • to build up FramsClass? representation of objects not present at remote server
  • allow using primitive types (instead of wraping counterparts) in reflected classes
  • rework FramsClass? creation process (add FramsClassBuilder?)
  • add more tests

CHANGELOG:
Prepare model.World class.

Minor change.

Use primitive types for Genotype and Creature classes.

Use primitive types in model.Neuro* classes.

Use primitive types in model.Joint* classes.

Use primitive types in model.Part* classes.

Fix primitive values.

Extract FramsClassBuilder?.

Add tests of Model classes.

More fixes.

Refactorize out ParamCandidate?.

Several fixes.

Fix all regressions after introducing annotations.

Use annotations throughout the project.

Add exception classes.

Improve creation of FramsClass?.

More changes.

Many changes regarding annotations.

Annotate classes in com.framsticks.model package.

Remove manual FramsClass? constructor.

Construct FramsClass? for Creature. Add test.

Add default values to the ParamAnnotation?.

Add ParamBuilderTest? and ParamAnnotation?.

Add FramsClassAnnotation?.

File size: 2.4 KB
Line 
1package com.framsticks.hosting;
2
3import com.framsticks.core.*;
4import com.framsticks.params.CompositeParam;
5import com.framsticks.params.ConstructionException;
6import com.framsticks.params.FramsClass;
7import com.framsticks.params.FramsClassBuilder;
8import com.framsticks.params.Param;
9import com.framsticks.core.LocalInstance;
10import com.framsticks.util.dispatching.Future;
11
12import org.apache.commons.configuration.Configuration;
13import org.apache.log4j.Logger;
14
15/**
16 * @author Piotr Sniegowski
17 */
18public class ServerInstance extends LocalInstance {
19
20        private final static Logger log = Logger.getLogger(ServerInstance.class.getName());
21
22        Entity hosted;
23
24        public ServerInstance() {
25        }
26
27        @Override
28        protected void run() {
29                super.run();
30                assert hosted != null;
31                hosted.start();
32        }
33
34        @Override
35        public void configure(Configuration config) {
36                super.configure(config);
37
38                Configuration hostedConfig = config.subset("hosted.entity");
39                hosted = Program.configureEntity(hostedConfig);
40                if (hosted == null) {
41                        log.fatal("failed to create hosted entity");
42                        return;
43                }
44                hosted.setName("hosted");
45                hosted.configure(hostedConfig);
46                root = new Node((CompositeParam) Param.build().name("root").id("root").type("o" + hosted.getClass().getCanonicalName()).finish(), hosted);
47        }
48
49        @Override
50        public FramsClass getInfoFromCache(String id) {
51                assert isActive();
52                if (id == null) {
53                        return null;
54                }
55                FramsClass cached = registry.getInfoFromCache(id);
56                if (cached != null) {
57                        return cached;
58                }
59                try {
60                        Class<?> nativeClass = Class.forName(id);
61                        FramsClass framsClass = FramsClassBuilder.buildForClass(nativeClass);
62
63                        if (!framsClass.getId().equals(id)) {
64                                log.error("no matching id");
65                                return null;
66                        }
67
68                        registry.registerReflectedClass(null, id, nativeClass);
69                        registry.putInfoIntoCache(framsClass);
70                        return framsClass;
71                } catch (ClassNotFoundException ignored) {
72                } catch (ConstructionException e) {
73                        log.error("failed to use info from cache: " + e);
74                }
75
76                return null;
77        }
78
79        @Override
80        protected void fetchInfo(Path path, Future<FramsClass> future) {
81                assert isActive();
82
83                FramsClass framsClass = getInfoFromCache(path.getTop().getObject().getClass().getCanonicalName());
84                if (framsClass == null) {
85                        log.error("failed to create frams class for: " + path.getTop().getObject().getClass());
86                        future.result(null, new Exception());
87                        return;
88                }
89                future.result(framsClass, null);
90
91        }
92
93        @Override
94        protected void tryRegisterOnChangeEvents(Path path) {
95        }
96
97}
Note: See TracBrowser for help on using the repository browser.