/* 
 * Connection.java describes a connection from one computer to another.  Every program on the network
 * will have one and only one Connection instance for every other program.  Each Connection object 
 * has the port the other coputer is listening, the name of the person using that computer, the other
 * person's IP address, and the socket used to communicate.  The user's address, listening port, and
 * username are also stored here to send to the connecting computer.
 *
 * Member functions include connect(), which is used to make a connection to another machine, GetIP(),
 * GetName(), and GetPort(), which get the IP, username, and Port from the user via the command line. 
 * These are then used to make a connection to another machine.  The last function is Print_to_Socket,
 * which sends a message to the other machine.
 */

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import Network;

  /* This is the Connection object.  Every connection to another computer is an instance of
       this class.  All the connections are stored in an array in the Network object. */
    public class Connection {
	
	/* member vars */
	private static Network net;
	
	private static String hostName = ""; // IP or address
	private static int port; // port other person is listening to
	private static String name; // username
	private static Socket connection;
		
	/* constructor */
	public Connection(Network network) {
	    net = network;
	}

	/* methods */
	public static void assign_hostName(String newhost) {
	    hostName = newhost;
	}

	public static void assign_port(int newport) {
	    port = newport;
	}

	public static void assign_name(String newname) {
	    name = newname;
	}

	public static void assign_connection(Socket newconnection) {
	    connection = newconnection;
	}

	public static String get_hostName() {
	    return hostName;
	}
	
	public static int get_Port() {
	    return port;
	}

	public static String get_Name() {
	    return name;
	}
	
	public static Socket get_socket() {
	    return connection;
	}

	/* connect() creates a socket to communicate to another machine.  It uses the hostename and the
	   port to establish the connection.  Then it sends the user's information to the other computer
	   so it can make the return connection automatically.  connect() returns the socket. */
	public static Socket connect() {
	    	    	    
	    try {
		
		/* make the connection */
		connection = new Socket((String) hostName, port);
		System.out.println("Connected.");
		
		/* send username, etc. to new connection.  don't need to send the address 
		 because there's a datagram method that will get it for us*/
		if (net.find_connection(name) == null)
		    {
			/* send the relevant data */
			Print_to_Socket(net.get_myUserName());
			// convert port to a string for Print_to_Socket
			String strPort = Integer.toString(net.get_myPort());
			Print_to_Socket(strPort);
		    }
	        else System.out.println("Connection already exists?\n");
		return connection; 
	    }
	    catch(IOException g) 
		{
		    System.out.println("Error opening connection to socket.");
		}  
	    return null;
	}

	/* Print_to_Socket takes a string as an argument, the message to be sent.  It then opens a 
	   datagram socket to the computer Connection describes.  The message is sent, and the datagram
	   socket is closed.*/

	/* NOTE: datagram is neither secure nor reliable, and the use of datagram will probably be 
	   changed. */
	public static void Print_to_Socket(String arg) {
	    try {

		/* make the instance of the datagram socket */
		DatagramSocket dsocket = new DatagramSocket();
		
		/* datagram gets messages as bytes */
		byte[] message;
		/* convert the message to be sent into a byte string */
		message = arg.getBytes();
		
		try {
		    /* get address of the person */
		    InetAddress address = InetAddress.getByName(hostName);
		    /* create the packet */
		    DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
		    /* send it */
		    dsocket.send(packet);
		}
		catch(IOException g) 
		    {
			System.out.println("Could not open a connection to " + name);
		    }	    
		dsocket.close();
	    }
	    catch(IOException e) {
		System.out.println("Unable to open datagram socket.");
	    }
	}

	/*	public static void GetConnections()
	{
	    int x = ptr;
	    GetDir();
	    try 
		{
		    File f = new File(directory, "connect.txt");
		    FileReader Connect = new FileReader(f);
	     
		    BufferedReader read = new BufferedReader(Connect);
		    while ( (list[x] = read.readLine()) != null)
			{
			    //System.out.println(list[x] + " read in");
			    x++;
			    //   read.readNext();
			}
		    ptr = x;
		    Connect.close();
		}
	    catch(IOException a) {
		System.out.println("Error reading from file.");
	    };
	
	    }*/
    }

