TimerTaskDef moved out of Scheduler class, renamed to Event

This commit is contained in:
7u83 2018-12-17 22:34:37 +01:00
parent 998dad7e66
commit 648e9d5863
6 changed files with 111 additions and 136 deletions

View File

@ -53,10 +53,10 @@ import org.json.JSONObject;
import opensesim.old_sesim.AutoTraderInterface; import opensesim.old_sesim.AutoTraderInterface;
import opensesim.old_sesim.Exchange; import opensesim.old_sesim.Exchange;
import opensesim.old_sesim.Scheduler; import opensesim.old_sesim.Scheduler;
import opensesim.old_sesim.Scheduler.TimerTaskDef;
import opensesim.world.GodWorld; import opensesim.world.GodWorld;
import opensesim.world.World; import opensesim.world.World;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.Scheduler.EventListener; import opensesim.world.scheduler.Scheduler.EventListener;
@ -66,7 +66,6 @@ import opensesim.world.scheduler.Scheduler.EventListener;
*/ */
public class SeSimApplication extends javax.swing.JFrame { public class SeSimApplication extends javax.swing.JFrame {
GodWorld godworld; GodWorld godworld;
/** /**
@ -83,8 +82,6 @@ public class SeSimApplication extends javax.swing.JFrame {
cfg = new JSONObject(Globals.prefs.get("world", "{}")); cfg = new JSONObject(Globals.prefs.get("world", "{}"));
godworld = new GodWorld(cfg); godworld = new GodWorld(cfg);
// Get default screen and place our window // Get default screen and place our window
// to the center of this screen // to the center of this screen
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
@ -606,44 +603,36 @@ public class SeSimApplication extends javax.swing.JFrame {
opensesim.world.scheduler.Scheduler s = godworld.getScheduler(); opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
class MyEvent extends Event {
int count = 0;
MyEvent(){
}
}
class MyListener implements EventListener { class MyListener implements EventListener {
World world; World world;
MyListener(World world) { MyListener(World world) {
this.world = world; this.world = world;
} }
@Override
public long receive(Event event) {
MyEvent e = (MyEvent) event;
System.out.printf("Received an Event %d\n",e.count);
e.count++;
world.schedule(this, event, 1000);
return -1;
}
@Override @Override
public long getID() { public long getID() {
return 1; return 1;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override
public long receive(opensesim.world.scheduler.Event task) {
System.out.printf("Received an Event %d\n", 0);
// e.count++;
world.schedule(this, 1000);
return -1;
}
} }
MyListener listener = new MyListener(godworld.getWorld()); MyListener listener = new MyListener(godworld.getWorld());
MyEvent arg = new MyEvent(); // MyEvent arg = new MyEvent();
s.startTimerTask(listener, arg, WIDTH); s.startTimerTask(listener, 1000);
/* /*
@ -680,7 +669,6 @@ public class SeSimApplication extends javax.swing.JFrame {
}; };
// Globals.se.timer.startTimerTask(tt, 0); // Globals.se.timer.startTimerTask(tt, 0);
*/ */
} }
void stopSim() { void stopSim() {
@ -973,8 +961,6 @@ public class SeSimApplication extends javax.swing.JFrame {
private void assetsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assetsMenuItemActionPerformed private void assetsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assetsMenuItemActionPerformed
opensesim.gui.AssetEditor.AssetListDialog dialog; opensesim.gui.AssetEditor.AssetListDialog dialog;
dialog = new opensesim.gui.AssetEditor.AssetListDialog(godworld, this, true); dialog = new opensesim.gui.AssetEditor.AssetListDialog(godworld, this, true);
dialog.setLocationRelativeTo(this); dialog.setLocationRelativeTo(this);
@ -1008,8 +994,6 @@ public class SeSimApplication extends javax.swing.JFrame {
public String name = "Euro"; public String name = "Euro";
}; };
/** /**
* @param args the command line arguments * @param args the command line arguments
* @throws java.lang.IllegalAccessException * @throws java.lang.IllegalAccessException
@ -1017,13 +1001,8 @@ public class SeSimApplication extends javax.swing.JFrame {
*/ */
public static void main(String args[]) throws IllegalAccessException, InstantiationException { public static void main(String args[]) throws IllegalAccessException, InstantiationException {
//opensesim.world.World.MasterApi mapi = new opensesim.world.World.MasterApi(); //opensesim.world.World.MasterApi mapi = new opensesim.world.World.MasterApi();
// testing // testing
Pojo p = new Pojo(); Pojo p = new Pojo();
Gson g = new Gson(); Gson g = new Gson();
@ -1034,16 +1013,8 @@ public class SeSimApplication extends javax.swing.JFrame {
JSONObject o = new JSONObject(p, new String[]{"name", "symbol"}); JSONObject o = new JSONObject(p, new String[]{"name", "symbol"});
System.out.printf("OJSON: %s\n", o.toString(8)); System.out.printf("OJSON: %s\n", o.toString(8));
// System.exit(0); // System.exit(0);
// end testing // end testing
// Initialize globals // Initialize globals
Class<?> c = opensesim.gui.SeSimApplication.class; Class<?> c = opensesim.gui.SeSimApplication.class;
Globals.initGlobals(c); Globals.initGlobals(c);
@ -1052,25 +1023,13 @@ public class SeSimApplication extends javax.swing.JFrame {
JSONObject world = new org.json.JSONObject(world_source); JSONObject world = new org.json.JSONObject(world_source);
Globals.world.putConfig(world); Globals.world.putConfig(world);
*/ */
//Globals.installLookAndFeels(); //Globals.installLookAndFeels();
// Globals.setLookAndFeel("Metal"); // Globals.setLookAndFeel("Metal");
//JDialog.setDefaultLookAndFeelDecorated(true); //JDialog.setDefaultLookAndFeelDecorated(true);
Globals.se = new Exchange(); Globals.se = new Exchange();
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, ""); Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, "");
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {

View File

@ -38,7 +38,7 @@ import opensesim.sesim.interfaces.GetJson;
import opensesim.util.Scollection; import opensesim.util.Scollection;
import opensesim.util.SeSimException; import opensesim.util.SeSimException;
import opensesim.util.idgenerator.IDGenerator; import opensesim.util.idgenerator.IDGenerator;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.Scheduler; import opensesim.world.scheduler.Scheduler;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -51,8 +51,8 @@ import org.json.JSONObject;
public class GodWorld implements GetJson, World { public class GodWorld implements GetJson, World {
@Override @Override
public void schedule(Scheduler.EventListener listener, Event arg, long t) { public void schedule(Scheduler.EventListener listener, long t) {
scheduler.startTimerTask(listener, arg, t); scheduler.startTimerTask(listener, t);
} }
public static final class JKEYS { public static final class JKEYS {

View File

@ -26,7 +26,7 @@
package opensesim.world; package opensesim.world;
import java.util.Collection; import java.util.Collection;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.Scheduler; import opensesim.world.scheduler.Scheduler;
/** /**
@ -61,8 +61,8 @@ public class RealWorld implements World{
} }
@Override @Override
public void schedule(Scheduler.EventListener listener, Event arg, long t) { public void schedule(Scheduler.EventListener listener, long t) {
godworld.schedule(listener, arg, t); godworld.schedule(listener, t);
} }
} }

View File

@ -26,7 +26,7 @@
package opensesim.world; package opensesim.world;
import java.util.Collection; import java.util.Collection;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.Scheduler.EventListener; import opensesim.world.scheduler.Scheduler.EventListener;
/** /**
@ -43,5 +43,5 @@ public interface World {
Collection<Trader> getTradersCollection(); Collection<Trader> getTradersCollection();
public void schedule(EventListener listener, Event arg, long t); public void schedule(EventListener listener, long t);
} }

View File

@ -25,10 +25,32 @@
*/ */
package opensesim.world.scheduler; package opensesim.world.scheduler;
import opensesim.world.scheduler.Scheduler.EventListener;
/** /**
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class Event { public class Event {
EventListener listener;
long curevtime;
long newevtime;
// int id;
Event(EventListener listener, long t) {
this.listener = listener;
newevtime = t;
// id = nextTimerTask.getAndAdd(1);
}
/* @Override
public int compareTo(Object o) {
return ((TimerTaskDef) o).id - this.id;
}
*/
} }

View File

@ -27,12 +27,9 @@ package opensesim.world.scheduler;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -57,14 +54,14 @@ public class Scheduler extends Thread {
return this.acceleration; return this.acceleration;
} }
private final SortedMap<Long, LinkedList<TimerTaskDef>> event_queue = new TreeMap<>(); private final SortedMap<Long, LinkedList<Event>> event_queue = new TreeMap<>();
public interface EventListener { public interface EventListener {
long receive(Event event); long receive(Event task);
long getID(); long getID();
} }
@ -149,7 +146,7 @@ public class Scheduler extends Thread {
AtomicInteger nextTimerTask = new AtomicInteger(0); AtomicInteger nextTimerTask = new AtomicInteger(0);
public class TimerTaskDef implements Comparable { /* public class TimerTaskDef {
EventListener listener; EventListener listener;
Event arg; Event arg;
@ -166,15 +163,12 @@ public class Scheduler extends Thread {
} }
@Override
public int compareTo(Object o) {
return ((TimerTaskDef) o).id - this.id;
}
} }
*/
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>(); //LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
ConcurrentLinkedQueue<TimerTaskDef> new_tasks = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<Event> new_tasks = new ConcurrentLinkedQueue<>();
/** /**
* *
@ -182,11 +176,11 @@ public class Scheduler extends Thread {
* @param time * @param time
* @return The TimerTask created * @return The TimerTask created
*/ */
public TimerTaskDef startTimerTask(EventListener listener, Event arg, long time) { public Event startTimerTask(EventListener listener, long time) {
long evtime = time + currentTimeMillis(); long evtime = time + currentTimeMillis();
TimerTaskDef task = new TimerTaskDef(listener,arg, evtime); Event task = new Event(listener, evtime);
new_tasks.add(task); new_tasks.add(task);
synchronized (this) { synchronized (this) {
@ -195,7 +189,7 @@ public class Scheduler extends Thread {
return task; return task;
} }
public void rescheduleTimerTask(TimerTaskDef task, long time) { public void rescheduleTimerTask(Event task, long time) {
long evtime = time + currentTimeMillis(); long evtime = time + currentTimeMillis();
task.newevtime = evtime; task.newevtime = evtime;
new_tasks.add(task); new_tasks.add(task);
@ -229,11 +223,11 @@ public class Scheduler extends Thread {
} }
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>(); // HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
private boolean addTimerTask(TimerTaskDef e) { private boolean addTimerTask(Event e) {
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime); // System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
// long evtime = time + currentTimeMillis(); // long evtime = time + currentTimeMillis();
LinkedList<TimerTaskDef> s = event_queue.get(e.newevtime); LinkedList<Event> s = event_queue.get(e.newevtime);
if (s == null) { if (s == null) {
s = new LinkedList<>(); s = new LinkedList<>();
event_queue.put(e.newevtime, s); event_queue.put(e.newevtime, s);
@ -251,13 +245,13 @@ public class Scheduler extends Thread {
cancel_queue.add(e); cancel_queue.add(e);
} }
private void cancelMy(TimerTaskDef e) { private void cancelMy(Event e) {
// Long evtime = tasks.get(e.curevtime); // Long evtime = tasks.get(e.curevtime);
// if (evtime == null) { // if (evtime == null) {
// return; // return;
// } // }
LinkedList<TimerTaskDef> s = event_queue.get(e.curevtime); LinkedList<Event> s = event_queue.get(e.curevtime);
if (s == null) { if (s == null) {
// System.out.printf("My not found\n"); // System.out.printf("My not found\n");
return; return;
@ -299,13 +293,13 @@ public class Scheduler extends Thread {
} }
// if (t <= ct) { // if (t <= ct) {
LinkedList<TimerTaskDef> s = event_queue.get(t); LinkedList<Event> s = event_queue.get(t);
Object rc; Object rc;
rc = event_queue.remove(t); rc = event_queue.remove(t);
while (s.size() > 0) { while (s.size() > 0) {
TimerTaskDef def = s.pop(); Event def = s.pop();
long next_t = this.fireEvent(def.listener,def.arg); long next_t = this.fireEvent(def.listener,def);
if (next_t == -1) if (next_t == -1)
continue; continue;
@ -363,7 +357,7 @@ public class Scheduler extends Thread {
while (!terminate) { while (!terminate) {
while (!new_tasks.isEmpty()) { while (!new_tasks.isEmpty()) {
TimerTaskDef td = new_tasks.poll(); Event td = new_tasks.poll();
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime); // System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
this.cancelMy(td); this.cancelMy(td);