danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 1 | package org.frc971; |
| 2 | |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 3 | import java.io.BufferedInputStream; |
| 4 | import java.io.BufferedOutputStream; |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.BufferedWriter; |
| 7 | import java.io.File; |
| 8 | import java.io.FileInputStream; |
| 9 | import java.io.FileNotFoundException; |
| 10 | import java.io.FileOutputStream; |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 11 | import java.io.IOException; |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 12 | import java.io.InputStream; |
| 13 | import java.io.InputStreamReader; |
| 14 | import java.io.InterruptedIOException; |
| 15 | import java.io.PrintWriter; |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 16 | |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 17 | import java.net.InetSocketAddress; |
| 18 | import java.net.ServerSocket; |
| 19 | import java.net.Socket; |
| 20 | import java.net.SocketException; |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.nio.CharBuffer; |
| 23 | import java.nio.channels.FileChannel; |
| 24 | import java.nio.channels.GatheringByteChannel; |
| 25 | import java.nio.channels.ServerSocketChannel; |
| 26 | import java.nio.channels.SocketChannel; |
| 27 | |
| 28 | import java.util.logging.Level; |
| 29 | import java.util.logging.Logger; |
| 30 | |
| 31 | import com.googlecode.javacv.OpenCVFrameGrabber; |
| 32 | import com.googlecode.javacv.cpp.opencv_core.*; |
| 33 | import static com.googlecode.javacv.cpp.opencv_highgui.*; |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 34 | |
| 35 | public class DebugServerRun { |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 36 | |
| 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 |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 102 | try { |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 103 | grabber.start(); |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 104 | } |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 105 | catch (Exception e) { |
| 106 | LOG.severe("Could not start frame grabber."); |
| 107 | return; |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 108 | } |
danielp | b3d24ee | 2013-02-22 19:47:11 +0000 | [diff] [blame^] | 109 | 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(); |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 190 | } |
| 191 | } |