//Send primitive to send data to the multicast(point to point) addresses

import java.io.*;

import java.net.*;

import java.util.*;


class Send extends Thread
{

    private int group_id;

    private DatagramSocket socket;

    private String[] address;

    private String unicast_address;

    private int unicast_port;

    private int[] port;
    
    Send(/*Message m,*/ int  g_id) 
    {
	
	group_id =g_id; 
	
	try
	    {
		socket = new DatagramSocket(send_port);
	    	
	    }
	
	catch(IOException e)
	    {
		
		System.out.println(e);
		
	    }
	
	t= new Thread(this);
	
	t.start();
    }	   
    
    run()
    {
	
       	If(group_id==0)// group_id=0 means unicast message 
	    {

		//resolveaddress();
		
		try
		    {
			
			//sends a point to point  message
			
			ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1000);
			
			os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
			
			os.flush();
			
			os.writeObject("Hi, How Are You");
			
			os.flush();
			
			//retrieves byte array
			
			byte[] msg = byteStream.toByteArray();
			
			DatagramPacket hi = new DatagramPacket(msg, msg.length,unicast_address,unicast_port);
			
			socket.send(hi);
		    }

		catch (IOException e)
		    {

			System.out.println(e);

		    }	
		
		
	    }
	
	if(group_id==1)//group_id=1 means multicast message
	    {

		//resolveaddresslist();/returns the list of addresses and the port

		for(i=0;i<5;i++)
		    {
			
			try
			    {
				
				// sends a message to the server to each server in the group
				
				ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1000);
				
				os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
				
				os.flush();
				
				os.writeObject("Hi, How Are You");
				
				os.flush();
				
				//retrieves byte array
				
				byte[] msg = byteStream.toByteArray();
				
				DatagramPacket hi = new DatagramPacket(msg, msg.length,address[i],port[i]);				
				
				socket.send(hi);
			    }

			catch (IOException e)
			    {

				System.out.println(e);

			    }	
			
		    }
		
	    }
	
	socket.close();

    }
    
    public resolveaddress()//returns the unicast address and port
    {
	unicast_address = "130.85.109.93";

	unicast_port = 1223;
    }
    
    public resolveaddresslist()//returns the address and corresponding port list for the multicast group
    {

	new String[5] address ={"130.85.109.92","130.85.109.93","130.85.109.92","130.85.109.92","130.85.109.92");

	new int[5] port={1223,1224,1225,1226,1227};

	
	
    }



}
