/*
 * OpenSocket.java
 *
 * Created on 17 août 2001, 11:27
 */
import java.net.*;
import java.io.*;
/**
 *
 * @author  ehugonne
 * @version
 */
public class OpenSocket
{
  /** Creates new OpenSocket */
  public OpenSocket(String host, int port, String action) throws SocketException,
      IOException
  {
    StringBuffer message = new StringBuffer("POST /UBSSoap HTTP/1.1\nHost:");
    message.append(host);
    message.append(':');
    message.append(port);
    message.append("\nConnection:keep-alive\nContent-type:text/xml\nSOAPAction:");
    /*message.append("\nContent-type:text/xml\nSOAPAction:");*/
    message.append(action);
    /*message.append("\nContent-length:206\n\n");*/
    /*message.append("\nTransfer-Encoding:chunked\n\n");*/
    message.append("\n\n");
    message.append("<message><sender>OpenSocket</sender><content>First message</content></message>\t\t\n");
    Socket socket = new Socket(host, port);
    socket.setKeepAlive(true);
    System.out.println("Socket opened");
    OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream());
    System.out.println("Got OutputStream");
    InputStreamReader socketIn = new InputStreamReader(socket.getInputStream());
    System.out.println("Got InputStream");
    socketOut.write(message.toString());
    socketOut.flush();
    System.out.println("Message sent :\n" + message.toString());
    char[] buffer = new char[8];
    char[] old = new char[]{'0','0','0','0','0','0','0','0'};
    int read;
    int end = -1;
    while(end == -1 && (read=socketIn.read(buffer)) != -1)
    {
      end = endOfDoc(old, buffer);
      if(end != -1)
      {
        for(int j = 0; j < end; j++)
          System.out.print(buffer[j]);
      }
      else
        System.out.print(buffer);
      old = buffer;
    }
    System.out.println("\nAll has been read");
 
    message = new StringBuffer("<message><sender>OpenSocket</sender><content>Second message</content></message>\n");
    message.append("\t\t\n");
    socketOut.write(message.toString());
    socketOut.flush();
    System.out.println("Second message sent :\n" + message.toString());
    socketOut.close();
  }
  
  private int endOfDoc(char[] old, char[] current) throws IOException
  {
    char[] all = new char[old.length + current.length];
    CharArrayWriter buffer = new CharArrayWriter(all.length);
    buffer.write(old);
    buffer.write(current);
    all = buffer.toCharArray();
    for(int i = 0; i < all.length-1; i++)
    {
      if(all[i] == '\t'&& all[i+1] == '\t')
        return i+1;
    }
    return -1;
  }
  
  /**
   * @param args the command line arguments
   */
  public static void main(String args[])
  {
    if (args.length != 3)
    {
      System.out.println("Usage :\n java OpenSocket host port action");
      System.exit(0);
    }
    try
    {
      new OpenSocket(args[0], Integer.parseInt(args[1]), args[2]);
      System.exit(1);
    }
    catch(NumberFormatException nfex)
    {
      System.out.println("Usage :\n java OpenSocket host port action");
      System.exit(0);
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
      System.exit(2);
    }
  }
  
}
