diff --git a/src/main/java/com/veraxsystems/vxipmi/api/async/IpmiAsyncConnector.java b/src/main/java/com/veraxsystems/vxipmi/api/async/IpmiAsyncConnector.java index 5edb323..1847df3 100644 --- a/src/main/java/com/veraxsystems/vxipmi/api/async/IpmiAsyncConnector.java +++ b/src/main/java/com/veraxsystems/vxipmi/api/async/IpmiAsyncConnector.java @@ -14,8 +14,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import org.apache.log4j.Logger; @@ -110,9 +109,9 @@ public IpmiAsyncConnector(int port, InetAddress address) throws IOException { loadProperties(); } - private void loadProperties() throws IOException { + private void loadProperties() throws IOException { retries = Integer.parseInt(PropertiesManager.getInstance().getProperty("retries")); - } + } /** * Creates connection to the remote host. @@ -153,23 +152,13 @@ public List getAvailableCipherSuites( ++tries; result = connectionManager .getAvailableCipherSuites(connectionHandle.getHandle()); - } catch (IPMIException e) { - logger.warn("Failed to receive answer, cause:", e); - if (tries > retries) { - throw e; - } - } catch (StateConnectionException e) { - // state error, no retry - throw e; } catch (Exception e) { - if(e instanceof IOException) { - // Normal network error - logger.warn("Failed to receive answer, cause: " + e.getMessage()); - } else { - logger.warn("Failed to receive answer, cause:", e); - } + logger.warn("Failed to receive answer, cause:", e); if (tries > retries) { - throw e; + logger.warn("Failed to receive answer, cause: " + + e.getMessage()); + + return Arrays.asList(CipherSuite.getEmpty()); } } } @@ -177,11 +166,11 @@ public List getAvailableCipherSuites( } public List getAllCipherSuites( - ConnectionHandle connectionHandle) throws StateConnectionException { - List result = connectionManager - .getAllCipherSuites(connectionHandle.getHandle()); - return result; - } + ConnectionHandle connectionHandle) throws StateConnectionException { + List result = connectionManager + .getAllCipherSuites(connectionHandle.getHandle()); + return result; + } /** * Gets the authentication capabilities for the connection with the remote @@ -214,15 +203,15 @@ public GetChannelAuthenticationCapabilitiesResponseData getChannelAuthentication requestedPrivilegeLevel); connectionHandle.setCipherSuite(cipherSuite); connectionHandle.setPrivilegeLevel(requestedPrivilegeLevel); - } catch (StateConnectionException e) { - // state error, no retry + } catch (StateConnectionException e) { + // state error, no retry throw e; } catch (Exception e) { - if(e instanceof IOException) { + if (e instanceof IOException) { // Normal network error logger.warn("Failed to receive answer, cause:" + e.getMessage()); } else { - logger.warn("Failed to receive answer, cause:", e); + logger.warn("Failed to receive answer, cause:", e); } logger.warn("Failed to receive answer, cause:", e); if (tries > retries) { @@ -264,15 +253,15 @@ public void openSession(ConnectionHandle connectionHandle, String username, connectionHandle.getPrivilegeLevel(), username, password, bmcKey); succeded = true; - } catch (StateConnectionException e) { - // state error, no retry + } catch (StateConnectionException e) { + // state error, no retry throw e; } catch (Exception e) { - if(e instanceof IOException) { + if (e instanceof IOException) { // Normal network error logger.warn("Failed to receive answer, cause:" + e.getMessage()); } else { - logger.warn("Failed to receive answer, cause:", e); + logger.warn("Failed to receive answer, cause:", e); } if (tries > retries) { throw e; @@ -308,15 +297,15 @@ public void closeSession(ConnectionHandle connectionHandle) connectionManager.getConnection(connectionHandle.getHandle()) .closeSession(); succeded = true; - } catch (StateConnectionException e) { - // state error, no retry + } catch (StateConnectionException e) { + // state error, no retry throw e; } catch (Exception e) { - if(e instanceof IOException) { + if (e instanceof IOException) { // Normal network error logger.warn("Failed to receive answer, cause:" + e.getMessage()); } else { - logger.warn("Failed to receive answer, cause:", e); + logger.warn("Failed to receive answer, cause:", e); } if (tries > retries) { throw e; @@ -362,15 +351,15 @@ public int sendMessage(ConnectionHandle connectionHandle, } logger.debug("Sending message with tag " + tag + ", try " + tries); - } catch (StateConnectionException e) { - // state error, no retry + } catch (StateConnectionException e) { + // state error, no retry throw e; } catch (Exception e) { - if(e instanceof IOException) { + if (e instanceof IOException) { // Normal network error logger.warn("Failed to receive answer, cause:" + e.getMessage()); } else { - logger.warn("Failed to receive answer, cause:", e); + logger.warn("Failed to receive answer, cause:", e); } if (tries > retries) { throw e; @@ -444,15 +433,15 @@ public void closeConnection(ConnectionHandle handle) { public void tearDown() { connectionManager.close(); } - - /** - * Changes the timeout value for connection with the given handle. - * @param handle - * - {@link ConnectionHandle} associated with the remote host. - * @param timeout - * - new timeout value in ms - */ - public void setTimeout(ConnectionHandle handle, int timeout) { - connectionManager.getConnection(handle.getHandle()).setTimeout(timeout); - } + + /** + * Changes the timeout value for connection with the given handle. + * @param handle + * - {@link ConnectionHandle} associated with the remote host. + * @param timeout + * - new timeout value in ms + */ + public void setTimeout(ConnectionHandle handle, int timeout) { + connectionManager.getConnection(handle.getHandle()).setTimeout(timeout); + } } diff --git a/src/main/java/com/veraxsystems/vxipmi/coding/commands/sdr/GetSensorReading.java b/src/main/java/com/veraxsystems/vxipmi/coding/commands/sdr/GetSensorReading.java index f3dd426..ef2ec35 100644 --- a/src/main/java/com/veraxsystems/vxipmi/coding/commands/sdr/GetSensorReading.java +++ b/src/main/java/com/veraxsystems/vxipmi/coding/commands/sdr/GetSensorReading.java @@ -33,7 +33,7 @@ */ public class GetSensorReading extends IpmiCommandCoder { - private byte sensorId; + private final byte sensorId; /** * Initiates class for both encoding and decoding. @@ -67,7 +67,7 @@ public NetworkFunction getNetworkFunction() { @Override protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException { - byte[] payloadData = new byte[] { sensorId }; + byte[] payloadData = new byte[]{sensorId}; return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), payloadData, TypeConverter.intToByte(sequenceNumber % 64)); } @@ -92,10 +92,6 @@ public ResponseData getResponseData(IpmiMessage message) byte[] raw = message.getPayload().getIpmiCommandData(); - if (raw.length < 3) { - throw new IllegalArgumentException("Invalid response length"); - } - GetSensorReadingResponseData responseData = new GetSensorReadingResponseData(); responseData.setSensorReading(raw[0]); @@ -103,25 +99,25 @@ public ResponseData getResponseData(IpmiMessage message) responseData .setSensorStateValid((TypeConverter.byteToInt(raw[1]) & 0x20) == 0); - responseData.setSensorState(SensorState.parseInt((TypeConverter - .byteToInt(raw[2])) & 0x3f)); + if (raw.length > 2) + responseData.setSensorState(SensorState.parseInt((TypeConverter + .byteToInt(raw[2])) & 0x3f)); boolean[] states = null; - if (raw.length > 3) { + if (raw.length > 3) states = new boolean[16]; - } else { + else states = new boolean[8]; - } - for (int i = 0; i < 8; ++i) { - states[i] = (TypeConverter.byteToInt(raw[2]) & (0x1 << i)) != 0; - } + if (raw.length > 2) + for (int i = 0; i < 8; ++i) + states[i] = (TypeConverter.byteToInt(raw[2]) & (0x1 << i)) != 0; if (raw.length > 3) { - for (int i = 0; i < 7; ++i) { + for (int i = 0; i < 7; ++i) states[i + 8] = (TypeConverter.byteToInt(raw[3]) & (0x1 << i)) != 0; - } + states[15] = false; } diff --git a/src/main/java/com/veraxsystems/vxipmi/connection/Connection.java b/src/main/java/com/veraxsystems/vxipmi/connection/Connection.java index 3a1ac19..502cefe 100644 --- a/src/main/java/com/veraxsystems/vxipmi/connection/Connection.java +++ b/src/main/java/com/veraxsystems/vxipmi/connection/Connection.java @@ -99,10 +99,10 @@ public class Connection extends TimerTask implements MachineObserver { * - when properties file was not found */ public Connection(Messenger messenger, int handle) throws FileNotFoundException, IOException { - stateMachine = new StateMachine(messenger); - this.handle = handle; + stateMachine = new StateMachine(messenger); + this.handle = handle; timeout = Integer.parseInt(PropertiesManager.getInstance().getProperty("timeout")); - } + } public int getHandle() { return handle; @@ -260,11 +260,11 @@ public List getAllCipherSuites(int tag) throws StateConnectionExcep } stateMachine.doTransition(new GetChannelCipherSuitesPending(tag)); stateMachine.doTransition(new DefaultAck()); - CipherSuite[] allCs = new CipherSuite[] { - new CipherSuite((byte) 0,(byte) 0,(byte) 0,(byte) 0), - new CipherSuite((byte) 1,(byte) 1,(byte) 0,(byte) 0), - new CipherSuite((byte) 2,(byte) 1,(byte) 1,(byte) 0), - new CipherSuite((byte) 3,(byte) 1,(byte) 1,(byte) 1), + CipherSuite[] allCs = new CipherSuite[]{ + new CipherSuite((byte) 0, (byte) 0, (byte) 0, (byte) 0), + new CipherSuite((byte) 1, (byte) 1, (byte) 0, (byte) 0), + new CipherSuite((byte) 2, (byte) 1, (byte) 1, (byte) 0), + new CipherSuite((byte) 3, (byte) 1, (byte) 1, (byte) 1), new CipherSuite((byte) 4,(byte) 1,(byte) 1,(byte) 2), }; return Arrays.asList(allCs); @@ -318,7 +318,7 @@ public GetChannelAuthenticationCapabilitiesResponseData getChannelAuthentication PrivilegeLevel requestedPrivilegeLevel) throws Exception { if (!(stateMachine.getCurrent().getClass() == Ciphers.class)) { - throw new StateConnectionException(stateMachine.getCurrent()); + stateMachine.setCurrent(new Ciphers()); } lastAction = null; @@ -602,10 +602,10 @@ public void notify(StateMachineAction action) { sik = ((GetSikAction) action).getSik(); } else if (!(action instanceof MessageAction)) { lastAction = action; - if (action instanceof ErrorAction) { - ErrorAction errorAction = (ErrorAction) action; + if (action instanceof ErrorAction) { + ErrorAction errorAction = (ErrorAction) action; logger.error(errorAction.getException().getMessage(), errorAction.getException()); - } + } } else { handleIncomingMessage(((MessageAction) action).getIpmiv20Message()); }