/// <summary>
 /// ctor
 /// </summary>
 /// <param name="responseListener"></param>
 public TableListener(ResponseListener responseListener)
 {
     mOrderMonitor = null;
     mResponseListener = responseListener;
     mTradesTable = null;
     mRequestID = string.Empty;
 }
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="session"></param>
 public ResponseListener(O2GSession session)
 {
     mRequestID = string.Empty;
     mResponse = null;
     mOrderMonitor = null;
     mTradesTable = null;
     mSyncResponseEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
     mSession = session;
 }
 public void onAdded(string sRowID, O2GRow rowData)
 {
     O2GTableType type = rowData.TableType;
     switch (type)
     {
         case O2GTableType.Orders:
             O2GOrderRow orderRow = (O2GOrderRow)rowData;
             if (mRequestID.Equals(orderRow.RequestID))
             {
                 if ((OrderMonitorNetting.IsClosingOrder(orderRow) || OrderMonitorNetting.IsOpeningOrder(orderRow)) &&
                         mOrderMonitor == null)
                 {
                     Console.WriteLine("The order has been added. Order ID: {0}, Rate: {1}, Time In Force: {2}",
                         orderRow.OrderID,
                         orderRow.Rate,
                         orderRow.TimeInForce);
                     O2GTradeRow trade = null;
                     string sTradeID = orderRow.TradeID;
                     if (mTradesTable != null)
                     {
                         for (int j = 0; j < mTradesTable.Count; j++)
                         {
                             if (sTradeID.Equals(mTradesTable.getRow(j).TradeID))
                             {
                                 trade = mTradesTable.getRow(j);
                                 break;
                             }
                         }
                     }
                     if (trade == null)
                     {
                         mOrderMonitor = new OrderMonitorNetting(orderRow);
                     }
                     else
                     {
                         mOrderMonitor = new OrderMonitorNetting(orderRow, trade.Amount);
                     }
                 }
             }
             break;
         case O2GTableType.Trades:
             O2GTradeRow tradeRow = (O2GTradeRow)rowData;
             if (mOrderMonitor != null)
             {
                 mOrderMonitor.OnTradeAdded(tradeRow);
                 if (mOrderMonitor.IsOrderCompleted)
                 {
                     PrintResult();
                     mResponseListener.StopWaiting();
                 }
             }
             break;
         case O2GTableType.ClosedTrades:
             O2GClosedTradeRow closedTradeRow = (O2GClosedTradeRow)rowData;
             if (mOrderMonitor != null)
             {
                 mOrderMonitor.OnClosedTradeAdded(closedTradeRow);
                 if (mOrderMonitor.IsOrderCompleted)
                 {
                     PrintResult();
                     mResponseListener.StopWaiting();
                 }
             }
             break;
         case O2GTableType.Messages:
             O2GMessageRow messageRow = (O2GMessageRow)rowData;
             if (mOrderMonitor != null)
             {
                 mOrderMonitor.OnMessageAdded(messageRow);
                 if (mOrderMonitor.IsOrderCompleted)
                 {
                     PrintResult();
                     mResponseListener.StopWaiting();
                 }
             }
             break;
     }
 }
 public void onTablesUpdates(O2GResponse data)
 {
     O2GResponseReaderFactory factory = mSession.getResponseReaderFactory();
     if (factory != null)
     {
         O2GTablesUpdatesReader reader = factory.createTablesUpdatesReader(data);
         for (int ii = 0; ii < reader.Count; ii++)
         {
             switch (reader.getUpdateTable(ii))
             {
                 case O2GTableType.Accounts:
                     O2GAccountRow account = reader.getAccountRow(ii);
                     //Show balance updates
                     Console.WriteLine("Balance: {0}", account.Balance);
                     break;
                 case O2GTableType.Orders:
                     O2GOrderRow order = reader.getOrderRow(ii);
                     switch (reader.getUpdateType(ii))
                     {
                         case O2GTableUpdateType.Insert:
                             if ((OrderMonitorNetting.IsClosingOrder(order) || OrderMonitorNetting.IsOpeningOrder(order)) &&
                                     mOrderMonitor == null)
                             {
                                 Console.WriteLine("The order has been added. Order ID: {0}, Rate: {1}, Time In Force: {2}",
                                     order.OrderID,
                                     order.Rate,
                                     order.TimeInForce);
                                 O2GTradeRow tradeRow = null;
                                 string sTradeID = order.TradeID;
                                 if (mTradesTable != null)
                                 {
                                     for (int j = 0; j < mTradesTable.Count; j++)
                                     {
                                         if (sTradeID.Equals(mTradesTable.getRow(j).TradeID))
                                         {
                                             tradeRow = mTradesTable.getRow(j);
                                             break;
                                         }
                                     }
                                 }
                                 if (tradeRow == null)
                                 {
                                     mOrderMonitor = new OrderMonitorNetting(order);
                                 }
                                 else
                                 {
                                     mOrderMonitor = new OrderMonitorNetting(order, tradeRow.Amount);
                                 }
                             }
                             break;
                         case O2GTableUpdateType.Delete:
                             if (mOrderMonitor != null)
                             {
                                 Console.WriteLine("The order has been deleted. Order ID: {0}", order.OrderID);
                                 mOrderMonitor.OnOrderDeleted(order);
                                 if (mOrderMonitor.IsOrderCompleted)
                                 {
                                     PrintResult();
                                     mSyncResponseEvent.Set();
                                 }
                             }
                             break;
                     }
                     break;
                 case O2GTableType.Trades:
                     O2GTradeRow trade = reader.getTradeRow(ii);
                     switch (reader.getUpdateType(ii))
                     {
                         case O2GTableUpdateType.Insert:
                             if (mOrderMonitor != null)
                             {
                                 mOrderMonitor.OnTradeAdded(trade);
                                 if (mOrderMonitor.IsOrderCompleted)
                                 {
                                     PrintResult();
                                     mSyncResponseEvent.Set();
                                 }
                             }
                             break;
                         case O2GTableUpdateType.Update:
                             if (mOrderMonitor != null)
                             {
                                 mOrderMonitor.OnTradeUpdated(trade);
                                 if (mOrderMonitor.IsOrderCompleted)
                                 {
                                     PrintResult();
                                     mSyncResponseEvent.Set();
                                 }
                             }
                             break;
                     }
                     break;
                 case O2GTableType.ClosedTrades:
                     O2GClosedTradeRow closedTrade = reader.getClosedTradeRow(ii);
                     if (reader.getUpdateType(ii) == O2GTableUpdateType.Insert)
                     {
                         if (mOrderMonitor != null)
                         {
                             mOrderMonitor.OnClosedTradeAdded(closedTrade);
                             if (mOrderMonitor.IsOrderCompleted)
                             {
                                 PrintResult();
                                 mSyncResponseEvent.Set();
                             }
                         }
                     }
                     break;
                 case O2GTableType.Messages:
                     O2GMessageRow message = reader.getMessageRow(ii);
                     if (reader.getUpdateType(ii) == O2GTableUpdateType.Insert)
                     {
                         if (mOrderMonitor != null)
                         {
                             mOrderMonitor.OnMessageAdded(message);
                             if (mOrderMonitor.IsOrderCompleted)
                             {
                                 PrintResult();
                                 mSyncResponseEvent.Set();
                             }
                         }
                     }
                     break;
             }
         }
     }
 }