/*
 * $Log$
 */
//////////////////////////////////////////////////
package general;

// Standard packages.
import java.io.*;
import java.net.*;

// Packages specific to network design tool.

class CGI {
public static String post(String home, String file, String message) {
  int i;
  int port = 80;
  Socket s = null;
  StringBuffer rbuf = new StringBuffer();
  String line;
  try {
    s = new Socket(home, port);
    DataOutputStream os = new DataOutputStream(s.getOutputStream());
    DataInputStream is = new DataInputStream(s.getInputStream());
    
    os.writeBytes("POST " + file
		  + " HTTP/1.0\r\n"
		  + "Content-type: text/plain\r\n"
		  + "Content-length: " 
		  + message.length() + "\r\n\r\n");
    os.writeBytes(message);
    while ( (line = is.readLine()) != null )
      rbuf.append(line + "\n");
    is.close();
    os.close();
  }
  catch (Exception e) {
    System.out.println("Error " + e.getMessage());
    if (s != null) {
      try {
	s.close();
      }
      catch (IOException ex) {
	System.out.println("IOException in post:");
      }
    }
  } 
  return rbuf.toString();
}

/**
 * call the CGI program which solves linear programming
 */
public static String linearProgramming(String sData) {
  String home = "www.cs.wustl.edu";
  String file = "/~hma/script/lp_solve.cgi";
  // the trailing (char)255 is needed so that the lp_solve can
  // know that it's the end of file
  return CGI.post(home, file, sData + (char)255);
}

/**
 * call the CGI program which does Delaunay Triangulation
 */
public static String DelaunayTriangulation(String sData) {
  String home = "www.cs.wustl.edu";
  String file = "/~hma/script/voronoi.cgi?-t"; // switch -t means triangulation
  return CGI.post(home, file, sData);
}

public static void main(String args[]) {
  StringBuffer sBuf = new StringBuffer("max: -x1 + 2 x2;\n" +
    "C1: 2x1 + x2 < 5;\n" +
      " -4 x1 + 4 x2 <5;\n" +
	"int x2,x1;\n");
  char EOF = (char)255;
  sBuf.append(EOF);
  String sData = sBuf.toString();
  System.out.println(sData);
  String rData = CGI.linearProgramming(sData);
  System.out.println(rData);
}

} // end of class CGI

