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.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() {
GodWorld world = new GodWorld(Globals.getWorld());
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 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();
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);
*/
}

View File

@ -38,6 +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;
@ -49,6 +50,11 @@ 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 static final class JKEYS {
public static final String ASSETS = "assets";
@ -78,15 +84,29 @@ public class GodWorld implements GetJson, World {
* @param 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() {
this(new JSONObject("{}"));
}
private void init(JSONObject cfg, boolean mt) {
this.scheduler = new Scheduler();
this.scheduler.start();
putJson(cfg);
}
private void putJson(JSONObject cfg) {
// Read assets
JSONArray jassets = cfg.optJSONArray(GodWorld.JKEYS.ASSETS);
@ -322,13 +342,12 @@ public class GodWorld implements GetJson, World {
Class cls;
try {
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) {
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return null;
}

View File

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