-Added a class to serve up results to atom. A matching client C++ class should follow shortly.
-Generally beautified code


git-svn-id: https://robotics.mvla.net/svn/frc971/2013/trunk/src@4148 f308d9b7-e957-4cde-b6ac-9a88185e7312
diff --git a/971CV/src/org/frc971/HTTPClient.java b/971CV/src/org/frc971/HTTPClient.java
index 4c7d0af..cc694ef 100644
--- a/971CV/src/org/frc971/HTTPClient.java
+++ b/971CV/src/org/frc971/HTTPClient.java
@@ -2,28 +2,17 @@
 
 //@author: daniel
 
-import static com.googlecode.javacv.cpp.opencv_highgui.cvEncodeImage;
-
 import java.io.*;
 import java.net.*;
 
 import java.awt.image.BufferedImage;
 
-import java.nio.channels.FileChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.ByteBuffer;
 
-import java.util.Iterator;
 import java.util.logging.Logger;
 
 import javax.imageio.ImageIO;
-import javax.imageio.ImageReadParam;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.ImageInputStream;
-
-import com.googlecode.javacv.cpp.opencv_core.CvArr;
-import com.googlecode.javacv.cpp.opencv_core.CvMat;
-import com.googlecode.javacv.cpp.opencv_core.IplImage;
 
 import aos.ChannelImageGetter;
 
@@ -32,14 +21,10 @@
 public class HTTPClient {
 	//Connects to HTTP Server on robot and receives images
 	
-	private final static boolean LOCAL_DEBUG = true;
+	/** whether or not to print debug messages to stdout. */
+	private final static boolean LOCAL_DEBUG = false;
 	
 	private SocketChannel sock;
-	private Socket core_sock;
-	
-	
-	private BufferedReader sock_in;
-	private PrintWriter sock_out;
 	
 	private final String ATOM_IP = "192.168.0.137";
 	
@@ -47,87 +32,37 @@
 	
 	private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
 	
+	/** Small helper method for printing debug messages to stdout. */
 	private void WriteDebug(String message) {
 		//small helper function to write debug messages
 		if (LOCAL_DEBUG)
 			LOG.info("LOCAL_DEBUG: " + message);
 	}
-	private String ReadtoBoundary(String boundary) {
-		//reads from socket until it encounters a specific character combination
-		//if boundary is null, it reads until it runs out of data
-		ByteBuffer recvd = ByteBuffer.allocate(1024);
-		StringBuilder sb = new StringBuilder();
-		String message = "";
-		try {
-			int ret;
-			while ((ret = sock.read(recvd)) != -1) {
-				if (ret == 0) {
-					//finished receiving
-					message = sb.toString();
-					if (boundary == null)
-						break;
-				}
-				else {
-					for (int i = 0; i < recvd.capacity() - recvd.remaining(); i++) {
-						sb.append((char)recvd.get(i));
-					}
-					recvd.clear();
-					if (boundary != null) {
-						if (sb.toString().contains(boundary)) {
-							message = sb.toString();
-							break;
-						}
-						else {
-							continue;
-						}
-					}
-				}
-			}
-		}
-		catch (IOException e) {
-			LOG.severe("Socket read failed.");
-			return null;
-		}
-		return message;
+	
+	/** the constructor, initializes connection, and sets up aos getter. 
+	 * @throws IOException */
+	public HTTPClient() throws IOException {
+		//Initialize socket connection to robot
+		sock = SocketChannel.open();
+		WriteDebug("Connecting to server at " + ATOM_IP);
+		sock.connect(new InetSocketAddress(ATOM_IP, 9714));
+		sock.configureBlocking(false);
+		//Write headers
+		//HTTPStreamer does not actually use the headers, so we can just write terminating chars.
+		WriteDebug("Writing headers...");
+		SocketCommon.sendAll(sock, "\r\n\r\n");
+		//Receive headers
+		WriteDebug("Reading headers...");
+		SocketCommon.readtoBoundary(sock, "donotcross\r\n");
+		WriteDebug("Now receiving data.");
+		cgetter = new ChannelImageGetter(sock);
 	}
 	
-	public HTTPClient() {
-		//Initialize socket connection to robot
-		try {
-			sock = SocketChannel.open();
-			core_sock = sock.socket();
-			WriteDebug("Connecting to server at " + ATOM_IP);
-			sock.connect(new InetSocketAddress(ATOM_IP, 9714));
-			sock.configureBlocking(false);
-			//sock_in = new BufferedReader(new InputStreamReader(core_sock.getInputStream()));
-			//sock_out = new PrintWriter(core_sock.getOutputStream(), true);
-			//Write headers
-			//HTTPStreamer does not actually use the headers, so we can just write terminating chars.
-			WriteDebug("Writing headers...");
-			String ending = "\r\n\r\n";
-			ByteBuffer header = ByteBuffer.wrap(ending.getBytes());
-			while (header.remaining() > 0) {
-				sock.write(header);
-			}
-			//Receive headers
-			WriteDebug("Reading headers...");
-			ReadtoBoundary("donotcross\r\n");
-			WriteDebug("Now receiving data.");
-			cgetter = new ChannelImageGetter(sock);
-		}
-		catch (UnknownHostException e) {
-			LOG.severe("Invalid host.");
-			System.exit(1);
-		}
-		catch (IOException e) {
-			LOG.severe("Socket IO failed: " + e.getMessage());
-			System.exit(2);
-		}
-		
-	}
+	/** Grabs the most current frame from the HTTPStreamer stream.
+	 * Returns a class instance with image and timestamp attributes. */
 	public ImageWithTimestamp GetFrame() {
-		//Use Brian's code to extract an image and timestamp from raw server data.
 		ImageWithTimestamp final_image = new ImageWithTimestamp();
+		//Use Brian's code to extract an image and timestamp from raw server data.
 		ByteBuffer binary_image = cgetter.getJPEG();
 		//Decode ByteBuffer into an IplImage
 		byte[] b = new byte[binary_image.remaining()];
@@ -141,6 +76,7 @@
 			return final_image;
 		}
 		catch (IOException e) {
+			LOG.warning("Image processing failed: " + e.getMessage());
 			return null;
 		}
 	}