-Added a class to serve up results to atom. A matching client C++ class should follow shortly.
-Generally beautified code
git-svn-id: https://robotics.mvla.net/svn/frc971/2013/trunk/src@4148 f308d9b7-e957-4cde-b6ac-9a88185e7312
diff --git a/971CV/src/org/frc971/DebugServerRun.java b/971CV/src/org/frc971/DebugServerRun.java
index c8146b1..0f60665 100644
--- a/971CV/src/org/frc971/DebugServerRun.java
+++ b/971CV/src/org/frc971/DebugServerRun.java
@@ -1,27 +1,14 @@
package org.frc971;
import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.InterruptedIOException;
-import java.io.PrintWriter;
import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketException;
import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
@@ -40,51 +27,8 @@
private ServerSocketChannel sock;
private SocketChannel client;
-
- private BufferedReader sock_in;
- private PrintWriter sock_out;
-
- private String ReadtoBoundary(String boundary) {
- //reads from socket until it encounters a specific character combination
- //if boundary is null, it reads until it runs out of data
- ByteBuffer recvd = ByteBuffer.allocate(1024);
- StringBuilder sb = new StringBuilder();
- String message = "";
- try {
- int ret = 0;
- while (ret != -1) {
- ret = client.read(recvd);
- //System.out.println(ret);
- if (ret == 0) {
- //finished receiving
- message = sb.toString();
- if (boundary == null)
- break;
- }
- else {
- for (int i = 0; i < recvd.capacity() - recvd.remaining(); i++) {
- sb.append((char)recvd.get(i));
- }
- recvd.clear();
- if (boundary != null) {
- if (sb.toString().contains(boundary)) {
- message = sb.toString();
- break;
- }
- else {
- continue;
- }
- }
- }
- }
- }
- catch (IOException e) {
- LOG.severe("Socket read failed.");
- return null;
- }
- return message;
- }
+ /** Constructs a formatted boundary header from a timestamp and content length. */
private ByteBuffer CreateTransmission(long content_length, double timestamp) {
StringBuilder ret = new StringBuilder();
ret.append("\r\n--boundarydonotcross\r\n");
@@ -97,8 +41,9 @@
ret.append("\r\n\r\n");
return ByteBuffer.wrap(ret.toString().getBytes());
}
+
+ /** Loop that pushes a data stream to the client. */
private void push() {
- //push data to client
try {
grabber.start();
}
@@ -116,6 +61,10 @@
try {
img = grabber.grab();
timestamp = System.currentTimeMillis();
+ /*We buffer through /dev/shm, just to make the conversion process easier.
+ * I know this is really ugly, but it works a lot better than what
+ * I was doing before, which segfaulted.
+ */
cvSaveImage("/dev/shm/DebugServerBuffer.jpg", img);
buff_file = new File("/dev/shm/DebugServerBuffer.jpg");
content_size = buff_file.length();
@@ -136,9 +85,7 @@
to_send.put(header);
to_send.put(bbuf);
to_send.rewind();
- while (to_send.remaining() > 0) {
- client.write(to_send);
- }
+ SocketCommon.sendAll(client, to_send);
}
catch (Exception e) {
LOG.warning("Could not grab frame.");
@@ -146,35 +93,26 @@
}
}
}
-
- public void Connect() throws IOException {
- client = sock.accept();
- client.configureBlocking(false);
- //sock_in = new BufferedReader(new InputStreamReader(client.socket().getInputStream()));
- //sock_out = new PrintWriter(client.socket().getOutputStream(), true);
- //we are now connected to our client. Wait for them to send us a header.
- LOG.info("Reading headers...");
- ReadtoBoundary("\r\n\r\n");
- //send one back
- LOG.info("Writing headers...");
- String ending = "donotcross\r\n";
- ByteBuffer buff = ByteBuffer.wrap(ending.getBytes());
- while (buff.remaining() > 0) {
- client.write(buff);
- }
- }
-
+ /** Constructor to start the server and bind it to a port. */
public DebugServerRun(final int port) throws IOException {
sock = ServerSocketChannel.open();
sock.socket().bind(new InetSocketAddress(9714));
+ client = sock.accept();
+ client.configureBlocking(false);
+ //we are now connected to our client. Wait for them to send us a header.
+ LOG.info("Reading headers...");
+ SocketCommon.readtoBoundary(client, "\r\n\r\n");
+ //send one back
+ LOG.info("Writing headers...");
+ SocketCommon.sendAll(client, "donotcross\r\n");
}
- public static void main(final String args[]) throws IOException{
+ /** Runs the server, and concurrently starts the vision processor with -vision flag. */
+ public static void main(final String args[]) throws IOException {
//main function for server
-
//set logger to log everything
LOG.setLevel(Level.ALL);
try {
- LogHandler handler = new LogHandler("../src/org/frc971/ds_vision.log");
+ LogHandler handler = new LogHandler("ds_vision.log");
TimeFormatter formatter = new TimeFormatter();
handler.setFormatter(formatter);
LOG.addHandler(handler);
@@ -183,9 +121,12 @@
System.err.println("Warning: Logging initialization failed.");
}
+ if (args[0].equals("-vision")) {
+ LOG.info("Starting vision processor.");
+ new TestClient();
+ }
+
DebugServerRun server = new DebugServerRun(9714);
- new TestClient();
- server.Connect();
server.push();
}
}