This file contains 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 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 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 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 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 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 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 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++) { |
This file contains 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
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
String path = req.getServletPath(); | |
LOGGER.debug("received command: '" + path + "'"); | |
String who = req.getParameter("userId"); | |
String productId = req.getParameter("productId"); | |
TradingEngineThread engine = kids.get(productId); | |
int quantity = Integer.parseInt(req.getParameter("quantity")); | |
int id = ID.getAndIncrement(); |
This file contains 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 = { "/sell2", "/buy2", "/result2" }) | |
public class TradingEngineServletWithActors extends HttpServlet { | |
private static final ActorSystem teSystem = ActorSystem.create("TradingEngines"); | |
private static final Map<String, ActorRef> 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); |
OlderNewer