Changeset 84 for java/main/src/main/java/com/framsticks/core
- Timestamp:
- 06/22/13 21:51:33 (11 years ago)
- Location:
- java/main
- Files:
-
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main
-
Property
svn:ignore
set to
target
-
Property
svn:ignore
set to
-
java/main/src/main/java/com/framsticks/core/Entity.java
r78 r84 1 1 package com.framsticks.core; 2 2 3 import com.framsticks.util.*; 4 import com.framsticks.util.Thread; 3 import com.framsticks.params.FramsClass; 4 import com.framsticks.util.dispatching.Thread; 5 import com.framsticks.util.dispatching.Dispatcher; 6 7 import org.apache.commons.configuration.Configuration; 5 8 import org.apache.log4j.Logger; 6 9 … … 8 11 * @author Piotr Sniegowski 9 12 */ 10 public abstract class Entity extends Parametersimplements Dispatcher {13 public abstract class Entity implements Dispatcher { 11 14 12 private final static Logger LOGGER= Logger.getLogger(Entity.class.getName());15 private final static Logger log = Logger.getLogger(Entity.class.getName()); 13 16 14 public Entity(Parameters parameters) { 15 super(parameters); 16 if (dispatcher == null) { 17 dispatcher = new Thread(name); 18 } 19 } 17 protected String name = "entity"; 18 protected EntityOwner owner; 19 protected Dispatcher dispatcher; 20 20 21 @Override 22 public final boolean isActive() { 23 return dispatcher.isActive(); 24 } 21 public Entity() { 22 } 25 23 26 @Override 27 public final void invokeLater(Runnable runnable) { 28 dispatcher.invokeLater(runnable); 29 } 24 public final String getName() { 25 return name; 26 } 30 27 31 protected void run() { 32 assert isActive(); 33 LOGGER.info("running: " + this); 34 } 28 /** 29 * @param name the name to set 30 */ 31 public final void setName(String name) { 32 this.name = name; 33 if (dispatcher instanceof Thread) { 34 ((Thread) dispatcher).setName(name); 35 } 36 } 35 37 36 public final void configurePublic() throws Exception { 37 configure(); 38 } 38 /** 39 * @return the owner 40 */ 41 public EntityOwner getOwner() { 42 return owner; 43 } 39 44 40 protected void configure() throws Exception { 45 /** 46 * @param owner the owner to set 47 */ 48 public void setOwner(EntityOwner owner) { 49 this.owner = owner; 50 } 41 51 42 } 52 @Override 53 public final boolean isActive() { 54 return dispatcher == null || dispatcher.isActive(); 55 } 43 56 44 public Dispatcher getDispatcher() { 45 return dispatcher; 46 } 57 @Override 58 public final void invokeLater(Runnable runnable) { 59 assert dispatcher != null; 60 dispatcher.invokeLater(runnable); 61 } 47 62 48 public final void start() { 49 invokeLater(new Runnable() { 50 @Override 51 public void run() { 52 Entity.this.run(); 53 } 54 }); 55 } 63 public Dispatcher createDefaultDispatcher() { 64 return new Thread(name); 65 } 66 67 protected void run() { 68 assert isActive(); 69 log.info("running: " + this); 70 } 71 72 73 public void configure(Configuration config) { 74 } 75 76 public Dispatcher getDispatcher() { 77 return dispatcher; 78 } 79 80 /** 81 * @param dispatcher the dispatcher to set 82 */ 83 public void setDispatcher(Dispatcher dispatcher) { 84 this.dispatcher = dispatcher; 85 } 86 87 public final void start() { 88 if (dispatcher == null) { 89 log.debug("no dispatcher set for " + this + ", creating default one"); 90 setDispatcher(createDefaultDispatcher()); 91 } 92 invokeLater(new Runnable() { 93 @Override 94 public void run() { 95 Entity.this.run(); 96 } 97 }); 98 } 56 99 57 100 public final void done() { 58 LOGGER.info("stopping entity");101 log.info("stopping entity"); 59 102 if (owner != null) { 60 103 owner.onDone(); … … 62 105 } 63 106 64 107 public static void constructFramsClass(FramsClass.Constructor constructor) { 108 constructor.method("getName"); 109 } 65 110 } -
java/main/src/main/java/com/framsticks/core/Instance.java
r78 r84 3 3 import com.framsticks.communication.*; 4 4 import com.framsticks.params.*; 5 import com.framsticks.params.types.CompositeParam;6 5 import com.framsticks.params.types.ObjectParam; 7 6 import com.framsticks.parsers.Loaders; … … 9 8 import com.framsticks.util.*; 10 9 import com.framsticks.util.UnsupportedOperationException; 10 import com.framsticks.util.dispatching.Dispatching; 11 import com.framsticks.util.dispatching.Future; 12 import com.framsticks.util.lang.Casting; 11 13 import org.apache.log4j.Logger; 12 14 … … 18 20 public abstract class Instance extends Entity { 19 21 20 private static final Logger LOGGER = Logger.getLogger(Instance.class.getName()); 21 22 23 protected Node root; 24 25 public Set<InstanceListener> listeners = new HashSet<InstanceListener>(); 26 27 public Instance(Parameters parameters) { 28 super(parameters); 29 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o").build(), null); 30 } 31 32 @Override 33 protected void run() { 34 super.run(); 22 private static final Logger log = Logger.getLogger(Instance.class.getName()); 23 24 25 protected Node root; 26 27 public Set<InstanceListener> listeners = new HashSet<InstanceListener>(); 28 29 public Instance() { 30 } 31 32 @Override 33 protected void run() { 34 super.run(); 35 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o").build(), null); 35 36 com.framsticks.model.Package.register(registry); 36 } 37 38 @Override 39 protected void configure() throws Exception { 40 super.configure(); 41 } 42 43 protected void fetchInfo(Path path, Future<FramsClass> future) { 37 } 38 39 protected void fetchInfo(Path path, Future<FramsClass> future) { 44 40 future.result(null, new UnsupportedOperationException()); 45 41 } … … 61 57 } 62 58 63 59 public void fetchValue(Path path, Param param, StateFunctor stateFunctor) { 64 60 stateFunctor.call(null); 65 61 } 66 62 67 63 public void fetchValues(Path path, StateFunctor stateFunctor) { 68 64 stateFunctor.call(null); 69 65 } 70 66 71 67 protected void tryRegisterOnChangeEvents(Path path) { 72 68 73 69 } … … 83 79 } 84 80 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 81 protected void fireRun(Exception e) { 82 for (InstanceListener l : this.listeners) { 83 l.onRun(e); 84 } 85 } 86 87 protected void fireStop(Exception e) { 88 for (InstanceListener l : this.listeners) { 89 l.onStop(e); 90 } 91 } 92 93 public void addListener(final InstanceListener listener) { 94 assert Dispatching.isThreadSafe(); 95 Dispatching.invokeLaterOrNow(this, new Runnable() { 96 @Override 97 public void run() { 98 listeners.add(listener); 99 } 100 }); 101 } 102 103 public void removeListener(final InstanceListener listener) { 104 assert Dispatching.isThreadSafe(); 105 Dispatching.invokeLaterOrNow(this, new Runnable() { 106 @Override 107 public void run() { 108 listeners.remove(listener); 109 } 110 }); 111 } 112 113 114 protected void fireListChange(Path path, ListChange change) { 115 assert isActive(); 116 for (InstanceListener l : this.listeners) { 121 117 l.onListChange(path, change); 122 118 } 123 119 } 124 120 125 126 public final FramsClass getInfoFromCache(Path path) { 127 return getInfoFromCache(path.getTop().getParam().getContainedTypeName()); 128 } 129 130 131 public FramsClass getInfoFromCache(String id) { 132 assert isActive(); 121 protected void fireFetch(Path path) { 122 assert isActive(); 123 for (InstanceListener l : this.listeners) { 124 l.onFetch(path); 125 } 126 } 127 128 129 public final FramsClass getInfoFromCache(Path path) { 130 return getInfoFromCache(path.getTop().getParam().getContainedTypeName()); 131 } 132 133 134 public FramsClass getInfoFromCache(String id) { 135 assert isActive(); 133 136 return registry.getInfoFromCache(id); 134 137 } … … 137 140 138 141 public AccessInterface createAccess(String name) { 139 assert isActive(); 140 if (name == null) { 142 assert isActive(); 143 return registry.createAccess(name); 144 } 145 146 147 // TODO: make ValueParam 148 public <T> T get(Node node, Param childParam, Class<T> type) { 149 return bindAccess(node).get((ValueParam) childParam, type); 150 } 151 152 public void findInfo(final Path path, final Future<FramsClass> future) { 153 assert isActive(); 154 final String name = path.getTop().getParam().getContainedTypeName(); 155 final FramsClass framsClass = getInfoFromCache(name); 156 if (framsClass != null) { 157 log.trace("info for " + name + " found in cache"); 158 future.result(framsClass, null); 159 return; 160 } 161 fetchInfo(path, future); 162 } 163 164 public final AccessInterface bindAccess(Node node) { 165 assert node.getObject() != null; 166 AccessInterface access = registry.prepareAccess(node.getParam()); 167 if (access == null) { 168 log.error("missing access for: " + node.getParam()); 141 169 return null; 142 170 } 143 FramsClass framsClass = getInfoFromCache(name); 144 if (framsClass == null) { 145 return null; 146 } 147 148 return registry.createAccess(name, framsClass); 149 } 150 151 public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) { 152 if (access == null) { 153 return null; 154 } 155 return param.prepareAccessInterface(access); 156 } 157 158 public AccessInterface prepareAccess(CompositeParam param) { 159 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName())); 160 } 161 162 public <T> T get(Node node, Param childParam, Class<T> type) { 163 return bindAccess(node).get(childParam, type); 164 } 165 166 public void findInfo(final Path path, final Future<FramsClass> future) { 167 assert isActive(); 168 final String name = path.getTop().getParam().getContainedTypeName(); 169 final FramsClass framsClass = getInfoFromCache(name); 170 if (framsClass != null) { 171 LOGGER.trace("info for " + name + " found in cache"); 172 future.result(framsClass, null); 173 return; 174 } 175 fetchInfo(path, future); 176 } 177 178 public final AccessInterface bindAccess(Node node) { 179 assert node.getObject() != null; 180 AccessInterface access = prepareAccess(node.getParam()); 181 if (access == null) { 182 LOGGER.error("missing access for: " + node.getParam()); 183 return null; 184 } 185 access.select(node.getObject()); 186 return access; 187 } 188 189 public final <T> T getParam(Path path, String id, Class<T> type) { 190 return Casting.tryCast(type, prepareAccess(path.getTop().getParam()).getParam(id)); 191 } 192 193 public final AccessInterface bindAccess(Path path) { 194 assert path.isResolved(); 195 return bindAccess(path.getTop()); 196 } 197 198 public void resolve(final String targetPath, final Future<Path> future) { 199 assert isActive(); 200 final Path path = new Path(this, targetPath); 201 resolve(path, new Future<Path>() { 202 @Override 203 public void result(Path result, Exception e) { 204 assert isActive(); 205 if (e != null) { 206 future.result(path, e); 207 return; 208 } 209 if (path.isResolved(targetPath)) { 210 future.result(path, null); 211 return; 212 } 213 if (path.isResolved()) { 214 future.result(path, new Exception("testing")); 215 return; 216 } 217 resolve(targetPath, future); 218 } 219 }); 220 } 221 222 public void resolveAndFetch(final String targetPath, final Future<Path> future) { 223 assert isActive(); 224 resolve(targetPath, new Future<Path>() { 225 @Override 226 public void result(final Path path, Exception e) { 227 if (e != null) { 228 future.result(path, e); 229 return; 230 } 231 assert path.isResolved(targetPath); 232 fetchValues(path, new StateFunctor() { 233 @Override 234 public void call(Exception e) { 235 future.result(path, e); 236 } 237 }); 238 } 239 }); 240 } 241 242 public Path createIfNeeded(String path) { 243 Path p; 244 while (!(p = new Path(this, path)).isResolved(path)) { 245 create(p); 246 } 247 return p; 248 } 249 250 public Path create(Path path) { 251 assert isActive(); 252 assert !path.isResolved(); 253 Path resolved = path.findResolution(); 254 if (!resolved.isResolved()) { 255 LOGGER.debug("creating: " + path); 256 AccessInterface access = prepareAccess(path.getTop().getParam()); 257 assert access != null; 258 Object child = access.createAccessee(); 259 assert child != null; 260 if (path.size() == 1) { 261 root = new Node(root.getParam(), child); 262 } else { 263 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); 264 } 265 resolved = path.appendResolution(child); 266 } 267 tryRegisterOnChangeEvents(resolved); 268 return resolved; 269 } 270 271 272 273 274 public FramsClass processFetchedInfo(File file) { 275 assert isActive(); 276 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 277 if ("/".equals(file.getPath())) { 278 if (root.getParam().getContainedTypeName() == null) { 279 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o " + framsClass.getId()).build(), root.getObject()); 280 } 281 } 282 registry.putInfoIntoCache(framsClass); 283 return framsClass; 284 } 285 286 public void processFetchedValues(Path path, List<File> files) { 287 assert isActive(); 288 assert files.size() == 1; 289 assert path.isTheSame(files.get(0).getPath()); 290 Node node = path.getTop(); 291 MultiParamLoader loader = new MultiParamLoader(); 292 loader.setNewSource(files.get(0).getContent()); 293 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 294 295 try { 296 if (node.getParam() instanceof ObjectParam) { 297 loader.addAccessInterface(bindAccess(node)); 298 loader.go(); 299 // for (NodeListener l : listeners) { 300 // l.onChange(this); 301 // } 302 return; 303 } 304 305 ListAccess listAccess = ((ListAccess)bindAccess(node)); 306 assert listAccess != null; 307 listAccess.clearValues(); 308 309 AccessInterface elementAccess = listAccess.getElementAccess(); 310 loader.addAccessInterface(elementAccess); 311 MultiParamLoader.Status status; 312 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 313 if (status == MultiParamLoader.Status.AfterObject) { 314 AccessInterface accessInterface = loader.getLastAccessInterface(); 315 316 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 317 Param param = new ParamBuilder().setType("o " + accessInterface.getId()).setId(id).build(); 318 Object child = accessInterface.getSelected(); 319 accessInterface.select(null); 320 assert child != null; 321 bindAccess(node).set(param, child); 322 } 323 } 324 // for (NodeListener l : listeners) { 325 // l.onChange(this); 326 // } 327 } catch (Exception e) { 328 LOGGER.error("exception occurred while loading: " + e); 329 } 330 331 } 332 333 public static Iterator<String> splitPath(String path) { 334 List<String> list = new LinkedList<String>(); 335 for (String s : path.split("/")) { 336 if (!s.isEmpty()) { 337 list.add(s); 338 } 339 } 340 return list.iterator(); 341 } 171 access.select(node.getObject()); 172 return access; 173 } 174 175 public final <T> T getParam(Path path, String id, Class<T> type) { 176 return Casting.tryCast(type, registry.prepareAccess(path.getTop().getParam()).getParam(id)); 177 } 178 179 public final AccessInterface bindAccess(Path path) { 180 assert path.isResolved(); 181 return bindAccess(path.getTop()); 182 } 183 184 public void resolve(final String targetPath, final Future<Path> future) { 185 assert isActive(); 186 final Path path = getPath(targetPath); 187 resolve(path, new Future<Path>() { 188 @Override 189 public void result(Path result, Exception e) { 190 assert isActive(); 191 if (e != null) { 192 future.result(path, e); 193 return; 194 } 195 if (path.isResolved(targetPath)) { 196 future.result(path, null); 197 return; 198 } 199 if (path.isResolved()) { 200 future.result(path, new Exception("testing")); 201 return; 202 } 203 resolve(targetPath, future); 204 } 205 }); 206 } 207 208 public void resolveAndFetch(final String targetPath, final Future<Path> future) { 209 assert isActive(); 210 resolve(targetPath, new Future<Path>() { 211 @Override 212 public void result(final Path path, Exception e) { 213 if (e != null) { 214 future.result(path, e); 215 return; 216 } 217 assert path.isResolved(targetPath); 218 fetchValues(path, new StateFunctor() { 219 @Override 220 public void call(Exception e) { 221 future.result(path, e); 222 } 223 }); 224 } 225 }); 226 } 227 228 public Path createIfNeeded(String path) { 229 Path p; 230 while (!(p = getPath(path)).isResolved(path)) { 231 create(p); 232 } 233 return p; 234 } 235 236 public Path createIfNeeded(Path path) { 237 assert isActive(); 238 if (path.isResolved()) { 239 return path; 240 } 241 return create(path); 242 } 243 244 public Path create(Path path) { 245 assert isActive(); 246 assert !path.isResolved(); 247 Path resolved = path.tryFindResolution(); 248 if (!resolved.isResolved()) { 249 log.debug("creating: " + path); 250 AccessInterface access = registry.prepareAccess(path.getTop().getParam()); 251 assert access != null; 252 Object child = access.createAccessee(); 253 assert child != null; 254 if (path.size() == 1) { 255 root = new Node(root.getParam(), child); 256 } else { 257 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); 258 } 259 resolved = path.appendResolution(child); 260 } 261 tryRegisterOnChangeEvents(resolved); 262 return resolved; 263 } 264 265 266 267 268 public FramsClass processFetchedInfo(File file) { 269 assert isActive(); 270 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 271 if ("/".equals(file.getPath())) { 272 if (root.getParam().getContainedTypeName() == null) { 273 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o " + framsClass.getId()).build(), root.getObject()); 274 } 275 } 276 registry.putInfoIntoCache(framsClass); 277 return framsClass; 278 } 279 280 public void processFetchedValues(Path path, List<File> files) { 281 assert isActive(); 282 assert files.size() == 1; 283 assert path.isTheSame(files.get(0).getPath()); 284 Node node = path.getTop(); 285 MultiParamLoader loader = new MultiParamLoader(); 286 loader.setNewSource(files.get(0).getContent()); 287 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 288 289 try { 290 if (node.getParam() instanceof ObjectParam) { 291 loader.addAccessInterface(bindAccess(node)); 292 loader.go(); 293 fireFetch(path); 294 // for (NodeListener l : listeners) { 295 // l.onChange(this); 296 // } 297 return; 298 } 299 300 ListAccess listAccess = ((ListAccess)bindAccess(node)); 301 assert listAccess != null; 302 listAccess.clearValues(); 303 304 AccessInterface elementAccess = listAccess.getElementAccess(); 305 loader.addAccessInterface(elementAccess); 306 MultiParamLoader.Status status; 307 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 308 if (status == MultiParamLoader.Status.AfterObject) { 309 AccessInterface accessInterface = loader.getLastAccessInterface(); 310 311 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 312 Param param = new ParamBuilder().setType("o " + accessInterface.getId()).setId(id).build(); 313 Object child = accessInterface.getSelected(); 314 accessInterface.select(null); 315 assert child != null; 316 bindAccess(node).set((ValueParam) param, child); 317 } 318 } 319 320 fireFetch(path); 321 // for (NodeListener l : listeners) { 322 // l.onChange(this); 323 // } 324 } catch (Exception e) { 325 log.error("exception occurred while loading: " + e); 326 } 327 328 } 329 330 public static Iterator<String> splitPath(String path) { 331 List<String> list = new LinkedList<String>(); 332 for (String s : path.split("/")) { 333 if (!s.isEmpty()) { 334 list.add(s); 335 } 336 } 337 return list.iterator(); 338 } 342 339 343 340 public Registry getRegistry() { 344 341 return registry; 345 342 } 343 344 public Path getPath(String textual) { 345 return new Path(this, textual); 346 } 347 348 public Path getRootPath() { 349 return getPath("/"); 350 } 346 351 } 347 352 -
java/main/src/main/java/com/framsticks/core/InstanceListener.java
r77 r84 5 5 */ 6 6 public interface InstanceListener { 7 public void onListChange(Path path, ListChange change); 8 public void onRun(Exception e); 9 public void onStop(Exception e); 7 public void onListChange(Path path, ListChange change); 8 public void onFetch(Path path); 9 public void onRun(Exception e); 10 public void onStop(Exception e); 10 11 } -
java/main/src/main/java/com/framsticks/core/ListChange.java
r77 r84 2 2 3 3 import com.framsticks.params.FramsClass; 4 import com.framsticks.params.ListSource;5 4 import com.framsticks.params.ParamBuilder; 6 5 import com.framsticks.params.types.DecimalParam; 7 6 import com.framsticks.params.types.EnumParam; 8 7 import com.framsticks.params.types.StringParam; 9 import com.framsticks.util. Strings;8 import com.framsticks.util.lang.Strings; 10 9 11 import java.util.ArrayList;12 10 import java.util.Arrays; 13 11 … … 17 15 public class ListChange { 18 16 19 20 21 17 public Action getAction() { 18 return action; 19 } 22 20 23 24 25 21 public Integer getPosition() { 22 return position; 23 } 26 24 27 28 29 25 public String getIdentifier() { 26 return identifier; 27 } 30 28 31 32 33 34 35 29 public static enum Action { 30 Add, 31 Remove, 32 Modify 33 } 36 34 37 38 39 35 private Action action; 36 private Integer position; 37 private String identifier; 40 38 41 42 39 public Integer getType() { return action.ordinal(); } 40 public void setType(Integer type) { action = Action.values()[type]; } 43 41 44 42 public Integer getPos() { return position; } … … 48 46 public void setId(String id) { identifier = id; } 49 47 50 51 52 53 54 55 48 public String getBestIdentifier() { 49 if (Strings.notEmpty(identifier)) { 50 return identifier; 51 } 52 return position.toString(); 53 } 56 54 57 public static FramsClass getFramsClass() { 55 public static FramsClass getFramsClass() { 56 return new FramsClass("ListChange", "ListChange", null) 57 .append(new ParamBuilder().setId("type").setName("type").setType(new EnumParam(Arrays.asList("Add", "Remove", "Modify"))).build()) 58 .append(new ParamBuilder().setId("id").setName("identifier").setType(StringParam.class).build()) 59 .append(new ParamBuilder().setId("pos").setName("position").setType(DecimalParam.class).build()); 60 } 58 61 59 ArrayList<String> actions = new ArrayList<String>(); 60 actions.add("Add"); 61 actions.add("Remove"); 62 actions.add("Modify"); 63 return new FramsClass("ListChange", "ListChange", null) 64 .append(new ParamBuilder().setId("type").setName("type").setType(new EnumParam(actions)).build()) 65 .append(new ParamBuilder().setId("id").setName("identifier").setType(StringParam.class).build()) 66 .append(new ParamBuilder().setId("pos").setName("position").setType(DecimalParam.class).build()); 67 } 68 69 @Override 70 public String toString() { 71 return action + " " + identifier + " " + position; 72 } 62 @Override 63 public String toString() { 64 return action + " " + identifier + " " + position; 65 } 73 66 } -
java/main/src/main/java/com/framsticks/core/LocalInstance.java
r78 r84 2 2 3 3 import com.framsticks.hosting.InstanceClient; 4 import com.framsticks.params.AccessInterface; 5 import com.framsticks.params.Param; 6 import com.framsticks.util.*; 7 import com.framsticks.util.Thread; 4 import com.framsticks.util.dispatching.Thread; 5 6 import org.apache.commons.configuration.Configuration; 8 7 import org.apache.log4j.Logger; 9 8 … … 21 20 public abstract class LocalInstance extends Instance { 22 21 23 private static final Logger LOGGER= Logger.getLogger(LocalInstance.class.getName());22 private static final Logger log = Logger.getLogger(LocalInstance.class.getName()); 24 23 25 public LocalInstance(Parameters parameters) { 26 super(parameters); 24 public LocalInstance() { 27 25 28 Integer accept = config.getInteger("accept", null); 29 if (accept != null) { 30 try { 31 acceptSocket = new ServerSocket(); 32 acceptSocket.setReuseAddress(true); 33 acceptThread = new Thread(name + "-accept"); 34 tryBind(accept); 35 } catch (IOException e) { 36 LOGGER.fatal("failed to create accept socket: " + e); 37 } 38 } 39 } 26 } 40 27 41 @Override 42 protected void run() { 43 super.run(); 44 } 28 @Override 29 public void configure(Configuration config) { 30 Integer accept = config.getInteger("accept", null); 31 if (accept != null) { 32 try { 33 acceptSocket = new ServerSocket(); 34 acceptSocket.setReuseAddress(true); 35 acceptThread = new Thread(name + "-accept"); 36 tryBind(accept); 37 } catch (IOException e) { 38 log.fatal("failed to create accept socket: " + e); 39 } 40 } 41 } 45 42 46 @Override 47 protected void configure() throws Exception { 48 super.configure(); 49 } 43 protected final Set<InstanceClient> clients = new HashSet<InstanceClient>(); 44 ServerSocket acceptSocket; 45 Thread acceptThread; 50 46 51 protected final Set<InstanceClient> clients = new HashSet<InstanceClient>(); 52 ServerSocket acceptSocket; 53 Thread acceptThread; 47 protected void acceptNext() { 48 acceptThread.invokeLater(new Runnable() { 49 @Override 50 public void run() { 51 try { 52 final Socket socket = acceptSocket.accept(); 53 assert socket != null; 54 log.debug("accepted socket: " + socket.getInetAddress().getHostAddress()); 55 invokeLater(new Runnable() { 56 @Override 57 public void run() { 58 InstanceClient client = new InstanceClient(LocalInstance.this, socket); 59 clients.add(client); 60 log.info("client connected: " + client); 61 } 62 }); 63 } catch (IOException e) { 64 log.error("failed to accept socket: " + e); 65 } 66 acceptNext(); 67 } 68 }); 69 } 54 70 55 protected void acceptNext() { 56 acceptThread.invokeLater(new Runnable() { 57 @Override 58 public void run() { 59 try { 60 final Socket socket = acceptSocket.accept(); 61 assert socket != null; 62 LOGGER.debug("accepted socket: " + socket.getInetAddress().getHostAddress()); 63 invokeLater(new Runnable() { 64 @Override 65 public void run() { 66 InstanceClient client = new InstanceClient(LocalInstance.this, socket); 67 clients.add(client); 68 LOGGER.info("client connected: " + client); 69 } 70 }); 71 } catch (IOException e) { 72 LOGGER.error("failed to accept socket: " + e); 73 } 74 acceptNext(); 75 } 76 }); 77 } 78 79 public void tryBind(final Integer accept) { 80 acceptThread.invokeLater(new Runnable() { 81 @Override 82 public void run() { 83 try { 84 acceptSocket.bind(new InetSocketAddress(accept)); 85 LOGGER.debug("started accepting on port " + accept); 86 acceptNext(); 87 } catch (IOException e) { 88 LOGGER.fatal("failed to accept on port " + accept + ": " + e); 89 } 90 try { 91 java.lang.Thread.sleep(1000); 92 } catch (InterruptedException ignored) { 93 } 94 tryBind(accept); 95 } 96 }); 97 } 71 public void tryBind(final Integer accept) { 72 acceptThread.invokeLater(new Runnable() { 73 @Override 74 public void run() { 75 try { 76 acceptSocket.bind(new InetSocketAddress(accept)); 77 log.debug("started accepting on port " + accept); 78 acceptNext(); 79 } catch (IOException e) { 80 log.fatal("failed to accept on port " + accept + ": " + e); 81 } 82 try { 83 java.lang.Thread.sleep(1000); 84 } catch (InterruptedException ignored) { 85 } 86 tryBind(accept); 87 } 88 }); 89 } 98 90 99 91 } -
java/main/src/main/java/com/framsticks/core/Node.java
r77 r84 1 1 package com.framsticks.core; 2 2 3 import com.framsticks.params.types.CompositeParam; 4 5 import java.util.HashMap; 6 import java.util.Map; 3 import com.framsticks.params.CompositeParam; 7 4 8 5 /** -
java/main/src/main/java/com/framsticks/core/Path.java
r77 r84 2 2 3 3 import com.framsticks.params.AccessInterface; 4 import com.framsticks.params.CompositeParam; 4 5 import com.framsticks.params.Param; 5 import com.framsticks.params.types.CompositeParam; 6 import com.framsticks.util.Dispatching; 7 import org.apache.log4j.Logger; 8 6 import com.framsticks.util.dispatching.Dispatching; 9 7 import java.util.Iterator; 10 8 import java.util.LinkedList; 9 import java.util.List; 10 11 import org.apache.commons.collections.ListUtils; 11 12 12 13 /** … … 14 15 */ 15 16 public class Path { 16 private final static Logger LOGGER = Logger.getLogger(Path.class.getName()); 17 18 final LinkedList<Node> nodes = new LinkedList<Node>(); 19 String textual; 20 Instance instance; 21 22 protected Object getKnownChild(AccessInterface access, CompositeParam param) { 23 Object child = access.get(param, Object.class); 24 if (child == null) { 25 return null; 26 } 27 return (instance.prepareAccess(param) != null) ? child : null; 28 } 29 30 public Path(Instance instance, String textual) { 31 assert instance.isActive(); 32 this.instance = instance; 33 StringBuilder b = new StringBuilder(); 34 nodes.add(instance.root); 35 Node current = instance.root; 36 Iterator<String> i = Instance.splitPath(textual); 37 while (i.hasNext() && current.getObject() != null) { 38 AccessInterface access = instance.prepareAccess(current.getParam()); 39 if (access == null) { 40 break; 41 } 42 String e = i.next(); 43 Param p = access.getParam(e); 44 if (!(p instanceof CompositeParam)) { 45 //entries.add(new Entry()); 46 break; 47 } 48 CompositeParam c = (CompositeParam)p; 49 b.append("/").append(e); 50 access.select(current.getObject()); 51 current = new Node(c, getKnownChild(access, c)); 52 nodes.add(current); 53 } 54 this.textual = (size() == 1) ? "/" : b.toString(); 55 } 56 57 protected Path() { 58 59 } 60 61 public Path appendNode(Node node) { 62 assert isResolved(); 63 Path result = new Path(); 64 result.textual = textual + ((size() == 1) ? "" : "/") + node.getParam().getId(); 65 result.instance = instance; 66 result.nodes.addAll(nodes); 67 result.nodes.add(node); 68 return result; 69 } 70 71 public Path appendParam(CompositeParam param) { 72 assert isResolved(); 73 return appendNode(new Node(param, null)); 74 } 75 76 public Path appendResolution(Object object) { 77 assert !isResolved(); 78 Path result = new Path(); 79 result.textual = textual; 80 result.instance = instance; 81 result.nodes.addAll(nodes); 82 result.nodes.add(new Node(result.nodes.pollLast().getParam(), object)); 83 return result; 84 } 85 86 public final Object getTopObject() { 87 return getTop().getObject(); 88 } 89 90 public final Node getTop() { 91 return nodes.getLast(); 92 } 93 94 public final Node getUnder() { 95 assert nodes.size() >= 2; 96 return nodes.get(nodes.size() - 2); 97 } 98 99 public final String getTextual() { 100 return textual; 101 } 102 103 public String toString() { 104 return instance + textual + (!isResolved() ? "!" : ""); 105 } 106 107 public final int size() { 108 assert Dispatching.isThreadSafe(); 109 return nodes.size(); 110 } 111 112 public final boolean isResolved() { 113 assert Dispatching.isThreadSafe(); 114 return getTop().getObject() != null; 115 } 116 117 public final boolean isResolved(String textual) { 118 assert Dispatching.isThreadSafe(); 119 return isTheSame(textual) && isResolved(); 120 } 121 122 public final boolean isTheSame(String textual) { 123 assert Dispatching.isThreadSafe(); 124 return this.textual.equals(textual); 125 } 126 127 public final Instance getInstance() { 128 assert Dispatching.isThreadSafe(); 129 return instance; 130 } 131 132 public Path findResolution() { 133 assert instance.isActive(); 134 assert !isResolved(); 135 if (size() == 1) { 17 // private final static Logger log = Logger.getLogger(Path.class.getName()); 18 19 final LinkedList<Node> nodes = new LinkedList<Node>(); 20 String textual; 21 Instance instance; 22 23 protected Object getKnownChild(AccessInterface access, CompositeParam param) { 24 Object child = access.get(param, Object.class); 25 if (child == null) { 26 return null; 27 } 28 return (instance.registry.prepareAccess(param) != null) ? child : null; 29 } 30 31 // public Path constructPath(Instance instance, String textual) { 32 // assert instance.isActive(); 33 34 // } 35 36 Path(Instance instance, String textual) { 37 assert instance.isActive(); 38 this.instance = instance; 39 40 nodes.add(instance.root); 41 Node current = instance.root; 42 43 StringBuilder b = new StringBuilder(); 44 Iterator<String> i = Instance.splitPath(textual); 45 while (i.hasNext() && current.getObject() != null) { 46 AccessInterface access = instance.registry.prepareAccess(current.getParam()); 47 if (access == null) { 48 break; 49 } 50 String e = i.next(); 51 Param p = access.getParam(e); 52 if (!(p instanceof CompositeParam)) { 53 //entries.add(new Entry()); 54 break; 55 } 56 CompositeParam c = (CompositeParam)p; 57 b.append("/").append(e); 58 access.select(current.getObject()); 59 current = new Node(c, getKnownChild(access, c)); 60 nodes.add(current); 61 } 62 this.textual = (size() == 1) ? "/" : b.toString(); 63 } 64 65 public Path(Instance instance, List<Node> nodes, Node node) { 66 this.instance = instance; 67 StringBuilder b = new StringBuilder(); 68 boolean add = false; 69 for (Node n : nodes) { 70 this.nodes.add(n); 71 if (add) { 72 b.append("/").append(n.getParam().getId()); 73 } 74 add = true; 75 if (n == node) { 76 break; 77 } 78 } 79 this.textual = (size() == 1) ? "/" : b.toString(); 80 } 81 82 protected Path() { 83 84 } 85 86 public Path appendNode(Node node) { 87 assert isResolved(); 88 Path result = new Path(); 89 result.textual = textual + ((size() == 1) ? "" : "/") + node.getParam().getId(); 90 result.instance = instance; 91 result.nodes.addAll(nodes); 92 result.nodes.add(node); 93 return result; 94 } 95 96 public Path appendParam(CompositeParam param) { 97 assert isResolved(); 98 return appendNode(new Node(param, null)); 99 } 100 101 public Path appendResolution(Object object) { 102 assert !isResolved(); 103 Path result = new Path(); 104 result.textual = textual; 105 result.instance = instance; 106 result.nodes.addAll(nodes); 107 result.nodes.add(new Node(result.nodes.pollLast().getParam(), object)); 108 return result; 109 } 110 111 public final Object getTopObject() { 112 return getTop().getObject(); 113 } 114 115 public final Node getTop() { 116 return nodes.getLast(); 117 } 118 119 public final Node getUnder() { 120 assert nodes.size() >= 2; 121 return nodes.get(nodes.size() - 2); 122 } 123 124 public final String getTextual() { 125 return textual; 126 } 127 128 public String toString() { 129 return instance + textual + (!isResolved() ? "!" : ""); 130 } 131 132 public final int size() { 133 assert Dispatching.isThreadSafe(); 134 return nodes.size(); 135 } 136 137 public final boolean isResolved() { 138 assert Dispatching.isThreadSafe(); 139 return getTop().getObject() != null; 140 } 141 142 public final boolean isResolved(String textual) { 143 assert Dispatching.isThreadSafe(); 144 return isTheSame(textual) && isResolved(); 145 } 146 147 public final boolean isTheSame(String textual) { 148 assert Dispatching.isThreadSafe(); 149 return this.textual.equals(textual); 150 } 151 152 public final Instance getInstance() { 153 assert Dispatching.isThreadSafe(); 154 return instance; 155 } 156 157 158 /** Attach resolution at end, if available. 159 * 160 * @return Modified path, if resolution was available, this otherwise. 161 */ 162 public Path tryFindResolution() { 163 assert instance.isActive(); 164 assert !isResolved(); 165 if (size() == 1) { 136 166 return new Path(instance, "/");//appendResolution(instance.root.object); 137 } 138 Object child = getKnownChild(instance.bindAccess(getUnder()), getTop().getParam()); 139 if (child == null) { 140 return this; 141 } 142 return appendResolution(child); 143 } 144 145 public boolean matches(Path p) { 146 assert Dispatching.isThreadSafe(); 147 assert instance == p.instance; 148 Iterator<Node> a = nodes.iterator(); 149 Iterator<Node> b = p.nodes.iterator(); 150 while (a.hasNext() && b.hasNext()) { 151 Node an = a.next(); 152 Node bn = b.next(); 153 if (an.object != bn.object) { 154 return false; 155 } 156 } 157 return a.hasNext() == b.hasNext(); 158 } 159 160 public String getLastElement() { 161 return getTop().getParam().getId(); 162 } 163 164 public final boolean isOwner(Instance instance) { 165 return this.instance == instance; 166 } 167 } 168 Object child = getKnownChild(instance.bindAccess(getUnder()), getTop().getParam()); 169 if (child == null) { 170 return this; 171 } 172 return appendResolution(child); 173 } 174 175 public boolean matches(Path p) { 176 assert Dispatching.isThreadSafe(); 177 assert instance == p.instance; 178 Iterator<Node> a = nodes.iterator(); 179 Iterator<Node> b = p.nodes.iterator(); 180 while (a.hasNext() && b.hasNext()) { 181 Node an = a.next(); 182 Node bn = b.next(); 183 if (an.object != bn.object) { 184 return false; 185 } 186 } 187 return a.hasNext() == b.hasNext(); 188 } 189 190 public String getLastElement() { 191 return getTop().getParam().getId(); 192 } 193 194 public final boolean isOwner(Instance instance) { 195 return this.instance == instance; 196 } 197 198 @SuppressWarnings("unchecked") 199 public 200 List<Node> getNodes() { 201 return ListUtils.unmodifiableList(nodes); 202 } 167 203 } 168 204 -
java/main/src/main/java/com/framsticks/core/Program.java
r78 r84 1 1 package com.framsticks.core; 2 2 3 import com.framsticks.util.Pair; 4 import com.framsticks.util.Strings; 3 import com.framsticks.util.lang.IterableIterator; 4 import com.framsticks.util.lang.Pair; 5 import com.framsticks.util.lang.Strings; 5 6 import org.apache.commons.configuration.*; 6 7 import org.apache.log4j.Logger; 7 8 import org.apache.log4j.PropertyConfigurator; 8 //import sun.misc.Signal;9 //import sun.misc.SignalHandler;10 9 11 10 import java.lang.reflect.Constructor; … … 15 14 * @author Piotr Sniegowski 16 15 */ 17 public class Program extends com.framsticks.util. Thread {16 public class Program extends com.framsticks.util.dispatching.Thread { 18 17 19 private final static Logger LOGGER= Logger.getLogger(Program.class.getName());18 private final static Logger log = Logger.getLogger(Program.class.getName()); 20 19 21 /* 22 protected SignalHandler signalHandler = new SignalHandler() { 23 @Override 24 public void handle(Signal signal) { 25 LOGGER.info("caught " + signal); 26 Runtime.getRuntime().exit(0); 27 } 28 }; 29 */ 20 protected Entity entity; 30 21 31 protected Entity entity;22 Configuration config; 32 23 33 Configuration config; 24 public Program(String name) { 25 super(name, java.lang.Thread.currentThread()); 26 } 34 27 28 public static Entity configureEntity(Configuration config) { 29 String typeName = config.getString("class"); 30 log.info("configuring instance " + typeName); 31 try { 32 Class<?> type = Class.forName(typeName); 33 Constructor<?> constructor = type.getConstructor(); 34 Entity entity = (Entity) constructor.newInstance(); 35 return entity; 36 } catch (Exception e) { 37 log.error("failed to instantiate: " + e); 38 } 39 return null; 40 } 35 41 36 public Program(String name) { 37 super(name, java.lang.Thread.currentThread()); 38 // Signal.handle(new Signal("INT"), signalHandler); 39 // Signal.handle(new Signal("TERM"), signalHandler); 40 } 42 protected static void resolve(Configuration config, String prefix) { 43 for (String p : new IterableIterator<String>(prefix == null ? config.getKeys() : config.getKeys(prefix))) { 44 if (p.endsWith(".mount")) { 45 String source = config.getString(p); 46 log.info("mounting " + source + " at " + p); 47 config.clearProperty(p); 48 Configuration mount = config.subset(source); 49 for (String mk : new IterableIterator<String>(mount.getKeys())) { 50 config.addProperty(p.substring(0, p.length() - 6) + "." + mk, mount.getProperty(mk)); 51 } 52 } 53 } 54 } 41 55 42 public static Entity configureEntity(Parameters parameters) { 43 String typeName = parameters.getConfig().getString("class"); 44 LOGGER.info("configuring instance " + typeName); 45 try { 46 Class type = Class.forName(typeName); 47 Constructor constructor = type.getConstructor(Parameters.class); 48 return (Entity) constructor.newInstance(parameters); 49 } catch (Exception e) { 50 LOGGER.error("failed to instantiate: " + e); 51 } 52 return null; 53 } 54 55 protected static void resolve(Configuration config, String prefix) { 56 57 Iterator i = prefix == null ? config.getKeys() : config.getKeys(prefix); 58 while (i.hasNext()) { 59 String p = (String) i.next(); 60 if (p.endsWith(".mount")) { 61 String source = config.getString(p); 62 LOGGER.info("mounting " + source + " at " + p); 63 config.clearProperty(p); 64 Configuration mount = config.subset(source); 65 Iterator mi = mount.getKeys(); 66 while (mi.hasNext()) { 67 String mk = (String) mi.next(); 68 config.addProperty(p.substring(0, p.length() - 6) + "." + mk, mount.getProperty(mk)); 69 } 70 } 71 } 72 } 73 74 public void run(String[] args) { 56 public void run(String[] args) { 75 57 76 58 PropertyConfigurator.configure(getClass().getResource("/configs/log4j.properties")); 77 LOGGER.debug("started in " + System.getProperty("user.dir"));78 59 log.debug("started in " + System.getProperty("user.dir")); 60 try { 79 61 config = new PropertiesConfiguration(getClass().getResource("/configs/framsticks.properties")); 80 62 81 82 83 84 63 for (String a : args) { 64 Pair<String, String> p = Strings.splitIntoPair(a, '=', "true"); 65 config.setProperty(p.first, p.second); 66 } 85 67 86 68 resolve(config, null); 87 69 88 Iteratori = config.getKeys();89 90 91 LOGGER.debug(p + " = " + config.getProperty(p));92 70 Iterator<?> i = config.getKeys(); 71 while (i.hasNext()) { 72 String p = (String) i.next(); 73 log.debug(p + " = " + config.getProperty(p)); 74 } 93 75 94 } catch (ConfigurationException e) { 95 System.err.print("failed to parse configuration:" + e); 96 return; 97 } 76 } catch (ConfigurationException e) { 77 System.err.print("failed to parse configuration:" + e); 78 return; 79 } 80 Configuration entityConfig = config.subset("com.framsticks.entity"); 98 81 99 entity = configureEntity(new Parameters(config.subset("com.framsticks.entity"), "main", null, new EntityOwner() { 82 entity = configureEntity(entityConfig); 83 entity.setOwner(new EntityOwner() { 100 84 @Override 101 85 public void onDone() { 102 LOGGER.info("exiting");86 log.info("exiting"); 103 87 Runtime.getRuntime().exit(0); 104 88 } 105 })); 89 }); 90 entity.setName("main"); 106 91 try { 107 entity.configurePublic();92 entity.configure(entityConfig); 108 93 } catch (Exception e) { 109 LOGGER.fatal("exception caught during configuration: " + e);94 log.fatal("exception caught during configuration: " + e); 110 95 } 111 LOGGER.info("all entities were configured");112 113 96 log.info("all entities were configured"); 97 entity.start(); 98 } 114 99 115 public static void main(final String[] args) { 116 final Program program = new Program("program"); 117 program.invokeLater(new Runnable() { 118 @Override 119 public void run() { 120 program.run(args); 121 } 122 }); 123 program.routine(); 124 } 100 public static void main(final String[] args) { 101 102 final Program program = new Program("program"); 103 program.invokeLater(new Runnable() { 104 @Override 105 public void run() { 106 program.run(args); 107 } 108 }); 109 program.routine(); 110 } 125 111 126 112 }
Note: See TracChangeset
for help on using the changeset viewer.