using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace grof.protocols.membership{
/// <summary>
/// The <c>MulticastSender</c> is used for
/// sending LEAVE and JOIN messages to all the other
/// group members of the same group. The messages are
/// sent as UDP datagrams.
/// </summary>
public class MulticastSender
{
/// <summary>
/// The IP address of the multicast group where
/// the messages are sent.
/// </summary>
private IPEndPoint ipEndPoint;
/// <summary>
/// Helper class for sending multicast messages via
/// UDP.
/// </summary>
private UdpClient sender;
/// <summary>
/// The name of the group member.
/// </summary>
private string memberName;
/// <summary>
/// Creates a MulticastSender object which is able to send messages
/// to a multicast group.
/// </summary>
/// <param name="groupAddress">The IP address of the multicast group as string.</param>
/// <param name="groupPort">The multicast port.</param>
public MulticastSender( string memberName, string groupAddress, int groupPort )
{
this.memberName = memberName;
this.sender = new UdpClient();
this.ipEndPoint = new IPEndPoint( IPAddress.Parse( groupAddress ), groupPort );
Debug.WriteLine( "[MulticastSender#constructor] " + memberName + ": Multicast sender object created." );
}
/// <summary>
/// Sends a message to a multicast group.
/// </summary>
/// <param name="message">The message which is sent.</param>
public void Send( Message message )
{
Debug.WriteLine( "[MulticastSender#Send] " + memberName + ": Sending message..." );
byte[] data = message.ToBytes();
sender.Send( data, data.Length, this.ipEndPoint );
Debug.WriteLine( "[MulticastSender#Send] " + memberName + ": Message was sent, content: " + message.GetData() );
}
/// <summary>
/// Stops the <c>MulticastReceiver</c> instance
/// and cleans all required resources.
/// </summary>
public void Stop()
{
sender.Close();
Debug.WriteLine( "[MulticastSender#Stop] " + memberName + ": Multicast sender stopped." );
}
}
}
|