예제 #1
0
 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();
 }
예제 #2
0
 public void startNewDay() {
   _numOfClosedBrockers = 0;
   _day++;
   _stockExchangeStompClient.send("/topic/Calendar", "newDay " + _day + "\n");
   connectNewBrokers();
   connectNewClients();
   publishPrices();
 }
예제 #3
0
  public StockExchange(String server, int port, String login, String pass)
      throws FileNotFoundException, IOException, LoginException {
    _stockExchangeStompClient = new Client(server, port, login, pass);
    _companies = initCompanies();
    _brokers = new TreeSet<StockExchangeBroker>(new BrokerCompare());

    _cash = 0;
    _numOfClosedBrockers = 0;
    _day = -1;
    _numActiveClients = 0;
    _numActiveCBrokers = 0;
    _newBrokers = new Vector<String>();
    _newClients = new Vector<String>();
    // _clients= new HashMap<String,StockExchangeBroker>();
    _stockExchangeStompClient.subscribe("/topic/bConnect", this);
    _stockExchangeStompClient.subscribe("/topic/cConnect", this);
    _stockExchangeStompClient.subscribe("/topic/Orders", this);
    _stockExchangeStompClient.subscribe("/topic/cDisconnect", this);
  }
예제 #4
0
 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");
 }
예제 #5
0
 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());
       }
     }
   }
 }
예제 #6
0
 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();
 }
예제 #7
0
 private void publishPrices() {
   String mesg = "Prices " + _day + ":\n";
   for (Company company : _companies.values())
     mesg += company.getName() + " " + company.getPrice() + "\n";
   _stockExchangeStompClient.send("/topic/Prices", mesg);
 }