2018-12-08 11:11:08 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import opensesim.sesim.interfaces.GetJson;
|
2018-12-08 12:17:53 +01:00
|
|
|
import opensesim.util.Scollection;
|
2018-12-08 11:11:08 +01:00
|
|
|
import opensesim.util.SeSimException;
|
|
|
|
import opensesim.util.idgenerator.IDGenerator;
|
2018-12-17 19:27:45 +01:00
|
|
|
import opensesim.world.scheduler.Event;
|
2018-12-09 01:53:47 +01:00
|
|
|
import opensesim.world.scheduler.Scheduler;
|
2018-12-08 11:11:08 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author 7u83 <7u83@mail.ru>
|
|
|
|
*/
|
|
|
|
public class GodWorld implements GetJson, World {
|
|
|
|
|
2018-12-17 19:27:45 +01:00
|
|
|
@Override
|
|
|
|
public void schedule(Scheduler.EventListener listener, Event arg, long t) {
|
|
|
|
scheduler.startTimerTask(listener, arg, t);
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
public static final class JKEYS {
|
|
|
|
|
|
|
|
public static final String ASSETS = "assets";
|
|
|
|
public static final String EXCHANGES = "exchanges";
|
|
|
|
|
|
|
|
public static final String ASSET_SYMBOL = "symbol";
|
|
|
|
public static final String ASSET_TYPE = "type";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
/* HashSet<AbstractAsset> assetsById = new HashSet<>();
|
2018-12-08 11:11:08 +01:00
|
|
|
HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>();
|
2018-12-09 01:53:47 +01:00
|
|
|
*/
|
|
|
|
Scollection<String, AbstractAsset> assets = new Scollection<>();
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
IDGenerator assetIdGenerator = new IDGenerator();
|
|
|
|
IDGenerator orderIdGenerator = new IDGenerator();
|
|
|
|
|
|
|
|
HashSet<AssetPair> assetPairs = new HashSet<>();
|
|
|
|
|
2018-12-08 12:17:53 +01:00
|
|
|
//ArrayList<Exchange> exchanges = new ArrayList<>();
|
2018-12-09 01:53:47 +01:00
|
|
|
private Scheduler scheduler = new Scheduler();
|
2018-12-08 11:11:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a World object.
|
|
|
|
*
|
|
|
|
* @param cfg
|
|
|
|
*/
|
|
|
|
public GodWorld(JSONObject cfg) {
|
2018-12-17 19:27:45 +01:00
|
|
|
init(cfg,false);
|
2018-12-08 11:11:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:27:45 +01:00
|
|
|
public Scheduler getScheduler() {
|
|
|
|
return scheduler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setScheduler(Scheduler scheduler) {
|
|
|
|
this.scheduler = scheduler;
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
public GodWorld() {
|
|
|
|
this(new JSONObject("{}"));
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:27:45 +01:00
|
|
|
private void init(JSONObject cfg, boolean mt) {
|
|
|
|
this.scheduler = new Scheduler();
|
|
|
|
this.scheduler.start();
|
|
|
|
putJson(cfg);
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
private void putJson(JSONObject cfg) {
|
|
|
|
// Read assets
|
|
|
|
JSONArray jassets = cfg.optJSONArray(GodWorld.JKEYS.ASSETS);
|
|
|
|
if (jassets == null) {
|
|
|
|
jassets = new JSONArray();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < jassets.length(); i++) {
|
|
|
|
JSONObject o = jassets.optJSONObject(i);
|
|
|
|
AbstractAsset a;
|
|
|
|
try {
|
|
|
|
a = createAsset(o);
|
|
|
|
} catch (SeSimException ex) {
|
|
|
|
Logger.getLogger(RealWorld.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (a == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-08 12:17:53 +01:00
|
|
|
|
|
|
|
assets.add(a.getSymbol(), a);
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
2018-12-09 01:53:47 +01:00
|
|
|
|
|
|
|
// Read exchanges
|
|
|
|
JSONArray exs = cfg.optJSONArray(GodWorld.JKEYS.EXCHANGES);
|
|
|
|
if (exs == null) {
|
|
|
|
exs = new JSONArray();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < exs.length(); i++) {
|
|
|
|
JSONObject o = exs.optJSONObject(i);
|
2018-12-10 11:28:30 +01:00
|
|
|
if (o == null) {
|
2018-12-09 01:53:47 +01:00
|
|
|
continue;
|
2018-12-10 11:28:30 +01:00
|
|
|
}
|
2018-12-09 01:53:47 +01:00
|
|
|
createExchange(o);
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JSONObject getJson() {
|
|
|
|
JSONObject cfg = new JSONObject();
|
|
|
|
// Write assets
|
2018-12-09 01:53:47 +01:00
|
|
|
JSONArray out;
|
|
|
|
out = new JSONArray();
|
2018-12-08 11:11:08 +01:00
|
|
|
for (AbstractAsset asset : this.getAssetCollection()) {
|
2018-12-09 01:53:47 +01:00
|
|
|
out.put(asset.getJson());
|
|
|
|
}
|
|
|
|
cfg.put(GodWorld.JKEYS.ASSETS, out);
|
|
|
|
|
|
|
|
// Write exchanges
|
|
|
|
out = new JSONArray();
|
|
|
|
for (Exchange ex : this.getExchangeCollection()) {
|
|
|
|
out.put(ex.getJson());
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
2018-12-09 01:53:47 +01:00
|
|
|
cfg.put(GodWorld.JKEYS.EXCHANGES, out);
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long masterkey;
|
|
|
|
|
|
|
|
public GodWorld(JSONObject cfg, long masterkey) {
|
|
|
|
this.masterkey = masterkey;
|
|
|
|
putJson(cfg);
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
/* public boolean checkMasterKey(long masterkey) {
|
2018-12-08 11:11:08 +01:00
|
|
|
return masterkey == this.masterkey;
|
|
|
|
}
|
2018-12-09 01:53:47 +01:00
|
|
|
*/
|
2018-12-08 11:11:08 +01:00
|
|
|
public AbstractAsset createAsset(JSONObject cfg) throws SeSimException {
|
|
|
|
AbstractAsset a;
|
|
|
|
String class_name;
|
|
|
|
Class<AbstractAsset> cls;
|
|
|
|
|
|
|
|
try {
|
|
|
|
class_name = cfg.getString(JKEYS.ASSET_TYPE);
|
|
|
|
} catch (JSONException jex) {
|
|
|
|
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, jex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
cls = (Class<AbstractAsset>) Class.forName(class_name);
|
|
|
|
a = cls.getConstructor(GodWorld.class, JSONObject.class).newInstance(this, cfg);
|
|
|
|
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
|
|
|
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-08 12:17:53 +01:00
|
|
|
if (this.assets.get(a.getSymbol()) != null) {
|
2018-12-08 11:11:08 +01:00
|
|
|
throw new SeSimException("Already defined");
|
|
|
|
}
|
2018-12-09 01:53:47 +01:00
|
|
|
|
2018-12-08 12:17:53 +01:00
|
|
|
assets.add(a.getSymbol(), a);
|
2018-12-08 11:11:08 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
HashMap<String, Exchange> exchanges = new HashMap<>();
|
|
|
|
|
|
|
|
public void createExchange(JSONObject cfg) {
|
|
|
|
Exchange ex = new Exchange(this.getWorld(), cfg);
|
|
|
|
exchanges.put(ex.getSymbol(), ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Collection<Exchange> getExchangeCollection() {
|
|
|
|
return Collections.unmodifiableCollection(exchanges.values());
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:11:08 +01:00
|
|
|
public Collection<AbstractAsset> getAssetCollection() {
|
2018-12-08 12:17:53 +01:00
|
|
|
return assets.getCollection(); //Collections.unmodifiableCollection(assetsById);
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public AbstractAsset getAssetBySymbol(String symbol) {
|
2018-12-08 12:17:53 +01:00
|
|
|
return this.assets.get(symbol);
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<AssetPair> getAssetPairsCollection() {
|
|
|
|
return Collections.unmodifiableCollection(assetPairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(AssetPair pair) {
|
|
|
|
assetPairs.add(pair);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Exchange addExchange(String symbol) throws SeSimException {
|
|
|
|
for (Exchange ex : getExchangeCollection()) {
|
|
|
|
if (ex.getSymbol().equals(symbol)) {
|
|
|
|
throw (new SeSimException("Exchange already defined."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange ex = new Exchange(this, symbol);
|
|
|
|
// exchanges.add(ex);
|
|
|
|
// return ex;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public AbstractAsset createAsset(long key, JSONObject cfg) throws SeSimException{
|
|
|
|
if (key!=masterkey)
|
|
|
|
throw new SeSimException("Access denied.");
|
|
|
|
return this.createAsset_p(cfg);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// static final String JSON_ASSET = "asset";
|
|
|
|
// static final String JSON_EXCHANGES = "exchanges";
|
|
|
|
/*
|
|
|
|
public JSONObject getConfig() {
|
|
|
|
JSONObject cfg = new JSONObject();
|
|
|
|
|
|
|
|
// save assets
|
|
|
|
JSONArray arr;
|
|
|
|
arr = new JSONArray();
|
|
|
|
for (AbstractAsset a : getAssetCollection()) {
|
|
|
|
arr.put(a.getConfig());
|
|
|
|
}
|
|
|
|
cfg.put(JSON_ASSET, arr);
|
|
|
|
|
|
|
|
// save exchanges
|
|
|
|
arr = new JSONArray();
|
|
|
|
for (Exchange ex : getExchangeCollection()) {
|
|
|
|
arr.put(ex.getConfig());
|
|
|
|
}
|
|
|
|
cfg.put(JSON_EXCHANGES, arr);
|
|
|
|
|
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void putConfig(JSONObject cfg) {
|
|
|
|
JSONArray arr = cfg.optJSONArray(JSON_ASSET);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new JSONArray();
|
|
|
|
}
|
|
|
|
for (Object o : arr) {
|
|
|
|
JSONObject acfg = (JSONObject) o;
|
|
|
|
AbstractAsset.create(this, acfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* public AbstractAsset createAsset(Class cls, String symbol) throws Exception {
|
|
|
|
return AbstractAsset.create(this, cls, symbol);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
static public JSONArray toJson() {
|
|
|
|
|
|
|
|
JSONArray all = new JSONArray();
|
|
|
|
for (Map.Entry<Id, Asset> entry : assetsById.entrySet()) {
|
|
|
|
Id key = entry.getKey();
|
|
|
|
Asset value = entry.getValue();
|
|
|
|
all.put(value.getConfig());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return all;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Get the typename of an AbstractAsset class
|
2018-12-09 01:53:47 +01:00
|
|
|
*
|
2018-12-08 11:11:08 +01:00
|
|
|
* @param asset_type AbstractAsset
|
|
|
|
* @return the type name
|
|
|
|
*/
|
|
|
|
public static String getTypeName(Class<AbstractAsset> asset_type) {
|
|
|
|
Constructor<AbstractAsset> c;
|
|
|
|
try {
|
2018-12-10 11:28:30 +01:00
|
|
|
c = asset_type.getConstructor(GodWorld.class, JSONObject.class);
|
2018-12-08 11:11:08 +01:00
|
|
|
AbstractAsset ait = c.newInstance(null, null);
|
|
|
|
return ait.getTypeName();
|
|
|
|
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
|
|
|
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
|
2018-12-09 18:43:29 +01:00
|
|
|
return asset_type.getName();
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
public World getWorld() {
|
2018-12-08 12:17:53 +01:00
|
|
|
return new RealWorld(this);
|
|
|
|
}
|
2018-12-12 19:58:10 +01:00
|
|
|
|
2018-12-10 11:28:30 +01:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Stuff belonging to traders
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
private final HashSet<Trader> traders = new HashSet<>();
|
|
|
|
|
|
|
|
public Trader createTrader(JSONObject cfg) {
|
2018-12-12 19:58:10 +01:00
|
|
|
AbstractTrader trader;
|
|
|
|
String strategy = cfg.optString("strategy", null);
|
|
|
|
if (strategy == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Class cls;
|
|
|
|
try {
|
|
|
|
cls = (Class<Trader>) Class.forName(strategy);
|
2018-12-17 19:27:45 +01:00
|
|
|
trader = (AbstractTrader) cls.getConstructor(World.class, JSONObject.class).newInstance(null, cfg);
|
2018-12-12 19:58:10 +01:00
|
|
|
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
|
|
|
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:28:30 +01:00
|
|
|
@Override
|
|
|
|
public Collection<Trader> getTradersCollection() {
|
|
|
|
return Collections.unmodifiableCollection(traders);
|
|
|
|
}
|
2018-12-12 19:58:10 +01:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Stuff belonging to accounts
|
|
|
|
// --------------------------------------------------------------------
|
2018-12-08 11:11:08 +01:00
|
|
|
}
|