This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ///////////////////////////////////////////////// | |
| // this file contains all the classes related to a | |
| // market. | |
| // ///////////////////////////////////////////////// | |
| var log4js = require('log4js'); | |
| var logger = log4js.getLogger(require('path').basename(__filename, '.js')); | |
| require('es6-collections'); | |
| var _ = require('underscore'); | |
| /** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| this.trade = function(){ | |
| var self = this; | |
| var sales = []; | |
| var productsInMarket = this.getProductsInMarket().values(); | |
| ... | |
| //trade each product in succession | |
| _.each(productsInMarket, function(productId){ | |
| var soldOutOfProduct = false; | |
| logger.debug('trading product ' + productId); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| prepareMarket(self.market, timeout); | |
| var sales = self.market.trade(); | |
| logger.info('trading completed'); | |
| noteMarketPricesAndVolumes(self.marketPrices, self.volumeRecords, sales); | |
| persistSale(sales, function(err){ | |
| if(err) logger.warn(err); | |
| else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| logger.info('setting up HTTP server for receiving commands'); | |
| var express = require('express') | |
| var app = express() | |
| var id = 0; | |
| app.get('/buy', function (req, res) { | |
| logger.info(id + ') buying "' + req.query.quantity + '" of "' + req.query.productId + '"'); | |
| ... | |
| }); | |
| app.get('/sell', function (req, res) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // //////////////// | |
| // Parent | |
| // //////////////// | |
| ... | |
| var cp = require('child_process'); | |
| ... | |
| //TODO use config to decide how many child processes to start | |
| var NUM_KIDS = 2; | |
| var PRODUCT_IDS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
| '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ch.maxant.tradingengine.model; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| import org.apache.logging.log4j.LogManager; | |
| import org.apache.logging.log4j.Logger; | |
| import ch.maxant.tradingengine.model.TradingEngine.Listener; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<Sale> trade() { | |
| List<Sale> sales = new ArrayList<>(); | |
| Set<String> productsInMarket = getProductsInMarket(); | |
| collectMarketInfo(); | |
| // trade each product in succession | |
| productsInMarket.stream() | |
| .forEach(productId -> { | |
| MutableBoolean soldOutOfProduct = new MutableBoolean(false); | |
| LOGGER.debug("trading product " + productId); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void run() { | |
| while (running) { | |
| prepareMarket(); | |
| List<Sale> sales = market.trade(); | |
| LOGGER.info("trading completed"); | |
| noteMarketPricesAndVolumes(sales); | |
| persistSale(sales); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TradingEngineThread extends Thread { | |
| private final TradingEngine engine; | |
| public TradingEngineThread(long delay, long timeout, Listener listener) throws NamingException { | |
| super("engine-" + ID++); | |
| engine = new TradingEngine(delay, timeout, listener); | |
| } | |
| @Override | |
| public void run() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @WebServlet(urlPatterns = { "/sell", "/buy", "/result" }) | |
| public class TradingEngineServlet extends HttpServlet { | |
| private static final Map<String, TradingEngineThread> kids = new HashMap<>(); | |
| static { | |
| int chunk = PRODUCT_IDS.length / NUM_KIDS; | |
| for (int i = 0, j = PRODUCT_IDS.length; i < j; i += chunk) { | |
| String[] temparray = Arrays.copyOfRange(PRODUCT_IDS, i, i + chunk); | |
| LOGGER.info("created engine for products " + temparray); | |
| TradingEngineThread engineThread = new TradingEngineThread(DELAY, TIMEOUT, (type, data) -> event(type, data)); | |
| for (int k = 0; k < temparray.length; k++) { |
OlderNewer