private long ParseLedgerHeight(Protos.Discovery.Peer peerRet) { if (-1L == LedgerHeight) { try { Envelope stateInfo = peerRet.StateInfo; GossipMessage stateInfoGossipMessage = GossipMessage.Parser.ParseFrom(stateInfo.Payload); if (stateInfoGossipMessage.ContentCase != GossipMessage.ContentOneofCase.StateInfo) { throw new ArgumentException("Error " + stateInfoGossipMessage.ContentCase + " Expected StateInfo"); } StateInfo stateInfo1 = stateInfoGossipMessage.StateInfo; LedgerHeight = (long)stateInfo1.Properties.LedgerHeight; chaincodesList = stateInfo1.Properties.Chaincodes.ToList(); } catch (InvalidProtocolBufferException e) { throw new InvalidProtocolBufferRuntimeException(e); } } return(LedgerHeight); }
private void OnMessageReceived(IPEndPoint remoteEndPoint, GossipMessage message) { _configuration.Logger.Verbose("Received {@Message} from {RemoteEndPoint}", message, remoteEndPoint); switch (message.Type) { case GossipMessageType.Ping: HandlePing(remoteEndPoint, (PingMessage)message); break; case GossipMessageType.Ack: HandleAck(remoteEndPoint, (AckMessage)message); break; case GossipMessageType.Alive: HandleAlive(remoteEndPoint, (AliveMessage)message); break; case GossipMessageType.Dead: HandleDead(remoteEndPoint, (DeadMessage)message); break; default: throw new NotSupportedException(); } }
private string ParseEndpoint(Protos.Discovery.Peer peerRet) { if (null == Endpoint) { try { Envelope membershipInfo = peerRet.MembershipInfo; ByteString membershipInfoPayloadBytes = membershipInfo.Payload; GossipMessage gossipMessageMemberInfo = GossipMessage.Parser.ParseFrom(membershipInfoPayloadBytes); if (GossipMessage.ContentOneofCase.AliveMsg != gossipMessageMemberInfo.ContentCase) { throw new ArgumentException("Error " + gossipMessageMemberInfo.ContentCase + " Expected AliveMsg"); } AliveMessage aliveMsg = gossipMessageMemberInfo.AliveMsg; Endpoint = aliveMsg.Membership.Endpoint; if (Endpoint != null) { Endpoint = Endpoint.ToLowerInvariant().Trim(); //makes easier on comparing. } } catch (InvalidProtocolBufferException e) { throw new InvalidProtocolBufferRuntimeException(e); } } return(Endpoint); }
private static IPacket GetGossipMessagePkt(ulong guid, GossipMessage gossipMessage) { IPacket packet = WorldPacketFactory.Create(WMSG.SMSG_GOSSIP_MESSAGE); BinaryWriter writer = packet.CreateWriter(); writer.Write(guid); writer.Write(0); writer.Write(gossipMessage.TextId); writer.Write(gossipMessage.GossipMenu.Count); foreach (GossipMenuItem menuItem in gossipMessage.GossipMenu) { writer.Write(menuItem.MenuId); writer.Write((byte)menuItem.Icon); writer.Write((byte)(menuItem.InputBox ? 1 : 0)); writer.Write(menuItem.Cost); writer.WriteCString(menuItem.Text); writer.WriteCString(menuItem.AcceptText); } writer.Write(gossipMessage.QuestsMenu.Count); foreach (QuestsMenuItem menuItem in gossipMessage.QuestsMenu) { writer.Write(menuItem.Id); writer.Write(menuItem.Icon); writer.Write((uint)0); writer.Write(menuItem.Text); } return(packet); }
private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message) { var rawMessage = message as RawGossipMessage; if (rawMessage == null) return; if (rawMessage.MessageType == 1) { Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower())); Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size); Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer))); } }
public void Dispatch(ISession session, IPacket packet) { ulong targetGuid = packet.CreateReader().ReadUInt64(); var message = new GossipMessage(2, new[] { new GossipMenuItem(2, GossipMenuIcon.Banker, false, "Need bank?"), new GossipMenuItem(1, GossipMenuIcon.Gossip, false, "I would die!") }, new[] { new QuestsMenuItem(3, 2, "hi"), }); session.SendGossipMessage(targetGuid, message); }
public async Task BroadcastMessageAsync(GossipMessage message, Func<GossipClusterMember, bool> selector = null) { if (message == null) throw new ArgumentNullException("message"); if (selector == null) selector = m => true; var tasks = new List<Task>(); foreach (var member in _members.Values.Where(selector)) { tasks.Add(member.SendMessageAsync(message)); } await Task.Run(() => Task.WaitAll(tasks.ToArray())); }
public int BroadcastMessage(GossipMessage message, Func<GossipClusterMember, bool> selector = null) { if (message == null) throw new ArgumentNullException("message"); if (selector == null) selector = m => true; var waitTasks = new List<Task>(); var resultTasks = new List<Task<bool>>(); foreach (var member in _members.Values.Where(selector)) { var task = member.SendMessageAsync(message); waitTasks.Add(task); resultTasks.Add(task); } Task.WaitAll(waitTasks.ToArray()); return resultTasks.Count(x => x.Result); }
public void describe_custom_gossip_message_factory_creating_different_message_types() { var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") }; var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1)); var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2)); var message1 = new GossipMessage1(); var message2 = new GossipMessage2(); GossipMessageFactory.CreateMessage += CreateMessage; using (var node1 = new GossipNode(nodeConfig1, clusterConfig)) using (var node2 = new GossipNode(nodeConfig2, clusterConfig)) { var received = new AutoResetEvent(false); GossipMessage receivedMsg = null; node1.OnMessageReceived += (node, conn, msg) => { receivedMsg = msg; received.Set(); }; node1.StartListening(); node2.StartListening(); node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator)); node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator)); node2.Cluster.BroadcastMessage(message1); if (!received.WaitOne(Timeout)) { Assert.Fail(); } Assert.IsInstanceOf <GossipMessage1>(receivedMsg); node2.Cluster.BroadcastMessage(message2); if (!received.WaitOne(Timeout)) { Assert.Fail(); } Assert.IsInstanceOf <GossipMessage2>(receivedMsg); } }
private void ProcessInfo(GossipNode node, GossipConnection connection, GossipMessage message) { var rawMessage = message as RawGossipMessage; string msg = Encoding.UTF8.GetString(rawMessage.Buffer); if (msg == "") { Console.WriteLine("First Handshake"); JoinCluster(node.NodeConfig.BindToEndPoint); string senMsg = JsonConvert.SerializeObject(RingInfos); Gossip.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes(senMsg))).Wait(); } else { Console.WriteLine("Second Handshake"); var ringInfos = JsonConvert.DeserializeObject <List <RingInfo> >(msg); RingInfos = ringInfos; } }
private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message) { var rawMessage = message as RawGossipMessage; if (rawMessage == null) { return; } if (rawMessage.MessageType == 1) { Debug.WriteLine(String.Format("Node Info: {0}", node.NodeConfig.Tags.First())); Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower())); Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size); Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer))); } }
public async Task<bool> SendMessageAsync(GossipMessage message) { if (Connection == null) return false; await message.WriteToStreamAsync(Connection.Stream); return true; }
public async Task<bool> SendMessageAsync(GossipMessage message) { if (message == null) throw new ArgumentNullException("message"); if (message.Expires >= GossipTimestampProvider.CurrentTimestamp) { OnMessageExpired(this, message); return false; } using (var session = CreateSession()) { if (message.Expires >= GossipTimestampProvider.CurrentTimestamp) { OnMessageExpired(this, message); return false; } try { return await session.SendMessageAsync(message); } catch (IOException) { session.DisposeAction = c => c.Close(); return false; } catch (ObjectDisposedException) { session.DisposeAction = c => c.Close(); return false; } } }
public void SendGossipMessage(ulong targetGuid, GossipMessage message) { Send(GetGossipMessagePkt(targetGuid, message)); }
private void ProcessInfo(GossipNode node, GossipConnection connection, GossipMessage message) { var rawMessage = message as RawGossipMessage; string msg = Encoding.UTF8.GetString(rawMessage.Buffer); if (msg == "") { Console.WriteLine("First Handshake"); JoinCluster(node.NodeConfig.BindToEndPoint); string senMsg = JsonConvert.SerializeObject(RingInfos); Gossip.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes(senMsg))).Wait(); } else { Console.WriteLine("Second Handshake"); var ringInfos = JsonConvert.DeserializeObject<List<RingInfo>>(msg); RingInfos = ringInfos; } }
public void Send(IPEndPoint remoteEndPoint, GossipMessage message) { Debug.Assert(remoteEndPoint != null); Debug.Assert(message != null); List <EventWaitHandle> eventsToTrigger = null; using (var ms = new MemoryStream()) { try { _configuration.Logger.Verbose("Sending {@Message} to {RemoteEndPoint}", message, remoteEndPoint); _messageEncoder.Encode(message, ms); List <byte[]> messageBytes = null; foreach (var broadcast in _broadcasts.GetBroadcasts(0, 4096)) { _configuration.Logger.Verbose("Sending {@Message} to {RemoteEndPoint}", broadcast.Message, remoteEndPoint); if (messageBytes == null) { messageBytes = new List <byte[]>(); messageBytes.Add(ms.ToArray()); ms.SetLength(0); } messageBytes.Add(broadcast.MessageBytes); if (broadcast.Event != null) { if (eventsToTrigger == null) { eventsToTrigger = new List <EventWaitHandle>(); } eventsToTrigger.Add(broadcast.Event); } } if (messageBytes != null) { message = new CompoundMessage(messageBytes); ms.SetLength(0); _messageEncoder.Encode(message, ms); } if (_configuration.CompressionType.HasValue) { _configuration.Logger.Verbose("Compressing {@Message} to {RemoteEndPoint} with {CompressionType}", message, remoteEndPoint, _configuration.CompressionType.Value); message = new CompressedMessage(_configuration.CompressionType.Value, message); ms.SetLength(0); _messageEncoder.Encode(message, ms); } } catch (Exception ex) { _configuration.Logger.Error(ex, "Unable to send message(s) to {RemoteEndPoint}.", remoteEndPoint); return; } _client.Send(remoteEndPoint, ms.ToArray()); if (eventsToTrigger != null) { foreach (var @event in eventsToTrigger) { @event.Set(); } } } }