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.
  • Field Details

  • 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()
    • getManagedSystemSessionId

      public int getManagedSystemSessionId()
      Returns:
      the managed system session ID of the last session opened, which the BMC generated
    • 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 the connection cannot be created
      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 the connection cannot be created
      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). Sends Get Channel Authentication Capabilities first, in the IPMI v1.5 format at the User level (IPMI 2.0 section 13.13), and Get Channel Cipher Suites, an RMCP+ request, only when its reply reports IPMI v2.0 connections (table 22-15).
      Parameters:
      tag - - the integer from range 0-63 to match request with response
      Returns:
      list of the CipherSuites supported by the managed system.
      Throws:
      Ipmi20UnsupportedException - when the BMC does not support IPMI 2.0 (RMCP+)
      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
      Declares the cipher suite and the privilege of the session, and returns the authentication capabilities of the managed system that getAvailableCipherSuites(int), which must be performed first, read. Sends nothing.
      Parameters:
      tag - - unused: the reply was read by getAvailableCipherSuites(int)
      cipherSuite - - CipherSuite requested for the session
      requestedPrivilegeLevel - - PrivilegeLevel requested for the session
      Returns:
      GetChannelAuthenticationCapabilitiesResponseData, requested at the User level
      Throws:
      ConnectionException - when connection is in the state that does not allow to perform this operation.
      Exception - when the state machine fails
    • startSession

      public int startSession(int tag, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel, String username, byte[] 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 handleId, int tag, ResponseData responseData, Exception exception)
    • notifyRequestListeners

      public void notifyRequestListeners(IpmiPayload payload)
      Notifies the listeners of a payload the remote system sent on its own initiative, with the handle of this connection.
      Parameters:
      payload - the payload received
    • 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 a no-op message to keep the session up: a Get Device ID (as ipmitool sends) sent one-way, whose reply is discarded unread and whose timeout is not reported to the listeners. When the message queue is full, the keep-alive of this period is skipped.
      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)