protected bool IsValid(SactaMsg msg) { return((msg.DomainOrg == Cfg.SactaProtocol.Scv.Domain) && (msg.CenterOrg == Cfg.SactaProtocol.Scv.Center) && (msg.UserOrg == Cfg.SactaProtocol.Scv.Scv) && (msg.DomainDst == Cfg.SactaProtocol.Sacta.Domain) && (msg.CenterDst == Cfg.SactaProtocol.Sacta.Center) && (msg.UserDst == Cfg.SactaProtocol.Sacta.PsiGroup)); }
protected bool IsValid(SactaMsg msg) { var validUsers = msg.Type == SactaMsg.MsgType.Sectorization ? SactaSPSIUsers : SactaSPVUsers; return((msg.DomainOrg == Cfg.SactaProtocol.Sacta.Domain) && (msg.CenterOrg == Cfg.SactaProtocol.Sacta.Center) && validUsers.ContainsKey(msg.UserOrg) && (msg.DomainDst == Cfg.SactaProtocol.Scv.Domain) && (msg.CenterDst == Cfg.SactaProtocol.Scv.Center) && (msg.UserDst == Cfg.SactaProtocol.Scv.Scv)); }
protected void SendSectAnsw(int version, int result) { Logger.Info <ScvManager>($"On {Id} (TXE {TxEnabled}) Sending SectAnsw Msg (Sequence {Sequence}, Version {version}, result {result}) ..."); var msg = SactaMsg.MsgToSacta(Cfg, SactaMsg.MsgType.SectAnswer, 0, Sequence, version, result).Serialize(); if (BroadMessage(msg)) { Sequence = Sequence >= 287 ? 0 : Sequence + 1; Logger.Info <ScvManager>($"On {Cfg.Id} SectAnswer Msg sended. (New Sequence {Sequence}, Version {version}, result {result})"); } }
protected void SendInit() { Logger.Info <ScvManager>($"On {Id} (TXE {TxEnabled}) Sending Init Msg ..."); var msg = SactaMsg.MsgToSacta(Cfg, SactaMsg.MsgType.Init, SactaMsg.InitId, 0).Serialize(); if (BroadMessage(msg)) { Sequence = 0; Logger.Info <ScvManager>($"On {Cfg.Id} Init Msg sended."); } }
protected void SendSectAsk() { Logger.Info <ScvManager>($"On {Id} (TXE {TxEnabled}) Sending SectAsk Msg (Sequence {Sequence}) ..."); var msg = SactaMsg.MsgToSacta(Cfg, SactaMsg.MsgType.SectAsk, 0, Sequence).Serialize(); if (BroadMessage(msg)) { Sequence = Sequence >= 287 ? 0 : Sequence + 1; Logger.Info <ScvManager>($"On {Cfg.Id} SectAsk Msg sended. (New Sequence {Sequence})"); } WhenSectorAsked = DateTime.Now; }
protected void SendPresence() { Logger.Info <ScvManager>($"On {Id} (TXE {TxEnabled}) Sending Presence Msg (Sequence {Sequence}) ..."); var msg = SactaMsg.MsgToSacta(Cfg, SactaMsg.MsgType.Presence, 0, Sequence).Serialize(); if (BroadMessage(msg)) { Sequence = Sequence >= 287 ? 0 : Sequence + 1; LastPresenceSended = DateTime.Now; Logger.Info <ScvManager>($"On {Cfg.Id} Presence Msg sended. (New Sequence {Sequence}) "); } }
protected void SendSectorizationMsg(Dictionary <string, int> sectorUcs) { Logger.Info <PsiManager>($"On PSI (TXE {TxEnabled}) Sending Sectorization Msg (Sequence {Sequence}..."); var msg = SactaMsg.MsgToScv(Cfg, SactaMsg.MsgType.Sectorization, 0, Sequence, SectorizationVersion, sectorUcs).Serialize(); if (BroadMessage(msg)) { Sequence = Sequence >= 287 ? 0 : Sequence + 1; SectorizationVersion++; Logger.Info <PsiManager>($"On PSI Sectorization Msg sended. (New Sequence {Sequence}, New Version {SectorizationVersion})"); } }
public static void Deserialize(byte[] data, Action <SactaMsg> deliver, Action <string> deliverError) { try { MemoryStream ms = new MemoryStream(data); CustomBinaryFormatter bf = new CustomBinaryFormatter(); SactaMsg msg = bf.Deserialize <SactaMsg>(ms); deliver(msg); } catch (Exception x) { Logger.Exception <SactaMsg>(x); deliverError(x.Message); } }
public static SactaMsg MsgToScv(Configuration.DependecyConfig cfg, MsgType type, int id, int seq, int version = 0, Dictionary <string, int> sectorUcs = null) { var msg = new SactaMsg(type, id, seq, sectorUcs, version) { DomainOrg = (byte)cfg.SactaProtocol.Sacta.Domain, CenterOrg = (byte)cfg.SactaProtocol.Sacta.Center, UserOrg = type == MsgType.Init || type == MsgType.Presence ? (ushort)cfg.SactaProtocol.Sacta.SpvsList()[0] : (ushort)cfg.SactaProtocol.Sacta.PsisList()[0], DomainDst = (byte)cfg.SactaProtocol.Scv.Domain, CenterDst = (byte)cfg.SactaProtocol.Scv.Center, UserDst = (ushort)cfg.SactaProtocol.Scv.Scv }; if (type == MsgType.Presence) { (msg.Info as PresenceInfo).PresencePerioditySg = (ushort)cfg.SactaProtocol.TickAlive; (msg.Info as PresenceInfo).ActivityTimeOutSg = (ushort)cfg.SactaProtocol.TimeoutAlive; } return(msg); }
public static SactaMsg MsgToSacta(Configuration.DependecyConfig cfg, MsgType type, int id, int seq, int version = 0, int result = 0) { var msg = new SactaMsg(type, id, seq, null, version, result) { DomainOrg = (byte)cfg.SactaProtocol.Scv.Domain, CenterOrg = (byte)cfg.SactaProtocol.Scv.Center, UserOrg = (ushort)cfg.SactaProtocol.Scv.Scv, DomainDst = (byte)cfg.SactaProtocol.Sacta.Domain, CenterDst = (byte)cfg.SactaProtocol.Sacta.Center, UserDst = (ushort)cfg.SactaProtocol.Sacta.PsiGroup }; if (type == MsgType.Presence) { (msg.Info as PresenceInfo).PresencePerioditySg = (ushort)cfg.SactaProtocol.TickAlive; (msg.Info as PresenceInfo).ActivityTimeOutSg = (ushort)cfg.SactaProtocol.TimeoutAlive; } return(msg); }
protected bool IsSecondSectMsg(SactaMsg msg) { PsiOrScvInfo psi = SactaSPSIUsers.ContainsKey(msg.UserOrg) ? SactaSPSIUsers[msg.UserOrg]: null; if (psi == null) { Logger.Warn <ScvManager>($"On {Cfg.Id} => Mensaje de Usuario Desconocido: {msg.UserOrg}"); return(true); } else if ((psi.LastSectMsgId == msg.Id) && (psi.LastSectVersion == ((SactaMsg.SectInfo)(msg.Info)).Version)) { Logger.Info <ScvManager>($"On {Cfg.Id} => Segundo MSG Sectorizacion UserOrg={msg.UserOrg}, IDs [Last:Current] {psi.LastSectMsgId}:{msg.Id}, Versions [Last:Current]{psi.LastSectVersion}:{((SactaMsg.SectInfo)(msg.Info)).Version}"); return(true); } else { Logger.Info <ScvManager>($"On {Cfg.Id} => Primer MSG Sectorizacion UserOrg={msg.UserOrg}, IDs [Last:Current] {psi.LastSectMsgId}:{msg.Id}, Versions [Last:Current]{psi.LastSectVersion}:{((SactaMsg.SectInfo)(msg.Info)).Version}"); } psi.LastSectMsgId = msg.Id; psi.LastSectVersion = ((SactaMsg.SectInfo)(msg.Info)).Version; return(false); }
protected void ProccessSectorization(SactaMsg msg, Action <bool, string> deliver) { var sectorsReceived = ((SactaMsg.SectInfo)(msg.Info)).Sectors.ToList(); var sectorsToProcess = sectorsReceived .Where(s => Cfg.Sectorization.VirtualsList().Contains(int.Parse(s.SectorCode)) == false) .ToList(); var idSectorsToProcess = sectorsToProcess .Select(s => int.Parse(s.SectorCode)).ToList(); var SectorsNotFound = Cfg.Sectorization.SectorsList() .Where(s => idSectorsToProcess.Contains(s) == false) .Select(s => s.ToString()) .ToList(); var UnknowUcs = sectorsToProcess .Where(s => Cfg.Sectorization.PositionsList().Contains((int)s.Ucs) == false) .Select(s => s.Ucs.ToString()) .ToList(); var UnknowSectors = sectorsToProcess .Where(s => Cfg.Sectorization.SectorsList().Contains(int.Parse(s.SectorCode)) == false) .Select(s => s.SectorCode) .ToList(); var duplicatedSect = sectorsToProcess .GroupBy(s => s.SectorCode) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); bool err = SectorsNotFound.Count > 0 || UnknowUcs.Count() > 0 || UnknowSectors.Count() > 0 || duplicatedSect.Count() > 0; if (err) { var message = ""; message += SectorsNotFound.Count() > 0 ? $"Sectores no Encontrados: {String.Join(", ",SectorsNotFound)}. " : ""; message += UnknowUcs.Count() > 0 ? $"Posiciones Desconocidas: {String.Join(", ", UnknowUcs)}. " : ""; message += UnknowSectors.Count() > 0 ? $"Sectores Desconocidos: {String.Join(", ", UnknowSectors)}. " : ""; message += duplicatedSect.Count() > 0 ? $"Sectores Duplicados: {String.Join(", ", duplicatedSect)}. " : ""; // deliver(false, message); // Evento para el Historico. SafeLaunchEvent <SectorizationReceivedArgs>(EventSectorization, new SectorizationReceivedArgs() { Accepted = false, ScvId = Cfg.Id, //SectorMap = sectorsToProcess.ToDictionary(s => s.SectorCode, s => (int)s.Ucs), ReceivedMap = String.Join(",", sectorsReceived.Select(s => s.ToString())), RejectCause = message, Acknowledge = (result) => deliver(false, message) });; } else { //deliver(true, ""); // Actulizar con los datos recibidos la sectorizacion global... SafeLaunchEvent <SectorizationReceivedArgs>(EventSectorization, new SectorizationReceivedArgs() { Accepted = true, ScvId = Cfg.Id, SectorMap = sectorsToProcess.ToDictionary(s => s.SectorCode, s => (int)s.Ucs), Acknowledge = (result) => deliver(result, "") }); } }
protected void OnDataReceived(object sender, DataGram dg) { lock (Locker) { SactaMsg.Deserialize(dg.Data, (msg) => { ManageOnLan(sender as UdpSocket, dg.Client.Address, (lan) => { try { if (IsValid(msg)) { switch (msg.Type) { case SactaMsg.MsgType.Init: case SactaMsg.MsgType.Presence: case SactaMsg.MsgType.Sectorization: Logger.Debug <ScvManager>($"On {Cfg.Id} from Sacta Lan {lan} Valid message {msg.Type} received"); if (msg.Type == SactaMsg.MsgType.Init) { // todo } else if (msg.Type == SactaMsg.MsgType.Sectorization) { if (IsSecondSectMsg(msg) == false) { ProccessSectorization(msg, (ok, error) => { // Será llamado por el WorkingThread del servicio PPAL en la gestion del evento que se genere... lock (Locker) { if (ok) { Logger.Info <ScvManager>($"On {Cfg.Id}. Sectorization {msg.Id} Processed."); SendSectAnsw((int)((SactaMsg.SectInfo)(msg.Info)).Version, 1); } else { Logger.Warn <ScvManager>($"On {Cfg.Id}. Sectorization {msg.Id} Rejected => {error}"); SendSectAnsw((int)((SactaMsg.SectInfo)(msg.Info)).Version, 0); } GlobalState = SactaState.SendingPresences; } }); } else { Logger.Info <ScvManager>($"Sectorization Request (Red = {lan}, Versión = {((SactaMsg.SectInfo)(msg.Info)).Version}, IGNORED. Already in Progress..."); } } else { // todo } break; default: Logger.Warn <ScvManager>($"On {Cfg.Id} from Sacta Lan {lan} Invalid message {msg.Type} received"); Logger.Trace <ScvManager>($"On {Cfg.Id} from Sacta Lan {lan} Invalid message received: {msg.ToString()}"); break; } } else { Logger.Warn <ScvManager>($"On {Cfg.Id} from Sacta Lan {lan} Invalid message {msg.Type} received"); Logger.Trace <ScvManager>($"On {Cfg.Id} from Sacta Lan {lan} Invalid message received: {msg.ToString()}"); } } catch (Exception x) { Logger.Exception <ScvManager>(x, $"On {Cfg.Id}"); } }); }, (error) => // Error en el Deserialize. { Logger.Warn <ScvManager>($"On {Cfg.Id} Deserialize Error: {error}"); }); } }
protected void OnDataReceived(object sender, DataGram dg) { lock (Locker) { SactaMsg.Deserialize(dg.Data, (msg) => { ManageOnLan(sender as UdpSocket, (lan) => { try { if (IsValid(msg)) { switch (msg.Type) { case SactaMsg.MsgType.Init: case SactaMsg.MsgType.Presence: case SactaMsg.MsgType.SectAsk: case SactaMsg.MsgType.SectAnswer: Logger.Debug <PsiManager>($"On PSI from Scv Lan {lan} Valid message {msg.Type} received. Id = {msg.Id}"); if (msg.Type == SactaMsg.MsgType.Init) { ScvInfo.LastSectMsgId = -1; } else if (msg.Type == SactaMsg.MsgType.Presence) { } else if (msg.Type == SactaMsg.MsgType.SectAsk) { Logger.Info <PsiManager>($"On PSI from Scv Lan {lan} Sectorization Sectoriztion Request Received"); if (ScvInfo.LastSectMsgId != msg.Id) { // Si el mensaje llega por las dos redes solo respondo una vez... SafeLaunchEvent <SectorizationRequestArgs>(EventSectRequest, new SectorizationRequestArgs()); ScvInfo.LastSectMsgId = msg.Id; } } else if (msg.Type == SactaMsg.MsgType.SectAnswer) { SactaMsg.SectAnswerInfo info = (SactaMsg.SectAnswerInfo)(msg.Info); Logger.Info <PsiManager>($"On PSI from Scv Lan {lan} Sectorization {info.Version} {(info.Result == 1 ? "Accepted" : "Rejected")}."); } break; default: Logger.Warn <PsiManager>($"On PSI from SCV Lan {lan} Invalid message {msg.Type} received"); Logger.Trace <PsiManager>($"On PSI from SCV Lan {lan} Invalid message received: {msg.ToString()}"); break; } } else { Logger.Warn <PsiManager>($"On PSI from SCV Lan {lan} Invalid message {msg.Type} received"); Logger.Trace <PsiManager>($"On PSI from SCV Lan {lan} Invalid message received: {msg.ToString()}"); } } catch (Exception x) { Logger.Exception <PsiManager>(x, $"On PSI"); } }); }, (error) => // Error en el Deserialize. { Logger.Warn <PsiManager>($"On PSI Deserialize Error: {error}"); }); } }