| 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 |
* Client.cpp |
| 17 |
* Represents a connected client. |
| 18 |
* Copyright (C) 2005 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <map> |
| 22 |
#include <utility> |
| 23 |
#include "common/protocol/Ola.pb.h" |
| 24 |
#include "common/protocol/OlaService.pb.h" |
| 25 |
#include "ola/Callback.h" |
| 26 |
#include "ola/Logging.h" |
| 27 |
#include "ola/rdm/UID.h" |
| 28 |
#include "ola/stl/STLUtils.h" |
| 29 |
#include "olad/plugin_api/Client.h" |
| 30 |
|
| 31 |
namespace ola { |
| 32 |
|
| 33 |
using ola::rdm::UID; |
| 34 |
using ola::rpc::RpcController; |
| 35 |
using std::map; |
| 36 |
|
| 37 |
Client::Client(ola::proto::OlaClientService_Stub *client_stub, |
| 38 |
const ola::rdm::UID &uid) |
| 39 |
: m_client_stub(client_stub), |
| 40 |
m_uid(uid) { |
| 41 |
} |
| 42 |
|
| 43 |
Client::~Client() { |
| 44 |
m_data_map.clear(); |
| 45 |
} |
| 46 |
|
| 47 |
bool Client::SendDMX(unsigned int universe, uint8_t priority, |
| 48 |
const DmxBuffer &buffer) { |
| 49 |
if (!m_client_stub.get()) { |
| 50 |
OLA_FATAL << "client_stub is null"; |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
RpcController *controller = new RpcController(); |
| 55 |
ola::proto::DmxData dmx_data; |
| 56 |
ola::proto::Ack *ack = new ola::proto::Ack(); |
| 57 |
|
| 58 |
dmx_data.set_priority(priority); |
| 59 |
dmx_data.set_universe(universe); |
| 60 |
dmx_data.set_data(buffer.Get()); |
| 61 |
|
| 62 |
m_client_stub->UpdateDmxData( |
| 63 |
controller, |
| 64 |
&dmx_data, |
| 65 |
ack, |
| 66 |
ola::NewSingleCallback(this, &ola::Client::SendDMXCallback, |
| 67 |
controller, ack)); |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
void Client::DMXReceived(unsigned int universe, const DmxSource &source) { |
| 72 |
STLReplace(&m_data_map, universe, source); |
| 73 |
} |
| 74 |
|
| 75 |
const DmxSource Client::SourceData(unsigned int universe) const { |
| 76 |
map<unsigned int, DmxSource>::const_iterator iter = |
| 77 |
m_data_map.find(universe); |
| 78 |
|
| 79 |
if (iter != m_data_map.end()) { |
| 80 |
return iter->second; |
| 81 |
} else { |
| 82 |
DmxSource source; |
| 83 |
return source; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
ola::rdm::UID Client::GetUID() const { |
| 88 |
return m_uid; |
| 89 |
} |
| 90 |
|
| 91 |
void Client::SetUID(const ola::rdm::UID &uid) { |
| 92 |
m_uid = uid; |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* Called when UpdateDmxData completes. |
| 97 |
*/ |
| 98 |
void Client::SendDMXCallback(RpcController *controller, |
| 99 |
ola::proto::Ack *reply) { |
| 100 |
delete controller; |
| 101 |
delete reply; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
} // namespace ola |