Esempio n. 1
0
        public MasterServer(MasterServerConfig c, GalacticProperties gp, IEnumerable <PSystemModel> allSystemModels, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer)
        {
            _config = c;

            Dictionary <IDTypes, GlobalIDManager> globalIDManagers = new Dictionary <IDTypes, GlobalIDManager>();

            globalIDManagers.Add(IDTypes.GalaxyID, new GlobalGalaxyIDManager(dbIdIoService, gp));
            globalIDManagers.Add(IDTypes.TeamID, new GlobalTeamIDManager(dbIdIoService, gp));
            globalIDManagers.Add(IDTypes.TransactionID, new GlobalTransactionIDManager(dbIdIoService, gp));
            globalIDManagers.Add(IDTypes.AccountID, new GlobalAccountIDManager(dbIdIoService, gp));

            GlobalIDManagers = globalIDManagers;

            _redisServer     = redisServer;
            _databaseManager = dbm;

            _systemIDToSlaveServer = new ConcurrentDictionary <int, SlaveServer>();

            AccountManager_MasterServer acm = new AccountManager_MasterServer(new LocalIDManager_MS((GlobalAccountIDManager)globalIDManagers[IDTypes.AccountID], IDTypes.AccountID), dbm, true);

            _allSystemModels = new Dictionary <int, PSystemModel>();
            foreach (var sm in allSystemModels)
            {
                if (!_allSystemModels.ContainsKey(sm.Id))
                {
                    _allSystemModels.Add(sm.Id, sm);
                }
            }

            _redisServer.Subscribe(MessageTypes.Redis_SlaveConnectionRequest, _slaveConnected);
            _redisServer.Subscribe(MessageTypes.Redis_IDRequest, _handleIDRequest);

            _initTimeMs = TimeKeeper.MsSinceInitialization;//If this class gets an initializer or a reset method, this should be moved there.
        }
Esempio n. 2
0
        public DBFiller(DBFillerConfig config, IDatabaseManager databaseManager)
        {
            _config = config;
            gp      = new GalacticProperties();
            dbm     = databaseManager;

            MockServer = new MinimalMockServer(gp, databaseManager, databaseManager);

            _dbFillerUtils = new DBFillerUtils(MockServer.DatabaseManager, MockServer.GalaxyManager, MockServer.LocatorService, MockServer.WarpManager, MockServer.GalaxyIDManager);
        }
Esempio n. 3
0
        void InitializeIdData(GalacticProperties gp)
        {
            foreach (var idd in gp.IdProperties.Values)
            {
                IdData idData = new IdData(idd.IDType,
                                           idd.LastIDAdded,
                                           new HashSet <int>(Enumerable.Range(0, idd.LastIDAdded + 1).Where(i => !idd.ReservedIDs.Contains(i))),
                                           new HashSet <int>(),
                                           idd.ReservedIDs);

                DbIdIoService.SaveIdDataAsync(idData).Wait();
            }
        }
