/*
 * Created on 09-feb-2006
 *
 * To change the template for this generated file go to
 */
import java.io.*;
import java.net.*;

import com.sun.rsasign.al;

import sun.security.x509.IPAddressName;
/**
 * @author jballesteros
 *
 * Cliente del SMS Alamin
 */
public class gsgc {
	//Informacion necesaria para construir el objeto de alamin

	/** Telefono al que se envia el mensaje */	
	private String telefono;
	/** Mensaje a enviar */
	private String mensaje;
	/** Opciones y parametros para encolar el mensaje */
	private String opt_smsc="default";
	/** Opciones y parametros para encolar el mensaje */
	private String opt_priority="5"; 
	/** IP o nombre del servidor que alberga el alamin */
	private String hostname="129.47.1.90";
	/** Puerto en el que escucha el Alamin*/
	private int puerto=11201;
	
	/** Constantes para hablar con alamin */
	final static String EOL = "<EOL>";
	final static String EOM = "<EOM>";
	
	public gsgc(){
		//Constructor sin argumentos no hace nada
	}
	
	public gsgc(String tlfno,String sms ){
		telefono=tlfno;
		mensaje=sms;
		
	}
	
/** 
 * Funcion que hace el envio del mensaje conectandose al puerto del alamin, por defecto el 11201
 * 
 * @return String error : OK o ERROR en funcion de si se ha podido enviar el mensaje o no
 */	
	public String enviar_sms(){
		
		DataInputStream in=null;
		DataOutputStream out=null;
		Socket al=null;
		String error=null;
/** Antes de enviar el mensaje lo troceamos en varias partes si es mas largo de 160 caracteres*/
		int longitud = mensaje.length();
		System.out.println("ROLLO DE PURACION: Limtacion del mensaje:"+longitud);
		try {
		
		al=new Socket(hostname,11201);
		out = new DataOutputStream (al.getOutputStream());
		in = new DataInputStream (al.getInputStream());
					
		} catch (UnknownHostException k){
			System.out.println("ERROR HOSTNAME");
		 
		}catch (IOException e){
			System.out.println("ERROR DE CONEXION");
		}
		
		if (al != null && in != null && out != null){
			try {
			String Respuesta;
			while ((Respuesta = in.readLine()) != null){
				System.out.println("SERVIDOR: "+Respuesta);
				if (Respuesta.indexOf("READY")!= -1){
					System.out.println("SERVIDOR [CONECTADO]: "+Respuesta);
					out.writeBytes("send "+opt_priority+ " - "+opt_smsc+" <list>");
					out.writeBytes("\n");
					out.writeBytes(telefono);
					out.writeBytes("\n");
					out.writeBytes(EOL);
					out.writeBytes("\n");
					out.writeBytes(mensaje);
					out.writeBytes("\n");
					out.writeBytes(EOM);
					out.writeBytes("\n");
					
					while ((Respuesta = in.readLine()) != null ){
					System.out.println("SERVIDOR [RESPUESTA]: "+Respuesta);
					//Respuesta = in.readLine();
					if (Respuesta.indexOf("WAIT")!= -1){
								String Respuesta2=in.readLine();
								if (Respuesta2.indexOf("OK")!= -1){
										String Respuesta3=in.readLine();
										if (Respuesta3.indexOf("READY")!= -1){
											out.writeBytes("CLOSE");
											out.writeBytes("\n");
											error="OK";
											return error;
										}
									}
								}
					else  {
						
							error="ERROR";
							return error;
							}
					}									
				}					
			}
			
			}catch (Exception e){
				e.printStackTrace();
				error = "Error de conexion II ";
				return 	error;	
			}
			
			
		}
		
		return error;
	}
	
	

	public static void main(String[] args) {
		
		gsgc n = new gsgc ("619461236","HOLA DESDE GAIAAAAAA ESTE ES UN MENSAJE QUE PODRIA CAUSAAR PROBLEMAS O NO DE PENDE DEL NUMERO DE CARATR ASD ASD ASD ASD asdasd");
		//gsgc n1 = new gsgc ("619461236","Probando las limitaciones del contructor");
		String resultado = n.enviar_sms();
	//	String resultado2 = n1.enviar_sms();
		System.out.println("MENSAJE ENVIADO, RESULTADO: "+resultado);
	//	System.out.println("MENSAJE ENVIADO, RESULTADO: "+resultado2);
		
	}
}










