Skip to content

Commit

Permalink
Changed logging to use SLF4J. Ran some JDK 1.7 refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
StanAccy committed Jan 16, 2014
1 parent dbfb8a7 commit 0924c7b
Show file tree
Hide file tree
Showing 42 changed files with 480 additions and 812 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public BufferedNodeComponent() {
}

public BufferedNodeComponent(MessageCondition msgCondition, MessageCondition ackCondition) {
msgBuffer = new SharedMetaBuffer<MessageMetaWrapper<StandardMessage>>(MSG_BUFFER_LENGTH, msgCondition);
ackBuffer = new SharedMetaBuffer<MessageMetaWrapper<Response>>(ACK_BUFFER_LENGTH, ackCondition);
msgBuffer = new SharedMetaBuffer<>(MSG_BUFFER_LENGTH, msgCondition);
ackBuffer = new SharedMetaBuffer<>(ACK_BUFFER_LENGTH, ackCondition);
}

/**
Expand Down
64 changes: 31 additions & 33 deletions jformica-core/src/main/java/org/cowboycoders/ant/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@
*/
package org.cowboycoders.ant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import org.cowboycoders.ant.defines.AntDefine;
import org.cowboycoders.ant.events.BroadcastListener;
import org.cowboycoders.ant.events.BroadcastMessenger;
import org.cowboycoders.ant.events.EventMachine;
import org.cowboycoders.ant.events.MessageCondition;
import org.cowboycoders.ant.events.MessageConditionFactory;
import org.cowboycoders.ant.messages.ChannelMessage;
Expand All @@ -48,6 +36,7 @@
import org.cowboycoders.ant.messages.commands.ChannelRequestMessage.Request;
import org.cowboycoders.ant.messages.config.AddChannelIdMessage;
import org.cowboycoders.ant.messages.config.ChannelAssignMessage;
import org.cowboycoders.ant.messages.config.ChannelAssignMessage.ExtendedAssignment;
import org.cowboycoders.ant.messages.config.ChannelFrequencyMessage;
import org.cowboycoders.ant.messages.config.ChannelIdMessage;
import org.cowboycoders.ant.messages.config.ChannelLowPrioritySearchTimeoutMessage;
Expand All @@ -56,28 +45,37 @@
import org.cowboycoders.ant.messages.config.ChannelSearchTimeoutMessage;
import org.cowboycoders.ant.messages.config.ChannelTxPowerMessage;
import org.cowboycoders.ant.messages.config.ChannelUnassignMessage;
import org.cowboycoders.ant.messages.config.ConfigListIdMessage;
import org.cowboycoders.ant.messages.config.FrequencyAgilityMessage;
import org.cowboycoders.ant.messages.config.ProximitySearchMessage;
import org.cowboycoders.ant.messages.config.ChannelAssignMessage.ExtendedAssignment;
import org.cowboycoders.ant.messages.config.ConfigListIdMessage;
import org.cowboycoders.ant.messages.data.BurstDataMessage;
import org.cowboycoders.ant.messages.data.BurstData;
import org.cowboycoders.ant.messages.data.BurstDataMessage;
import org.cowboycoders.ant.messages.nonstandard.CombinedBurst;
import org.cowboycoders.ant.messages.nonstandard.CombinedBurst.StatusFlag;
import org.cowboycoders.ant.messages.responses.Response;
import org.cowboycoders.ant.messages.responses.ChannelStatusResponse;
import org.cowboycoders.ant.messages.responses.ChannelStatusResponse.State;
import org.cowboycoders.ant.messages.responses.Response;
import org.cowboycoders.ant.messages.responses.ResponseCode;
import org.cowboycoders.ant.utils.BurstMessageSequenceGenerator;
import org.cowboycoders.ant.utils.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Channel {

public static final int SEARCH_TIMEOUT_NEVER = 255;

public final static Logger LOGGER = Logger.getLogger(EventMachine.class
.getName());

private static final Logger log = LoggerFactory.getLogger( Channel.class );
private static final int RAW_CHANNEL_PERIOD_DEFAULT = 8192;

private static final double CHANNEL_PERIOD_SCALE_FACTOR = 32768.0;
Expand Down Expand Up @@ -111,7 +109,7 @@ public Node getParent() {

private long burstTimeout = BURST_TIMEOUT_NANOS_DEFAULT;

private final BroadcastMessenger<CombinedBurst> burstMessenger = new BroadcastMessenger<CombinedBurst>();
private final BroadcastMessenger<CombinedBurst> burstMessenger = new BroadcastMessenger<>();

private CombinedBurst.Builder burstBuilder = new CombinedBurst.Builder();

Expand Down Expand Up @@ -176,7 +174,7 @@ public synchronized boolean isFree() {
* the isFree to set
*/
protected synchronized void setFree(boolean free) {
LOGGER.entering(Channel.class.getSimpleName(), "setFree");
log.trace(Channel.class.getSimpleName(), "setFree");

// Make sure channel is in reusable state
if (free) {
Expand All @@ -185,7 +183,7 @@ protected synchronized void setFree(boolean free) {

// clean up complete, set free
this.free = free;
LOGGER.exiting(Channel.class.getSimpleName(), "setFree");
log.trace(Channel.class.getSimpleName(), "setFree");
}

/**
Expand All @@ -200,18 +198,18 @@ private void cleanUp() {
state = status.getState();
} catch (ChannelError e){
// ignore (assume assigned)
LOGGER.warning("Error requesting channel status");
log.warn("Error requesting channel status");
}

LOGGER.finer("pre close: " + state);
log.trace("pre close: " + state);

// if channel is open (try and close)
if (state.equals(State.TRACKING) || state.equals(State.SEARCHING)) {
try {
this.close();

} catch (ChannelError e){
LOGGER.warning("Error closing channel in state: " + state);
log.warn("Error closing channel in state: " + state);
}
}

Expand All @@ -224,18 +222,18 @@ private void cleanUp() {
state = status.getState();
} catch (ChannelError e){
// ignore (assume assigned)
LOGGER.warning("Error requesting channel status");
log.warn("Error requesting channel status");
}

LOGGER.finer("pre unassign: " + state);
log.trace("pre unassign: " + state);

// channel is assigned, but not open
if (state.equals(State.ASSIGNED)) {
try {
this.unassign();

} catch (ChannelError e){
LOGGER.warning("Error unassigning channel in state: " + state);
log.warn("Error unassigning channel in state: " + state);
}
}

Expand All @@ -260,7 +258,7 @@ public List<MessageMetaWrapper<? extends StandardMessage>> send(
StandardMessage msg) {
MessageMetaWrapper<StandardMessage> sentMeta = Channel.this
.send((ChannelMessage) msg);
List<MessageMetaWrapper<? extends StandardMessage>> rtn = new ArrayList<MessageMetaWrapper<? extends StandardMessage>>(
List<MessageMetaWrapper<? extends StandardMessage>> rtn = new ArrayList<>(
1);
rtn.add(sentMeta);
return rtn;
Expand Down Expand Up @@ -385,7 +383,7 @@ public boolean test(StandardMessage msg) {
* We wrap listeners to look for message of specific class - this is a map
* from the original to the new one
*/
private Map<Object, BroadcastListener<ChannelMessage>> mAdapterListenerMap = new HashMap<Object, BroadcastListener<ChannelMessage>>();
private Map<Object, BroadcastListener<ChannelMessage>> mAdapterListenerMap = new HashMap<>();

private ChannelType type;

Expand Down Expand Up @@ -423,7 +421,7 @@ public synchronized <V extends ChannelMessage> void removeRxListener(
if (adapter != null) {
parent.removeRxListener(adapter);
} else {
LOGGER.warning("removeRxListener: ignoring unknown listener");
log.warn("removeRxListener: ignoring unknown listener");
}

}
Expand Down Expand Up @@ -711,7 +709,7 @@ public synchronized void assign(NetworkKey key,
if (assignedNetwork != null) {
networkNumber = assignedNetwork.getNumber();
} else {
LOGGER.warning("network key not found: default to network 0");
log.warn("network key not found: default to network 0");
}

assignMessage.setNetworkNumber(networkNumber);
Expand Down Expand Up @@ -859,7 +857,7 @@ public boolean test(StandardMessage msg) {
@Override
public ArrayList<MessageMetaWrapper<? extends StandardMessage>> send(
StandardMessage msgIn) {
ArrayList<MessageMetaWrapper<? extends StandardMessage>> sentMessages = new ArrayList<MessageMetaWrapper<? extends StandardMessage>>(
ArrayList<MessageMetaWrapper<? extends StandardMessage>> sentMessages = new ArrayList<>(
list.size());

// handle all but last
Expand Down
64 changes: 33 additions & 31 deletions jformica-core/src/main/java/org/cowboycoders/ant/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@
*/
package org.cowboycoders.ant;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.logging.Logger;

import org.cowboycoders.ant.AntLogger.Direction;
import org.cowboycoders.ant.AntLogger.LogDataContainer;
import org.cowboycoders.ant.events.BroadcastListener;
Expand All @@ -56,15 +41,33 @@
import org.cowboycoders.ant.messages.config.NetworkKeyMessage;
import org.cowboycoders.ant.messages.config.TxPowerMessage;
import org.cowboycoders.ant.messages.notifications.StartupMessage;
import org.cowboycoders.ant.messages.responses.Capability;
import org.cowboycoders.ant.messages.responses.CapabilityCategory;
import org.cowboycoders.ant.messages.responses.CapabilityResponse;
import org.cowboycoders.ant.messages.responses.Capability;
import org.cowboycoders.ant.messages.responses.Response;
import org.cowboycoders.ant.messages.responses.ResponseCode;
import org.cowboycoders.ant.messages.responses.ResponseExceptionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;

public class Node {

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

class ThreadedWait implements
Callable<MessageMetaWrapper<? extends StandardMessage>> {

Expand All @@ -82,8 +85,6 @@ public MessageMetaWrapper<? extends StandardMessage> call()

}

public final static Logger LOGGER = Logger.getLogger(EventMachine.class
.getName());
private Set<AntLogger> antLoggers = Collections
.newSetFromMap(new WeakHashMap<AntLogger, Boolean>());

Expand Down Expand Up @@ -130,7 +131,7 @@ public boolean test(StandardMessage msg) {

};

private boolean running = false;
private volatile boolean running = false;
private EventMachine evm;

/**
Expand All @@ -144,7 +145,7 @@ protected synchronized EventMachine getEvm() {
private Network[] networks = new Network[0];
private CapabilityResponse capabilities;
private AntChipInterface antChipInterface;
private BroadcastMessenger<AntStatusUpdate> mStatusMessenger = new BroadcastMessenger<AntStatusUpdate>();
private BroadcastMessenger<AntStatusUpdate> mStatusMessenger = new BroadcastMessenger<>();

private class StatusListener implements BroadcastListener<AntStatusUpdate> {

Expand All @@ -156,7 +157,7 @@ public void receiveMessage(AntStatusUpdate message) {
// message
// // NOTE: should we use the reset ANtInterface method?
//
// LOGGER.warning("Node: external reset");
// log.warn("Node: external reset");
}
// weReset = false;
}
Expand All @@ -175,7 +176,7 @@ public void receiveMessage(StandardMessage message) {
// we don't actually get a reset intent if we send a raw
// message
// NOTE: should we use the reset ANtInterface method?
LOGGER.warning("Node: external reset");
log.warn( "Node: external reset" );
}
weReset = false;
}
Expand Down Expand Up @@ -255,6 +256,7 @@ public synchronized void start() throws AntError {
init();

running = true;
log.info( "Node started." );
}

// private void registerChannels(boolean add) {
Expand Down Expand Up @@ -285,7 +287,7 @@ private CapabilityResponse getCapabilityResponse(final int maxRetries)
null, null);
} catch (TimeoutException e) {
e.printStackTrace();
LOGGER.warning("getCapabilityResponse : timeout");
log.warn( "getCapabilityResponse : timeout" );
retries++;
if (retries >= maxRetries) {
throw e;
Expand All @@ -297,7 +299,7 @@ private CapabilityResponse getCapabilityResponse(final int maxRetries)
}

private void init() {
LOGGER.finer("entering init");
log.trace( "entering init" );
// reset();
try {
// android interface automatically requests ant version response.
Expand All @@ -321,7 +323,7 @@ private void init() {
channels[i] = new Channel(this, i);
}

LOGGER.finer("exiting init");
log.trace( "exiting init" );
}

private void freeNetwork(int i) {
Expand Down Expand Up @@ -462,7 +464,7 @@ public synchronized int getMaxChannels() {
public List<MessageMetaWrapper<? extends StandardMessage>> send(
StandardMessage msg) {
MessageMetaWrapper<StandardMessage> sentMeta = Node.this.send(msg);
List<MessageMetaWrapper<? extends StandardMessage>> rtn = new ArrayList<MessageMetaWrapper<? extends StandardMessage>>(
List<MessageMetaWrapper<? extends StandardMessage>> rtn = new ArrayList<>(
1);
rtn.add(sentMeta);
return rtn;
Expand Down Expand Up @@ -555,7 +557,7 @@ public StandardMessage sendAndWaitWithAdapter(WaitAdapter waitAdapter,
* We wrap listeners to look for message of specific class - this is a map
* from the original to the new one
*/
private Map<Object, BroadcastListener<StandardMessage>> mAdapterListenerMap = new HashMap<Object, BroadcastListener<StandardMessage>>();
private Map<Object, BroadcastListener<StandardMessage>> mAdapterListenerMap = new HashMap<>();

public synchronized <V extends StandardMessage> void registerRxListener(
final BroadcastListener<V> listener, final Class<V> clazz) {
Expand Down Expand Up @@ -585,7 +587,7 @@ public synchronized <V extends StandardMessage> void removeRxListener(
if (adapter != null) {
evm.removeRxListener(adapter);
} else {
LOGGER.warning("removeRxListener: ignoring unknown listener");
log.warn( "removeRxListener: ignoring unknown listener" );
}

}
Expand Down Expand Up @@ -711,19 +713,19 @@ private void logMessage(AntLogger.Direction direction, StandardMessage msg) {
logger.log(data);
} catch (Exception e) {
// don't bring us down for the sake of logging
LOGGER.severe("error logging data");
log.error( "error logging data" );
}
}
}
}

public synchronized MessageMetaWrapper<StandardMessage> send(
StandardMessage msg) {
LOGGER.finer("sent: " + msg.toString() + " to chip");
log.debug( "sent: " + msg.toString() + " to chip" );
antChipInterface.send(msg.encode());
// now that we have sent, inform the loggers
logMessage(Direction.SENT, msg);
return new MessageMetaWrapper<StandardMessage>(msg);
return new MessageMetaWrapper<>(msg);
}

public synchronized void stop() {
Expand Down
Loading

0 comments on commit 0924c7b

Please sign in to comment.