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.net.InetSocketAddress; |
| 9 | |
| 10 | import java.nio.channels.ServerSocketChannel; |
| 11 | |
| 12 | /** |
| 13 | * @author daniel |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | /** Serves processing results back to the atom. */ |
| 18 | public class ResultSender { |
| 19 | private static final int PORT = 9716; |
| 20 | |
| 21 | private ServerSocketChannel sock; |
| 22 | |
| 23 | AccepterThread acceptor; |
| 24 | |
| 25 | /** Constructor. Connects to a socket and starts the accepter thread. */ |
| 26 | public ResultSender() throws IOException { |
| 27 | sock = ServerSocketChannel.open(); |
| 28 | sock.socket().bind(new InetSocketAddress(PORT)); |
| 29 | |
| 30 | //start accepter thread |
| 31 | acceptor = new AccepterThread(sock); |
| 32 | } |
| 33 | |
| 34 | /** Sends a new message of calculated attributes to the clients. |
| 35 | * |
| 36 | * @param azimuth is the calculated optimum azimuth for the shot. |
| 37 | * @param elevation is the calculated optimum elevation for the shot. |
| 38 | * @param range is the calculated optimum range for the shot. |
| 39 | */ |
| 40 | public void send(double azimuth, double elevation, double range) { |
| 41 | //Formulate a message as a String similar to an HTTP header. |
| 42 | if (azimuth != 0.0d && elevation != 0.0d && range != 0.0d) { |
| 43 | StringBuilder message = new StringBuilder(); |
| 44 | message.append("\r\n--boundarydonotcross\r\n"); |
| 45 | message.append("Azimuth: "); |
| 46 | message.append(azimuth); |
| 47 | message.append("\r\nElevation: "); |
| 48 | message.append(elevation); |
| 49 | message.append("\r\nRange: "); |
| 50 | message.append(range); |
| 51 | |
| 52 | acceptor.sendtoAll(message.toString()); |
| 53 | } |
| 54 | } |
| 55 | } |