Esempio n. 4
0
        public MinimalMockServer(GalacticProperties gp, IDatabaseManager databaseManager, IDbIdIoService dbIdIoService)
        {
            Logger.Initialize();

            DatabaseManager = databaseManager;
            DbIdIoService   = dbIdIoService;

            //Minimal initializations
            GalaxyIDManager  = new LocalIDManager(null, IDTypes.GalaxyID);
            TeamIDManager    = new LocalIDManager(null, IDTypes.TeamID);
            accountIDManager = new LocalIDManager(null, IDTypes.AccountID);

            InitializeIdData(gp);

            globalGalaxyIDManager = new GlobalGalaxyIDManager(dbIdIoService, gp);
            GenerateIDsForLocalIDManager(globalGalaxyIDManager, GalaxyIDManager, gp.IdProperties[IDTypes.GalaxyID].LastIDAdded);

            globalTeamIDManager = new GlobalTeamIDManager(dbIdIoService, gp);
            GenerateIDsForLocalIDManager(globalTeamIDManager, TeamIDManager, gp.IdProperties[IDTypes.TeamID].LastIDAdded);

            globalAccountIDManager = new GlobalAccountIDManager(dbIdIoService, gp);
            GenerateIDsForLocalIDManager(globalAccountIDManager, accountIDManager, gp.IdProperties[IDTypes.AccountID].LastIDAdded);

            _redisServer = new RedisServer(Logger.LogRedisError, Logger.LogRedisInfo, new RedisConfig().Address);


            TeamManager        = new GlobalTeamManager(TeamIDManager, null, null, null, DatabaseManager);
            PlayerManager      = new PlayerManager(DatabaseManager, null, _redisServer, GalaxyIDManager, null);
            GalaxyManager      = new GalaxyManager(gp.SolID, TeamManager);
            shipManager        = new ShipManager(null, GalaxyManager, null, null, DatabaseManager);
            CollisionManager   = new CollisionManager(GalaxyManager, null, null, null);
            GalaxyGenerator    = new DebugGalaxyGenerator(PlayerManager, GalaxyManager, shipManager);
            AccountManager     = new AccountManager_MasterServer(accountIDManager, DatabaseManager, false);
            CargoSynchronizer  = new Server.Managers.CargoSynchronizer();
            StructureManager   = new StructureManager(DatabaseManager, GalaxyManager, GalaxyIDManager, CargoSynchronizer);
            RegistrationManger = new GalaxyRegistrationManager(GalaxyManager, shipManager, CollisionManager, GalaxyIDManager, PlayerManager, null, CargoSynchronizer, StructureManager);
            ColonyFactory.Initialize(GalaxyIDManager, RegistrationManger);
            StructureFactory.Initialize(GalaxyIDManager, RegistrationManger);
            LocatorService = new LocatorService(RegistrationManger, PlayerManager, GalaxyManager, shipManager, AccountManager, TeamManager, TeamManager, null, StructureManager, null);

            WarpManager = new WarpManager(GalaxyManager, null, null, _redisServer, AccountManager, null);

            CargoSynchronizer.Start(10, 4);
        }
Esempio n. 5
0
        /// <summary>
        /// Sets IsMasterServer=true if succesful
        /// </summary>
        /// <param name="c"></param>
        /// <param name="gp"></param>
        /// <param name="allSystemModels"></param>
        /// <param name="dbm"></param>
        /// <param name="redisServer"></param>
        void _promoteToMasterServer(MasterServerConfig c, GalacticProperties gp, IEnumerable <PSystemModel> allSystemModels, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer)
        {
            #if DEBUG
            ConsoleManager.WriteLine("Promoting to master server...", ConsoleMessageType.Notification);
            #endif


            string masterServerID = Rand.Random.Next(-int.MaxValue, int.MaxValue).ToString();


            bool setSuccessful = _redisServer.SetValue(
                RedisDBKeyTypes.MasterServerTimestamp,
                new MasterServerTimestamp()
            {
                MasterServerID = masterServerID
            },
                new TimeSpan(0, 0, 0, 0, c.InitializationTimestampTimeoutMS),
                SetWhen.NotExists
                );

            // Check if this instance was the first to set a timestamp, so we're clear to initialize a master server
            if (!setSuccessful)
            {
                ConsoleManager.WriteLine("Slave promotion failed, master server already exists, although master server check passed. Race Condition?", ConsoleMessageType.Error);
                // Another server exists already.
                return;
            }

            // Clear to initialize a master server.
            _masterServer = new MasterServer(c, gp, allSystemModels, dbm, dbIdIoService, redisServer);

            _masterServer.Id = int.Parse(masterServerID);

            IsMasterServer = true;

#if DEBUG
            ConsoleManager.WriteLine("Master Server spawn Successful", ConsoleMessageType.Notification);
#endif
        }
