public void HandleRequest(Message request, Message response) { //System.Diagnostics.Debug.WriteLine(string.Format( // "{0}\t{1} received {2}", // DateTime.Now, // this.node.Entry.Address, // sourceEndPoint.Port)); // TODO: Here we handle the messages that get received by the node. // TODO: Call methods on the Node class to merge hash rings and do failure detection. // TODO: Liskov substitution principle violation that I'm not happy about. if (response != null) { response.Source = this.node.Entry.Address; var heartbeat = new HeartBeat(); heartbeat.Nodes = node.Nodes; response.Parts.Add(heartbeat.GetType().Name.ToLowerInvariant(), JsonSerializer.SerializeToString(heartbeat)); string[] sourceAddress = request.Source.Split(':'); var sourceEndPoint = new IPEndPoint(IPAddress.Parse(sourceAddress[0]), int.Parse(sourceAddress[1])); } }
public void Send(Message message, IPEndPoint endPoint) { System.Diagnostics.Debug.WriteLine(string.Format( "{0} SENT {1}", this.node.Entry.Address, message.Id)); string msg = this.serializer.SerializeToString(message); transport.Prepare(message); this.transport.Send(msg, endPoint); }
public void HandleResponse(Message response) { // TODO: Here we handle the messages that get received by the node. // TODO: Call methods on the Node class to merge hash rings and do failure detection. // TODO: Liskov substitution principle violation that I'm not happy about. //System.Diagnostics.Debug.WriteLine(string.Format( // "{0}\t{1} received {2}", // DateTime.Now, // this.node.Entry.Address, // sourceEndPoint.Port)); }
private void HandleMessage(string message, Socket requestSocket) { if (message != string.Empty) { var request = this.serializer.DeserializeFromString(message); //System.Diagnostics.Debug.WriteLine(string.Format( // "{0} RECV {1}", // this.EndPoint.Port, // request.Id)); Message response = null; if (request.CorrelationId == null) { response = new Message(); response.CorrelationId = request.Id; response.DestinationAddress = request.SourceAddress; foreach (var handler in this.RequestHandlers) { handler.HandleRequest(request, response); } } else { foreach (var handler in this.ResponseHandlers) { handler.HandleResponse(request); } } if (request.CorrelationId == null) { //System.Diagnostics.Debug.WriteLine(string.Format( // "{0} SENT {1}", // this.EndPoint.Port, // response.Id)); var msg = this.serializer.SerializeToString(response); this.Send(requestSocket, msg); } } }
private void HeartBeatTimer(object state) { var heartbeat = new HeartBeat(); heartbeat.Nodes = this.Nodes; var msg = new Message(); messageBus.AddMessage<HeartBeat>(msg, heartbeat); // Choose a random node from the ring to gossip with. var nodeNumber = random.Next(0, this.Nodes.Count); if (this.Nodes.Count > 0) { var destinationEndpoint = IPEndPointParser.Parse(this.Nodes.ElementAt(nodeNumber).Value.Address); this.messageBus.Send(msg, destinationEndpoint); } }
public void Prepare(Message message) { message.Source = EndPoint.ToString(); }
private void HandleMessage(string message, Socket requestSocket) { if (message != string.Empty) { var request = this.serializer.DeserializeFromString(message); System.Diagnostics.Debug.WriteLine("{0} RECV {1}", this.EndPoint.Port, request.Id); Message response = null; if (request.CorrelationId == null) { response = new Message { CorrelationId = request.Id, Destination = request.Source }; foreach (var handler in this.requestHandlers) { handler.HandleRequest(request, response); } } else { foreach (var handler in this.responseHandlers) { handler.HandleResponse(request); } } if (request.CorrelationId == null) { System.Diagnostics.Debug.WriteLine(string.Format( "{0} SENT {1}", this.EndPoint.Port, response.Id)); var msg = this.serializer.SerializeToString(response); requestSocket.Send(msg, Encoding.UTF8); } } }
public Message CreateMessage(object part) { var message = new Message(); message.Parts.Add(part.GetType().Name.ToLowerInvariant(), JsonSerializer.SerializeToString(part)); return message; }