diff --git a/IOTManagment/Model/Messages/Message.cs b/IOTManagment/Model/Messages/Message.cs index 3b16f06..6f800f7 100644 --- a/IOTManagment/Model/Messages/Message.cs +++ b/IOTManagment/Model/Messages/Message.cs @@ -5,8 +5,8 @@ namespace Model.Messages { public class Message - { - public Guid messageId { get; } + { + public Guid messageId { get; set; } = Guid.NewGuid(); public string messageBody { get; } // = PayloadBody public MessageType messageType { get; } public string senderIP { get; } @@ -14,7 +14,6 @@ public class Message public Message(string messageBody, MessageType messageType, string senderIP,int senderPort) { - this.messageId = Guid.NewGuid(); this.messageBody = messageBody; this.messageType = messageType; this.senderIP = senderIP; @@ -28,5 +27,6 @@ public enum MessageType FORWARD=2, RESPONSEAPI=3, QUERY = 4, + STOP = 5, }; } \ No newline at end of file diff --git a/IOTManagment/Model/Queries/Query.cs b/IOTManagment/Model/Queries/Query.cs index 79350f4..e4b7de4 100644 --- a/IOTManagment/Model/Queries/Query.cs +++ b/IOTManagment/Model/Queries/Query.cs @@ -9,7 +9,7 @@ namespace Model.Queries { public class Query { - public Guid Id { get; private set; } = Guid.NewGuid(); + public Guid Id { get; set; } = Guid.NewGuid(); public SelectStatement SelectStatement { get; set; } public IntervalStatement IntervalStatement { get; set; } public WhereStatement? WhereStatement { get; set; } diff --git a/IOTManagment/NodeEngine/Networking/MessageHandler.cs b/IOTManagment/NodeEngine/Networking/MessageHandler.cs index 39dae85..da33936 100644 --- a/IOTManagment/NodeEngine/Networking/MessageHandler.cs +++ b/IOTManagment/NodeEngine/Networking/MessageHandler.cs @@ -73,6 +73,28 @@ public async void HandleMessage(string message) sender.SendMessage(); } break; + } + case MessageType.STOP: + { + Console.WriteLine($"MessageType: STOP QUERY {msg.messageBody}"); + + try + { + var id = JsonSerializer.Deserialize(msg.messageBody); + await scheduler.RemoveQueryjobAsync(id); + + foreach (IPEndPoint child in nodeChildren) + { + var sender = new NetworkSender(child, message); + sender.SendMessage(); + } + } + catch (Exception e) + { + Console.WriteLine($"Could not parse {msg.messageBody} as GUID"); + Console.WriteLine(e); + } + break; } default: // TODO: Handle if no type is given diff --git a/IOTManagment/Server/Networking/MessageHandler.cs b/IOTManagment/Server/Networking/MessageHandler.cs index 03db8a0..3bbdd1f 100644 --- a/IOTManagment/Server/Networking/MessageHandler.cs +++ b/IOTManagment/Server/Networking/MessageHandler.cs @@ -37,7 +37,7 @@ public void HandleMessage(string message) var NodeEndPoint = new IPEndPoint(IPAddress.Parse(node.Address), node.AddressPort); if (nodeChildren.Any(x => x.Address.Equals(IPAddress.Parse(msg.senderIP)))) { - Console.WriteLine("Node already exists."); + Console.WriteLine($"Node {msg.senderIP} reconnected."); break; } _topologyManager.AddNode(NodeEndPoint, node); @@ -83,6 +83,20 @@ public void HandleMessage(string message) } } + public void SendStop(Guid id) + { + string body = JsonSerializer.Serialize(id); + Message msg = new Message(body, MessageType.STOP, localIp.Address.ToString(), localIp.Port); + // Serialize message + var sendMsg = JsonSerializer.Serialize(msg); + + foreach (IPEndPoint child in nodeChildren) + { + var sender = new NetworkSender(child, sendMsg); + sender.SendMessage(); + } + } + public void SendQuery(Query query) { string body = JsonSerializer.Serialize(query); diff --git a/IOTManagment/Server/Program.cs b/IOTManagment/Server/Program.cs index 10700d8..09acc32 100644 --- a/IOTManagment/Server/Program.cs +++ b/IOTManagment/Server/Program.cs @@ -107,7 +107,8 @@ { Console.Clear(); queries.Remove(foundQ); - colorConsole("Query removed", ConsoleColor.Yellow, ConsoleColor.Black); + colorConsole($"Query removed {id}", ConsoleColor.Yellow, ConsoleColor.Black); + messageHandler.SendStop(id); break; } else { colorConsole("No such Query found", ConsoleColor.Red, ConsoleColor.White); Console.WriteLine("Please re-enter query id or write quit"); } @@ -115,7 +116,7 @@ } break; case "-help": - printCommands(1); + Console.Clear(); break; case "back": Console.Clear();