some work on the scheduler

This commit is contained in:
7u83 2018-12-17 19:27:45 +01:00
parent 9008eed00b
commit 918bc91e16
6 changed files with 143 additions and 36 deletions

View File

@ -54,6 +54,10 @@ import opensesim.old_sesim.Exchange;
import opensesim.old_sesim.Scheduler; import opensesim.old_sesim.Scheduler;
import opensesim.world.GodWorld; import opensesim.world.GodWorld;
import opensesim.world.World;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.Scheduler.EventListener;
/** /**
* *
@ -592,14 +596,43 @@ public class SeSimApplication extends javax.swing.JFrame {
void startSim() { void startSim() {
GodWorld world = new GodWorld(Globals.getWorld()); GodWorld godworld = new GodWorld(Globals.getWorld());
JSONObject cfg = new JSONObject("{" JSONObject cfg = new JSONObject("{"
+ "strategy: opensesim.trader.SimpleTrader" + "strategy: opensesim.trader.SimpleTrader"
+ "}"); + "}");
world.createTrader(cfg); // world.createTrader(cfg);
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
class MyListener implements EventListener{
World world;
MyListener(World world){
this.world = world;
}
@Override
public long receive(Event event) {
System.out.println("Received an Event");
world.schedule(this, event, 2000);
return -1;
}
@Override
public long getID() {
return 1;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
MyListener listener = new MyListener(godworld.getWorld());
Event arg = new Event();
s.startTimerTask(listener, arg, WIDTH);
/*
resetSim(); resetSim();
JSONObject jo = new JSONObject(Globals.prefs.get("Exchange", "{}")); JSONObject jo = new JSONObject(Globals.prefs.get("Exchange", "{}"));
@ -633,6 +666,7 @@ public class SeSimApplication extends javax.swing.JFrame {
}; };
// Globals.se.timer.startTimerTask(tt, 0); // Globals.se.timer.startTimerTask(tt, 0);
*/
} }

View File

