Fawkes API Fawkes Development Version
interface_info.cpp
1
2/***************************************************************************
3 * interface_info.h - BlackBoard Interface Info
4 *
5 * Created: Mon Mar 03 15:44:46 2008
6 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <interface/interface.h>
25#include <interface/interface_info.h>
26#include <utils/misc/strndup.h>
27#include <utils/time/time.h>
28
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
32
33namespace fawkes {
34
35/** @class InterfaceInfo <interface/interface_info.h>
36 * Interface info.
37 * This class holds information about a specific interface.
38 * @author Tim Niemueller
39 */
40
41/** Constructor.
42 * @param type type of the interface
43 * @param id id of the interface
44 * @param hash version hash
45 * @param has_writer true if there is a writer, false otherwise
46 * @param num_readers number of readers
47 * @param serial instance serial
48 * @param readers name of readers of interface
49 * @param writer name of writer of interface
50 * @param timestamp interface timestamp (time of last write or data timestamp)
51 */
53 const char * id,
54 const unsigned char * hash,
55 unsigned int serial,
56 bool has_writer,
57 unsigned int num_readers,
58 const std::list<std::string> &readers,
59 const std::string & writer,
60 const Time * timestamp)
61{
62 type_ = strndup(type, INTERFACE_TYPE_SIZE_);
63 id_ = strndup(id, INTERFACE_ID_SIZE_);
64 hash_ = (unsigned char *)malloc(INTERFACE_HASH_SIZE_);
65 memcpy(hash_, hash, INTERFACE_HASH_SIZE_);
66 has_writer_ = has_writer;
67 num_readers_ = num_readers;
68 serial_ = serial;
69 timestamp_ = new Time(timestamp);
70 readers_ = readers;
71 writer_ = writer;
72}
73
74/** Copy constructor.
75 * @param i info to copy
76 */
78{
79 type_ = strndup(i.type_, INTERFACE_TYPE_SIZE_);
80 id_ = strndup(i.id_, INTERFACE_ID_SIZE_);
81 hash_ = (unsigned char *)malloc(INTERFACE_HASH_SIZE_);
82 memcpy(hash_, i.hash_, INTERFACE_HASH_SIZE_);
83 has_writer_ = i.has_writer_;
84 num_readers_ = i.num_readers_;
85 serial_ = i.serial_;
86 timestamp_ = new Time(i.timestamp_);
87 readers_ = i.readers_;
88 writer_ = i.writer_;
89}
90
91/** Destructor. */
93{
94 free(type_);
95 free(id_);
96 free(hash_);
97 delete timestamp_;
98}
99
100/** Assignment operator.
101 * @param i info to copy from
102 * @return reference to this instance
103 */
106{
107 free(type_);
108 free(id_);
109 free(hash_);
110 delete timestamp_;
111
112 type_ = strndup(i.type_, INTERFACE_TYPE_SIZE_);
113 id_ = strndup(i.id_, INTERFACE_ID_SIZE_);
114 hash_ = (unsigned char *)malloc(INTERFACE_HASH_SIZE_);
115 memcpy(hash_, i.hash_, INTERFACE_HASH_SIZE_);
116 has_writer_ = i.has_writer_;
117 num_readers_ = i.num_readers_;
118 serial_ = i.serial_;
119 timestamp_ = new Time(i.timestamp_);
120 readers_ = i.readers_;
121 writer_ = i.writer_;
122
123 return *this;
124}
125
126/** Get interface type.
127 * @return type string
128 */
129const char *
131{
132 return type_;
133}
134
135/** Get interface ID.
136 * @return ID string
137 */
138const char *
140{
141 return id_;
142}
143
144/** Get interface version hash.
145 * @return interface version hash
146 */
147const unsigned char *
149{
150 return hash_;
151}
152
153/** Get interface version hash in printable format.
154 * @return interface version hash printable string
155 */
156std::string
158{
159 char phash[INTERFACE_HASH_SIZE_ * 2 + 1];
160 phash[INTERFACE_HASH_SIZE_ * 2] = 0;
161 for (size_t s = 0; s < INTERFACE_HASH_SIZE_; ++s) {
162 snprintf(&phash[s * 2], 3, "%02X", hash_[s]);
163 }
164 return std::string(phash);
165}
166
167/** Check if there is a writer.
168 * @return true if there is a writer, false otherwise
169 */
170bool
172{
173 return has_writer_;
174}
175
176/** Get number of readers.
177 * @return number of readers
178 */
179unsigned int
181{
182 return num_readers_;
183}
184
185/** Get readers of interface.
186 * @return string of names of readers of this interface
187 */
188const std::list<std::string> &
190{
191 return readers_;
192}
193
194/** Get name of writer on interface.
195 * @return name of writer owner or empty string of no writer or unknown
196 */
197const std::string &
199{
200 return writer_;
201}
202
203/** Get interface instance serial.
204 * @return type string
205 */
206unsigned int
208{
209 return serial_;
210}
211
212/** Get interface timestamp.
213 * @return point to interface last update time
214 */
215const Time *
217{
218 return timestamp_;
219}
220
221/** < operator
222 * This compares two interface infos with respect to the less than (<) relation
223 * considering the type and id of an interface.
224 * An interface info A is less than an interface info B (A < B) iff
225 * (A.type < B.type) or ((A.type == B.type) && A.id < B.id).
226 * @param ii interface info to compare this to
227 * @return true if this instance is considered less than @p ii, false otherwise
228 */
229bool
231{
232 int td = strncmp(type_, ii.type_, INTERFACE_TYPE_SIZE_);
233 if (td < 0) {
234 return true;
235 } else if (td > 0) {
236 return false;
237 } else {
238 return (strncmp(id_, ii.id_, INTERFACE_ID_SIZE_) < 0);
239 }
240}
241
242/** @class InterfaceInfoList <interface/interface_info.h>
243 * Interface information list.
244 * List with InterfaceInfo instances.
245 * @author Tim Niemueller
246 */
247
248/** Append an interface info.
249 * @param type type of the interface
250 * @param id id of the interface
251 * @param hash version hash
252 * @param has_writer true if there is a writer, false otherwise
253 * @param num_readers number of readers
254 * @param serial instance serial
255 * @param readers name of readers of interface
256 * @param writer name of writer of interface
257 * @param timestamp interface timestamp (time of last write or data timestamp)
258 */
259void
261 const char * id,
262 const unsigned char * hash,
263 unsigned int serial,
264 bool has_writer,
265 unsigned int num_readers,
266 const std::list<std::string> &readers,
267 const std::string & writer,
268 const Time & timestamp)
269{
270 push_back(
271 InterfaceInfo(type, id, hash, serial, has_writer, num_readers, readers, writer, &timestamp));
272}
273
274} // end namespace fawkes
void append(const char *type, const char *id, const unsigned char *hash, unsigned int serial, bool has_writer, unsigned int num_readers, const std::list< std::string > &readers, const std::string &writer, const Time &timestamp)
Append an interface info.
Interface info.
const std::list< std::string > & readers() const
Get readers of interface.
bool has_writer() const
Check if there is a writer.
unsigned int serial() const
Get interface instance serial.
bool operator<(const InterfaceInfo &ii) const
< operator This compares two interface infos with respect to the less than (<) relation considering t...
const char * type() const
Get interface type.
InterfaceInfo & operator=(const InterfaceInfo &i)
Assignment operator.
const char * id() const
Get interface ID.
InterfaceInfo(const char *type, const char *id, const unsigned char *hash, unsigned int serial, bool has_writer, unsigned int num_readers, const std::list< std::string > &readers, const std::string &writer, const Time *timestamp)
Constructor.
~InterfaceInfo()
Destructor.
const std::string & writer() const
Get name of writer on interface.
const unsigned char * hash() const
Get interface version hash.
unsigned int num_readers() const
Get number of readers.
std::string hash_printable() const
Get interface version hash in printable format.
const Time * timestamp() const
Get interface timestamp.
A class for handling time.
Definition: time.h:93
Fawkes library namespace.