public bool Start(string instance) { try { var configFile = Constants.NodeConfigFileName; if (instance != null) { var baseName = configFile.Split('.').FirstOrDefault(); configFile = $"{baseName}_{instance}.json"; } var nodeConfig = new NodeConfig { TcpPort = Constants.DefaultPort, IsPersistent = true }; if (File.Exists(configFile)) { try { var configFromFile = SerializationHelper.FormattedSerializer.Deserialize <NodeConfig>( new JsonTextReader(new StringReader(File.ReadAllText(configFile)))); nodeConfig = configFromFile; HostServices.Start(configFromFile.DataPath); Log.LogInfo("----------------------------------------------------------"); Log.LogInfo($"Reading configuration file {configFile} "); } catch (Exception e) { Log.LogError($"Error reading configuration file {configFile} : {e.Message}"); } } else { HostServices.Start(nodeConfig.DataPath); Log.LogWarning($"Configuration file {configFile} not found. Using defaults"); } _cacheServer = new Server.Server(nodeConfig); _listener = new TcpServerChannel(); _cacheServer.Channel = _listener; _listener.Init(nodeConfig.TcpPort); _listener.Start(); var fullDataPath = Path.GetFullPath(nodeConfig.DataPath ?? Constants.DataPath); var persistentDescription = nodeConfig.IsPersistent ? fullDataPath : " NO"; Console.Title = $"Cachalot Core on port {nodeConfig.TcpPort} persistent = {persistentDescription}"; Log.LogInfo( $"Starting hosted service on port {nodeConfig.TcpPort} persistent = {persistentDescription}"); _cacheServer.StopRequired += (sender, args) => { HostServices.Stop(); Stop(); _stopEvent.Set(); }; _cacheServer.Start(); } catch (Exception e) { Log.LogError($"Failed to start host: {e}"); return(false); } Log.LogInfo("Host started successfully"); return(true); }
public bool Start(HostControl hostControl) { HostServices.HostServices.Start(); Log.LogInfo("----------------------------------------------------------"); try { var nodeConfig = new NodeConfig { TcpPort = Constants.DefaultPort, IsPersistent = true }; if (File.Exists(Constants.NodeConfigFileName)) { try { var configFromFile = SerializationHelper.FormattedSerializer.Deserialize <NodeConfig>( new JsonTextReader(new StringReader(File.ReadAllText(Constants.NodeConfigFileName)))); nodeConfig = configFromFile; } catch (Exception e) { Log.LogError($"Error reading configuration file {Constants.NodeConfigFileName} : {e.Message}"); } } else { Log.LogWarning($"Configuration file {Constants.NodeConfigFileName} not found. Using defaults"); } _cacheServer = new Server.Server(nodeConfig); _listener = new TcpServerChannel(); _cacheServer.Channel = _listener; _listener.Init(nodeConfig.TcpPort); _listener.Start(); var fullDataPath = Path.GetFullPath(nodeConfig.DataPath ?? Constants.DataPath); var persistentDescription = nodeConfig.IsPersistent ? fullDataPath : " NO"; Log.LogInfo( $"Starting Cachalot server on port {nodeConfig.TcpPort} persistent {persistentDescription}"); Console.Title = $"Cachalot server on port {nodeConfig.TcpPort} persistent = {persistentDescription}"; _cacheServer.Start(); } catch (Exception e) { Log.LogError($"Failed to start host: {e}"); return(false); } Log.LogInfo("Host started successfully"); return(true); }