@ -38,6 +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;
@ -49,6 +50,11 @@ import org.json.JSONObject;
*/ */
public class GodWorld implements GetJson, World { public class GodWorld implements GetJson, World {
@Override
public void schedule(Scheduler.EventListener listener, Event arg, long t) {
scheduler.startTimerTask(listener, arg, t);
}
public static final class JKEYS { public static final class JKEYS {
public static final String ASSETS = "assets"; public static final String ASSETS = "assets";
@ -78,15 +84,29 @@ public class GodWorld implements GetJson, World {
* @param cfg * @param cfg
*/ */
public GodWorld(JSONObject cfg) { public GodWorld(JSONObject cfg) {
init(cfg,false);
putJson(cfg);
} }
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public GodWorld() { public GodWorld() {
this(new JSONObject("{}")); this(new JSONObject("{}"));
} }
private void init(JSONObject cfg, boolean mt) {
this.scheduler = new Scheduler();
this.scheduler.start();
putJson(cfg);
}
private void putJson(JSONObject cfg) { private void putJson(JSONObject cfg) {
// Read assets // Read assets
JSONArray jassets = cfg.optJSONArray(GodWorld.JKEYS.ASSETS); JSONArray jassets = cfg.optJSONArray(GodWorld.JKEYS.ASSETS);
@ -322,13 +342,12 @@ public class GodWorld implements GetJson, World {
Class cls; Class cls;
try { try {
cls = (Class<Trader>) Class.forName(strategy); cls = (Class<Trader>) Class.forName(strategy);
trader = (AbstractTrader) cls.getConstructor(JSONObject.class).newInstance(cfg); trader = (AbstractTrader) cls.getConstructor(World.class, JSONObject.class).newInstance(null, cfg);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
return null; return null;
} }
return null; return null;
} }

View File

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

View File

@ -26,6 +26,8 @@
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;
/** /**
* *
@ -40,4 +42,6 @@ public interface World {
Collection<Exchange> getExchangeCollection(); Collection<Exchange> getExchangeCollection();
Collection<Trader> getTradersCollection(); Collection<Trader> getTradersCollection();
public void schedule(EventListener listener, Event arg, long t);
} }

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * 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.
*
* 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 opensesim.world.scheduler;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Event {
}

View File

@ -44,9 +44,9 @@ public class Scheduler extends Thread {
private double acceleration = 1.0; private double acceleration = 1.0;
public void setAcceleration(double val) { public void setAcceleration(double acceleration) {
this.acceleration = val; this.acceleration = acceleration;
synchronized (this) { synchronized (this) {
this.notify(); this.notify();
} }
@ -58,11 +58,14 @@ public class Scheduler extends Thread {
} }
private final SortedMap<Long, SortedSet<TimerTaskDef>> event_queue = new TreeMap<>(); private final SortedMap<Long, SortedSet<TimerTaskDef>> event_queue = new TreeMap<>();
public interface TimerTaskRunner {
long timerTask(); public interface EventListener {
long receive(Event event);
long getID(); long getID();
} }
@ -93,7 +96,7 @@ public class Scheduler extends Thread {
@Override @Override
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
return (((TimerTaskRunner) o1).getID() - ((TimerTaskRunner) o2).getID()) < 0 ? -1 : 1; return (((EventListener) o1).getID() - ((EventListener) o2).getID()) < 0 ? -1 : 1;
//return System.identityHashCode(o1) - System.identityHashCode(o2); //return System.identityHashCode(o1) - System.identityHashCode(o2);
} }
} }
@ -148,13 +151,16 @@ public class Scheduler extends Thread {
public class TimerTaskDef implements Comparable { public class TimerTaskDef implements Comparable {
TimerTaskRunner taskRunner; EventListener listener;
Event arg;
long curevtime; long curevtime;
long newevtime; long newevtime;
int id; int id;
TimerTaskDef(TimerTaskRunner e, long t) { TimerTaskDef(EventListener listener, Event arg, long t) {
taskRunner = e; this.listener = listener;
this.arg=arg;
newevtime = t; newevtime = t;
id = nextTimerTask.getAndAdd(1); id = nextTimerTask.getAndAdd(1);
@ -168,20 +174,20 @@ public class Scheduler extends Thread {
} }
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>(); //LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
ConcurrentLinkedQueue<TimerTaskDef> set_tasks = new ConcurrentLinkedQueue<>(); ConcurrentLinkedQueue<TimerTaskDef> new_tasks = new ConcurrentLinkedQueue<>();
/** /**
* *
* @param e * @param listener
* @param time * @param time
* @return The TimerTask created * @return The TimerTask created
*/ */
public TimerTaskDef startTimerTask(TimerTaskRunner e, long time) { public TimerTaskDef startTimerTask(EventListener listener, Event arg, long time) {
long evtime = time + currentTimeMillis(); long evtime = time + currentTimeMillis();
TimerTaskDef task = new TimerTaskDef(e, evtime); TimerTaskDef task = new TimerTaskDef(listener,arg, evtime);
set_tasks.add(task); new_tasks.add(task);
synchronized (this) { synchronized (this) {
notify(); notify();
@ -192,7 +198,7 @@ public class Scheduler extends Thread {
public void rescheduleTimerTask(TimerTaskDef task, long time) { public void rescheduleTimerTask(TimerTaskDef task, long time) {
long evtime = time + currentTimeMillis(); long evtime = time + currentTimeMillis();
task.newevtime = evtime; task.newevtime = evtime;
set_tasks.add(task); new_tasks.add(task);
synchronized (this) { synchronized (this) {
notify(); notify();
@ -218,8 +224,8 @@ public class Scheduler extends Thread {
return pause; return pause;
} }
public long fireEvent(TimerTaskRunner e) { public long fireEvent(EventListener e, Event arg) {
return e.timerTask(); return e.receive(arg);// .receive(e,arg);
} }
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>(); // HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
@ -239,9 +245,9 @@ public class Scheduler extends Thread {
return s.add(e); return s.add(e);
} }
private final LinkedList<TimerTaskRunner> cancel_queue = new LinkedList(); private final LinkedList<EventListener> cancel_queue = new LinkedList();
public void cancelTimerTask(TimerTaskRunner e) { public void cancelTimerTask(EventListener e) {
cancel_queue.add(e); cancel_queue.add(e);
} }
@ -302,14 +308,15 @@ public class Scheduler extends Thread {
Iterator<TimerTaskDef> it = s.iterator(); Iterator<TimerTaskDef> it = s.iterator();
while (it.hasNext()) { while (it.hasNext()) {
TimerTaskDef e = it.next(); TimerTaskDef def = it.next();
// if (s.size() > 1) {
// System.out.printf("Sicku: %d %d\n", e.id, e.curevtime);
// }
long next_t = this.fireEvent(e.taskRunner);
e.newevtime = next_t + t; long next_t = this.fireEvent(def.listener,def.arg);
this.addTimerTask(e); if (next_t == -1)
continue;
def.newevtime = next_t + t;
this.addTimerTask(def);
} }
return 0; return 0;
@ -317,10 +324,11 @@ public class Scheduler extends Thread {
} }
class EmptyCtr implements TimerTaskRunner { /*
class EmptyCtr implements EventListener {
@Override @Override
public long timerTask() { public long receive() {
// System.out.printf("Current best brice %f\n", Globals.se.getBestPrice()); // System.out.printf("Current best brice %f\n", Globals.se.getBestPrice());
return 1000; return 1000;
} }
@ -332,10 +340,11 @@ public class Scheduler extends Thread {
} }
} }
*/
void initScheduler() { void initScheduler() {
current_time_millis = 0.0; current_time_millis = 0.0;
this.startTimerTask(new EmptyCtr(), 0); // this.startTimerTask(new EmptyCtr(), 0);
terminate = false; terminate = false;
} }
@ -345,8 +354,8 @@ public class Scheduler extends Thread {
while (!terminate) { while (!terminate) {
while (!set_tasks.isEmpty()) { while (!new_tasks.isEmpty()) {
TimerTaskDef td = set_tasks.poll(); TimerTaskDef 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);