blob: 460b9b48960a555354193f18f637b513867d0a43 [file] [log] [blame]
danielpb913fa72013-03-03 06:23:20 +00001package org.spartanrobotics;
2
3//@author: daniel
4
5import java.io.*;
6import java.net.*;
7
8import java.awt.image.BufferedImage;
9
10import javax.imageio.ImageIO;
11
12import java.nio.channels.SocketChannel;
13import java.nio.ByteBuffer;
14
15import java.util.logging.Logger;
16
17import aos.ChannelImageGetter;
18
19import edu.wpi.first.wpijavacv.WPIColorImage;
20
21public class HTTPClient implements ImageGetter {
22 //Connects to HTTP Server on robot and receives images
23
24 /** whether or not to print debug messages to stdout. */
25 private final static boolean LOCAL_DEBUG = false;
26
27 private String atomIP;
28
29 private SocketChannel sock;
30
31 private ChannelImageGetter cgetter;
32
33 private final static Logger LOG = Logger.getLogger(
34 HTTPClient.class.getName());
35
36 /** Small helper method for printing debug messages to stdout. */
37 private void WriteDebug(String message) {
38 //small helper function to write debug messages
39 if (LOCAL_DEBUG)
40 LOG.info("LOCAL_DEBUG: " + message);
41 }
42
43 /** the constructor, initializes connection, and sets up aos getter.
44 * @throws IOException */
45 public HTTPClient(String atomIP) throws IOException {
46 //Initialize socket connection to robot
47 this.atomIP = atomIP;
48 sock = SocketChannel.open();
49 WriteDebug("Connecting to server at " + atomIP);
50 sock.connect(new InetSocketAddress(atomIP, 9714));
51 sock.configureBlocking(false);
52 //Write headers
53 //HTTPStreamer does not actually use the headers, so we can just write terminating chars.
54 WriteDebug("Writing headers...");
55 SocketCommon.sendAll(sock, "\r\n\r\n");
56 //Receive headers
57 WriteDebug("Reading headers...");
58 SocketCommon.readtoBoundary(sock, "donotcross\r\n");
59 WriteDebug("Now receiving data.");
60 cgetter = new ChannelImageGetter(sock);
61 }
62
63 /** Grabs the most current frame from the HTTPStreamer stream.
64 * Returns a class instance with image and timestamp attributes. */
65 public WPIColorImage getFrame() {
66 //Use Brian's code to extract an image and timestamp from raw server data.
67 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 }
72 //Decode ByteBuffer into an IplImage
73 byte[] b = new byte[binaryImage.remaining()];
74 binaryImage.get(b);
75 try {
76 InputStream iis = new ByteArrayInputStream(b);
77 BufferedImage bImageFromConvert = ImageIO.read(iis);
78 WriteDebug("Image processing successful.");
79 return new WPIColorImage(bImageFromConvert);
80 }
81 catch (IOException e) {
82 LOG.warning("Image processing failed: " + e.getMessage());
83 return null;
84 }
85 }
86
87 /** Gets the name to display at the top of the image window. */
88 public String getName() {
89 return atomIP;
90 }
91
92 /** Gets the current image's timestamp. */
93 public double getTimestamp() {
94 return cgetter.getTimestamp();
95 }
96}