TimerTaskDef moved out of Scheduler class, renamed to Event
This commit is contained in:
parent
998dad7e66
commit
648e9d5863
@ -53,10 +53,10 @@ import org.json.JSONObject;
|
||||
import opensesim.old_sesim.AutoTraderInterface;
|
||||
import opensesim.old_sesim.Exchange;
|
||||
import opensesim.old_sesim.Scheduler;
|
||||
import opensesim.old_sesim.Scheduler.TimerTaskDef;
|
||||
|
||||
import opensesim.world.GodWorld;
|
||||
import opensesim.world.World;
|
||||
import opensesim.world.scheduler.Event;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
|
||||
@ -65,25 +65,22 @@ import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SeSimApplication extends javax.swing.JFrame {
|
||||
|
||||
|
||||
GodWorld godworld;
|
||||
|
||||
GodWorld godworld;
|
||||
|
||||
/**
|
||||
* Creates new form NewMDIApplication
|
||||
*/
|
||||
public SeSimApplication() {
|
||||
|
||||
Globals.setXClassLoader();
|
||||
Globals.setLookAndFeel(Globals.prefs.get(Globals.PrefKeys.LAF, "Nimbus"));
|
||||
// Globals.setLookAndFeel("Metal");
|
||||
Globals.setLookAndFeel(Globals.prefs.get(Globals.PrefKeys.LAF, "Nimbus"));
|
||||
// Globals.setLookAndFeel("Metal");
|
||||
initComponents();
|
||||
|
||||
|
||||
JSONObject cfg;
|
||||
cfg = new JSONObject(Globals.prefs.get("world","{}"));
|
||||
cfg = new JSONObject(Globals.prefs.get("world", "{}"));
|
||||
godworld = new GodWorld(cfg);
|
||||
|
||||
|
||||
|
||||
// Get default screen and place our window
|
||||
// to the center of this screen
|
||||
@ -596,40 +593,24 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
}
|
||||
|
||||
void startSim() {
|
||||
|
||||
|
||||
GodWorld godworld = new GodWorld(Globals.getWorld());
|
||||
|
||||
|
||||
JSONObject cfg = new JSONObject("{"
|
||||
+ "strategy: opensesim.trader.SimpleTrader"
|
||||
+ "}");
|
||||
// world.createTrader(cfg);
|
||||
|
||||
// world.createTrader(cfg);
|
||||
|
||||
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;
|
||||
MyListener(World world){
|
||||
|
||||
MyListener(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
|
||||
public long getID() {
|
||||
@ -637,15 +618,23 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
//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());
|
||||
MyEvent arg = new MyEvent();
|
||||
|
||||
s.startTimerTask(listener, arg, WIDTH);
|
||||
|
||||
/*
|
||||
// MyEvent arg = new MyEvent();
|
||||
|
||||
s.startTimerTask(listener, 1000);
|
||||
|
||||
/*
|
||||
|
||||
resetSim();
|
||||
JSONObject jo = new JSONObject(Globals.prefs.get("Exchange", "{}"));
|
||||
@ -679,8 +668,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
|
||||
};
|
||||
// Globals.se.timer.startTimerTask(tt, 0);
|
||||
*/
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
void stopSim() {
|
||||
@ -972,105 +960,76 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
}//GEN-LAST:event_jMenuItem1ActionPerformed
|
||||
|
||||
private void assetsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assetsMenuItemActionPerformed
|
||||
|
||||
|
||||
|
||||
|
||||
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.setVisible(rootPaneCheckingEnabled);
|
||||
dialog.dispose();
|
||||
dialog.dispose();
|
||||
}//GEN-LAST:event_assetsMenuItemActionPerformed
|
||||
|
||||
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
|
||||
JDialog dialog = new NewJDialog(this,true);
|
||||
JDialog dialog = new NewJDialog(this, true);
|
||||
dialog.setVisible(true);
|
||||
dialog.dispose();
|
||||
|
||||
|
||||
}//GEN-LAST:event_jMenuItem2ActionPerformed
|
||||
|
||||
private void exchangesMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exchangesMenuItemActionPerformed
|
||||
|
||||
|
||||
//ExchangeListDialog.runDialog(this,this.godworld);
|
||||
ExchangeListDialog.runDialog(this, godworld);
|
||||
|
||||
}//GEN-LAST:event_exchangesMenuItemActionPerformed
|
||||
public static class Pojo {
|
||||
public static class Pojo {
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String symbol = "EUR";
|
||||
public String name = "Euro";
|
||||
};
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String symbol = "EUR";
|
||||
public String name = "Euro";
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
* @throws java.lang.IllegalAccessException
|
||||
* @throws java.lang.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
|
||||
|
||||
Pojo p = new Pojo();
|
||||
|
||||
|
||||
Gson g = new Gson();
|
||||
String r = g.toJson(p);
|
||||
|
||||
|
||||
System.out.printf("GSON: %s\n", r);
|
||||
|
||||
JSONObject o = new JSONObject(p,new String[]{"name","symbol"});
|
||||
System.out.printf("OJSON: %s\n", o.toString(8));
|
||||
|
||||
|
||||
|
||||
|
||||
// System.exit(0);
|
||||
|
||||
|
||||
|
||||
JSONObject o = new JSONObject(p, new String[]{"name", "symbol"});
|
||||
System.out.printf("OJSON: %s\n", o.toString(8));
|
||||
|
||||
// System.exit(0);
|
||||
// end testing
|
||||
|
||||
|
||||
|
||||
// Initialize globals
|
||||
Class<?> c = opensesim.gui.SeSimApplication.class;
|
||||
Globals.initGlobals(c);
|
||||
|
||||
/* String world_source = Globals.prefs.get("world", "{}");
|
||||
|
||||
/* String world_source = Globals.prefs.get("world", "{}");
|
||||
JSONObject world = new org.json.JSONObject(world_source);
|
||||
Globals.world.putConfig(world);
|
||||
*/
|
||||
|
||||
|
||||
*/
|
||||
//Globals.installLookAndFeels();
|
||||
|
||||
// Globals.setLookAndFeel("Metal");
|
||||
|
||||
//JDialog.setDefaultLookAndFeelDecorated(true);
|
||||
|
||||
|
||||
|
||||
|
||||
// Globals.setLookAndFeel("Metal");
|
||||
//JDialog.setDefaultLookAndFeelDecorated(true);
|
||||
Globals.se = new Exchange();
|
||||
|
||||
|
||||
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, "");
|
||||
|
||||
|
||||
|
||||
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -38,7 +38,7 @@ import opensesim.sesim.interfaces.GetJson;
|
||||
import opensesim.util.Scollection;
|
||||
import opensesim.util.SeSimException;
|
||||
import opensesim.util.idgenerator.IDGenerator;
|
||||
import opensesim.world.scheduler.Event;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@ -51,8 +51,8 @@ import org.json.JSONObject;
|
||||
public class GodWorld implements GetJson, World {
|
||||
|
||||
@Override
|
||||
public void schedule(Scheduler.EventListener listener, Event arg, long t) {
|
||||
scheduler.startTimerTask(listener, arg, t);
|
||||
public void schedule(Scheduler.EventListener listener, long t) {
|
||||
scheduler.startTimerTask(listener, t);
|
||||
}
|
||||
|
||||
public static final class JKEYS {
|
||||
|
@ -26,7 +26,7 @@
|
||||
package opensesim.world;
|
||||
|
||||
import java.util.Collection;
|
||||
import opensesim.world.scheduler.Event;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler;
|
||||
|
||||
/**
|
||||
@ -61,8 +61,8 @@ public class RealWorld implements World{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void schedule(Scheduler.EventListener listener, Event arg, long t) {
|
||||
godworld.schedule(listener, arg, t);
|
||||
public void schedule(Scheduler.EventListener listener, long t) {
|
||||
godworld.schedule(listener, t);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,7 @@
|
||||
package opensesim.world;
|
||||
|
||||
import java.util.Collection;
|
||||
import opensesim.world.scheduler.Event;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
|
||||
/**
|
||||
@ -43,5 +43,5 @@ public interface World {
|
||||
|
||||
Collection<Trader> getTradersCollection();
|
||||
|
||||
public void schedule(EventListener listener, Event arg, long t);
|
||||
public void schedule(EventListener listener, long t);
|
||||
}
|
||||
|
@ -25,10 +25,32 @@
|
||||
*/
|
||||
package opensesim.world.scheduler;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
*/
|
||||
}
|
@ -27,12 +27,9 @@ package opensesim.world.scheduler;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@ -57,14 +54,14 @@ public class Scheduler extends Thread {
|
||||
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 {
|
||||
|
||||
long receive(Event event);
|
||||
long receive(Event task);
|
||||
|
||||
long getID();
|
||||
}
|
||||
@ -149,7 +146,7 @@ public class Scheduler extends Thread {
|
||||
|
||||
AtomicInteger nextTimerTask = new AtomicInteger(0);
|
||||
|
||||
public class TimerTaskDef implements Comparable {
|
||||
/* public class TimerTaskDef {
|
||||
|
||||
EventListener listener;
|
||||
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<>();
|
||||
ConcurrentLinkedQueue<TimerTaskDef> new_tasks = new ConcurrentLinkedQueue<>();
|
||||
ConcurrentLinkedQueue<Event> new_tasks = new ConcurrentLinkedQueue<>();
|
||||
|
||||
/**
|
||||
*
|
||||
@ -182,11 +176,11 @@ public class Scheduler extends Thread {
|
||||
* @param time
|
||||
* @return The TimerTask created
|
||||
*/
|
||||
public TimerTaskDef startTimerTask(EventListener listener, Event arg, long time) {
|
||||
public Event startTimerTask(EventListener listener, long time) {
|
||||
|
||||
long evtime = time + currentTimeMillis();
|
||||
|
||||
TimerTaskDef task = new TimerTaskDef(listener,arg, evtime);
|
||||
Event task = new Event(listener, evtime);
|
||||
new_tasks.add(task);
|
||||
|
||||
synchronized (this) {
|
||||
@ -195,7 +189,7 @@ public class Scheduler extends Thread {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void rescheduleTimerTask(TimerTaskDef task, long time) {
|
||||
public void rescheduleTimerTask(Event task, long time) {
|
||||
long evtime = time + currentTimeMillis();
|
||||
task.newevtime = evtime;
|
||||
new_tasks.add(task);
|
||||
@ -229,11 +223,11 @@ public class Scheduler extends Thread {
|
||||
}
|
||||
|
||||
// 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);
|
||||
// long evtime = time + currentTimeMillis();
|
||||
LinkedList<TimerTaskDef> s = event_queue.get(e.newevtime);
|
||||
LinkedList<Event> s = event_queue.get(e.newevtime);
|
||||
if (s == null) {
|
||||
s = new LinkedList<>();
|
||||
event_queue.put(e.newevtime, s);
|
||||
@ -251,13 +245,13 @@ public class Scheduler extends Thread {
|
||||
cancel_queue.add(e);
|
||||
}
|
||||
|
||||
private void cancelMy(TimerTaskDef e) {
|
||||
private void cancelMy(Event e) {
|
||||
|
||||
// Long evtime = tasks.get(e.curevtime);
|
||||
// if (evtime == null) {
|
||||
// return;
|
||||
// }
|
||||
LinkedList<TimerTaskDef> s = event_queue.get(e.curevtime);
|
||||
LinkedList<Event> s = event_queue.get(e.curevtime);
|
||||
if (s == null) {
|
||||
// System.out.printf("My not found\n");
|
||||
return;
|
||||
@ -299,13 +293,13 @@ public class Scheduler extends Thread {
|
||||
}
|
||||
|
||||
// if (t <= ct) {
|
||||
LinkedList<TimerTaskDef> s = event_queue.get(t);
|
||||
LinkedList<Event> s = event_queue.get(t);
|
||||
Object rc;
|
||||
rc = event_queue.remove(t);
|
||||
|
||||
while (s.size() > 0) {
|
||||
TimerTaskDef def = s.pop();
|
||||
long next_t = this.fireEvent(def.listener,def.arg);
|
||||
Event def = s.pop();
|
||||
long next_t = this.fireEvent(def.listener,def);
|
||||
if (next_t == -1)
|
||||
continue;
|
||||
|
||||
@ -363,7 +357,7 @@ public class Scheduler extends Thread {
|
||||
while (!terminate) {
|
||||
|
||||
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);
|
||||
|
||||
this.cancelMy(td);
|
||||
|
Loading…
Reference in New Issue
Block a user