// <summary> // The following commands are internally used in the server // </summary> public dynamic debug(int commandID, int myserverID, int senderID, int receiverID, ComData comdata, DeviceRouting devices) { string CurrentTime = DateTime.Now.ToString("HH:mm:ss"); DeviceRegistry sender = devices.FindByID(receiverID); if (sender != null) {//has sender that is acknowledged by the server { string message1 = ""; comdata.Get(3, ref message1); string message2 = ""; comdata.Get(4, ref message2); Console.WriteLine("'" + sender.Name + "' at " + CurrentTime + ": " + message1 + '\n' + message2); } } else { Console.WriteLine("Incoming command doesn't have proper sender at " + CurrentTime); } return(null); }
/// <summary> /// Perform all handshaking to give and validate ID's as well as device names /// </summary> /// <param name="step">the step it is following</param> /// <param name="givenvalue">any value that is supposed to be supplied per step</param> /// <returns></returns> public string Handshake(int step, string givenvalue) { if (step > 0) { switch (step) { //step 1: supplied unique devicename and type in list, requests deviceID from server case 1: { string [] parameters = givenvalue.Split(",".ToArray(), 3); if (parameters != null) { string devicename = parameters[0]; string devicetype = parameters[1]; if (devicename.Length > 0 && devicetype.Length > 0) { if (devices.FindByName(devicename) == null) { DeviceRegistry device = new DeviceRegistry(devicename, devicetype); devices.Add(device); string deviceID = devices.FindIDByDevice(device).ToString(); Console.WriteLine(device.Name + ": registered to ID '" + deviceID + "'"); Console.WriteLine(DateTime.Now + " - Handshake step 1 - Device: " + devicename + " (" + deviceID + ")"); return(deviceID); } else { Console.WriteLine(DateTime.Now + " - Handshake step failed - Already registered Device: " + devicename); } } } break; } //Step 2: supplied deviceID, requests server name case 2: { int deviceID; if (Int32.TryParse(givenvalue.ToString(), out deviceID)) { //validate the existence of the device by id if (devices.FindByID(deviceID) != null) { Console.WriteLine(DateTime.Now + " - Handshake step 2 - DeviceID: " + deviceID); return(devices.FindByID(myserverID).Name); } else { Console.WriteLine(DateTime.Now + " - Handshake step failed - tried handshaking with unknown device on id '" + deviceID + "'"); } } else { } break; } //supplied deviceID, requests serverID case 3: { int deviceID; if (Int32.TryParse(givenvalue.ToString(), out deviceID)) { //validate the existence of the device by id if (devices.FindByID(deviceID) != null) { Console.WriteLine(DateTime.Now + " - Handshake step complete - Device: " + devices.FindByID(deviceID).Name + " (" + deviceID + ")"); return(myserverID.ToString()); } else { Console.WriteLine(DateTime.Now + " - Handshake step failed - tried handshaking with unknown device on id '" + deviceID + "'"); } } break; } } } return(""); }