Esempio n. 6
0
        public MasterServerManager(MasterServerConfig c, GalacticProperties gp, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer, SlaveServerConfigService slaveServerConfigService)
        {
            _redisServer              = redisServer;
            _databaseManager          = dbm;
            _dbIdIoService            = dbIdIoService;
            _slaveServerConfigService = slaveServerConfigService;
            MasterServerConfig        = c;
            GalacticProperties        = gp;

            SlaveID = _slaveServerConfigService.CurrentServiceId;

            if (!_checkForMasterServer())
            {
                List <PSystemModel> allSystems = new List <PSystemModel>(_databaseManager.GetAllSystemsAsync().Result);
                _promoteToMasterServer(MasterServerConfig, GalacticProperties, allSystems, _databaseManager, _dbIdIoService, _redisServer);
            }
            else
            {
#if DEBUG
                ConsoleManager.WriteLine("Existing master server detected. Initializating as slave only.", ConsoleMessageType.Debug);
#endif
            }

            if (!IsMasterServer)
            {
                ConsoleManager.WriteLine("Initializing as slave only.", ConsoleMessageType.Notification);//Leaving this here to remember to log it later, might be useful
            }

            _connectToServer();

            redisServer.Subscribe(MessageTypes.Redis_SlaveConnectionResponse, _handleSlaveConnectionResponse);


            _pingTimer          = new Timer(c.SlaveHeartbeatPeriodMS);
            _pingTimer.Elapsed += _updateSlaveHeartbeat;
            _pingTimer.Start();
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes the entire server
        /// </summary>
        private void Initialize()
        {
            for (int i = 0; i < 20; i++)
            {
                msgAvgQue.Enqueue(0);
            }

            var rand = new Random(3);

            // Logging Enabled
            Logger.Initialize();

            _synchronizers = new List <ISynchronizer>();


            //Load configs
            ConnectionManagerConfig connectionManagerConfig = new ConnectionManagerConfig(new CoreNetworkConfig());
            GalacticProperties      galacticProperties      = new GalacticProperties();

            _consoleManager = new ConsoleManager();

            _projectileManager = new ProjectileManager();

            ConsoleManager.WriteLine("Starting database...", ConsoleMessageType.Startup);
            _databaseManager = new MongoDatabaseManager();

            ShipStatManager.ReadShipsFromDBSList(_databaseManager.GetStatsFromDBAsync().Result);
            ConsoleManager.WriteLine("Ship types loaded.", ConsoleMessageType.Startup);

            RedisConfig rc = new RedisConfig();

            _redisServer = new RedisServer(LogRedisError, LogRedisInfo, rc.Address);

            var slaveId = SlaveServerConfigService.GetFreeSlaveID(_redisServer).Result;

            _slaveServerConfigService = new SlaveServerConfigService(_redisServer, slaveId);

            _masterServerManager = new MasterServerManager(new MasterServerConfig(), new GalacticProperties(), _databaseManager, _databaseManager, _redisServer, _slaveServerConfigService);

            _cargoSynchronizer = new CargoSynchronizer();
            _synchronizers.Add(_cargoSynchronizer);

            connectionManagerConfig.MyConfig.Port = ConnectionManager.GetFreePort(28002, 28010);
            _connectionManager = new ConnectionManager();
            _connectionManager.Initialize(connectionManagerConfig);

            //Poll to listen to Lidgren until it is ready
            //LidgrenMessagePoller_Init initializationPoller = new LidgrenMessagePoller_Init(_connectionManager.Server, this);
            //initializationPoller.Poll();

            _galaxyIDManager      = new LocalIDManager(_masterServerManager, IDTypes.GalaxyID);
            _teamIDManager        = new LocalIDManager(_masterServerManager, IDTypes.TeamID);
            _accountIDManager     = new LocalIDManager(_masterServerManager, IDTypes.AccountID);
            _transactionIDManager = new LocalIDManager(_masterServerManager, IDTypes.TransactionID);

            _accountManager = new AccountManager(_accountIDManager, _databaseManager);

            _messageManager = new MessageManager(_connectionManager);

            _clientUpdateManager = new ClientUpdateManager(_playerManager);

            _playerManager = new PlayerManager(_databaseManager, _connectionManager, _redisServer, _galaxyIDManager, _clientUpdateManager);

            var chatCommands = new List <IChatCommand>()
            {
                new HelpCommand(),
                new ShoutCommand(_redisServer),
                new RadioCommand(),
                new TellCommand(_playerManager)
            };

            var asyncChatCommands = new List <IAsyncChatCommand>()
            {
                new AdminWarpCommand(_databaseManager, _redisServer, new Random())
            };

            _teamManager = new GlobalTeamManager(_teamIDManager, _connectionManager, _redisServer, _playerManager, _databaseManager);

            _galaxyManager = new GalaxyManager(galacticProperties.SolID, _teamManager);

            _chatManager = new ChatManager(chatCommands, asyncChatCommands, _playerManager, _messageManager, _redisServer);

            _warpManager = new WarpManager(_galaxyManager, _messageManager, _chatManager, _redisServer, _accountManager, _databaseManager);

            _shipManager = new ShipManager(_messageManager, _galaxyManager, _warpManager, _connectionManager, _databaseManager);

            _structureManager = new StructureManager(_databaseManager, _galaxyManager, _galaxyIDManager, _cargoSynchronizer);

            loginManager = new LoginManager(_accountManager, _playerManager, _connectionManager, _redisServer);

            // Todo: Convert everything over to ServerNetworkMessage to propogate full request context.
            _simulatorManager = new SimulatorManager(new SimulatorConfig(), _redisServer, (sender, container) => ProcessMessage(sender, new ServerNetworkMessage(container, null)));

            StructureStatManager.Initialize();

            ConsoleManager.WriteLine("Completed Initialization", ConsoleMessageType.Startup);

            _economyManager      = new EconomyManager(_transactionIDManager, _playerManager, _galaxyManager, _cargoSynchronizer, _shipManager, _databaseManager, _masterServerManager);
            _killManager         = new KillManager(_cargoSynchronizer, _playerManager, _galaxyManager, _messageManager, _connectionManager, _warpManager, _chatManager, _economyManager);
            _collisionManager    = new CollisionManager(_galaxyManager, _messageManager, _killManager, _projectileManager);
            _registrationManager = new GalaxyRegistrationManager(_galaxyManager, _shipManager, _collisionManager, _galaxyIDManager, _playerManager, _accountManager, _cargoSynchronizer, _structureManager);
            _warpManager.SetRegistrationManager(_registrationManager);//Gross, I know.
            _locatorService   = new LocatorService(_registrationManager, _playerManager, _galaxyManager, _shipManager, _accountManager, _teamManager, _teamManager, _messageManager, _structureManager, _masterServerManager);
            _msMessageHandler = new MasterServerMessageHandler((int)_masterServerManager.SlaveID, _redisServer, _connectionManager, _locatorService, _accountManager, _accountIDManager, _databaseManager, _galaxyManager, _galaxyIDManager, _playerManager, _shipManager, _registrationManager, _teamIDManager, _messageManager, _teamManager, _warpManager, _transactionIDManager, ProcessRoutedMessage);

            StructureFactory.Initialize(_galaxyIDManager, _registrationManager);
            ColonyFactory.Initialize(_galaxyIDManager, _registrationManager);

            dbSyncer = new DBSyncer(_databaseManager, _galaxyManager, _shipManager, _playerManager, _accountManager, _structureManager);

#if DEBUG
            _typesToExcludeIgnore.Add(MessageTypes.PositionUpdateData);
            _typesToExcludeIgnore.Add(MessageTypes.ShipFireRequest);
            _typesToExcludeIgnore.Add(MessageTypes.ProjectileCollisionReport);
            _typesToExcludeIgnore.Add(MessageTypes.StructureFireRequest);
            _typesToExcludeIgnore.Add(MessageTypes.ObjectPickupRequest);
#endif
        }
Esempio n. 8
0
 public GlobalTransactionIDManager(IDbIdIoService dbIdIoService, GalacticProperties c)
     : base(dbIdIoService, IDTypes.AccountID)
 {
 }
Esempio n. 9
0
 public GlobalTeamIDManager(IDbIdIoService dbIdIoService, GalacticProperties c)
     : base(dbIdIoService, IDTypes.TeamID)
 {
 }