Permalink
Browse files

Use new Java 9 System.Logger instead of old JUL

  • Loading branch information...
tbsalling committed Apr 2, 2018
1 parent 4c197d3 commit d7537a6752e3167e32e69747d3be1f29405ba015
@@ -36,6 +36,7 @@
import java.util.stream.Stream;

import static dk.tbsalling.aismessages.ais.Decoders.UNSIGNED_INTEGER_DECODER;
import static java.lang.System.Logger.Level.WARNING;
import static java.lang.reflect.Modifier.isPublic;
import static java.util.Objects.requireNonNull;

@@ -52,7 +53,7 @@
@SuppressWarnings("serial")
public abstract class AISMessage implements Serializable, CachedDecodedValues {

private transient static final Logger LOG = Logger.getLogger(AISMessage.class.getName());
private transient static final System.Logger LOG = System.getLogger(AISMessage.class.getName());

public transient static final String VERSION = "3.0.0";

@@ -439,9 +440,180 @@ public static AISMessage create(NMEAMessage... nmeaMessages) {
return aisMessageConstructor.apply(nmeaMessages, bitString);
}

/**
* Decode an encoded six-bit string into a binary string of 0's and 1's
*/
public boolean isValid() {
final String bitString = getBitString();

if (bitString.length() < 6) {
LOG.log(WARNING,"Message is too short: " + bitString.length() + " bits.");
return Boolean.FALSE;
}

int messageType = Integer.parseInt(bitString.substring(0, 6), 2);
if (messageType < AISMessageType.MINIMUM_CODE || messageType > AISMessageType.MAXIMUM_CODE) {
LOG.log(WARNING,"Unsupported message type: " + messageType);
return Boolean.FALSE;
}

int actualMessageLength = bitString.length();
switch (messageType) {
case 1:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 1: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 2:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 2: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 3:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 3: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 4:
if (actualMessageLength != 168) return Boolean.FALSE;
break;
case 5:
if (actualMessageLength != 424 && actualMessageLength != 422) {
LOG.log(WARNING,"Message type 5: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 6:
if (actualMessageLength > 1008) {
LOG.log(WARNING,"Message type 6: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 7:
if (actualMessageLength != 72 && actualMessageLength != 104 && actualMessageLength != 136 && actualMessageLength != 168) {
LOG.log(WARNING,"Message type 7: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 8:
if (actualMessageLength > 1008) {
LOG.log(WARNING,"Message type 8: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 9:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 9: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 10:
if (actualMessageLength != 72) {
LOG.log(WARNING,"Message type 10: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 11:
if (actualMessageLength != 168) return Boolean.FALSE;
break;
case 12:
if (actualMessageLength > 1008) {
LOG.log(WARNING,"Message type 12: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 13:
if (actualMessageLength != 72 && actualMessageLength != 104 && actualMessageLength != 136 && actualMessageLength != 168) {
LOG.log(WARNING,"Message type 13: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 14:
if (actualMessageLength > 1008) {
LOG.log(WARNING,"Message type 14: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 15:
if (actualMessageLength != 88 && actualMessageLength != 110 && actualMessageLength != 112 && actualMessageLength != 160) return Boolean.FALSE;
break;
case 16:
if (actualMessageLength != 96 && actualMessageLength != 144) {
LOG.log(WARNING,"Message type 16: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 17:
if (actualMessageLength < 80 || actualMessageLength > 816) {
LOG.log(WARNING,"Message type 17: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 18:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 18: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 19:
if (actualMessageLength != 312) {
LOG.log(WARNING,"Message type 19: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 20:
if (actualMessageLength < 72 || actualMessageLength > 160) {
LOG.log(WARNING,"Message type 20: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 21:
if (actualMessageLength < 272 || actualMessageLength > 360) {
LOG.log(WARNING,"Message type 21: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 22:
if (actualMessageLength != 168) {
LOG.log(WARNING,"Message type 22: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 23:
if (actualMessageLength != 160) {
LOG.log(WARNING,"Message type 23: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 24:
if (actualMessageLength != 160 && actualMessageLength != 168 && actualMessageLength != 158 ) {
LOG.log(WARNING,"Message type 24: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 25:
if (actualMessageLength > 168) {
LOG.log(WARNING,"Message type 25: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
case 26:
// ??
break;
case 27:
if (actualMessageLength != 96 && actualMessageLength != 168) {
LOG.log(WARNING,"Message type 27: Illegal message length: " + bitString.length() + " bits.");
return Boolean.FALSE;
}
break;
default:
return Boolean.FALSE;
}

return Boolean.TRUE;
}

/** Decode an encoded six-bit string into a binary string of 0's and 1's */
private static String toBitString(String encodedString, Integer paddingBits) {
StringBuilder bitString = new StringBuilder();
int n = encodedString.length();
@@ -20,8 +20,6 @@
import dk.tbsalling.aismessages.nmea.exceptions.InvalidMessage;
import dk.tbsalling.aismessages.nmea.messages.NMEAMessage;

import java.util.logging.Logger;

import static dk.tbsalling.aismessages.ais.Decoders.UNSIGNED_INTEGER_DECODER;
import static java.lang.String.format;

@@ -38,8 +36,6 @@
@SuppressWarnings("serial")
public class DataLinkManagement extends AISMessage {

private static final Logger log = Logger.getLogger(DataLinkManagement.class.getName());

public DataLinkManagement(NMEAMessage[] nmeaMessages) {
super(nmeaMessages);
}
@@ -17,14 +17,14 @@
package dk.tbsalling.aismessages.ais.messages.types;

import java.io.Serializable;
import java.util.logging.Logger;

import static dk.tbsalling.aismessages.ais.Decoders.UNSIGNED_INTEGER_DECODER;
import static java.lang.System.Logger.Level.WARNING;
import static java.util.Objects.requireNonNull;

public class SOTDMACommunicationState extends CommunicationState implements Serializable {

private transient static final Logger LOG = Logger.getLogger(SOTDMACommunicationState.class.getName());
private static final System.Logger LOG = System.getLogger(SOTDMACommunicationState.class.getName());

private SOTDMACommunicationState(SyncState syncState, Integer slotTimeout, Integer numberOfReceivedStations, Integer slotNumber, Integer utcHour, Integer utcMinute, Integer slotOffset) {
super(syncState);
@@ -24,7 +24,9 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Logger;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.WARNING;

/**
* This class receives NMEA messages containing armoured and encoded AIS strings.
@@ -37,9 +39,9 @@
*/
public class NMEAMessageHandler implements Consumer<NMEAMessage> {

private static final Logger LOG = Logger.getLogger(NMEAMessageHandler.class.getName());
private static final System.Logger LOG = System.getLogger(NMEAMessageHandler.class.getName());

private final String source;
private final String source;
private final ArrayList<NMEAMessage> messageFragments = new ArrayList<>();
private final List<Consumer<? super AISMessage>> aisMessageReceivers = new LinkedList<>();

@@ -56,48 +58,48 @@ public NMEAMessageHandler(String source, Consumer<? super AISMessage>... aisMess
*/
@Override
public void accept(NMEAMessage nmeaMessage) {
LOG.finer("Received for processing: " + nmeaMessage.getRawMessage());
LOG.log(DEBUG, "Received for processing: " + nmeaMessage.getRawMessage());

if (! nmeaMessage.isValid()) {
LOG.warning("NMEA message is invalid: " + nmeaMessage.toString());
LOG.log(WARNING, "NMEA message is invalid: " + nmeaMessage.toString());
return;
}

int numberOfFragments = nmeaMessage.getNumberOfFragments();
if (numberOfFragments <= 0) {
LOG.warning("NMEA message is invalid: " + nmeaMessage.toString());
LOG.log(WARNING, "NMEA message is invalid: " + nmeaMessage.toString());
messageFragments.clear();
} else if (numberOfFragments == 1) {
LOG.finest("Handling unfragmented NMEA message");
LOG.log(DEBUG, "Handling unfragmented NMEA message");
AISMessage aisMessage = AISMessage.create(new Metadata(source), nmeaMessage);
sendToAisMessageReceivers(aisMessage);
messageFragments.clear();
} else {
int fragmentNumber = nmeaMessage.getFragmentNumber();
LOG.finest("Handling fragmented NMEA message with fragment number " + fragmentNumber);
LOG.log(DEBUG, "Handling fragmented NMEA message with fragment number " + fragmentNumber);
if (fragmentNumber < 0) {
LOG.warning("Fragment number cannot be negative: " + fragmentNumber + ": " + nmeaMessage.getRawMessage());
LOG.log(WARNING, "Fragment number cannot be negative: " + fragmentNumber + ": " + nmeaMessage.getRawMessage());
messageFragments.clear();
} else if (fragmentNumber > numberOfFragments) {
LOG.fine("Fragment number " + fragmentNumber + " higher than expected " + numberOfFragments + ": " + nmeaMessage.getRawMessage());
LOG.log(DEBUG, "Fragment number " + fragmentNumber + " higher than expected " + numberOfFragments + ": " + nmeaMessage.getRawMessage());
messageFragments.clear();
} else {
int expectedFragmentNumber = messageFragments.size() + 1;
LOG.finest("Expected fragment number is: " + expectedFragmentNumber + ": " + nmeaMessage.getRawMessage());
LOG.log(DEBUG, "Expected fragment number is: " + expectedFragmentNumber + ": " + nmeaMessage.getRawMessage());

if (expectedFragmentNumber != fragmentNumber) {
LOG.fine("Expected fragment number " + expectedFragmentNumber + "; not " + fragmentNumber + ": " + nmeaMessage.getRawMessage());
LOG.log(DEBUG, "Expected fragment number " + expectedFragmentNumber + "; not " + fragmentNumber + ": " + nmeaMessage.getRawMessage());
messageFragments.clear();
} else {
messageFragments.add(nmeaMessage);
LOG.finest("nmeaMessage.getNumberOfFragments(): " + nmeaMessage.getNumberOfFragments());
LOG.finest("messageFragments.size(): " + messageFragments.size());
LOG.log(DEBUG, "nmeaMessage.getNumberOfFragments(): " + nmeaMessage.getNumberOfFragments());
LOG.log(DEBUG, "messageFragments.size(): " + messageFragments.size());
if (nmeaMessage.getNumberOfFragments() == messageFragments.size()) {
AISMessage aisMessage = AISMessage.create(new Metadata(source), messageFragments.toArray(new NMEAMessage[messageFragments.size()]));
sendToAisMessageReceivers(aisMessage);
messageFragments.clear();
} else
LOG.finest("Fragmented message not yet complete; missing " + (nmeaMessage.getNumberOfFragments() - messageFragments.size()) + " fragment(s).");
LOG.log(DEBUG, "Fragmented message not yet complete; missing " + (nmeaMessage.getNumberOfFragments() - messageFragments.size()) + " fragment(s).");
}
}
}
@@ -26,13 +26,14 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import static java.lang.System.Logger.Level.*;

public class NMEAMessageInputStreamReader {

private static final Logger log = Logger.getLogger(NMEAMessageInputStreamReader.class.getName());
private static final System.Logger LOG = System.getLogger(NMEAMessageInputStreamReader.class.getName());

public NMEAMessageInputStreamReader(InputStream inputStream, Consumer<? super NMEAMessage> nmeaMessageHandler) {
this.nmeaMessageHandler = nmeaMessageHandler;
@@ -44,7 +45,7 @@ public final void requestStop() {
}

public void run() throws IOException {
log.info("NMEAMessageInputStreamReader running.");
LOG.log(INFO, "NMEAMessageInputStreamReader running.");

InputStreamReader reader = new InputStreamReader(inputStream, Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(reader);
@@ -53,17 +54,17 @@ public void run() throws IOException {
try {
NMEAMessage nmea = NMEAMessage.fromString(string);
nmeaMessageHandler.accept(nmea);
log.fine("Received: " + nmea.toString());
LOG.log(DEBUG, "Received: " + nmea.toString());
} catch (InvalidMessage invalidMessageException) {
log.warning("Received invalid AIS message: \"" + string + "\"");
LOG.log(WARNING, "Received invalid AIS message: \"" + string + "\"");
} catch (UnsupportedMessageType unsupportedMessageTypeException) {
log.warning("Received unsupported NMEA message: \"" + string + "\"");
LOG.log(WARNING, "Received unsupported NMEA message: \"" + string + "\"");
} catch (NMEAParseException parseException) {
log.warning("Received non-compliant NMEA message: \"" + string + "\"");
LOG.log(WARNING, "Received non-compliant NMEA message: \"" + string + "\"");
}
}

log.info("NMEAMessageInputStreamReader stopping.");
LOG.log(INFO, "NMEAMessageInputStreamReader stopping.");
}

public final Boolean isStopRequested() {
Oops, something went wrong.

0 comments on commit d7537a6

Please sign in to comment.