/*************************************************
 * Author: 	Evelyn Lai-Tee Cheok
 *			Department of Electrical Engineering
 *			Center for Telecommunications Research
 *			Schapiro Research Building
 *			Columbia University
 *
 * Email: 	laitee@ctr.columbia.edu
 *
 **************************************************/
/* NB: In writting this class , has tried to subclass from 
 * MulticastSocket class but not allowed to do so since 
 * MulticastSocket is a "final" class.
 */
import sun.net.*;
import java.net.*;
import java.lang.*;

/** Class implements sender multicast socket based on the 
	sun.net.MulticastSocket, since JDK 1.1b wasn't officially
	released at the time this project has been developed **/
class SenderMCSocket {
    MulticastSocket senderSock;
    InetAddress senderMCAddr;
    int senderPort;
    byte senderTTL;
    byte senderByte[] = new byte[1024];

	/** Constructor for class. 
		accepts mcAddr in String format, port number and ttl value.
		The mcAddr will be converted to InetAddress which is required
		by the SendByteData function **/
    public SenderMCSocket(String mcAddr, int port, byte ttl) {
        senderPort = port;
        senderTTL = ttl;

		try {
        	senderSock = new MulticastSocket();
		} catch (java.net.SocketException e) {
			System.out.println("failed to create senderSock: " + e);
		} catch (java.io.IOException e) {
			System.out.println("IOException,failed to create senderSock: " + e);
		}

		try {
        	senderMCAddr = InetAddress.getByName(mcAddr);
		} catch (java.net.UnknownHostException e) {
			System.out.println("failed to create senderMCAddr: " + e);
		}

    }

	/** creates datagram packet to contain the byte array data before
		sending through the multicast socket **/
    public void sendByteData(byte[] data_byte) {
        DatagramPacket senderPkt = new DatagramPacket( data_byte,
                                data_byte.length, senderMCAddr,
                                senderPort );
		try {
        	senderSock.send( senderPkt, senderTTL );
		} catch (java.net.SocketException e) {
			System.out.println("failed to send datagram packet: " + e);
		} catch (java.io.IOException e) {
			System.out.println("failed to send datagram packet: " + e);
		} 
    }

}


