private void connectNewClients() { while (_numActiveClients == 0 && _newClients.size() == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (String client : _newClients) { // StockExchangeBroker keyB = _brokers.firstKey(); // StockExchangeBroker broker=_brokers.get(keyB);; StockExchangeBroker broker = _brokers.pollFirst(); ; if (broker.getNumOfClients() == N) { _stockExchangeStompClient.send("/topic/Connected", "connectFailed " + client + "\n"); _numActiveClients++; } else { if (broker.getNumOfClients() == 0) _numActiveCBrokers++; broker.addClient(client); _stockExchangeStompClient.send( "/topic/cConnected", "connected " + client + " " + broker.getName() + "\n"); _stockExchangeStompClient.subscribe("/topic/cDeals-" + client, this); _numActiveClients++; } _brokers.add(broker); } _newClients.clear(); }
public void startNewDay() { _numOfClosedBrockers = 0; _day++; _stockExchangeStompClient.send("/topic/Calendar", "newDay " + _day + "\n"); connectNewBrokers(); connectNewClients(); publishPrices(); }
private void computeDeals() { for (Company company : _companies.values()) { while ((company._buyOrders.size() > 0) && (company._sellOrders.size() > 0)) { StockOrder buy = company._buyOrders.get(company._buyOrders.firstKey()); StockOrder sell = company._sellOrders.get(company._sellOrders.firstKey()); if (sell.getPrice() > buy.getPrice()) break; double price = Math.min(buy.getPrice(), sell.getPrice()); int amount = Math.min(buy.getAmount(), sell.getAmount()); String mes = "deal " + buy.getClientName() + " " + buy.getBrokerName() + " " + sell.getClientName() + " " + sell.getBrokerName() + " " + buy.getStockName() + " " + amount + " " + price + "\n"; if (sell.getClientName().equals("StockExchange")) { _cash += price; _stockExchangeStompClient.send("/topic/bDeals-" + buy.getBrokerName(), mes); company._buyOrders.remove(buy.getClientName()); company._sellOrders.remove(sell.getClientName()); company.addDefaultOrder(); } else { _stockExchangeStompClient.send("/topic/bDeals-" + buy.getBrokerName(), mes); _stockExchangeStompClient.send("/topic/bDeals-" + sell.getBrokerName(), mes); company._buyOrders.remove(buy.getClientName()); company._sellOrders.remove(sell.getClientName()); } } } }
private void connectNewBrokers() { while (_numActiveCBrokers == 0 && _newBrokers.size() == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (String broker : _newBrokers) { _brokers.add(new StockExchangeBroker(broker)); _stockExchangeStompClient.send("/topic/bConnected", "connected " + broker + "\n"); } _newBrokers.clear(); }
private void disconnectClient(String client) { for (Company company : _companies.values()) { StockOrder order = company.getBuyOrders().get(client); while (order != null) { // remove buy orders company.getBuyOrders().remove(client); order = company.getBuyOrders().get(client); } order = company.getSellOrders().get(client); while (order != null) { // sell buy orders company.getSellOrders().remove(client); order = company.getSellOrders().get(client); } } _numActiveClients--; for (StockExchangeBroker broker : _brokers) broker.removeClient(client); // //_brokers.get(_clients.get(client)).decClientNum(); TODO: // _clients.remove(client); _stockExchangeStompClient.unsubscribe("/topic/cDeals-" + client, this); _stockExchangeStompClient.send("/topic/cDisconnected", "disconnected " + client + "\n"); }
private void publishPrices() { String mesg = "Prices " + _day + ":\n"; for (Company company : _companies.values()) mesg += company.getName() + " " + company.getPrice() + "\n"; _stockExchangeStompClient.send("/topic/Prices", mesg); }