danielp | b913fa7 | 2013-03-03 06:23:20 +0000 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.spartanrobotics; |
| 5 | |
| 6 | import java.io.IOException; |
| 7 | |
| 8 | import java.nio.ByteBuffer; |
| 9 | import java.nio.channels.SocketChannel; |
| 10 | import java.util.logging.Logger; |
| 11 | |
| 12 | /** |
| 13 | * @author daniel |
| 14 | * Socket operations used by other classes |
| 15 | */ |
| 16 | public class SocketCommon { |
| 17 | |
| 18 | private final static Logger LOG = Logger.getLogger( |
| 19 | SocketCommon.class.getName()); |
| 20 | |
| 21 | /** Reads on a SocketStream until it finds a given character sequence. */ |
| 22 | public static String readtoBoundary(SocketChannel sock, String boundary) { |
| 23 | //reads from socket until it encounters a specific character combination |
| 24 | //if boundary is null, it reads until it runs out of data |
| 25 | ByteBuffer recvd = ByteBuffer.allocate(1024); |
| 26 | StringBuilder sb = new StringBuilder(); |
| 27 | String message = ""; |
| 28 | try { |
| 29 | int ret = 0; |
| 30 | while (ret != -1) { |
| 31 | ret = sock.read(recvd); |
| 32 | //System.out.println(ret); |
| 33 | if (ret == 0) { |
| 34 | //finished receiving |
| 35 | message = sb.toString(); |
| 36 | if (boundary == null) |
| 37 | break; |
| 38 | } |
| 39 | else { |
| 40 | for (int i = 0; i < recvd.capacity() - recvd.remaining(); i++) { |
| 41 | sb.append((char)recvd.get(i)); |
| 42 | } |
| 43 | recvd.clear(); |
| 44 | if (boundary != null) { |
| 45 | if (sb.toString().contains(boundary)) { |
| 46 | message = sb.toString(); |
| 47 | break; |
| 48 | } |
| 49 | else { |
| 50 | continue; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | catch (IOException e) { |
| 57 | LOG.severe("Socket read failed. Check your network configuration."); |
| 58 | Messages.severe("Socket read failed. Check your network configuration."); |
| 59 | return null; |
| 60 | } |
| 61 | return message; |
| 62 | } |
| 63 | |
| 64 | /** Guarantees that large messages will be completely sent through a socket. |
| 65 | * @return Returns 0 for success, -1 for failure. |
| 66 | */ |
| 67 | public static int sendAll(SocketChannel sock, ByteBuffer message) { |
| 68 | message.rewind(); |
| 69 | while (message.remaining() > 0) { |
| 70 | try { |
| 71 | sock.write(message); |
| 72 | } |
| 73 | catch (IOException e) { |
| 74 | LOG.warning("Socket write failed. Check your network configuration."); |
| 75 | Messages.severe("Socket write failed. Check your network configuration."); |
| 76 | return -1; |
| 77 | } |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /** Overloaded method for sending a byte array. */ |
| 83 | public static void sendAll(SocketChannel sock, byte[] message) { |
| 84 | ByteBuffer buff = ByteBuffer.wrap(message); |
| 85 | sendAll(sock, buff); |
| 86 | } |
| 87 | |
| 88 | /** Overloaded method for sending a String. */ |
| 89 | public static void sendAll(SocketChannel sock, String message) { |
| 90 | sendAll(sock, message.getBytes()); |
| 91 | } |
| 92 | } |