/**
 * TCPDevice.java
 * Created May 2, 2006
 * Copyright (C) Tandberg Television 2006
 */
package com.tandbergtv.workflow.comm;

/**
 * Represents a network host that is capable of accepting and emitting TCP packets
 * 
 * @author Sahil Verma
 */
public class TCPDevice implements ISource, IDestination {

	private static final int DEFAULT_PORT = 0;
	
	private String ip;
	
	private int port;
	
	private String name=null;
	
	/**
	 * Creates a TCP device
	 */
	public TCPDevice() {
		super();
	}

	/**
	 * @param ip
	 */
	public TCPDevice(String ip) {
		this(ip, DEFAULT_PORT);
	}
	
	public TCPDevice(String ip, String name) {
		this(ip, DEFAULT_PORT, name);
	}

	/**
	 * @param ip
	 * @param port
	 */
	public TCPDevice(String ip, int port) {
		super();
		this.ip = ip;
		this.port = port;
	}

	public TCPDevice(String ip, int port, String name) {
		super();
		this.ip = ip;
		this.port = port;
		this.name=name;
	}

	/**
	 * @return Returns the ip.
	 */
	public String getIP() {
		return this.ip;
	}

	/**
	 * @param ip The ip to set.
	 */
	public void setIP(String ip) {
		this.ip = ip;
	}

	/**
	 * @return Returns the port.
	 */
	public int getPort() {
		return this.port;
	}

	/**
	 * @param port The port to set.
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "tcp://" + this.ip + ":" + this.port;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TCPDevice) {
			TCPDevice destination = (TCPDevice)obj;
			return this.ip != null && this.ip.equals(destination.ip) && this.port == destination.port;
		}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.ip.hashCode() ^ Integer.valueOf(this.port).hashCode();
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.comm.IDevice#getName()
	 */
	public String getName() {
		return name;
	}
}
