Skip to content

Commit

Permalink
Merge pull request #12 from anes300/BP-64-stop-query-message
Browse files Browse the repository at this point in the history
Bp 64 stop query message
  • Loading branch information
Chillhound authored Mar 25, 2022
2 parents 6b4babd + 9e7309c commit 0e699fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions IOTManagment/Model/Messages/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
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; }
public int senderPort { get; }

public Message(string messageBody, MessageType messageType, string senderIP,int senderPort)
{
this.messageId = Guid.NewGuid();
this.messageBody = messageBody;
this.messageType = messageType;
this.senderIP = senderIP;
Expand All @@ -28,5 +27,6 @@ public enum MessageType
FORWARD=2,
RESPONSEAPI=3,
QUERY = 4,
STOP = 5,
};
}
2 changes: 1 addition & 1 deletion IOTManagment/Model/Queries/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
22 changes: 22 additions & 0 deletions IOTManagment/NodeEngine/Networking/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Guid>(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
Expand Down
16 changes: 15 additions & 1 deletion IOTManagment/Server/Networking/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions IOTManagment/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@
{
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"); }
}
}
break;
case "-help":
printCommands(1);
Console.Clear();
break;
case "back":
Console.Clear();
Expand Down

0 comments on commit 0e699fc

Please sign in to comment.