Skip to content

Commit

Permalink
jector-monkey: added deleted sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrappers-glitch committed Jul 29, 2023
1 parent abee3f0 commit 2a4b1d7
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/.idea
/.vscode
/.git
/.gradle
/jector/build
/jector/bin
/jector-examples/build
/jector-examples/bin
/jector-monkey/build
/jector-monkey/bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2023, The AvrSandbox Project, Jector Framework
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Provides a specialized implementation of the jector framework for jMonkeyEngine.
*/
package com.avrsandbox.jector.monkey.core;
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2023, The AvrSandbox Project, Jector Framework
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.avrsandbox.jector.monkey.core.work;

import com.avrsandbox.jector.core.work.TaskExecutor;
import com.avrsandbox.jector.core.work.WorkerTask;
import com.jme3.app.Application;
import com.jme3.app.state.BaseAppState;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
* A base implementation of the jector {@link TaskExecutor}s to a jMonkeyEngine app state.
*
* @author pavl_g
*/
public class MonkeyTaskExecutor extends BaseAppState implements TaskExecutor {

/**
* Tasks wrapping the methods to be bound to their specified annotated methods.
*/
protected final Map<String, WorkerTask> tasks = new HashMap<>();

/**
* A flag to order the executor for termination.
*/
protected volatile boolean terminate;

/**
* A flag to order the executor to start running.
*/
protected volatile boolean active = false;

/**
* Provides a universal tpf attribute for the associated tasks.
*/
protected float timePerFrame;

/**
* Provides an interface to command-state the initialization.
*/
protected OnExecutorInitialized onInitialized;

/**
* Instantiates a new instance of a task executor specialized to run on the JME thread only.
*
* @param id the associated state id
*/
public MonkeyTaskExecutor(String id) {
super(id);
}

/**
* Retrieves the application time-per-frame in seconds.
*
* @return the JME app tpf in seconds
*/
public float getTimePerFrame() {
return timePerFrame;
}

/**
* Adjusts the initialization listener instance.
*
* @param onInitialized the initializer instance
*/
public void setOnInitialized(OnExecutorInitialized onInitialized) {
this.onInitialized = onInitialized;
}

@Override
protected void initialize(Application app) {
if (onInitialized != null) {
onInitialized.onInitialized(app);
}
}

@Override
protected void cleanup(Application app) {

}

@Override
protected void onEnable() {

}

@Override
protected void onDisable() {

}

@Override
public void update(float tpf) {
this.timePerFrame = tpf;
/* 2) Run Worker Method tasks */
executeTasks(tpf);
}

@Override
public boolean isActive() {
return isEnabled();
}

@Override
public void setActive(boolean active) {
setEnabled(active);
}

@Override
public void addTask(Method method, WorkerTask task) {
tasks.put(method.getName(), task);
}

@Override
public void executeTasks(Object arguments) {
try {
for (String task : tasks.keySet()) {
if (!tasks.get(task).isActive()) {
continue;
}
if (!(tasks.get(task) instanceof MonkeyWorkerTask)) {
throw new IllegalArgumentException("WorkerTasks must be of type: " + MonkeyWorkerTask.class.getName());
}
MonkeyWorkerTask monkeyTask = (MonkeyWorkerTask) tasks.get(task);
/* Invokes and Saves the result of the execution order! */
monkeyTask.setTimePerFrame((float) arguments);
monkeyTask.setResult(monkeyTask.call());
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public Map<String, WorkerTask> getTasks() {
return tasks;
}

@Override
public void terminate() {
getStateManager().detach(this);
this.terminate = true;
}

@Override
public boolean isTerminated() {
return terminate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2023, The AvrSandbox Project, Jector Framework
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.avrsandbox.jector.monkey.core.work;

import com.avrsandbox.jector.core.command.MethodArguments;
import com.avrsandbox.jector.core.work.TaskExecutorsManager;
import com.avrsandbox.jector.core.work.Worker;
import com.avrsandbox.jector.core.work.*;
import java.lang.reflect.Method;

/**
* A JME-thread-safe specialized implementation of the {@link TaskExecutorsManager} for jMonkeyEngine.
*
* @author pavl_g
*/
public class MonkeyTaskExecutorsManager extends TaskExecutorsManager {

/**
* Instantiates a JME task binder with a single worker instance.
*
* @param worker the worker instance holding the worker methods to be executed
*/
public MonkeyTaskExecutorsManager(Worker worker) {
super(worker);
}

/**
* Instantiates a JME task binder with multiple worker instances.
*
* @param workers an array of worker instances
*/
public MonkeyTaskExecutorsManager(Worker[] workers) {
super(workers);
}

@Override
public void bind(MethodArguments methodArguments) {
if (!Thread.currentThread().getName().equals("jME3 Main")) {
throw new IllegalStateException("Cannot bind on a non-application thread!");
}
super.bind(methodArguments);
}

@Override
protected void bind(TaskExecutor taskExecutor, Worker worker, Method method,
MethodArguments args) {
/* binds the method invocation to the specified executor object */
taskExecutor.addTask(method, new MonkeyWorkerTask() {
@Override
public Object call() {
/* The Triple Check Pattern (No. of parameters - Input args - Types compatibility) */
if (method.getParameters() != null) {
return executeMethod(worker, method, args, MonkeyTaskExecutorsManager.this);
} else {
/* Force null args for non-parameterized methods */
return executeMethod(worker, method, null, MonkeyTaskExecutorsManager.this);
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2023, The AvrSandbox Project, Jector Framework
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.avrsandbox.jector.monkey.core.work;

import com.avrsandbox.jector.core.work.WorkerTask;

/**
* A specialized implementation of the jector WorkerTask for jMonkeyEngine
* supporting time-per-frame monitoring.
*
* @author pavl_g
*/
public abstract class MonkeyWorkerTask extends WorkerTask {

/**
* Value for time per frame in seconds, this value
* is internally synchronized with the JME update thread.
*/
protected float timePerFrame;

/**
* Updates the time-per-frame value (in seconds).
*
* @param timePerFrame the new value (in seconds)
*/
public void setTimePerFrame(float timePerFrame) {
this.timePerFrame = timePerFrame;
}

/**
* Retrieves the time-per-frame value (in seconds).
*
* @return the time-per-frame value (in seconds)
*/
public float getTimePerFrame() {
return timePerFrame;
}
}
Loading

0 comments on commit 2a4b1d7

Please sign in to comment.