private static void SendSideObjects(UdpClient client, IPEndPoint remote) { Console.WriteLine("What's the color of your figure? (You can press enter if it is not relevant)"); var color = Console.ReadLine(); Console.WriteLine("What's the shape of your figure? (You can press enter if it is not relevant)"); var shape = Console.ReadLine(); Console.WriteLine("What's the X position of your figure? (You can press enter if it is not relevant)"); var positionX = Console.ReadLine(); Console.WriteLine("What's the Y position of your figure? (You can press enter if it is not relevant)"); var positionY = Console.ReadLine(); if (color == NoValue && shape == NoValue && positionX == NoValue && positionY == NoValue) { Console.WriteLine("At least one of the parameters must be specified. Abort."); return; } Console.WriteLine("At which side we should look for objects? (0 - Left, 1 - Right, 2 - Up, 3 - Down)"); var sideLine = Console.ReadLine(); if (String.IsNullOrWhiteSpace(sideLine)) { Console.WriteLine("Side must be specified. Abort."); return; } var side = Convert.ToByte(sideLine); var message = new GetSideObjects(); message.Side = side; if (!String.IsNullOrWhiteSpace(color)) { message.Color = color; } if (!String.IsNullOrWhiteSpace(shape)) { message.Shape = shape; } if (!String.IsNullOrWhiteSpace(positionX)) { message.PositionX = float.Parse(positionX, CultureInfo.InvariantCulture); } else { message.PositionX = float.NaN; } if (!String.IsNullOrEmpty(positionY)) { message.PositionY = float.Parse(positionY, CultureInfo.InvariantCulture); } else { message.PositionY = float.NaN; } var jsonMessage = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); var messageBytes = new byte[jsonMessage.Length + 1]; messageBytes[0] = (byte)MessageType.GetSideObjects; Array.Copy(jsonMessage, 0, messageBytes, 1, jsonMessage.Length); var stopWatch = new Stopwatch(); client.Send(messageBytes, messageBytes.Length, remote); Console.WriteLine("Request is sent."); Console.WriteLine("Waiting for an answer..."); try { stopWatch.Start(); var answerBytes = client.Receive(ref remote); stopWatch.Stop(); Console.WriteLine("Answer is received."); Console.WriteLine("Request took " + stopWatch.Elapsed.ToString()); var foundObjects = JsonConvert.DeserializeObject <List <PrimitiveObject> >(Encoding.UTF8.GetString(answerBytes)); Console.WriteLine("Objects found: " + foundObjects.Count.ToString()); foreach (var obj in foundObjects) { Console.WriteLine(obj.ToString()); } } catch (Exception e) { Console.WriteLine("Request is not handled"); Console.WriteLine(e); } }
private static List <PrimitiveObject> GetSideObjects(GetSideObjects message) { /* Form properties based on the provided message */ var targetNodeProps = new Dictionary <String, object>(); if (!String.IsNullOrWhiteSpace(message.Color)) { targetNodeProps.Add("Color", message.Color); } if (!String.IsNullOrWhiteSpace(message.Shape)) { targetNodeProps.Add("Shape", message.Shape); } if (!float.IsNaN(message.PositionX)) { targetNodeProps.Add("X", message.PositionX); } if (!float.IsNaN(message.PositionY)) { targetNodeProps.Add("Y", message.PositionY); } string side; if (message.Side == 0) { side = "Left"; } else if (message.Side == 1) { side = "Right"; } else if (message.Side == 2) { side = "Up"; } else { side = "Down"; } /* Run a query on the graph database */ var query = new Query(GraphDatabase); query.Match(new NodeDescription("Primitive", targetNodeProps)); query.To(new RelationDescription(side)); var foundNodes = query.Match(new NodeDescription("Primitive")); query.Execute(); var sideObjects = new List <PrimitiveObject>(); foreach (var obj in foundNodes.Nodes) { sideObjects.Add(new PrimitiveObject() { Shape = (string)obj.Properties["Shape"].Value, Color = (string)obj.Properties["Color"].Value, X = (float)obj.Properties["X"].Value, Y = (float)obj.Properties["Y"].Value }); } return(sideObjects); }