public virtual List <ZooDataModel> GetModels(ZooContext context, ModelType type) { switch (type) { case ModelType.Animal: return(context.GetModels <Animal>().Cast <ZooDataModel>().ToList()); case ModelType.Attraction: return(context.GetModels <Attraction>().Cast <ZooDataModel>().ToList()); case ModelType.BalanceType: return(context.GetModels <BalanceType>().Cast <ZooDataModel>().ToList()); case ModelType.CashBalance: return(context.GetModels <CashBalance>().Cast <ZooDataModel>().ToList()); case ModelType.Food: return(context.GetModels <Food>().Cast <ZooDataModel>().ToList()); case ModelType.Overtime: return(context.GetModels <Overtime>().Cast <ZooDataModel>().ToList()); case ModelType.Place: return(context.GetModels <Place>().Cast <ZooDataModel>().ToList()); case ModelType.Worker: return(context.GetModels <Worker>().Cast <ZooDataModel>().ToList()); default: return(null); } }
public virtual bool ModifyModel(ZooContext context, ModelType type, ZooDataModel model) { switch (type) { case ModelType.Animal: return(context.ModifyModel((Animal)model)); case ModelType.Attraction: return(context.ModifyModel((Attraction)model)); case ModelType.BalanceType: return(context.ModifyModel((BalanceType)model)); case ModelType.CashBalance: return(context.ModifyModel((CashBalance)model)); case ModelType.Food: return(context.ModifyModel((Food)model)); case ModelType.Overtime: return(context.ModifyModel((Overtime)model)); case ModelType.Place: return(context.ModifyModel((Place)model)); case ModelType.Worker: return(context.ModifyModel((Worker)model)); default: return(false); } }
public virtual bool AddModel(ZooContext context, ModelType type, ZooDataModel model) { switch (type) { case ModelType.Animal: return(context.AddModel((Animal)model)); case ModelType.Attraction: return(context.AddModel((Attraction)model)); case ModelType.BalanceType: return(context.AddModel((BalanceType)model)); case ModelType.CashBalance: CashBalance balance = (CashBalance)model; if (balance.BalanceTypeID == 0) { PayMonthSalary(); return(true); } else { return(context.AddModel(balance)); } case ModelType.Food: return(context.AddModel((Food)model)); case ModelType.Overtime: return(context.AddModel((Overtime)model)); case ModelType.Place: return(context.AddModel((Place)model)); case ModelType.Worker: return(context.AddModel((Worker)model)); default: return(false); } }
public virtual Tuple <bool, byte> DeleteModel(ZooContext context, ModelType type, int id) { switch (type) { case ModelType.Animal: return(context.DeleteAnimal(id)); case ModelType.Attraction: return(context.DeleteAttraction(id)); case ModelType.Food: return(context.DeleteFood(id)); case ModelType.Place: return(context.DeletePlace(id)); case ModelType.Worker: return(context.DeleteWorker(id)); default: return(new Tuple <bool, byte>(false, 0)); } }
public virtual void Start() { serverModifyModel = new TcpListener(localIp, tcpPort + 3); serverAddModel = new TcpListener(localIp, tcpPort + 2); serverDeleteModel = new TcpListener(localIp, tcpPort + 1); serverGetModels = new TcpListener(localIp, tcpPort); threadModifyModel = new Thread(() => { serverModifyModel.Start(); while (isServerRunning) { Socket client = serverModifyModel.AcceptSocket(); new Thread(() => { Logger.LogInfo("New modify model request", GetType(), "threadModifyModel"); byte[] data = new byte[1]; client.Receive(data, 1, SocketFlags.None); ModelType type = (ModelType)data[0]; data = new byte[sizeof(int)]; client.Receive(data, sizeof(int), SocketFlags.None); data = new byte[BitConverter.ToInt32(data, 0)]; client.Receive(data, data.Length, SocketFlags.None); object model = new BinaryFormatter().Deserialize(new MemoryStream(data)); using (ZooContext context = new ZooContext()) { if (ModifyModel(context, type, ConvertFromZooComModel(model, type))) { Logger.LogInfo($"Modified {type.ToString()}", GetType(), "threadModifyModel"); } else { Logger.LogWarning($"Cannot modify {type.ToString()}", GetType(), "threadModifyModel"); } } client.Close(); }).Start(); } serverModifyModel.Stop(); }); threadAddModel = new Thread(() => { serverAddModel.Start(); while (isServerRunning) { Socket client = serverAddModel.AcceptSocket(); new Thread(() => { Logger.LogInfo("New add model request", GetType(), "threadAddModel"); byte[] data = new byte[1]; client.Receive(data, 1, SocketFlags.None); ModelType type = (ModelType)data[0]; data = new byte[sizeof(int)]; client.Receive(data, sizeof(int), SocketFlags.None); data = new byte[BitConverter.ToInt32(data, 0)]; client.Receive(data, data.Length, SocketFlags.None); object model = new BinaryFormatter().Deserialize(new MemoryStream(data)); using (ZooContext context = new ZooContext()) { if (AddModel(context, type, ConvertFromZooComModel(model, type))) { Logger.LogInfo($"Added new {type.ToString()}", GetType(), "threadAddModel"); } else { Logger.LogWarning($"Cannot add new {type.ToString()}", GetType(), "threadAddModel"); } } client.Close(); }).Start(); } serverAddModel.Stop(); }); threadDeleteModel = new Thread(() => { serverDeleteModel.Start(); while (isServerRunning) { Socket client = serverDeleteModel.AcceptSocket(); new Thread(() => { Logger.LogInfo("New delete model request", GetType(), "threadDeleteModel"); byte[] data = new byte[1]; client.Receive(data, 1, SocketFlags.None); ModelType type = (ModelType)data[0]; data = new byte[sizeof(int)]; client.Receive(data, sizeof(int), SocketFlags.None); using (ZooContext context = new ZooContext()) { int id = BitConverter.ToInt32(data, 0); Tuple <bool, byte> operationDetails = DeleteModel(context, type, id); client.Send(BitConverter.GetBytes(operationDetails.Item1)); if (operationDetails.Item1) { Logger.LogInfo($"Deleted model of type {type.ToString()}, ID = {id}", GetType(), "threadDeleteModel"); } else { Logger.LogWarning($"Cannot delete model of type {type.ToString()}, ID = {id}", GetType(), "threadDeleteModel"); client.Send(new byte[1] { operationDetails.Item2 }); } } client.Close(); }).Start(); } serverDeleteModel.Stop(); }); threadGetModels = new Thread(() => { serverGetModels.Start(); while (isServerRunning) { Socket client = serverGetModels.AcceptSocket(); new Thread(() => { Logger.LogInfo("New get models request", GetType(), "threadGetModels"); byte[] data = new byte[1]; client.Receive(data, 1, SocketFlags.None); ModelType type = (ModelType)data[0]; using (MemoryStream dataStream = new MemoryStream()) using (ZooContext context = new ZooContext()) { try { new BinaryFormatter().Serialize(dataStream, GetModels(context, type).Select(model => ConvertToZooComModel(type, model)).ToList()); client.Send(BitConverter.GetBytes(dataStream.ToArray().Length), sizeof(int), SocketFlags.None); client.Send(dataStream.ToArray()); Logger.LogInfo($"Send models of type {type.ToString()}", GetType(), "threadGetModels"); } catch (Exception e) { Logger.LogError(e.ToString()); } } client.Close(); }).Start(); } serverGetModels.Stop(); }); isServerRunning = true; threadModifyModel.Start(); threadAddModel.Start(); threadDeleteModel.Start(); threadGetModels.Start(); }