f1-2018_telemetry/src/main/java/com/eh7n/f1telemetry/F12018TelemetryUDPServer.java
/**
	 * Start the F1 2018 Telemetry UDP server
	 * 
	 * @throws IOException           if the server fails to start
	 * @throws IllegalStateException if you do not define how the packets should be
	 *                               consumed
	 */
Start the F1 2018 Telemetry UDP server

@throws IOException           if the server fails to start
@throws IllegalStateException if you do not define how the packets should be
consumed
Start the F1 2018 Telemetry UDP server @throws IOException if the server fails to start @throws IllegalStateException if you do not define how the packets should be consumed
public void start() throws IOException {
		if (packetConsumer == null) {
			throw new IllegalStateException("You must define how the packets will be consumed.");
		}
		log.info("F1 2018 - Telemetry UDP Server");
		ExecutorService executor = Executors.newSingleThreadExecutor();
		try (DatagramChannel channel = DatagramChannel.open()) {
			channel.socket().bind(new InetSocketAddress(bindAddress, port));
			log.info("Listening on " + bindAddress + ":" + port + "...");
			ByteBuffer buf = ByteBuffer.allocate(MAX_PACKET_SIZE);
			buf.order(ByteOrder.LITTLE_ENDIAN);
			while (true) {
				channel.receive(buf);
				final Packet packet = PacketDeserializer.read(buf.array());
				executor.submit(() -> {
					packetConsumer.accept(packet);
				});
				buf.clear();
			}
		} finally {
			executor.shutdown();
		}
	}
public void start ( ) throws IOException { if ( packetConsumer == null ) { throw new IllegalStateException ( "You must define how the packets will be consumed." ) ; } log . info ( "F1 2018 - Telemetry UDP Server" ) ; ExecutorService executor = Executors . newSingleThreadExecutor ( ) ; try ( DatagramChannel channel = DatagramChannel . open ( ) ) { channel . socket ( ) . bind ( new InetSocketAddress ( bindAddress , port ) ) ; log . info ( "Listening on " + bindAddress + ":" + port + "..." ) ; ByteBuffer buf = ByteBuffer . allocate ( MAX_PACKET_SIZE ) ; buf . order ( ByteOrder . LITTLE_ENDIAN ) ; while ( true ) { channel . receive ( buf ) ; final Packet packet = PacketDeserializer . read ( buf . array ( ) ) ; executor . submit ( ( ) -> { packetConsumer . accept ( packet ) ; } ) ; buf . clear ( ) ; } } finally { executor . shutdown ( ) ; } }
Start the F1 2018 Telemetry UDP server
Start the F1 2018 Telemetry UDP server
// Create an executor to process the Packets in a separate thread // To be honest, this is probably an over-optimization due to the use of NIO, // but it was done to provide a simple way of providing back pressure on the // incoming UDP packet handling to allow for long-running processing of the // Packet object, if required.