

package Tandem;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class Enviar
{
    public void EnviarTrama(String xmlString) throws Exception
    {


        String tandem ="100";  //Inicializar 100
        String recibe="";
          
        
      
         try
                    {
                        Socket socket=null;
                        socket = new Socket();
                        InetAddress addr = InetAddress.getByName(this.ObtenerHost());
                        SocketAddress sockaddr = new InetSocketAddress(addr, Integer.parseInt(this.ObtenerPuerto()));
                        socket.connect(sockaddr, Integer.parseInt(this.ObtenerTimeOut()));
                        DataInputStream delServidor;
                        delServidor =new DataInputStream(socket.getInputStream());
                        DataOutputStream alServidor;
                        alServidor =new DataOutputStream(socket.getOutputStream());
                        tandem="TIMEOUT";
                         try{
                         socket.setSoTimeout(Integer.parseInt(this.ObtenerTimeOut()));
                         
                         System.out.println("Envio al Tandem Cargas:"+xmlString);
                         alServidor.writeUTF(xmlString);
                         recibe=delServidor.readUTF();                         
                         System.out.println("Respuesta del Tandem Cargas:"+recibe);
                         
                         //String recibe="<HEADING><MESSAGE TYPE=\"RESP\" PROC_CDE=\"CHANGE\" SEQ_NUM=\"0001\" RESP_CDE=\"00\" /></HEADING>";
                         recibe = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + recibe;

                         DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
                         DocumentBuilder builder = factory1.newDocumentBuilder();
                         Document document = builder.parse(new InputSource(new StringReader(recibe)));
                         NodeList nlist2=document.getElementsByTagName("MESSAGE");
                         recibe = nlist2.item(0).getAttributes().item(1).getNodeValue();

                         if(recibe.equals("00"))
                            {tandem="00";}
                         else
                            {tandem=recibe;}

                         }
                         catch(Exception e)
                         {
                             System.out.println("Cargas:Tandem:Error Enviar Trama tandem:"+e.getMessage());
                             alServidor.close();
                             delServidor.close();
                             socket.close();

                         }
                         alServidor.close();
                         delServidor.close();
                         socket.close();

                    }
                    catch(Exception e)
                    {
                        System.out.println("Cargas:Tandem:Error Enviar Regla tandem:"+e.getMessage());
                        throw e;
                    }
  
       
        
    }

    private String ObtenerPuerto() throws Exception
    {
       Properties properties = new Properties();        
       String valor="";
       
       try 
       {
              InputStream inputStream = getClass().getResourceAsStream("config.properties");
              properties.load(inputStream);           
              valor=properties.getProperty("port");
 
    	} 
        catch (Exception ex) 
        {
    	     System.out.println("Cargas:Tandem: Error ObtenerPuerto:"+ ex.getLocalizedMessage());
             throw ex;
        }

     return valor;
     }

    private String ObtenerHost() throws Exception
    {
       Properties properties = new Properties();        
       String valor="";
       
       try 
       {
              InputStream inputStream = getClass().getResourceAsStream("config.properties");
              properties.load(inputStream);           
              valor=properties.getProperty("host");
 
    	} 
        catch (Exception ex) 
        {
    	    System.out.println("Cargas:Tandem: Error ObtenerHost:"+ ex.getLocalizedMessage());
            throw ex;
        }

     return valor;
     }
    
    private String ObtenerTimeOut() throws Exception
    {
       Properties properties = new Properties();        
       String valor="";
       
       try 
       {
              InputStream inputStream = getClass().getResourceAsStream("config.properties");
              properties.load(inputStream);           
              valor=properties.getProperty("timeout");
 
    	} 
        catch (Exception ex) 
        {
    	    System.out.println("Cargas:Tandem: Error ObtenerTimeOut:"+ ex.getLocalizedMessage());
            throw ex;
        }

     return valor;
     }

}
