danielp | b913fa7 | 2013-03-03 06:23:20 +0000 | [diff] [blame^] | 1 | package org.spartanrobotics; |
| 2 | |
| 3 | import java.io.BufferedInputStream; |
| 4 | import java.io.File; |
| 5 | import java.io.FileInputStream; |
| 6 | import java.io.FileNotFoundException; |
| 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
| 9 | |
| 10 | import java.net.InetSocketAddress; |
| 11 | import java.nio.ByteBuffer; |
| 12 | import java.nio.channels.ServerSocketChannel; |
| 13 | import java.nio.channels.SocketChannel; |
| 14 | |
| 15 | import java.util.Arrays; |
| 16 | import java.util.logging.Level; |
| 17 | import java.util.logging.Logger; |
| 18 | |
| 19 | import com.googlecode.javacv.OpenCVFrameGrabber; |
| 20 | import com.googlecode.javacv.cpp.opencv_core.*; |
| 21 | import static com.googlecode.javacv.cpp.opencv_highgui.*; |
| 22 | |
| 23 | public class DebugServerRun { |
| 24 | |
| 25 | private final static Logger LOG = Logger.getLogger( |
| 26 | DebugServerRun.class.getName()); |
| 27 | |
| 28 | final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(-1); |
| 29 | |
| 30 | private ServerSocketChannel sock; |
| 31 | private SocketChannel client; |
| 32 | |
| 33 | /** Constructs a formatted boundary header from a timestamp and content length. */ |
| 34 | private ByteBuffer CreateTransmission(long content_length, double timestamp) { |
| 35 | StringBuilder ret = new StringBuilder(); |
| 36 | ret.append("\r\n--boundarydonotcross\r\n"); |
| 37 | ret.append("Content-Type: image/jpeg\r\n"); |
| 38 | ret.append("Content-Length: "); |
| 39 | ret.append(content_length); |
| 40 | ret.append("\r\n"); |
| 41 | ret.append("X-Timestamp: "); |
| 42 | ret.append(timestamp); |
| 43 | ret.append("\r\n\r\n"); |
| 44 | return ByteBuffer.wrap(ret.toString().getBytes()); |
| 45 | } |
| 46 | |
| 47 | /** Loop that pushes a data stream to the client. */ |
| 48 | private void push() { |
| 49 | try { |
| 50 | grabber.start(); |
| 51 | } |
| 52 | catch (Exception e) { |
| 53 | LOG.severe("Could not start frame grabber."); |
| 54 | return; |
| 55 | } |
| 56 | IplImage img; |
| 57 | long content_size; |
| 58 | File buff_file; |
| 59 | InputStream input; |
| 60 | double timestamp; |
| 61 | while (true) { |
| 62 | //get some image data |
| 63 | try { |
| 64 | img = grabber.grab(); |
| 65 | timestamp = System.currentTimeMillis(); |
| 66 | /*We buffer through /dev/shm, just to make the conversion process easier. |
| 67 | * I know this is really ugly, but it works a lot better than what |
| 68 | * I was doing before, which segfaulted. |
| 69 | */ |
| 70 | cvSaveImage("/dev/shm/DebugServerBuffer.jpg", img); |
| 71 | buff_file = new File("/dev/shm/DebugServerBuffer.jpg"); |
| 72 | content_size = buff_file.length(); |
| 73 | int totalBytesRead = 0; |
| 74 | input = new BufferedInputStream(new FileInputStream(buff_file)); |
| 75 | byte[] result = new byte[(int)content_size]; |
| 76 | while(totalBytesRead < result.length){ |
| 77 | int bytesRemaining = result.length - totalBytesRead; |
| 78 | //input.read() returns -1, 0, or more : |
| 79 | int bytesRead = input.read(result, totalBytesRead, bytesRemaining); |
| 80 | if (bytesRead > 0){ |
| 81 | totalBytesRead = totalBytesRead + bytesRead; |
| 82 | } |
| 83 | } |
| 84 | ByteBuffer header = CreateTransmission(content_size, timestamp); |
| 85 | ByteBuffer bbuf = ByteBuffer.wrap(result); |
| 86 | ByteBuffer to_send = ByteBuffer.allocate(header.capacity() + bbuf.capacity()); |
| 87 | to_send.put(header); |
| 88 | to_send.put(bbuf); |
| 89 | to_send.rewind(); |
| 90 | SocketCommon.sendAll(client, to_send); |
| 91 | } |
| 92 | catch (Exception e) { |
| 93 | LOG.warning("Could not grab frame."); |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** Constructor to start the server and bind it to a port. */ |
| 100 | public DebugServerRun(final int port) throws IOException { |
| 101 | sock = ServerSocketChannel.open(); |
| 102 | sock.socket().bind(new InetSocketAddress(9714)); |
| 103 | client = sock.accept(); |
| 104 | client.configureBlocking(false); |
| 105 | //we are now connected to our client. Wait for them to send us a header. |
| 106 | LOG.info("Reading headers..."); |
| 107 | SocketCommon.readtoBoundary(client, "\r\n\r\n"); |
| 108 | //send one back |
| 109 | LOG.info("Writing headers..."); |
| 110 | SocketCommon.sendAll(client, "donotcross\r\n"); |
| 111 | } |
| 112 | |
| 113 | /** Runs the server, and concurrently starts the vision processor with -vision flag. */ |
| 114 | public static void main(final String args[]) throws IOException { |
| 115 | //main function for server |
| 116 | |
| 117 | String atomIP = null; |
| 118 | try { |
| 119 | atomIP = args[0]; |
| 120 | } |
| 121 | catch (ArrayIndexOutOfBoundsException e) { |
| 122 | System.out.println("Usage: DebugServerRun [atom IP address]"); |
| 123 | System.exit(0); |
| 124 | } |
| 125 | |
| 126 | //set logger to log everything |
| 127 | LOG.setLevel(Level.ALL); |
| 128 | try { |
| 129 | LogHandler handler = new LogHandler("ds_vision.log"); |
| 130 | TimeFormatter formatter = new TimeFormatter(); |
| 131 | handler.setFormatter(formatter); |
| 132 | LOG.addHandler(handler); |
| 133 | } |
| 134 | catch (FileNotFoundException e) { |
| 135 | System.err.println("Warning: Logging initialization failed."); |
| 136 | } |
| 137 | |
| 138 | if (Arrays.asList(args).contains("-vision")) { |
| 139 | LOG.info("Starting vision processor."); |
| 140 | new TestClient(atomIP); |
| 141 | } |
| 142 | |
| 143 | DebugServerRun server = new DebugServerRun(9714); |
| 144 | server.push(); |
| 145 | } |
| 146 | } |