Class Connection

java.lang.Object
java.util.TimerTask
org.metricshub.ipmi.core.connection.Connection
All Implemented Interfaces:
Runnable, MachineObserver

public class Connection extends TimerTask implements MachineObserver
A connection with the specific remote host.
  • Constructor Details

    • Connection

      public Connection(Messenger messenger, int handle)
      Creates the connection.
      Parameters:
      messenger - - Messenger associated with the proper Constants.IPMI_PORT
      handle - - id of the connection
  • Method Details

    • getHandle

      public int getHandle()
    • getTimeout

      public int getTimeout()
    • setTimeout

      public void setTimeout(int timeout)
    • registerListener

      public void registerListener(ConnectionListener listener)
      Registers the listener so it will receive notifications from this connection
      Parameters:
      listener - - ConnectionListener to processResponse
    • unregisterListener

      public void unregisterListener(ConnectionListener listener)
      Unregisters the ConnectionListener
      Parameters:
      listener - ConnectionListener to unregister
    • connect

      public void connect(InetAddress address, int port, long pingPeriod) throws IOException
      Starts the connection to the specified InetAddress
      Parameters:
      address - - IP address of the managed system
      pingPeriod - - frequency of the no-op commands that will be sent to keep up the session
      Throws:
      IOException - - when properties file was not found
      FileNotFoundException - - when properties file was not found
      See Also:
    • connect

      public void connect(InetAddress address, int port, long pingPeriod, boolean skipCiphers) throws IOException
      Starts the connection to the specified InetAddress
      Parameters:
      address - - IP address of the managed system
      pingPeriod - - frequency of the no-op commands that will be sent to keep up the session
      skipCiphers - - determines if the getAvailableCipherSuites and getChannelAuthenticationCapabilities phases should be skipped
      Throws:
      IOException - - when properties file was not found
      FileNotFoundException - - when properties file was not found
      See Also:
    • disconnect

      public void disconnect()
      Ends the connection.
      See Also:
    • isActive

      public boolean isActive()
      Checks if the connection is active
      Returns:
      true if the connection is active, false otherwise
      See Also:
    • getAvailableCipherSuites

      public List<CipherSuite> getAvailableCipherSuites(int tag) throws Exception
      Gets from the managed system supported CipherSuites. Should be performed only immediately after connect(InetAddress, int, int).
      Parameters:
      tag - - the integer from range 0-63 to match request with response
      Returns:
      list of the CipherSuites supported by the managed system.
      Throws:
      ConnectionException - when connection is in the state that does not allow to perform this operation.
      Exception - when sending message to the managed system fails
    • getChannelAuthenticationCapabilities

      public GetChannelAuthenticationCapabilitiesResponseData getChannelAuthenticationCapabilities(int tag, CipherSuite cipherSuite, PrivilegeLevel requestedPrivilegeLevel) throws Exception
      Queries the managed system for the details of the authentification process. Must be performed after getAvailableCipherSuites(int)
      Parameters:
      tag - - the integer from range 0-63 to match request with response
      cipherSuite - - CipherSuite requested for the session
      requestedPrivilegeLevel - - PrivilegeLevel requested for the session
      Returns:
      GetChannelAuthenticationCapabilitiesResponseData
      Throws:
      ConnectionException - when connection is in the state that does not allow to perform this operation.
      Exception - when sending message to the managed system fails
    • startSession

      public int startSession(int tag, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel, String username, String password, byte[] bmcKey) throws Exception
      Initiates the session with the managed system. Must be performed after getChannelAuthenticationCapabilities(int, CipherSuite, PrivilegeLevel) or closeSession()
      Parameters:
      tag - - the integer from range 0-63 to match request with response
      cipherSuite - - CipherSuite that will be used during the session
      privilegeLevel - - requested PrivilegeLevel - most of the time it will be PrivilegeLevel.User
      username - - the username
      password - - the password matching the username
      bmcKey - - the key that should be provided if the two-key authentication is enabled, null otherwise.
      Returns:
      id of the new session
      Throws:
      ConnectionException - when connection is in the state that does not allow to perform this operation.
      Exception - when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
    • closeSession

      public void closeSession() throws ConnectionException
      Closes the session. Can be performed only if the session is already open.
      Throws:
      ConnectionException - when connection is in the state that does not allow to perform this operation.
    • sendMessage

      public int sendMessage(PayloadCoder payloadCoder, boolean isOneWay) throws ConnectionException
      Attempts to send IPMI request to the managed system.
      Parameters:
      payloadCoder - - PayloadCoder representing the request
      isOneWay - - tells whether message is one way or not
      Returns:
      ID of the message that will be also attached to the response to pair request with response if queue was not full and message was sent, -1 if sending of the message failed.
      Throws:
      ConnectionException - when connection isn't in state where sending commands is allowed
      ArithmeticException - when Connection runs out of available ID's for the messages. If this happens session needs to be restarted.
    • retry

      public int retry(int tag, PayloadType messagePayloadType) throws ConnectionException
      Attempts to retry sending a message.
      Parameters:
      tag - - tag of the message to retry
      messagePayloadType - - PayloadType of the message that should be retried
      Returns:
      new tag if message was retried, -1 if operation failed
      Throws:
      ConnectionException - when connection isn't in state where sending commands is allowed
    • notifyResponseListeners

      public void notifyResponseListeners(int handle, int tag, ResponseData responseData, Exception exception)
    • notifyRequestListeners

      public void notifyRequestListeners(IpmiPayload payload)
    • notify

      public void notify(StateMachineAction action)
      Description copied from interface: MachineObserver
      Notifies observer of action performed by the State Machine.
      Specified by:
      notify in interface MachineObserver
      Parameters:
      action - - action performed
    • run

      public void run()
      TimerTask runner - periodically sends no-op messages to keep the session up
      Specified by:
      run in interface Runnable
      Specified by:
      run in class TimerTask
    • getRemoteMachineAddress

      public InetAddress getRemoteMachineAddress()
    • getRemoteMachinePort

      public int getRemoteMachinePort()
    • isSessionValid

      public boolean isSessionValid()
      Checks if session is currently open.
    • getNextSessionSequenceNumber

      public int getNextSessionSequenceNumber()
    • getDefaultCipherSuite

      public static CipherSuite getDefaultCipherSuite()
      Returns:
      Default cipher suite (3)