blob: c8146b1d846e1b67ece3dd6d49519c17148d34af [file] [log] [blame]
danielp54e997e2013-02-21 01:54:23 +00001package org.frc971;
2
danielpb3d24ee2013-02-22 19:47:11 +00003import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream;
5import java.io.BufferedReader;
6import java.io.BufferedWriter;
7import java.io.File;
8import java.io.FileInputStream;
9import java.io.FileNotFoundException;
10import java.io.FileOutputStream;
danielp54e997e2013-02-21 01:54:23 +000011import java.io.IOException;
danielpb3d24ee2013-02-22 19:47:11 +000012import java.io.InputStream;
13import java.io.InputStreamReader;
14import java.io.InterruptedIOException;
15import java.io.PrintWriter;
danielp54e997e2013-02-21 01:54:23 +000016
danielpb3d24ee2013-02-22 19:47:11 +000017import java.net.InetSocketAddress;
18import java.net.ServerSocket;
19import java.net.Socket;
20import java.net.SocketException;
21import java.nio.ByteBuffer;
22import java.nio.CharBuffer;
23import java.nio.channels.FileChannel;
24import java.nio.channels.GatheringByteChannel;
25import java.nio.channels.ServerSocketChannel;
26import java.nio.channels.SocketChannel;
27
28import java.util.logging.Level;
29import java.util.logging.Logger;
30
31import com.googlecode.javacv.OpenCVFrameGrabber;
32import com.googlecode.javacv.cpp.opencv_core.*;
33import static com.googlecode.javacv.cpp.opencv_highgui.*;
danielp54e997e2013-02-21 01:54:23 +000034
35public class DebugServerRun {
danielpb3d24ee2013-02-22 19:47:11 +000036
37 private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
38
39 final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(-1);
40
41 private ServerSocketChannel sock;
42 private SocketChannel client;
43
44 private BufferedReader sock_in;
45 private PrintWriter sock_out;
46
47 private String ReadtoBoundary(String boundary) {
48 //reads from socket until it encounters a specific character combination
49 //if boundary is null, it reads until it runs out of data
50 ByteBuffer recvd = ByteBuffer.allocate(1024);
51 StringBuilder sb = new StringBuilder();
52 String message = "";
53 try {
54 int ret = 0;
55 while (ret != -1) {
56 ret = client.read(recvd);
57 //System.out.println(ret);
58 if (ret == 0) {
59 //finished receiving
60 message = sb.toString();
61 if (boundary == null)
62 break;
63 }
64 else {
65 for (int i = 0; i < recvd.capacity() - recvd.remaining(); i++) {
66 sb.append((char)recvd.get(i));
67 }
68 recvd.clear();
69 if (boundary != null) {
70 if (sb.toString().contains(boundary)) {
71 message = sb.toString();
72 break;
73 }
74 else {
75 continue;
76 }
77 }
78 }
79 }
80 }
81 catch (IOException e) {
82 LOG.severe("Socket read failed.");
83 return null;
84 }
85 return message;
86 }
87
88 private ByteBuffer CreateTransmission(long content_length, double timestamp) {
89 StringBuilder ret = new StringBuilder();
90 ret.append("\r\n--boundarydonotcross\r\n");
91 ret.append("Content-Type: image/jpeg\r\n");
92 ret.append("Content-Length: ");
93 ret.append(content_length);
94 ret.append("\r\n");
95 ret.append("X-Timestamp: ");
96 ret.append(timestamp);
97 ret.append("\r\n\r\n");
98 return ByteBuffer.wrap(ret.toString().getBytes());
99 }
100 private void push() {
101 //push data to client
danielp54e997e2013-02-21 01:54:23 +0000102 try {
danielpb3d24ee2013-02-22 19:47:11 +0000103 grabber.start();
danielp54e997e2013-02-21 01:54:23 +0000104 }
danielpb3d24ee2013-02-22 19:47:11 +0000105 catch (Exception e) {
106 LOG.severe("Could not start frame grabber.");
107 return;
danielp54e997e2013-02-21 01:54:23 +0000108 }
danielpb3d24ee2013-02-22 19:47:11 +0000109 IplImage img;
110 long content_size;
111 File buff_file;
112 InputStream input;
113 double timestamp;
114 while (true) {
115 //get some image data
116 try {
117 img = grabber.grab();
118 timestamp = System.currentTimeMillis();
119 cvSaveImage("/dev/shm/DebugServerBuffer.jpg", img);
120 buff_file = new File("/dev/shm/DebugServerBuffer.jpg");
121 content_size = buff_file.length();
122 int totalBytesRead = 0;
123 input = new BufferedInputStream(new FileInputStream(buff_file));
124 byte[] result = new byte[(int)content_size];
125 while(totalBytesRead < result.length){
126 int bytesRemaining = result.length - totalBytesRead;
127 //input.read() returns -1, 0, or more :
128 int bytesRead = input.read(result, totalBytesRead, bytesRemaining);
129 if (bytesRead > 0){
130 totalBytesRead = totalBytesRead + bytesRead;
131 }
132 }
133 ByteBuffer header = CreateTransmission(content_size, timestamp);
134 ByteBuffer bbuf = ByteBuffer.wrap(result);
135 ByteBuffer to_send = ByteBuffer.allocate(header.capacity() + bbuf.capacity());
136 to_send.put(header);
137 to_send.put(bbuf);
138 to_send.rewind();
139 while (to_send.remaining() > 0) {
140 client.write(to_send);
141 }
142 }
143 catch (Exception e) {
144 LOG.warning("Could not grab frame.");
145 continue;
146 }
147 }
148 }
149
150 public void Connect() throws IOException {
151 client = sock.accept();
152 client.configureBlocking(false);
153 //sock_in = new BufferedReader(new InputStreamReader(client.socket().getInputStream()));
154 //sock_out = new PrintWriter(client.socket().getOutputStream(), true);
155 //we are now connected to our client. Wait for them to send us a header.
156 LOG.info("Reading headers...");
157 ReadtoBoundary("\r\n\r\n");
158 //send one back
159 LOG.info("Writing headers...");
160 String ending = "donotcross\r\n";
161 ByteBuffer buff = ByteBuffer.wrap(ending.getBytes());
162 while (buff.remaining() > 0) {
163 client.write(buff);
164 }
165 }
166
167 public DebugServerRun(final int port) throws IOException {
168 sock = ServerSocketChannel.open();
169 sock.socket().bind(new InetSocketAddress(9714));
170 }
171 public static void main(final String args[]) throws IOException{
172 //main function for server
173
174 //set logger to log everything
175 LOG.setLevel(Level.ALL);
176 try {
177 LogHandler handler = new LogHandler("../src/org/frc971/ds_vision.log");
178 TimeFormatter formatter = new TimeFormatter();
179 handler.setFormatter(formatter);
180 LOG.addHandler(handler);
181 }
182 catch (FileNotFoundException e) {
183 System.err.println("Warning: Logging initialization failed.");
184 }
185
186 DebugServerRun server = new DebugServerRun(9714);
187 new TestClient();
188 server.Connect();
189 server.push();
danielp54e997e2013-02-21 01:54:23 +0000190 }
191}