| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU Library General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
* |
| 16 |
* PortBroker.cpp |
| 17 |
* Acts as the glue between ports and the RDM request path. |
| 18 |
* Copyright (C) 2010 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <set> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
#include "ola/Callback.h" |
| 25 |
#include "ola/Logging.h" |
| 26 |
#include "olad/PortBroker.h" |
| 27 |
|
| 28 |
namespace ola { |
| 29 |
|
| 30 |
using std::pair; |
| 31 |
using std::set; |
| 32 |
using std::string; |
| 33 |
using std::vector; |
| 34 |
|
| 35 |
/** |
| 36 |
* Add a port to the broker |
| 37 |
*/ |
| 38 |
void PortBroker::AddPort(const Port *port) { |
| 39 |
port_key key(port->UniqueId(), port); |
| 40 |
m_ports.insert(key); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Remove a port from the broker |
| 46 |
*/ |
| 47 |
void PortBroker::RemovePort(const Port *port) { |
| 48 |
port_key key(port->UniqueId(), port); |
| 49 |
m_ports.erase(key); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Make an RDM call |
| 55 |
* @param port the OlaPortService that should exist when the call returns |
| 56 |
* @param universe the universe to send the RDM request on |
| 57 |
* @param request the RDM request |
| 58 |
* @param callback the callback to run when the request completes |
| 59 |
*/ |
| 60 |
void PortBroker::SendRDMRequest(const Port *port, |
| 61 |
Universe *universe, |
| 62 |
ola::rdm::RDMRequest *request, |
| 63 |
ola::rdm::RDMCallback *callback) { |
| 64 |
port_key key(port->UniqueId(), port); |
| 65 |
set<port_key>::const_iterator iter = m_ports.find(key); |
| 66 |
if (iter == m_ports.end()) |
| 67 |
OLA_WARN << |
| 68 |
"Making an RDM call but the port doesn't exist in the broker!"; |
| 69 |
|
| 70 |
universe->SendRDMRequest( |
| 71 |
request, |
| 72 |
NewSingleCallback(this, |
| 73 |
&PortBroker::RequestComplete, |
| 74 |
key, |
| 75 |
callback)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Return from an RDM call |
| 81 |
* @param key the port associated with this request |
| 82 |
* @param callback the callback to run if the key still exists |
| 83 |
* @param status the status of the RDM request |
| 84 |
* @param response the RDM response |
| 85 |
*/ |
| 86 |
void PortBroker::RequestComplete(port_key key, |
| 87 |
ola::rdm::RDMCallback *callback, |
| 88 |
ola::rdm::RDMReply *reply) { |
| 89 |
set<port_key>::const_iterator iter = m_ports.find(key); |
| 90 |
if (iter == m_ports.end()) { |
| 91 |
OLA_INFO << "Port no longer exists, cleaning up from RDM response"; |
| 92 |
delete callback; |
| 93 |
} else { |
| 94 |
callback->Run(reply); |
| 95 |
} |
| 96 |
} |
| 97 |
} // namespace ola |