From 964d6a706c6d33949146e703ff1a7700aa364284 Mon Sep 17 00:00:00 2001 From: jsrobson10 Date: Tue, 9 Jul 2019 18:32:35 +1000 Subject: [PATCH] Created a functional mainloop --- .classpath | 1 + src/mainloop/Tests.java | 95 +++++++++++++++++++++++ src/mainloop/event/IMainloopEvent.java | 9 +++ src/mainloop/manager/MainloopManager.java | 79 +++++++++++++++++++ src/mainloop/task/IMainloopTask.java | 8 ++ src/mainloop/task/MainloopTask.java | 59 ++++++++++++++ src/mainloop/time/MainloopTimer.java | 50 ++++++++++++ 7 files changed, 301 insertions(+) create mode 100755 src/mainloop/Tests.java create mode 100755 src/mainloop/event/IMainloopEvent.java create mode 100755 src/mainloop/manager/MainloopManager.java create mode 100755 src/mainloop/task/IMainloopTask.java create mode 100755 src/mainloop/task/MainloopTask.java create mode 100755 src/mainloop/time/MainloopTimer.java diff --git a/.classpath b/.classpath index 8c49c50..fb50116 100755 --- a/.classpath +++ b/.classpath @@ -1,5 +1,6 @@ + diff --git a/src/mainloop/Tests.java b/src/mainloop/Tests.java new file mode 100755 index 0000000..0d12c58 --- /dev/null +++ b/src/mainloop/Tests.java @@ -0,0 +1,95 @@ +package mainloop; + +import mainloop.event.IMainloopEvent; +import mainloop.manager.MainloopManager; +import mainloop.task.IMainloopTask; + +public class Tests +{ + public static long timer = 0; + + public static void main(String[] args) + { + MainloopManager ml = new MainloopManager(new IMainloopEvent() { + + @Override + public void onStart() { + System.out.println("Started"); + } + + @Override + public void onLate() { + } + + @Override + public void onEarly() { + } + + @Override + public void onClose() { + System.out.println("Closed"); + } + }); + + ml.register(new IMainloopTask() { + + @Override + public void MainLoopUpdate() { + System.out.print(timer); + System.out.print(" "); + System.out.print(System.currentTimeMillis()); + System.out.println(); + //try {Thread.sleep(100);} catch(Exception e) {} + timer++; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public boolean MainLoopDelay(long delay) { + return delay > 1000; + } + }); + + ml.register(new IMainloopTask() { + + @Override + public void MainLoopUpdate() { + //ml.stop(); + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public boolean MainLoopDelay(long delay) { + return delay > 1000; + } + }); + + ml.register(new IMainloopTask() { + + @Override + public void MainLoopUpdate() { + System.out.println("Hello, World!"); + } + + @Override + public boolean MainLoopRepeat() { + return false; + } + + @Override + public boolean MainLoopDelay(long delay) { + return delay > 10000; + } + }); + + ml.start(); + } +} diff --git a/src/mainloop/event/IMainloopEvent.java b/src/mainloop/event/IMainloopEvent.java new file mode 100755 index 0000000..cdb2c95 --- /dev/null +++ b/src/mainloop/event/IMainloopEvent.java @@ -0,0 +1,9 @@ +package mainloop.event; + +public interface IMainloopEvent +{ + public void onEarly(); + public void onLate(); + public void onClose(); + public void onStart(); +} diff --git a/src/mainloop/manager/MainloopManager.java b/src/mainloop/manager/MainloopManager.java new file mode 100755 index 0000000..f192c80 --- /dev/null +++ b/src/mainloop/manager/MainloopManager.java @@ -0,0 +1,79 @@ +package mainloop.manager; + +import java.util.ArrayList; +import java.util.Iterator; + +import mainloop.event.IMainloopEvent; +import mainloop.task.IMainloopTask; +import mainloop.task.MainloopTask; +import mainloop.time.MainloopTimer; + + +public class MainloopManager +{ + protected ArrayList tasks; + protected IMainloopEvent handler; + protected boolean running = false; + protected MainloopTimer timer; + + public MainloopManager(IMainloopEvent handler) + { + // Initialize the variables + this.handler = handler; + this.tasks = new ArrayList(); + this.timer = new MainloopTimer(handler); + } + + public void start() + { + // Set running to true + this.running = true; + + // Call the on start function + this.handler.onStart(); + + // Loop while the running flag is true + while(this.running) + { + // Sleep until the timer is on time + this.timer.sleep(); + + // Update the processes + this.update(); + } + } + + public void stop() + { + // Call the on close function + this.handler.onClose(); + + // Set the running flag to false + this.running = false; + } + + public void update() + { + // Loop over the tasks + for(Iterator i=tasks.iterator();i.hasNext();) + { + // Get the task + MainloopTask task = (MainloopTask) i.next(); + + // Update the task + task.update(); + + // Should the task be removed + if(task.shouldRemove()) + { + // Remove the task + i.remove(); + } + } + } + + public void register(IMainloopTask task) { + tasks.add(new MainloopTask(task)); + } + +} diff --git a/src/mainloop/task/IMainloopTask.java b/src/mainloop/task/IMainloopTask.java new file mode 100755 index 0000000..471eaf8 --- /dev/null +++ b/src/mainloop/task/IMainloopTask.java @@ -0,0 +1,8 @@ +package mainloop.task; + +public interface IMainloopTask +{ + public void MainLoopUpdate(); + public boolean MainLoopDelay(long delay); + public boolean MainLoopRepeat(); +} diff --git a/src/mainloop/task/MainloopTask.java b/src/mainloop/task/MainloopTask.java new file mode 100755 index 0000000..0f46dab --- /dev/null +++ b/src/mainloop/task/MainloopTask.java @@ -0,0 +1,59 @@ +package mainloop.task; + +public class MainloopTask +{ + protected long timer; + protected IMainloopTask task; + protected boolean remove; + + public MainloopTask(IMainloopTask task) + { + // Initialize all the variables + this.task = task; + this.timer = 1; + this.remove = false; + } + + public void update() + { + // Increase the timer + timer += 1; + + if(!this.remove) + { + // Is it time for the function to update + if(task.MainLoopDelay(this.timer)) + { + // Set the remove flag to true + this.remove = true; + + // Execute the function + task.MainLoopUpdate(); + } + } + } + + public boolean shouldRemove() + { + // Is the remove flag set + if(this.remove) + { + // Does this task repeat + if(task.MainLoopRepeat()) + { + // Set the flags to false and reset the timer + this.remove = false; + this.timer = 1; + } + + else + { + // Send back true + return true; + } + } + + // Send back false by default + return false; + } +} diff --git a/src/mainloop/time/MainloopTimer.java b/src/mainloop/time/MainloopTimer.java new file mode 100755 index 0000000..f57f825 --- /dev/null +++ b/src/mainloop/time/MainloopTimer.java @@ -0,0 +1,50 @@ +package mainloop.time; + +import mainloop.event.IMainloopEvent; + +public class MainloopTimer +{ + protected long millis_now; + protected long millis_count; + IMainloopEvent handler; + + public MainloopTimer(IMainloopEvent handler) + { + // Initialize the variables + this.millis_now = System.currentTimeMillis(); + this.millis_count = this.millis_now; + this.handler = handler; + } + + public void sleep() + { + // Get the current millis and update the millis count + this.millis_now = System.currentTimeMillis(); + this.millis_count += 1; + + // Is the timer late + if(this.millis_now > this.millis_count) + { + // Call the late handler + handler.onLate(); + } + + // Is the timer early + else + { + // Call the early handler + handler.onEarly(); + + // Loop until the timer is on time + while(System.currentTimeMillis() <= this.millis_count) + { + // Sleep for 1 millisecond + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } +}