Beispiel #1
0
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            log.Info("Starting program");

            //Loading configuration from file
            try
            {
                config = new ConfLoader().loadConfFromFile("LMAX_sender_config.txt");
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Debug(e.StackTrace.ToString());
                log.Error("Exit program");
                return;
            }
            random = new Random((int)DateTime.Now.Ticks / 10000);
            isQuit = isRefrash = false;
            Thread t = new Thread(new ThreadStart(KeyReadHandler));

            t.Name = "Key handler thread";
            t.Start();

            syncUsers = new List <string>();
            NewUsers  = new List <string>();

            //Create lock objects
            usersLock      = new Object();
            errorWriteLock = new Object();
            mustSyncLock   = new Object();
            randomLock     = new Object();
            newUserLock    = new Object();

            //Create necessary datas
            BlockingCollection <Operations> blokingQueue = new BlockingCollection <Operations>();

            //create sender db handler
            dbHandler = new ErrorDbHandler();

            //create object for manipulat with local DB
            localDbHandler = new LocalDbHandler();

            //Create error handler class for error solutions
            ErrorHandler errorHandler = new ErrorHandler("ytsnotify", "YTS_admin password");

            // Get datas from DB about users
            remoteDbHandler = new DataBase();

            try
            {
                lock (usersLock)
                {
                    idUsers = remoteDbHandler.getConnectedUsers(dbHandler, errorHandler);

                    /*
                     * idUsers = new Dictionary<int, List<TradingClass>>();
                     * List<TradingClass> list1 = new List<TradingClass>();
                     * List<TradingClass> list2 = new List<TradingClass>();
                     *
                     * for (int i = 0; i < 2; ++i)
                     * {
                     *  list.Add(new TradingClass("user_"+i,"userName_"+i,"Passwd_0"+i,"URL","userEmail",dbHandler));
                     *  list[i].Login();
                     *  Console.WriteLine("\tConnected users {0}", i);
                     * }
                     *
                     * list1.Add(new TradingClass("user_1", "userName_1", "Passwd_1", "URL", "userEmail", dbHandler));
                     * list2.Add(new TradingClass("user_2", "userName_2", "Passwd_2", "URL", "userEmail", dbHandler));
                     * list1[0].Login();
                     * list2[0].Login();
                     * idUsers.Add(10001, list1);
                     * idUsers.Add(10002, list2);*/
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Debug(e.StackTrace.ToString());
                log.Error("Exit program");
                log.Info("Exit program becouse of error");
                return;
            }
            System.Console.WriteLine("Get {0} users from DB", idUsers.Values.Count);

            //Create class for user control directly after create users
            externalClientManager = new ExternalClientManager();

            //Create listen port
            Type serviceType = typeof(ExternalClientManager);
            Uri  serviceUri  = new Uri("http://localhost:9081/");

            System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(serviceType, serviceUri);
            host.Open();

            // Start NETlistener to get new users from DB
            //NETlistener netListener = new NETlistener();

            DateTime time0 = new DateTime();
            DateTime oTime = new DateTime();

            //Create time reader from file
            DateFileWritter dateFile = new DateFileWritter();

            time0 = dateFile.ReadTime();

            //Create systemContainer to cach DB lastest data
            systemContainer = new SystemContainer(blokingQueue, time0);

            //Create sender class
            SenderClass sender = new SenderClass(blokingQueue);

            List <DBResult> res = new List <DBResult>();

            Console.WriteLine("Loaded time value : {0}", time0.ToString("yyyy-MM-dd HH:mm:ss.fff"));

            long start;
            long finish;
            long waitTime;

            while (true)
            {
                if (isQuit)
                {
                    Console.WriteLine("Buy....");
                    return;
                }
                //Створити нових фоловерів якщо вони є в списку
                //CreateFromMarketFollowers(time0);
                //Вирішити проблеми синхронізації
                ProcessUsersSolveProblem(time0);

                start = DateTime.Now.Ticks / 10000;

                res = (List <DBResult>)remoteDbHandler.GetDBResult(time0);

                if (res != null && res.Count > 0)
                {
                    System.Console.WriteLine("Get results : {0}", res.Count);
                    oTime = time0;
                    time0 = res[0].DbTime;
                    dateFile.WriteTime(time0);

                    foreach (DBResult item in res)
                    {
                        systemContainer.AddElement(item);
                    }
                    systemContainer.processSystems(idUsers, oTime);
                }

                finish   = DateTime.Now.Ticks / 10000;
                waitTime = MUST_WAIT - (finish - start);

                if (waitTime > 0)
                {
                    Thread.Sleep((int)waitTime);
                }
                Console.WriteLine("wait in main loop");
            }
            //netListener.ShutDown();
            sender.ShutDown();
        }