Esempio n. 1
0
        public override void Initialize()
        {
            {
                Log("Initializing Server");
                bool isFirstRun = !File.Exists(BasePath + DB.StampPath);
                if (isFirstRun)
                {
                    if (!Directory.Exists(BasePath + DB.DBPath))
                    {
                        Directory.CreateDirectory(BasePath + DB.DBPath);
                    }
                    if (!Directory.Exists(BasePath + DB.HistoryPath))
                    {
                        Directory.CreateDirectory(BasePath + DB.HistoryPath);
                    }

                    byte[] stamp = new byte[256];
                    random.NextBytes(stamp);
                    File.WriteAllBytes(BasePath + DB.StampPath, stamp);

                    Log($"Created new Data Base on {DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day} \n");
                    MessageBox.Show("Welcome To LankaStocks!\nNote :\n\tUsername - preLogin\n\tPassword - preLogin LankaS", "LanakaStocks > Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                LoadDatabasesFromDisk(isFirstRun);

                exe = new ServerExecute {
                    svr = this
                };

                Statics.ReCalculate();

                if (Live.Session.Name != DateTime.Now.ToString("yyyyMMdd"))
                {
                    Log($"Saving last day session {Live.Session.Name}", ConsoleColor.Cyan);
                    Live.Session.sessionEnd = DateTime.Now;
                    History.SaveSession(Live.Session);

                    Log("Starting a fresh Session just for you. Have a good day!", ConsoleColor.Cyan);
                    Live.Session.CreateNewDatabase();
                    foreach (var item in Live.Cashiers)
                    {
                        Live.Session.BeginingCashiers.Add(item.Key, item.Value);
                    }
                    foreach (var item in Live.Items)
                    {
                        Live.Session.BeginingItems.Add(item.Key, item.Value.Clone());
                    }
                }

                PeerTimer          = new System.Timers.Timer(10000);
                PeerTimer.Elapsed += PeerTimer_Elapsed;
                PeerTimer.Start();



                System.Threading.Thread DBSaveThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThrDBSave));
                DBSaveThread.Start();
            }
        }
Esempio n. 2
0
        public override void Initialize()
        {
            IsHost = false;
AquirePeerID:
            DateTime time = DateTime.Now;

            status = new PeerStatus()
            {
                ID           = Core.user?.ID.ToString("00"),
                LastActivity = time
            };

            exe = new ServerExecute {
                svr = this
            };

            Response resp = client.Peer(status.ID, 0U, "add me");

            if (resp.result == (byte)Response.Result.notfound)
            {
                (DBLive Live, DBPeople People, DBHistory History, DBSettings Settings, PeerStatus peer)tup = resp.obj;
                Live = tup.Live; People = tup.People; History = tup.History; Settings = tup.Settings;

                status.ID = tup.peer.ID; status.LastActivity = tup.peer.LastActivity; status.LastPoint = tup.peer.LastPoint;

                Log($"Using peer ID {status.ID}");
                Log("Downloaded Peer Database");
            }
            else if (resp.result == (byte)Response.Result.ok)
            {
                //Somene is using that PeerID
                Log($"Somene is using that PeerID. Changing...");
                goto AquirePeerID;
            }
            else
            {
                Log($"Fatel error. Cannot Download Peer Databases. {resp.msg} \t {resp.expr}");
            }



            timer          = new System.Timers.Timer(3000);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }