private void Shutdown(O2GSession session, StatusListener statusListener, TableListener tableListener, O2GTableManager tableManager) { if (tableListener != null) { tableListener.UnsubscribeEvents(tableManager); } if (statusListener != null) { if (session != null) { session.logout(); } statusListener.WaitEvents(); } }
static void Main(string[] args) { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("GetOffers", loginParams, sampleParams); session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin); session.subscribeSessionStatus(statusListener); statusListener.Reset(); session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { TableListener tableListener = new TableListener(); O2GTableManager tableManager = session.getTableManager(); O2GTableManagerStatus managerStatus = tableManager.getStatus(); while (managerStatus == O2GTableManagerStatus.TablesLoading) { Thread.Sleep(50); managerStatus = tableManager.getStatus(); } if (managerStatus == O2GTableManagerStatus.TablesLoadFailed) { throw new Exception("Cannot refresh all tables of table manager"); } tableListener.SetInstrument(sampleParams.Instrument); tableListener.SubscribeEvents(tableManager); O2GOffersTable offers = null; offers = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers); tableListener.PrintOffers(offers, null); // Do nothing 10 seconds, let offers print Thread.Sleep(10000); Console.WriteLine("Done!"); tableListener.UnsubscribeEvents(tableManager); statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (session != null) { session.Dispose(); } } }
private void DoWork() { O2GSession session = null; O2GTableManager tableManager = null; StatusListener statusListener = null; TableListener tableListener = null; bool canUnsubscribeSessionStatus = false; try { session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); var sessionId = Guid.NewGuid().ToString("N"); statusListener = new StatusListener(session, sessionId); statusListener.OnStatus += (s, e) => { var status = e.Item.ToStatus(); OnStatus?.Invoke(this, new GenericArgs <Status>(status)); if (status == Status.Disconnected) { OnDisconnected?.Invoke(this, EventArgs.Empty); } }; session.subscribeSessionStatus(statusListener); session.login(userName, password, URL, connection.ToString()); if (statusListener.WaitEvents() && statusListener.Connected) { tableListener = new TableListener(testingMode); tableListener.OnOffer += (s, e) => OnOffer?.Invoke(this, new GenericArgs <Offer>(e.Item)); // improve this plus get rid of response listener tableListener.OnOfferIds += (s, e) => { //var request = GetSetSubscriptionStatusRequest(session, e.Item); //if (request == null) // throw new Exception("Cannot create request"); //var responseListener = new ResponseListener(); //session.subscribeResponse(responseListener); //responseListener.SetRequestID(request.RequestID); //session.sendRequest(request); //if (!responseListener.WaitEvents()) // throw new Exception("Response waiting timeout expired"); }; tableManager = session.getTableManager(); var managerStatus = tableManager.getStatus(); // max wait while (managerStatus == O2GTableManagerStatus.TablesLoading) { Thread.Sleep(50); managerStatus = tableManager.getStatus(); } if (managerStatus == O2GTableManagerStatus.TablesLoadFailed) { throw new Exception("TableManager refresh failed!"); } tableListener.SetSymbols( Enum.GetValues(typeof(Symbol)).Cast <Symbol>().ToList()); tableListener.SubscribeEvents(tableManager); O2GOffersTable offers = null; offers = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers); tableListener.HandleOffers(offers); cancelEvent.WaitOne(); OnCancelled?.Invoke(this, EventArgs.Empty); } } catch (Exception error) { OnError?.Invoke(this, new GenericArgs <Exception>(error)); } finally { Shutdown(session, statusListener, tableListener, tableManager); if (canUnsubscribeSessionStatus) { session.unsubscribeSessionStatus(statusListener); } if (session != null) { session.Dispose(); } } }