private synchronized void addToBook(BookSide side, Tradable trd) throws Exception {
    if (ProductService.getInstance().getMarketState().equals(MarketState.PREOPEN)) {
      switch (side) {
        case BUY:
          buySide.addToBook(trd);
          break;
        case SELL:
          sellSide.addToBook(trd);
          break;
      }
    } else {
      HashMap<String, FillMessage> allFills = null;

      switch (side) {
        case BUY:
          allFills = sellSide.tryTrade(trd);
          break;
        case SELL:
          allFills = buySide.tryTrade(trd);
          break;
      }

      if (allFills != null && !allFills.isEmpty()) {
        this.updateCurrentMarket();
        int difference = trd.getOriginalVolume() - trd.getRemainingVolume();
        Price lastSalePrice = this.determineLastSalePrice(allFills);
        LastSalePublisher.getInstance()
            .publishLastSale(this.productSymbol, lastSalePrice, difference);
      }

      if (trd.getRemainingVolume() > 0) {
        if (trd.getPrice().isMarket()) {
          CancelMessage cm =
              new CancelMessage(
                  trd.getUser(),
                  trd.getProduct(),
                  trd.getPrice(),
                  trd.getRemainingVolume(),
                  "Cancelled",
                  trd.getSide(),
                  trd.getId());

          MessagePublisher.getInstance().publishCancel(cm);
        } else {
          switch (side) {
            case BUY:
              buySide.addToBook(trd);
              break;
            case SELL:
              sellSide.addToBook(trd);
              break;
          }
        }
      }
    }
  }
  public synchronized void openMarket() throws Exception {
    Price bestBuyPrice = buySide.topOfBookPrice();
    Price bestSellPrice = sellSide.topOfBookPrice();

    if (bestBuyPrice != null && bestSellPrice != null) {
      while (bestBuyPrice.greaterOrEqual(bestSellPrice)
          || bestBuyPrice.isMarket()
          || bestSellPrice.isMarket()) {
        ArrayList<Tradable> topOfBuySide = buySide.getEntriesAtPrice(bestBuyPrice);
        ArrayList<Tradable> toRemove = new ArrayList<Tradable>();
        HashMap<String, FillMessage> allFills = null;

        for (Tradable t : topOfBuySide) {
          allFills = sellSide.tryTrade(t);
          if (t.getRemainingVolume() == 0) {
            toRemove.add(t);
          }
        }

        for (Tradable t : toRemove) {
          buySide.removeTradable(t);
        }
        updateCurrentMarket();

        Price lastSalePrice = this.determineLastSalePrice(allFills);
        int lastSaleVolume = this.determineLastSaleQuantity(allFills);

        LastSalePublisher.getInstance()
            .publishLastSale(productSymbol, lastSalePrice, lastSaleVolume);

        bestBuyPrice = buySide.topOfBookPrice();
        bestSellPrice = sellSide.topOfBookPrice();

        if (bestBuyPrice == null || bestSellPrice == null) break;
      }
    }
  }