blob: 96308a1cd75ce4aef417cd2b411a49589018fbcb [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
danielp3c598e52013-02-24 06:12:54 +000027 private String atomIP;
danielp6eb01d12013-02-20 05:36:09 +000028
danielp3c598e52013-02-24 06:12:54 +000029 private SocketChannel sock;
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 */
danielp3c598e52013-02-24 06:12:54 +000044 public HTTPClient(String atomIP) throws IOException {
danielp64c4e052013-02-23 07:21:41 +000045 //Initialize socket connection to robot
danielp3c598e52013-02-24 06:12:54 +000046 this.atomIP = atomIP;
danielp64c4e052013-02-23 07:21:41 +000047 sock = SocketChannel.open();
danielp3c598e52013-02-24 06:12:54 +000048 WriteDebug("Connecting to server at " + atomIP);
49 sock.connect(new InetSocketAddress(atomIP, 9714));
danielp64c4e052013-02-23 07:21:41 +000050 sock.configureBlocking(false);
51 //Write headers
52 //HTTPStreamer does not actually use the headers, so we can just write terminating chars.
53 WriteDebug("Writing headers...");
54 SocketCommon.sendAll(sock, "\r\n\r\n");
55 //Receive headers
56 WriteDebug("Reading headers...");
57 SocketCommon.readtoBoundary(sock, "donotcross\r\n");
58 WriteDebug("Now receiving data.");
59 cgetter = new ChannelImageGetter(sock);
danielp502ec002013-02-19 23:54:14 +000060 }
danielpb3d24ee2013-02-22 19:47:11 +000061
danielp64c4e052013-02-23 07:21:41 +000062 /** Grabs the most current frame from the HTTPStreamer stream.
63 * Returns a class instance with image and timestamp attributes. */
danielp502ec002013-02-19 23:54:14 +000064 public ImageWithTimestamp GetFrame() {
danielp6eb01d12013-02-20 05:36:09 +000065 ImageWithTimestamp final_image = new ImageWithTimestamp();
danielp64c4e052013-02-23 07:21:41 +000066 //Use Brian's code to extract an image and timestamp from raw server data.
danielp3c598e52013-02-24 06:12:54 +000067 ByteBuffer binaryImage = cgetter.getJPEG();
68 if (binaryImage == null) {
69 Messages.severe("Could not parse data from robot. See the log for details.");
70 return null;
71 }
danielp54e997e2013-02-21 01:54:23 +000072 //Decode ByteBuffer into an IplImage
danielp3c598e52013-02-24 06:12:54 +000073 byte[] b = new byte[binaryImage.remaining()];
74 binaryImage.get(b);
danielp6eb01d12013-02-20 05:36:09 +000075 try {
danielpb3d24ee2013-02-22 19:47:11 +000076 InputStream iis = new ByteArrayInputStream(b);
77 BufferedImage bImageFromConvert = ImageIO.read(iis);
danielp54e997e2013-02-21 01:54:23 +000078 final_image.image = new WPIColorImage(bImageFromConvert);
79 final_image.timestamp = cgetter.getTimestamp();
80 WriteDebug("Image processing successful.");
81 return final_image;
danielp6eb01d12013-02-20 05:36:09 +000082 }
83 catch (IOException e) {
danielp64c4e052013-02-23 07:21:41 +000084 LOG.warning("Image processing failed: " + e.getMessage());
danielp6eb01d12013-02-20 05:36:09 +000085 return null;
86 }
danielp502ec002013-02-19 23:54:14 +000087 }
danielp3c598e52013-02-24 06:12:54 +000088
89 /** Gets the name to display at the top of the image window. */
90 public String GetName() {
91 return atomIP;
92 }
danielp502ec002013-02-19 23:54:14 +000093}