/// <summary> /// Constructor whith four parameters /// </summary> /// <param name="savePath">Path to save data</param> /// <param name="slaves">Slaves' addresses</param> /// <param name="idAlgorithm">Alghorithm to generate user ids</param> /// <param name="logging">Determines if logging is on</param> public MasterUserService(string savePath, List <IPEndPoint> slaves, IIdAlgorithm idAlgorithm, bool logging) { if (idAlgorithm == null) { this.idAlgorithm = new IncrementAlgorithm(); } else { this.idAlgorithm = idAlgorithm as IncrementAlgorithm; } if (savePath == null) { throw new NullReferenceException(nameof(savePath)); } if (slaves == null) { throw new NullReferenceException(nameof(slaves)); } this.logging = logging; this.Data = new List <User>(); this.savePath = savePath; this.slaves = slaves; this.clients = new List <TcpClient>(); this.Load(); this.ConfigureSlaves(); if (this.logging) { Log.Trace("Master app started"); } }
private void Load() { var formatter = new XmlSerializer(typeof(MasterUserService)); try { using (var fs = new FileStream(this.savePath, FileMode.Open)) { var ob = formatter.Deserialize(fs) as MasterUserService; this.Data = ob.Data; this.idAlgorithm = ob.idAlgorithm; } } catch (FileNotFoundException ex) { } if (this.logging) { Log.Trace("Master loaded"); } }
/// <summary> /// Reads data from the xml file /// </summary> /// <param name="reader">Instance of the XmlReader</param> public void ReadXml(XmlReader reader) { reader.ReadStartElement(nameof(MasterUserService)); reader.ReadStartElement("master"); reader.ReadStartElement("idAlgorithm"); var idAlgorithm = reader.ReadContentAsString(); var type = Type.GetType(idAlgorithm); var ctor = type.GetConstructor(new[] { typeof(int) }); reader.ReadEndElement(); reader.ReadStartElement("id"); var id = reader.ReadContentAsString(); this.idAlgorithm = ctor.Invoke(new object[] { int.Parse(id) }) as IIdAlgorithm; reader.ReadEndElement(); reader.MoveToAttribute("count"); var count = int.Parse(reader.Value); reader.ReadStartElement("data"); var otherSer = new XmlSerializer(typeof(User)); for (var i = 0; i < count; i++) { var other = (User)otherSer.Deserialize(reader); this.Data.Add(other); } reader.ReadEndElement(); reader.ReadEndElement(); }