1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
Aseba - an event-based framework for distributed robot control
Copyright (C) 2007--2016:
Stephane Magnenat <stephane at magnenat dot net>
(http://stephane.magnenat.net)
and other contributors, see authors.txt for details
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <dashel/dashel.h>
#include "zeroconf.h"
using namespace std;
namespace Aseba
{
//! TXT data for an Aseba target, with node ids and product ids
Zeroconf::TxtRecord::TxtRecord(const unsigned int protovers, const std::string& type, bool busy, const std::vector<unsigned int>& ids, const std::vector<unsigned int>& pids)
{
assign("txtvers", 1);
assign("protovers", int(protovers));
assign("type", type);
assign("busy", busy);
assign("ids", ids);
assign("pids", pids);
}
//! TXT data for an Aseba target, deserialized from a TXT record string
Zeroconf::TxtRecord::TxtRecord(const std::string & txtRecord)
{
istringstream txt{txtRecord};
do
{
char length;
txt >> length;
string data;
txt >> setw(int(length)) >> data;
auto key_pos = data.find('=');
fields[data.substr(0, key_pos)] = (key_pos==std::string::npos) ? "" : data.substr(key_pos+1);
} while (txt.peek() != std::char_traits<char>::eof());
}
//! TXT data for an Aseba target, deserialized from a TXT record buffer
Zeroconf::TxtRecord::TxtRecord(const unsigned char * txtRecord, uint16_t txtLen)
{
size_t pos = 0;
while (pos < txtLen)
{
size_t length{size_t(txtRecord[pos++])};
string data{reinterpret_cast<const char*>(txtRecord)+pos, min(length,txtLen-pos)};
auto key_pos = data.find('=');
fields[data.substr(0, key_pos)] = (key_pos==std::string::npos) ? "" : data.substr(key_pos+1);
pos += length;
}
}
//! A string value in the TXT record is a sequence of bytes
void Zeroconf::TxtRecord::assign(const std::string& key, const std::string& value)
{
fields[key] = value.substr(0,20); // silently truncate name to 20 characters
}
//! A simple integer value in the TXT record is the string of its decimal value
void Zeroconf::TxtRecord::assign(const std::string& key, const int value)
{
ostringstream field;
field << value;
assign(key, field.str());
}
//! A vector of integers in the TXT record is a sequence of big-endian 16-bit values
//! note that the size of the vector is implicit in the record length: (record.length()-record.find("=")-1)/2.
void Zeroconf::TxtRecord::assign(const std::string& key, const std::vector<unsigned int>& values)
{
ostringstream field;
for (const auto &value : values)
field.put(value<<8), field.put(value % 0xff);
assign(key, field.str());
}
//! A boolean value in the TXT record is either the key (true) or nothing (false)
void Zeroconf::TxtRecord::assign(const std::string& key, const bool value)
{
if (value)
fields[key] = ""; // length will be 0, so no "=" in the record
}
//! Retrieve the encoded TXT record.
//! A serialized TXT record is composed of length-prefixed strings of the form KEY[=VALUE]
string Zeroconf::TxtRecord::record() const
{
ostringstream txt;
serializeField(txt, "txtvers");
serializeField(txt, "protovers");
serializeField(txt, "type");
serializeField(txt, "busy");
serializeField(txt, "ids");
serializeField(txt, "pids");
return txt.str();
}
//!< serialize a field into the encoded TXT record
void Zeroconf::TxtRecord::serializeField(ostringstream& txt, const std::string& key) const
{
if (fields.find(key) != fields.end()) // key will be absent if it was boolean and false
{
string record{key};
if (fields.at(key).length() > 0)
record += "=" + fields.at(key);
txt.put(record.length());
txt << record;
}
}
} // namespace Aseba
|