danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 1 | package org.frc971; |
| 2 | |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 3 | //@author: daniel |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 4 | |
| 5 | import java.io.*; |
| 6 | import java.net.*; |
| 7 | |
| 8 | import java.awt.image.BufferedImage; |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 9 | |
| 10 | import java.nio.channels.SocketChannel; |
| 11 | import java.nio.ByteBuffer; |
| 12 | |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 13 | import java.util.logging.Logger; |
| 14 | |
| 15 | import javax.imageio.ImageIO; |
| 16 | |
| 17 | import aos.ChannelImageGetter; |
| 18 | |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 19 | import edu.wpi.first.wpijavacv.WPIColorImage; |
| 20 | |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 21 | public class HTTPClient { |
| 22 | //Connects to HTTP Server on robot and receives images |
| 23 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 24 | /** whether or not to print debug messages to stdout. */ |
| 25 | private final static boolean LOCAL_DEBUG = false; |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 26 | |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 27 | private SocketChannel sock; |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 28 | |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame] | 29 | private final String ATOM_IP = "192.168.0.137"; |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 30 | |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 31 | private ChannelImageGetter cgetter; |
| 32 | |
| 33 | private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); |
| 34 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 35 | /** Small helper method for printing debug messages to stdout. */ |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 36 | private void WriteDebug(String message) { |
| 37 | //small helper function to write debug messages |
| 38 | if (LOCAL_DEBUG) |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 39 | LOG.info("LOCAL_DEBUG: " + message); |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 40 | } |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 41 | |
| 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); |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 59 | } |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame] | 60 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 61 | /** Grabs the most current frame from the HTTPStreamer stream. |
| 62 | * Returns a class instance with image and timestamp attributes. */ |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 63 | public ImageWithTimestamp GetFrame() { |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 64 | ImageWithTimestamp final_image = new ImageWithTimestamp(); |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 65 | //Use Brian's code to extract an image and timestamp from raw server data. |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 66 | ByteBuffer binary_image = cgetter.getJPEG(); |
| 67 | //Decode ByteBuffer into an IplImage |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame] | 68 | byte[] b = new byte[binary_image.remaining()]; |
| 69 | binary_image.get(b); |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 70 | try { |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame] | 71 | InputStream iis = new ByteArrayInputStream(b); |
| 72 | BufferedImage bImageFromConvert = ImageIO.read(iis); |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 73 | final_image.image = new WPIColorImage(bImageFromConvert); |
| 74 | final_image.timestamp = cgetter.getTimestamp(); |
| 75 | WriteDebug("Image processing successful."); |
| 76 | return final_image; |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 77 | } |
| 78 | catch (IOException e) { |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame^] | 79 | LOG.warning("Image processing failed: " + e.getMessage()); |
danielp | 6eb01d1 | 2013-02-20 05:36:09 +0000 | [diff] [blame] | 80 | return null; |
| 81 | } |
danielp | 502ec00 | 2013-02-19 23:54:14 +0000 | [diff] [blame] | 82 | } |
| 83 | } |