package mds;

import java.io.IOException;
import java.net.Socket;


/** Handles the TCP connection to the MDS+ server.
 * 
 * NEW/EXPERIMENTAL - will replaces MDSPlus etc
 * not actually written yet.
 * 
 */ 
public class MDSConnection {
	
	/** The TCP connection */
	private Socket s = null;
	private String host;
	private int port;
	
	/** Create TCP connection to specified server */
	public MDSConnection(String host, int port) {
			this.host = host;
			this.port = port;
			assureConnected();
	}
	
	/** Checks the TCP connection to the MDS+ server is alive and connects it if not */
	public final void assureConnected(){
		try {
			//if the socket exists but has disconnected, we need to start a new one
			if(s != null && !s.isConnected()){ 
				s.close(); 
				s = null; 
			};
		
			if(s == null){
				s = new Socket(host, port);				
			}
			
			
		} catch (IOException e) {
			throw new RuntimeException(e);
		} 	
		
	}
	
	/** Close the TCP socket */
	public final void close(){
		if (s != null)
			try {
				s.close();
			} catch (IOException e) { //Doesn't really matter since we don't ever write
				System.err.println("WARNING: Error on socket close: " + e.getMessage());
			}
	}
	
	public final void sendData(byte data[], short typeLen, byte idx, short dims[]){
		
		
	}
	
	public final void getReply(){
		
	}

}
