public static StartupArgJson deserialize(string s) { StartupArgJson json = null; object o; //JsonSerializer jss = new JsonSerializer(); //jss.MissingMemberHandling = MissingMemberHandling.Error; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.MissingMemberHandling = MissingMemberHandling.Error; //settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; che succederebbe? try { json = (StartupArgJson)(o = JsonConvert.DeserializeObject <StartupArgJson>(s, settings)); } catch (Exception e) { MessageBox.Show("Input JSON format should be like this: " + new StartupArgJson().ToString() + Environment.NewLine + "Found instead:" + s + Environment.NewLine + "Exception:" + e.ToString()); } return(json); }
public static string[] fakeinput() { string desktop = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); bool exclBind = false; string kafkaHost = "localhost";//"192.168.1.8";//"localhost" StartupArgJson json = new StartupArgJson() { myPartitionNumber = 0, partitionNumbers_Total = 2, broadcastPort_Tool = 20001, toolReceiverThreads = 1, //exclBind ? 1 : (int)Math.Ceiling(Environment.ProcessorCount / 2.0),//it's logical, not physical! it is the maximum number of thread executable simultaneously. slaveReceiverThreads = 1, // exclBind ? 1 : (int)Math.Floor(Environment.ProcessorCount / 2.0), enableGUI = true, enablePrintSlave = false, enablePrintTool = false, broadcastAddress = "192.168.1.255", criticalErrFile = desktop + @"\Listener_CriticalErrors.txt", errFile = desktop + @"\Listener_Errors.txt", logFile = desktop + @"\Listener_EventLog.txt", toolMsgFile = null, //desktop + @"\Listener_ToolLog.txt", slaveMsgFile = desktop + @"\Listener_SlaveLog.txt", logToolMsgOnReceive = false, exclusiveBind = exclBind, KafkaNodes = "http://" + kafkaHost + ":9093, http://" + kafkaHost + ":9094, http://" + kafkaHost + ":9095", KafkaTopic = "toolsEvents", benchmark = true, }; json.slaveNotifyMode_Batch = 100; json.dinamicallyStarted = false; json.broadcastPort_Slaves = 20002 + json.myPartitionNumber; ulong guid = Program.GetMACAddress(); int replicationDegree = 3; json.replicatorsList = new List <Slave>(replicationDegree); for (int i = 0; i < replicationDegree; i++) { Slave replica = new Slave(); replica.ip_string = "192.168.1." + (100 + i); replica.id = guid + (ulong)i; replica.isSelf = i == 0; json.replicatorsList.Add(replica); } string s = json.ToString(); return(new string[] { json.ToString() }); }
//public const int broadcastReceivePort = 20000 + 1;//unregistered until 20560 //public const int replication = 4; //private static IPEndPoint broadcastEP; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] private static void Main(string[] args_Raw) { //MessageBox.Show(args_Raw == null || args_Raw.Length < 1 ? "Empty argument" : args_Raw[0]); return; if (args_Raw == null || args_Raw.Length < 1) { args_Raw = StartupArgJson.fakeinput(); } if (args_Raw.Length != 1) { args_Raw = new string[] { "TriggerError Message: there must be only a single argument." } } ; Program.args = StartupArgJson.deserialize(args_Raw[0]); if (Program.args == null || !Program.args.Validate()) { return; } try { Clipboard.SetText(args_Raw[0]); }catch (Exception e) { Program.pe("Unable to load JSON-string argument in clipboard, reason:" + e.ToString()); } HotRestart(); }
/// <summary> /// processing executed after message got dequeued. /// </summary> public void consume() { Program.logSlave("consuming :" + this.ToPrintString()); Slave s; ulong id; int removedCount; switch (type) { case MessageType.argumentChange: StartupArgJson args; try { args = StartupArgJson.deserialize(this.data); } catch (Exception e) { Program.pe(this.type + " body is not deserializable: " + this.data, e); break; } if (args == null || !args.Validate()) { Program.pe(this.type + " body is deserializable but with invalid content: " + this.data); break; } Program.args = args; Master.MasterCrashChecker.Abort(); Master.MasterCrashChecker = null; new Thread(Program.HotRestart).Start(); /* * string[] arr = this.data.Split(myMessage.separator); * foreach (string str in arr) { * string[] kv = str.Split(myMessage.secondSeparator); * ulong slaveID; * if (!ulong.TryParse(kv[0], out slaveID)) { Program.pe("Unexpected slaveID key ("+kv[0]+") found in the body of messagetype."+this.type); continue; } * * }*/ //todo: crea anche un software che generi messaggi di ripartizionamento per gestire dinamicamente tutte le partizioni, un supermaster break; case MessageType.masterChange: //if required in future trigger messageType.dinamicallyAddSlave, per ora va tutto bene anche se il nuovo master non era nella lista slaves. string[] split = this.data.Split(myMessage.separator); if (!ulong.TryParse(split[0], out id)) { Program.pe(this.type + " have non-numerical body; expected two numeric id separated by a '" + myMessage.separator + "', found instead: " + this.data); break; } Slave oldMaster = Slave.getFromID(id); if (oldMaster == null) { break; } if (!ulong.TryParse(split[0], out id)) { Program.pe(this.type + " have non-numerical body; expected two numeric id separated by a '" + myMessage.separator + "', found instead: " + this.data); break; } Slave newMaster = Slave.getFromID(id); if (newMaster == null) { break; } if (Master.currentMaster == oldMaster) { Master.changeMaster(newMaster); //master checker msg received } Slave.Remove(oldMaster); break; case MessageType.dinamicallyRemoveSlave: if (!ulong.TryParse(this.data, out id)) { Program.pe(this.type + " have non-numerical body; expected numeric id, found: " + this.data); break; } s = Slave.getFromID(id); if (s == null) { break; } Slave.Remove(s); break; case MessageType.dinamicallyAddSlave: Slave.deserializeOrGet(this.data); if (Master.iAmTheMaster) { myMessage m = new myMessage(MessageType.provideSlaveList, ""); foreach (Slave s2 in Slave.all) { m.data += ";" + s2.serialize(); } m.data = m.data.Substring(1); m.launchToOutput(); } break; case MessageType.provideSlaveList: Volatile.Write(ref Master.lastMasterUpdate, DateTime.Now.Ticks); string[] jsons = this.data.Split(myMessage.separator); lock (Slave.all) foreach (string str in jsons) { Slave.deserializeOrGet(str); } break; case MessageType.confirmMessageSuccess_Single: Volatile.Write(ref Master.lastMasterUpdate, DateTime.Now.Ticks); removedCount = ReceiverTool.messageQueue.get(this.data, true) == null ? 0 : 1; Program.logSlave(removedCount + " removed from queue."); break; case MessageType.confirmMessageSuccess_Batch: Volatile.Write(ref Master.lastMasterUpdate, DateTime.Now.Ticks); removedCount = ReceiverTool.messageQueue.getOlderThan(this.data, true).Count; Program.logSlave(removedCount + " removed from queue."); break; case MessageType.xml: Program.pe("xml messages should be handled in Master thread without consuming."); return; default: case MessageType.uninitialized: Program.pe("uninitialized message consumed"); return; } }