View Javadoc
1   package org.metricshub.ipmi.core.connection;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import org.metricshub.ipmi.core.coding.PayloadCoder;
26  import org.metricshub.ipmi.core.coding.commands.ResponseData;
27  import org.metricshub.ipmi.core.coding.commands.session.GetChannelAuthenticationCapabilities;
28  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanMessage;
29  import org.metricshub.ipmi.core.coding.protocol.Ipmiv20Message;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.io.IOException;
35  
36  /**
37   * Default implementation of {@link MessageHandler} for {@link IpmiLanMessage}s.
38   */
39  public class IpmiMessageHandler extends MessageHandler {
40  
41      private static final Logger logger = LoggerFactory.getLogger(IpmiMessageHandler.class);
42  
43      public IpmiMessageHandler(Connection connection, int timeout) throws IOException {
44          super(connection, timeout, IpmiLanMessage.MIN_SEQUENCE_NUMBER, IpmiLanMessage.MAX_SEQUENCE_NUMBER);
45      }
46  
47      /**
48       * If message is of type {@link IpmiLanMessage}, finds corresponding request message and extracts response data from it,
49       * using data carried in this message.
50       *
51       * @param message
52       *          received IPMI message
53       */
54      @Override
55      protected void handleIncomingMessageInternal(Ipmiv20Message message) {
56          if (message.getPayload() instanceof IpmiLanMessage) {
57              IpmiLanMessage lanMessagePayload = (IpmiLanMessage) message.getPayload();
58  
59              PayloadCoder coder = messageQueue.getMessageFromQueue(lanMessagePayload.getSequenceNumber());
60              int tag = lanMessagePayload.getSequenceNumber();
61  
62              logger.debug("Received message with tag " + tag);
63  
64              if (coder == null) {
65                  logger.debug("No message tagged with " + tag
66                          + " in queue. Dropping orphan message.");
67                  return;
68              }
69  
70              if (coder.getClass() == GetChannelAuthenticationCapabilities.class) {
71                  messageQueue.remove(tag);
72              } else {
73  
74                  try {
75                      ResponseData responseData = coder.getResponseData(message);
76                      connection.notifyResponseListeners(connection.getHandle(), tag, responseData, null);
77                  } catch (Exception e) {
78                      connection.notifyResponseListeners(connection.getHandle(), tag, null, e);
79                  }
80                  messageQueue.remove(lanMessagePayload.getSequenceNumber());
81              }
82          }
83      }
84  
85  }