blob: cc694ef77ceb459d7400ba0aff71dd36c7d26a85 [file] [log] [blame]
danielp502ec002013-02-19 23:54:14 +00001package org.frc971;
2
danielp4a35a7a2013-02-20 20:45:39 +00003//@author: daniel
danielp502ec002013-02-19 23:54:14 +00004
5import java.io.*;
6import java.net.*;
7
8import java.awt.image.BufferedImage;
danielp6eb01d12013-02-20 05:36:09 +00009
10import java.nio.channels.SocketChannel;
11import java.nio.ByteBuffer;
12
danielp54e997e2013-02-21 01:54:23 +000013import java.util.logging.Logger;
14
15import javax.imageio.ImageIO;
16
17import aos.ChannelImageGetter;
18
danielp4a35a7a2013-02-20 20:45:39 +000019import edu.wpi.first.wpijavacv.WPIColorImage;
20
danielp502ec002013-02-19 23:54:14 +000021public class HTTPClient {
22 //Connects to HTTP Server on robot and receives images
23
danielp64c4e052013-02-23 07:21:41 +000024 /** whether or not to print debug messages to stdout. */
25 private final static boolean LOCAL_DEBUG = false;
danielp502ec002013-02-19 23:54:14 +000026
danielp4a35a7a2013-02-20 20:45:39 +000027 private SocketChannel sock;
danielp6eb01d12013-02-20 05:36:09 +000028
danielpb3d24ee2013-02-22 19:47:11 +000029 private final String ATOM_IP = "192.168.0.137";
danielp4a35a7a2013-02-20 20:45:39 +000030
danielp54e997e2013-02-21 01:54:23 +000031 private ChannelImageGetter cgetter;
32
33 private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
34
danielp64c4e052013-02-23 07:21:41 +000035 /** Small helper method for printing debug messages to stdout. */
danielp502ec002013-02-19 23:54:14 +000036 private void WriteDebug(String message) {
37 //small helper function to write debug messages
38 if (LOCAL_DEBUG)
danielp54e997e2013-02-21 01:54:23 +000039 LOG.info("LOCAL_DEBUG: " + message);
danielp502ec002013-02-19 23:54:14 +000040 }
danielp64c4e052013-02-23 07:21:41 +000041
42 /** the constructor, initializes connection, and sets up aos getter.
43 * @throws IOException */
44 public HTTPClient() throws IOException {
45 //Initialize socket connection to robot
46 sock = SocketChannel.open();
47 WriteDebug("Connecting to server at " + ATOM_IP);
48 sock.connect(new InetSocketAddress(ATOM_IP, 9714));
49 sock.configureBlocking(false);
50 //Write headers
51 //HTTPStreamer does not actually use the headers, so we can just write terminating chars.
52 WriteDebug("Writing headers...");
53 SocketCommon.sendAll(sock, "\r\n\r\n");
54 //Receive headers
55 WriteDebug("Reading headers...");
56 SocketCommon.readtoBoundary(sock, "donotcross\r\n");
57 WriteDebug("Now receiving data.");
58 cgetter = new ChannelImageGetter(sock);
danielp502ec002013-02-19 23:54:14 +000059 }
danielpb3d24ee2013-02-22 19:47:11 +000060
danielp64c4e052013-02-23 07:21:41 +000061 /** Grabs the most current frame from the HTTPStreamer stream.
62 * Returns a class instance with image and timestamp attributes. */
danielp502ec002013-02-19 23:54:14 +000063 public ImageWithTimestamp GetFrame() {
danielp6eb01d12013-02-20 05:36:09 +000064 ImageWithTimestamp final_image = new ImageWithTimestamp();
danielp64c4e052013-02-23 07:21:41 +000065 //Use Brian's code to extract an image and timestamp from raw server data.
danielp54e997e2013-02-21 01:54:23 +000066 ByteBuffer binary_image = cgetter.getJPEG();
67 //Decode ByteBuffer into an IplImage
danielpb3d24ee2013-02-22 19:47:11 +000068 byte[] b = new byte[binary_image.remaining()];
69 binary_image.get(b);
danielp6eb01d12013-02-20 05:36:09 +000070 try {
danielpb3d24ee2013-02-22 19:47:11 +000071 InputStream iis = new ByteArrayInputStream(b);
72 BufferedImage bImageFromConvert = ImageIO.read(iis);
danielp54e997e2013-02-21 01:54:23 +000073 final_image.image = new WPIColorImage(bImageFromConvert);
74 final_image.timestamp = cgetter.getTimestamp();
75 WriteDebug("Image processing successful.");
76 return final_image;
danielp6eb01d12013-02-20 05:36:09 +000077 }
78 catch (IOException e) {
danielp64c4e052013-02-23 07:21:41 +000079 LOG.warning("Image processing failed: " + e.getMessage());
danielp6eb01d12013-02-20 05:36:09 +000080 return null;
81 }
danielp502ec002013-02-19 23:54:14 +000082 }
83}