private void UnSubscribe(Subscription subscribe) { // TODO Do some locking foreach (Subscription subscriber in Subscribers) { if (subscriber.MessageType.Equals(subscribe.MessageType) && subscriber.Ip.Equals(subscribe.Ip)) { Subscribers.Remove(subscriber); break; } } }
private void UnSubscribe(string message) { string[] parts = message.Split(':'); // Validate the subscription message if (parts.Length == 3) { // Validate the IP address string[] ipParts = parts[2].Split('.'); if (ipParts.Length != 4) { return; } // Validate the message that we are unsubscribing from Subscription subscription = new Subscription(parts[1], parts[2],parts[3]); UnSubscribe(subscription); } }
private void SendMessage(Subscription subscription, string message) { try { TcpClient client = new TcpClient(subscription.Ip, subscription.Port); // Translate the passed message into ASCII and store it as a Byte array. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); stream.Flush(); stream.Close(); } catch (Exception ex) { Console.WriteLine("Exception while trying to send message {2} to {0} on port {1} Error: {3}",subscription.Ip,subscription.Port,message,ex.Message); // TODO remove subscribers that are not responding //Subscribers.Remove(subscription); } }
private void Subscribe(Subscription subscribe) { // TODO Do some locking bool found = false; foreach (Subscription subscriber in Subscribers) { if (subscriber.MessageType.Equals(subscribe.MessageType) && subscriber.Ip.Equals(subscribe.Ip)) { found = true; break; } } if (!found) Subscribers.Add(subscribe); }
public void Subscribe(Subscription subscription) { // Publish the message to the broker TcpClient client = new TcpClient(BrokerIp, BrokerPort); // Translate the passed message into ASCII and store it as a Byte array. Byte[] data = System.Text.Encoding.ASCII.GetBytes(subscription.SubscribeMessage); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); stream.Flush(); client.Close(); }