blob: 4e425538f213a2ab16781245d5e17ed44b0a7d6b [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;
9import javax.imageio.ImageIO;
10
danielp6eb01d12013-02-20 05:36:09 +000011import aos.ChannelImageGetter;
danielp6eb01d12013-02-20 05:36:09 +000012
13import java.nio.channels.SocketChannel;
14import java.nio.ByteBuffer;
15
danielp4a35a7a2013-02-20 20:45:39 +000016import edu.wpi.first.wpijavacv.WPIColorImage;
17
danielp502ec002013-02-19 23:54:14 +000018public class HTTPClient {
19 //Connects to HTTP Server on robot and receives images
20
21 private final static boolean LOCAL_DEBUG = true;
22
danielp4a35a7a2013-02-20 20:45:39 +000023 private SocketChannel sock;
24 private Socket core_sock;
danielp502ec002013-02-19 23:54:14 +000025
danielp502ec002013-02-19 23:54:14 +000026
danielp6eb01d12013-02-20 05:36:09 +000027 private BufferedReader sock_in;
28 private PrintWriter sock_out;
29
danielp4a35a7a2013-02-20 20:45:39 +000030 private final String ATOM_IP = "10.9.71.6";
31
danielp502ec002013-02-19 23:54:14 +000032 private void WriteDebug(String message) {
33 //small helper function to write debug messages
34 if (LOCAL_DEBUG)
35 System.out.println(message);
36 }
37 private String ReadtoBoundary(String boundary) {
38 //reads from socket until it encounters a specific character combination
39 //if boundary is null, it reads until it runs out of data
40 StringBuilder recvd = new StringBuilder();
41 String message = "";
42 try {
danielp6eb01d12013-02-20 05:36:09 +000043 core_sock.setSoTimeout(10000);
danielp502ec002013-02-19 23:54:14 +000044 }
45 catch (SocketException e) {
46 System.err.println("Warning: Could not set socket timeout.");
47 }
48 try {
49 int ret;
50 while ((ret = sock_in.read()) != -1) {
51 if (ret == 0) {
52 //finished receiving
53 message += recvd.toString();
54 recvd.setLength(0);
55 if (boundary == null)
56 break;
57 }
58 else {
59 recvd.append((char)ret);
60 if (boundary != null) {
61 if (message.contains(boundary))
62 break;
63 else
64 continue;
65 }
66 }
67 }
68 }
69 catch (InterruptedIOException e) {
70 System.err.println("Warning: Image receive timed out.");
71 return null;
72 }
73 catch (IOException e) {
74 System.err.println("Error: Socket read failed.");
75 return null;
76 }
77 return message;
78 }
79 public HTTPClient() {
80 //Initialize socket connection to robot
81 try {
danielp4a35a7a2013-02-20 20:45:39 +000082 sock = SocketChannel.open();
83 core_sock = sock.socket();
danielp502ec002013-02-19 23:54:14 +000084 WriteDebug("Connecting to server...");
danielp4a35a7a2013-02-20 20:45:39 +000085 sock.connect(new InetSocketAddress(ATOM_IP, 9714));
danielp6eb01d12013-02-20 05:36:09 +000086 sock_in = new BufferedReader(new InputStreamReader(core_sock.getInputStream()));
87 sock_out = new PrintWriter(core_sock.getOutputStream(), true);
danielp502ec002013-02-19 23:54:14 +000088 //Write headers
89 //HTTPStreamer does not actually use the headers, so we can just write terminating chars.
90 WriteDebug("Writing headers...");
91 sock_out.println("\r\n\r\n");
92 //Receive headers
93 WriteDebug("Reading headers...");
danielp6eb01d12013-02-20 05:36:09 +000094 ReadtoBoundary("donotcross\r\n");
danielp502ec002013-02-19 23:54:14 +000095 WriteDebug("Now receiving data.");
96 }
97 catch (UnknownHostException e) {
98 System.err.println("Error: Invalid host.");
99 System.exit(1);
100 }
101 catch (IOException e) {
102 System.err.println("Error: Socket IO failed.");
103 System.exit(2);
104 }
105
106 }
107 public ImageWithTimestamp GetFrame() {
danielp6eb01d12013-02-20 05:36:09 +0000108 //Use Brian's code to extract an image and timestamp from raw server data.
109 ImageWithTimestamp final_image = new ImageWithTimestamp();
110 try {
111 ChannelImageGetter cgetter = new ChannelImageGetter(sock);
112 ByteBuffer binary_image = cgetter.getJPEG();
danielp27139f62013-02-20 06:15:39 +0000113 //Decode ByteBuffer into an IplImage
114 InputStream in = new ByteArrayInputStream(binary_image.array());
115 try {
116 BufferedImage bImageFromConvert = ImageIO.read(in);
danielp4a35a7a2013-02-20 20:45:39 +0000117 final_image.image = new WPIColorImage(bImageFromConvert);
danielp27139f62013-02-20 06:15:39 +0000118 final_image.timestamp = cgetter.getTimestamp();
119 WriteDebug("Image processing successful.");
120 return final_image;
121 }
122 catch (IOException e) {
123 System.err.println(e.getMessage());
danielp6eb01d12013-02-20 05:36:09 +0000124 return null;
125 }
danielp27139f62013-02-20 06:15:39 +0000126
danielp6eb01d12013-02-20 05:36:09 +0000127 }
128 catch (IOException e) {
129 WriteDebug("Error: Failed to initialize ChannelImageGetter.");
130 return null;
131 }
danielp502ec002013-02-19 23:54:14 +0000132 }
133}