static void Main(string[] args)
        {
            O2GSession            session          = null;
            SessionStatusListener statusListener   = null;
            ResponseListener      responseListener = null;
            int iContingencyGroupType = 1; // OCO group

            try
            {
                Console.WriteLine("JoinNewGroup sample\n");

                ArgumentParser argParser = new ArgumentParser(args, "JoinNewGroup");

                argParser.AddArguments(ParserArgument.Login,
                                       ParserArgument.Password,
                                       ParserArgument.Url,
                                       ParserArgument.Connection,
                                       ParserArgument.SessionID,
                                       ParserArgument.Pin,
                                       ParserArgument.PrimaryID,
                                       ParserArgument.SecondaryID,
                                       ParserArgument.AccountID);

                argParser.ParseArguments();

                if (!argParser.AreArgumentsValid)
                {
                    argParser.PrintUsage();
                    return;
                }

                argParser.PrintArguments();

                LoginParams  loginParams  = argParser.LoginParams;
                SampleParams sampleParams = argParser.SampleParams;

                session        = O2GTransport.createSession();
                statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    responseListener = new ResponseListener(session);
                    session.subscribeResponse(responseListener);

                    O2GAccountRow account = GetAccount(session, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GRequest request = JoinToNewGroupRequest(session, sampleParams.AccountID, sampleParams.PrimaryID, sampleParams.SecondaryID, iContingencyGroupType);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }

                    List <string> orderIDList = new List <string>();
                    orderIDList.Add(sampleParams.PrimaryID);
                    orderIDList.Add(sampleParams.SecondaryID);
                    foreach (string sOrderID in orderIDList)
                    {
                        if (!IsOrderExists(session, sampleParams.AccountID, sOrderID, responseListener))
                        {
                            throw new Exception(string.Format("Order '{0}' does not exist", sOrderID));
                        }
                    }
                    responseListener.SetOrderIDs(orderIDList);
                    session.sendRequest(request);
                    if (responseListener.WaitEvents())
                    {
                        Console.WriteLine("Done!");
                    }
                    else
                    {
                        throw new Exception("Response waiting timeout expired");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (session != null)
                {
                    if (statusListener.Connected)
                    {
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                        if (responseListener != null)
                        {
                            session.unsubscribeResponse(responseListener);
                        }
                    }
                    session.unsubscribeSessionStatus(statusListener);
                    session.Dispose();
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            O2GSession session = null;
            int iContingencyGroupType = 1; // OCO group

            try
            {
                LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings);
                SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings);

                PrintSampleParams("JoinNewGroup", loginParams, sampleParams);

                session = O2GTransport.createSession();
                SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    ResponseListener responseListener = new ResponseListener(session);
                    session.subscribeResponse(responseListener);

                    O2GAccountRow account = GetAccount(session, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GRequest request = JoinToNewGroupRequest(session, sampleParams.AccountID, sampleParams.PrimaryID, sampleParams.SecondaryID, iContingencyGroupType);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }

                    List<string> orderIDList = new List<string>();
                    orderIDList.Add(sampleParams.PrimaryID);
                    orderIDList.Add(sampleParams.SecondaryID);
                    foreach (string sOrderID in orderIDList)
                    {
                        if (!IsOrderExists(session, sampleParams.AccountID, sOrderID, responseListener))
                        {
                            throw new Exception(string.Format("Order '{0}' does not exist", sOrderID));
                        }
                    }
                    responseListener.SetOrderIDs(orderIDList);
                    session.sendRequest(request);
                    if (responseListener.WaitEvents())
                    {
                        Console.WriteLine("Done!");
                    }
                    else
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    statusListener.Reset();
                    session.logout();
                    statusListener.WaitEvents();
                    session.unsubscribeResponse(responseListener);
                }
                session.unsubscribeSessionStatus(statusListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }