Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

[Cul] Fix formatting and synchronization issues #5095

Merged
merged 3 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public void open() {
try {
cul = culManager.getOpenCULHandler(config);
} catch (CULDeviceException e) {
logger.error("Can't open CUL", e);
logger.warn("Can't open CUL", e);
}

try {
listener.open(cul);
} catch (CULCommunicationException e) {
logger.error("Can't set parameters", e);
logger.warn("Can't start listener", e);
cul = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void run() {
try {
writeMessage(command);
} catch (CULCommunicationException e) {
logger.error("Error while writing command to CUL", e);
logger.warn("Error while writing command to CUL", e);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @author Till Klocke
* @since 1.5.0
*/
public class CULNetworkHandlerImpl extends AbstractCULHandler<CULNetworkConfig>implements Runnable {
public class CULNetworkHandlerImpl extends AbstractCULHandler<CULNetworkConfig> implements Runnable {

private static final int CUN_DEFAULT_PORT = 2323;

Expand Down Expand Up @@ -118,9 +118,9 @@ protected void write(String command) {
try {
send(buf);
} catch (InterruptedException e) {
logger.error("InterruptedException when sending command", e);
logger.warn("InterruptedException when sending command", e);
} catch (IOException e) {
logger.error("IOException when sending command", e);
logger.warn("IOException when sending command", e);
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ private void configureChannel(SocketChannel channel) throws IOException {

@Override
public void run() {
logger.info("event loop running");
logger.info("event loop starting");
try {
while (!Thread.interrupted()) { // reconnection loop
try {
Expand All @@ -199,7 +199,7 @@ public void run() {
}
}
} catch (Exception e) {
logger.error("exception", e);
logger.warn("exception", e);
} finally {
connected.set(false);
onDisconnected();
Expand All @@ -225,7 +225,7 @@ public void run() {
}
}
} catch (Exception e) {
logger.error("unrecoverable error", e);
logger.warn("unrecoverable error", e);
}

logger.info("event loop terminated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
* @author Till Klocke
* @since 1.4.0
*/
public class CULSerialHandlerImpl extends AbstractCULHandler<CULSerialConfig>implements SerialPortEventListener {
public class CULSerialHandlerImpl extends AbstractCULHandler<CULSerialConfig> implements SerialPortEventListener {

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

private SerialPort serialPort;

Expand All @@ -62,16 +62,16 @@ public CULSerialHandlerImpl(CULConfig config) {
@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
synchronized (br) {
synchronized (serialPort) {
try {
if (br == null) {
log.error("BufferedReader for serial connection is null");
logger.warn("BufferedReader for serial connection is null");
} else {
String line = br.readLine();
processNextLine(line);
}
} catch (IOException e) {
log.error("Can't read from serial device {}", config.getDeviceName(), e);
logger.warn("Can't read from serial device {}", config.getDeviceName(), e);
tryReopenHardware();
}
}
Expand All @@ -81,7 +81,7 @@ public void serialEvent(SerialPortEvent event) {
@Override
protected void openHardware() throws CULDeviceException {
String deviceName = config.getDeviceAddress();
log.debug("Opening serial CUL connection for " + deviceName);
logger.debug("Opening serial CUL connection for {}", deviceName);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(deviceName);
if (portIdentifier.isCurrentlyOwned()) {
Expand All @@ -98,15 +98,13 @@ protected void openHardware() throws CULDeviceException {
config.getParityMode());
InputStream is = serialPort.getInputStream();
OutputStream os = serialPort.getOutputStream();
synchronized (br) {
synchronized (serialPort) {
br = new BufferedReader(new InputStreamReader(is));
}
synchronized (bw) {
bw = new BufferedWriter(new OutputStreamWriter(os));
}

serialPort.notifyOnDataAvailable(true);
log.debug("Adding serial port event listener");
logger.debug("Adding serial port event listener");
serialPort.addEventListener(this);
} catch (NoSuchPortException e) {
throw new CULDeviceException(e);
Expand All @@ -124,7 +122,7 @@ protected void openHardware() throws CULDeviceException {

@Override
protected void closeHardware() {
log.debug("Closing serial device " + config.getDeviceAddress());
logger.debug("Closing serial device {}", config.getDeviceAddress());
if (serialPort != null) {
serialPort.removeEventListener();
}
Expand All @@ -136,7 +134,7 @@ protected void closeHardware() {
bw.close();
}
} catch (IOException e) {
log.error("Can't close the input and output streams propberly", e);
logger.warn("Can't close the input and output streams properly", e);
} finally {
if (serialPort != null) {
serialPort.close();
Expand All @@ -149,27 +147,24 @@ private void tryReopenHardware() {
try {
openHardware();
} catch (CULDeviceException e) {
log.error("Failed to reopen serial connection after connection error", e);
logger.warn("Failed to reopen serial connection after connection error", e);
}
}

@Override
protected void write(String command) {

try {
synchronized (bw) {
synchronized (serialPort) {
if (bw == null) {
log.error("BufferedWriter for serial connection is null");
logger.warn("BufferedWriter for serial connection is null");
} else {
bw.write(command);
bw.flush();
}
}
} catch (IOException e) {
log.error("Can't write to serial device {}", config.getDeviceName(), e);
logger.warn("Can't write to serial device {}", config.getDeviceName(), e);
tryReopenHardware();
}

}

}