Skip to content

Commit

Permalink
ODE-971 Cleaning up code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
hmusavi committed Dec 24, 2018
1 parent 33e5786 commit 7662191
Show file tree
Hide file tree
Showing 40 changed files with 91 additions and 1,966 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ public byte[] getBundle() {
if ( point == null )
return payload;
int payloadLength = payload != null ? payload.length : 0;
int HEADER_LENGTH = MIN_BUNDLE_LENGTH - 4 + point.address.length;
byte [] bundle = new byte[HEADER_LENGTH + payloadLength];
ByteBuffer buffer = ByteBuffer.allocate(HEADER_LENGTH).order(ByteOrder.BIG_ENDIAN);
int headerLength = MIN_BUNDLE_LENGTH - 4 + point.address.length;
byte [] bundle = new byte[headerLength + payloadLength];
ByteBuffer buffer = ByteBuffer.allocate(headerLength).order(ByteOrder.BIG_ENDIAN);
buffer.putInt(MAGIC_NUMBER);
buffer.putInt(point.port);
buffer.put((byte)(point.address.length == 16 ? 1 : 0));
buffer.put(point.address);
byte[] header = buffer.array();
assert(header.length == HEADER_LENGTH);
assert(header.length == headerLength);
CrcCccitt.setMsgCRC(header);
System.arraycopy(header, 0, bundle, 0, HEADER_LENGTH);
System.arraycopy(header, 0, bundle, 0, headerLength);
if ( payload != null )
System.arraycopy(payload, 0, bundle, HEADER_LENGTH, payloadLength);
System.arraycopy(payload, 0, bundle, headerLength, payloadLength);
return bundle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
*/
public class InetPacketSender {

private static final Logger log = Logger.getLogger(InetPacketSender.class);
private static final String INVALID_PARAMETERS_MSG = "Invalid Parameters. Parameters destination point and payload can not be null";

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

/**
* Inet address and port to forward packets to
Expand Down Expand Up @@ -90,7 +92,7 @@ public void send(DatagramPacket packet) throws InetPacketException {
*/
public void forward(InetPoint dstPoint, byte[] payload) throws InetPacketException {
if ( dstPoint == null || payload == null )
throw new InetPacketException("Invalid Parameters. Parameters destination point and payload can not be null");
throw new InetPacketException(INVALID_PARAMETERS_MSG);
if ( frwdPoint == null )
log.warn("Couldn't forward packet. Reason: Forwarding destination is not defined.");
if ( frwdPoint != null && (dstPoint.isIPv6Address() || isForwardAll()) ) {
Expand All @@ -110,7 +112,7 @@ public void forward(InetPoint dstPoint, byte[] payload) throws InetPacketExcepti
*/
public void forward(InetPoint dstPoint, byte[] payload, boolean fromForwarder) throws InetPacketException {
if ( dstPoint == null || payload == null )
throw new InetPacketException("Invalid Parameters. Parameters destination point and payload can not be null");
throw new InetPacketException(INVALID_PARAMETERS_MSG);
if ( frwdPoint != null && (dstPoint.isIPv6Address() || isForwardAll() || fromForwarder) ) {
send(frwdPoint, new InetPacket(dstPoint, payload).getBundle());
} else {
Expand All @@ -127,20 +129,14 @@ public void forward(InetPoint dstPoint, byte[] payload, boolean fromForwarder) t
*/
public void send(InetPoint dstPoint, byte[] payload) throws InetPacketException {
if ( dstPoint == null || payload == null )
throw new InetPacketException("Invalid Parameters. Parameters destination point and payload can not be null");
DatagramSocket sock = null;
try {
throw new InetPacketException(INVALID_PARAMETERS_MSG);
try(DatagramSocket sock = new DatagramSocket()) {
DatagramPacket packet = new DatagramPacket(payload, payload.length, dstPoint.getInetAddress(), dstPoint.port);
sock = new DatagramSocket();
sock.send(packet);
} catch (SocketException ex) {
throw new InetPacketException("Couldn't send packet because socket closed.", ex);
} catch (IOException ex) {
throw new InetPacketException("Couldn't send packet due to IO exception.", ex);
} finally {
if ( sock != null ) {
sock.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.dot.its.jpo.ode.util.CodecUtils;

public class InetPoint {
final public byte[] address;
final public int port;
final public boolean forward;
private final Logger logger = LoggerFactory.getLogger(this.getClass());

public final byte[] address;
public final int port;
public boolean forward;

public InetPoint(String host, int port, boolean forward) throws UnknownHostException {
this(InetAddress.getByName(host).getAddress(), port, forward);
Expand Down Expand Up @@ -54,6 +59,7 @@ public String toString() {
try {
host = InetAddress.getByAddress(address).getHostAddress();
} catch (UnknownHostException e) {
logger.error("Error", e);
}
return String.format("%s { port = %d (0x%x); address = %s (%s, %s); forward = %s }",
getClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
******************************************************************************/
package us.dot.its.jpo.ode.dds;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -182,7 +181,7 @@ private String getServiceTicket(String server, String ticketGrantingTicket,
}

private String getServiceCall(String service, String serviceTicket)
throws IOException, CASException {
throws CASException {

HttpClient httpClient = httpClientFactory.createHttpClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ private void init(
String websocketURL,
String keystoreFile,
String keystorePass
) throws URISyntaxException,
SSLException, CASException {
) throws URISyntaxException, SSLException {
if (uri == null) {
uri = new URI(websocketURL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public abstract class AbstractSubscriberProcessor<K, S> extends MessageProcessor<K, S> {

protected Logger logger = LoggerFactory.getLogger(this.getClass());
private Logger logger = LoggerFactory.getLogger(this.getClass());
protected int messagesConsumed = 0;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

public class ServiceRequest extends OdeObject {

public static class OdeInternal {
public static class OdeInternal extends OdeObject {

public enum RequestVerb {
private static final long serialVersionUID = 1L;

public enum RequestVerb {
POST, PUT, DELETE, GET
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,41 @@ public DdsAdvisorySituationData() {
groupID = CodecUtils.toHex(gid);
}

public DdsAdvisorySituationData(String startTime, String stopTime, Ieee1609Dot2DataTag advisoryMessage,
// This is a factory method instead of a constrcutor in order to avoid having a constructor with too many parameters
public static DdsAdvisorySituationData create(String startTime, String stopTime, Ieee1609Dot2DataTag advisoryMessage,
DdsGeoRegion serviceRegion, SituationDataWarehouse.SDW.TimeToLive ttl, String groupID, String recordID, byte distroType) throws ParseException {
this();
DdsAdvisorySituationData asd = new DdsAdvisorySituationData();

J2735DFullTime dStartTime = dFullTimeFromIsoTimeString(startTime);
J2735DFullTime dStartTime = asd.dFullTimeFromIsoTimeString(startTime);

J2735DFullTime dStopTime = dFullTimeFromIsoTimeString(stopTime);
J2735DFullTime dStopTime = asd.dFullTimeFromIsoTimeString(stopTime);

byte[] fourRandomBytes = new byte[4];
new Random(System.currentTimeMillis()).nextBytes(fourRandomBytes);
String id = CodecUtils.toHex(fourRandomBytes);
String stringDistroType = CodecUtils.toHex(distroType);
this.setAsdmDetails(
asd.setAsdmDetails(
new DdsAdvisoryDetails(id, AdvisoryBroadcastType.tim, stringDistroType, dStartTime, dStopTime, advisoryMessage));

this.setRequestID(id);
this.setServiceRegion(serviceRegion);
asd.setRequestID(id);
asd.setServiceRegion(serviceRegion);
if (ttl != null) {
this.setTimeToLive(ttl.ordinal());
asd.setTimeToLive(ttl.ordinal());
} else {
this.setTimeToLive(SituationDataWarehouse.SDW.TimeToLive.thirtyminutes.ordinal());
asd.setTimeToLive(SituationDataWarehouse.SDW.TimeToLive.thirtyminutes.ordinal());
}
if (groupID != null) {
this.setGroupID(groupID);
asd.setGroupID(groupID);
} else {
this.setGroupID(CodecUtils.toHex(new byte[] { 0, 0, 0, 0 }));
asd.setGroupID(CodecUtils.toHex(new byte[] { 0, 0, 0, 0 }));
}
if (recordID != null) {
this.setRecordID(recordID);
asd.setRecordID(recordID);
} else {
this.setRecordID(CodecUtils.toHex(new byte[] { 0, 0, 0, 0 }));
asd.setRecordID(CodecUtils.toHex(new byte[] { 0, 0, 0, 0 }));
}

return asd;
}

public J2735DFullTime dFullTimeFromIsoTimeString(String isoTime) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class OdeTravelerInformationMessage extends OdeObject {
@Expose
private DataFrame[] dataframes;
@Expose(serialize = false, deserialize = true)
private JsonNode asnDataFrames;
private transient JsonNode asnDataFrames;

public int getMsgCnt() {
return msgCnt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,9 @@ public static void validateITISCodes(String code) {
throw new IllegalArgumentException("Invalid ITIS code [0..65535]: " + cd);
} catch (NumberFormatException e) {
if (code.isEmpty())
throw new IllegalArgumentException("Invalid empty string");
throw new IllegalArgumentException("Empty ITIS Code or Text");
if (code.length() > 500)
throw new IllegalArgumentException("Invalid test phrase length [1..500]: " + code.length());
}
}

public static void validateContentCodes(String code) {
int cd;
try {
cd = Integer.parseInt(code);
if (cd < 0 || cd > 65535)
throw new IllegalArgumentException("Invalid ITIS code [0..65535]: " + cd);
} catch (NumberFormatException e) {
if (code.isEmpty())
throw new IllegalArgumentException("Invalid empty string");
if (code.length() > 16)
throw new IllegalArgumentException("Invalid test Phrase length [1..16]: " + code.length());
throw new IllegalArgumentException("Invalid text phrase length [1..500]: " + code.length());
}
}

Expand Down Expand Up @@ -309,29 +295,23 @@ public static void validateB12Scale(BigDecimal b) {
public static void validateNodeAttribute(String str) {
String myString = "reserved stopLine roundedCapStyleA roundedCapStyleB mergePoint divergePoint downstreamStopLine donwstreamStartNode closedToTraffic safeIsland curbPresentAtStepOff hydrantPresent";
CharSequence cs = str;
if (myString.contains(cs)) {
return;
} else {
if (!myString.contains(cs)) {
throw new IllegalArgumentException("Invalid NodeAttribute Enumeration");
}
}

public static void validateSegmentAttribute(String str) {
String myString = "reserved doNotBlock whiteLine mergingLaneLeft mergingLaneRight curbOnLeft curbOnRight loadingzoneOnLeft loadingzoneOnRight turnOutPointOnLeft turnOutPointOnRight adjacentParkingOnLeft adjacentParkingOnRight sharedBikeLane bikeBoxInFront transitStopOnLeft transitStopOnRight transitStopInLane sharedWithTrackedVehicle safeIsland lowCurbsPresent rumbleStripPresent audibleSignalingPresent adaptiveTimingPresent rfSignalRequestPresent partialCurbIntrusion taperToLeft taperToRight taperToCenterLine parallelParking headInParking freeParking timeRestrictionsOnParking costToPark midBlockCurbPresent unEvenPavementPresent";
CharSequence cs = str;
if (myString.contains(cs)) {
return;
} else {
if (!myString.contains(cs)) {
throw new IllegalArgumentException("Invalid SegmentAttribute Enumeration");
}
}

public static void validateSpeedLimitType(String str) {
String myString = "unknown maxSpeedInSchoolZone maxSpeedInSchoolZoneWhenChildrenArePresent maxSpeedInConstructionZone vehicleMinSpeed vehicleMaxSpeed vehicleNightMaxSpeed truckMinSpeed truckMaxSpeed truckNightMaxSpeed vehiclesWithTrailerMinSpeed vehiclesWithTrailersMaxSpeed vehiclesWithTrailersNightMaxSpeed";
CharSequence cs = str;
if (myString.contains(cs)) {
return;
} else {
if (!myString.contains(cs)) {
throw new IllegalArgumentException("Invalid SpeedLimitAttribute Enumeration");
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 7662191

Please sign in to comment.