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_info.h>
25 #include <interface/interface.h>
26 #include <utils/misc/strndup.h>
27 #include <utils/time/time.h>
28 
29 #include <cstdlib>
30 #include <cstring>
31 #include <cstdio>
32 
33 namespace 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  */
52 InterfaceInfo::InterfaceInfo(const char *type, const char *id, const unsigned char *hash,
53  unsigned int serial, bool has_writer, unsigned int num_readers,
54  const std::list<std::string> &readers, const std::string &writer,
55  const Time *timestamp)
56 {
57  __type = strndup(type, __INTERFACE_TYPE_SIZE);
58  __id = strndup(id, __INTERFACE_ID_SIZE);
59  __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
60  memcpy(__hash, hash, __INTERFACE_HASH_SIZE);
61  __has_writer = has_writer;
62  __num_readers = num_readers;
63  __serial = serial;
64  __timestamp = new Time(timestamp);
65  __readers = readers;
66  __writer = writer;
67 }
68 
69 
70 /** Copy constructor.
71  * @param i info to copy
72  */
74 {
75  __type = strndup(i.__type, __INTERFACE_TYPE_SIZE);
76  __id = strndup(i.__id, __INTERFACE_ID_SIZE);
77  __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
78  memcpy(__hash, i.__hash, __INTERFACE_HASH_SIZE);
79  __has_writer = i.__has_writer;
80  __num_readers = i.__num_readers;
81  __serial = i.__serial;
82  __timestamp = new Time(i.__timestamp);
83  __readers = i.__readers;
84  __writer = i.__writer;
85 }
86 
87 
88 /** Destructor. */
90 {
91  free(__type);
92  free(__id);
93  free(__hash);
94  delete __timestamp;
95 }
96 
97 
98 /** Get interface type.
99  * @return type string
100  */
101 const char *
103 {
104  return __type;
105 }
106 
107 
108 /** Get interface ID.
109  * @return ID string
110  */
111 const char *
113 {
114  return __id;
115 }
116 
117 
118 /** Get interface version hash.
119  * @return interface version hash
120  */
121 const unsigned char *
123 {
124  return __hash;
125 }
126 
127 /** Get interface version hash in printable format.
128  * @return interface version hash printable string
129  */
130 std::string
132 {
133  char phash[__INTERFACE_HASH_SIZE * 2 + 1];
134  phash[__INTERFACE_HASH_SIZE * 2] = 0;
135  for (size_t s = 0; s < __INTERFACE_HASH_SIZE; ++s) {
136  snprintf(&phash[s*2], 3, "%02X", __hash[s]);
137  }
138  return std::string(phash);
139 }
140 
141 
142 /** Check if there is a writer.
143  * @return true if there is a writer, false otherwise
144  */
145 bool
147 {
148  return __has_writer;
149 }
150 
151 
152 /** Get number of readers.
153  * @return number of readers
154  */
155 unsigned int
157 {
158  return __num_readers;
159 }
160 
161 
162 /** Get readers of interface.
163  * @return string of names of readers of this interface
164  */
165 const std::list<std::string> &
167 {
168  return __readers;
169 }
170 
171 
172 /** Get name of writer on interface.
173  * @return name of writer owner or empty string of no writer or unknown
174  */
175 const std::string &
177 {
178  return __writer;
179 }
180 
181 
182 /** Get interface instance serial.
183  * @return type string
184  */
185 unsigned int
187 {
188  return __serial;
189 }
190 
191 
192 /** Get interface timestamp.
193  * @return point to interface last update time
194  */
195 const Time *
197 {
198  return __timestamp;
199 }
200 
201 /** < operator
202  * This compares two interface infos with respect to the less than (<) relation
203  * considering the type and id of an interface.
204  * An interface info A is less than an interface info B (A < B) iff
205  * (A.type < B.type) or ((A.type == B.type) && A.id < B.id).
206  * @param ii interface info to compare this to
207  * @return true if this instance is considered less than @p ii, false otherwise
208  */
209 bool
211 {
212  int td = strncmp(__type, ii.__type, __INTERFACE_TYPE_SIZE);
213  if ( td < 0 ) {
214  return true;
215  } else if (td > 0) {
216  return false;
217  } else {
218  return (strncmp(__id, ii.__id, __INTERFACE_ID_SIZE) < 0);
219  }
220 }
221 
222 
223 /** @class InterfaceInfoList <interface/interface_info.h>
224  * Interface information list.
225  * List with InterfaceInfo instances.
226  * @author Tim Niemueller
227  */
228 
229 /** Append an interface info.
230  * @param type type of the interface
231  * @param id id of the interface
232  * @param hash version hash
233  * @param has_writer true if there is a writer, false otherwise
234  * @param num_readers number of readers
235  * @param serial instance serial
236  * @param readers name of readers of interface
237  * @param writer name of writer of interface
238  * @param timestamp interface timestamp (time of last write or data timestamp)
239  */
240 void
241 InterfaceInfoList::append(const char *type, const char *id, const unsigned char *hash,
242  unsigned int serial, bool has_writer, unsigned int num_readers,
243  const std::list<std::string> &readers, const std::string &writer,
244  const Time &timestamp)
245 {
246  push_back(InterfaceInfo(type, id, hash, serial, has_writer, num_readers, readers, writer, &timestamp));
247 }
248 
249 } // end namespace fawkes
const char * type() const
Get interface type.
Fawkes library namespace.
const char * id() const
Get interface ID.
bool operator<(const InterfaceInfo &ii) const
< operator This compares two interface infos with respect to the less than (<) relation considering t...
A class for handling time.
Definition: time.h:91
const unsigned char * hash() const
Get interface version hash.
const Time * timestamp() const
Get interface timestamp.
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.
const std::string & writer() const
Get name of writer on interface.
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.
~InterfaceInfo()
Destructor.
unsigned int num_readers() const
Get number of readers.
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.
std::string hash_printable() const
Get interface version hash in printable format.
Interface info.