RSB  0.17.0
Model.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2014, 2015 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "Model.h"
28 
29 #include <boost/format.hpp>
30 
31 #include <boost/date_time/posix_time/posix_time.hpp>
32 
33 #include <rsc/logging/Logger.h>
34 
35 namespace {
36 
37 rsc::logging::LoggerPtr logger
38 = rsc::logging::Logger::getLogger("rsb.introspection");
39 
40 }
41 
42 namespace rsb {
43 namespace introspection {
44 
45 // ParticipantInfo
46 
47 ParticipantInfo::ParticipantInfo(const std::string& kind,
48  const rsc::misc::UUID& id,
49  const rsc::misc::UUID& parentId,
50  const Scope& scope,
51  const std::string& type,
52  const std::set<std::string>& transportURLs)
53  : kind(kind), id(id), parentId(parentId), scope(scope), type(type),
54  transportURLs(transportURLs) {
55 }
56 
58 }
59 
60 const std::string& ParticipantInfo::getKind() const {
61  return this->kind;
62 }
63 
64 const rsc::misc::UUID& ParticipantInfo::getId() const {
65  return this->id;
66 }
67 
68 const rsc::misc::UUID& ParticipantInfo::getParentId() const {
69  return this->parentId;
70 }
71 
73  return this->scope;
74 }
75 
76 const std::string& ParticipantInfo::getType() const {
77  return this->type;
78 }
79 
80 const std::set<std::string>& ParticipantInfo::getTransportURLs() const {
81  return this->transportURLs;
82 }
83 
84 
85 // ProcessInfo
86 
87 std::string tryCurrentProgramName() {
88  try {
89  return rsc::os::currentProgramName();
90  } catch (const std::exception& e) {
91  RSCERROR(logger, boost::str(boost::format("tryCurrentProgramName failed: %1%")
92  % e.what()));
93  return "<unknown program>";
94  }
95 }
96 
97 std::vector<std::string> tryCurrentCommandlineArguments() {
98  try {
99  return rsc::os::currentCommandlineArguments();
100  } catch (const std::exception& e) {
101  RSCERROR(logger, boost::str(boost::format("tryCurrentCommandlineArguments failed: %1%")
102  % e.what()));
103  return std::vector<std::string>();
104  }
105 }
106 
107 boost::posix_time::ptime tryCurrentProcessStartTime() {
108  try {
109  return rsc::os::currentProcessStartTime();
110  } catch (const std::exception& e) {
111  RSCERROR(logger, boost::str(boost::format("tryCurrentProcessStartTime failed: %1%")
112  % e.what()));
113  static boost::posix_time::ptime firstCall
114  = boost::posix_time::microsec_clock::universal_time();
115  return firstCall;
116  }
117 }
118 
119 std::string tryCurrentExecutingUser() {
120  try {
121  return rsc::os::currentExecutingUser();
122  } catch (const std::exception& e) {
123  RSCERROR(logger, boost::str(boost::format("tryCurrentExecutingUser failed: %1%")
124  % e.what()));
125  return "";
126  }
127 }
128 
129 ProcessInfo::ProcessInfo(unsigned int pid,
130  const std::string& programName,
131  const std::vector<std::string>& arguments,
132  const boost::posix_time::ptime& startTime,
133  const std::string& rsbVersion,
134  const std::string& executingUser)
135  : pid(pid), programName(programName), arguments(arguments),
136  startTime(startTime), rsbVersion(rsbVersion),
137  executingUser(executingUser) {
138 }
139 
141 }
142 
143 unsigned int ProcessInfo::getPid() const {
144  return this->pid;
145 }
146 
147 const std::string& ProcessInfo::getProgramName() const {
148  return this->programName;
149 }
150 
151 const std::vector<std::string>& ProcessInfo::getArguments() const {
152  return this->arguments;
153 }
154 
155 const boost::posix_time::ptime& ProcessInfo::getStartTime() const {
156  return this->startTime;
157 }
158 
159 const std::string& ProcessInfo::getRSBVersion() const {
160  return this->rsbVersion;
161 }
162 
163 const std::string& ProcessInfo::getExecutingUser() const {
164  return this->executingUser;
165 }
166 
167 // HostInfo
168 
169 std::string tryCurrentHostId() {
170  try {
171  return rsc::os::currentHostId();
172  } catch (const std::exception& e) {
173  RSCERROR(logger, boost::str(boost::format("tryCurrentHostId failed: %1%")
174  % e.what()));
175  return "";
176  }
177 }
178 
179 std::string currentCompatibleHostId() {
180  // For compatibility with languages that have no easy way of
181  // calling C API functions, use hostname instead of calling a host
182  // id C function provided by respective operating system.
183  std::string softwareType = tryCurrentSoftwareType();
184  if (softwareType == "win32" || softwareType == "darwin") {
185  return tryCurrentHostname();
186  } else {
187  return tryCurrentHostId();
188  }
189 }
190 
191 std::string tryCurrentHostname() {
192  try {
193  return rsc::os::currentHostname();
194  } catch (const std::exception& e) {
195  RSCERROR(logger, boost::str(boost::format("tryCurrentHostname failed: %1%")
196  % e.what()));
197  return "<unknown host>";
198  }
199 }
200 
201 std::string tryCurrentMachineType() {
202  try {
203  return rsc::os::currentMachineType();
204  } catch (const std::exception& e) {
205  RSCERROR(logger, boost::str(boost::format("tryCurrentMachineType failed: %1%")
206  % e.what()));
207  return "";
208  }
209 }
210 
212  try {
213  return rsc::os::currentMachineVersion();
214  } catch (const std::exception& e) {
215  RSCERROR(logger, boost::str(boost::format("tryCurrentMachineVersion failed: %1%")
216  % e.what()));
217  return "";
218  }
219 }
220 
221 std::string tryCurrentSoftwareType() {
222  try {
223  return rsc::os::currentSoftwareType();
224  } catch (const std::exception& e) {
225  RSCERROR(logger, boost::str(boost::format("tryCurrentSoftwareType failed: %1%")
226  % e.what()));
227  return "";
228  }
229 }
230 
232  try {
233  return rsc::os::currentSoftwareVersion();
234  } catch (const std::exception& e) {
235  RSCERROR(logger, boost::str(boost::format("tryCurrentSoftwareVersion failed: %1%")
236  % e.what()));
237  return "";
238  }
239 }
240 
241 HostInfo::HostInfo(const std::string& id,
242  const std::string& hostname,
243  const std::string& machineType,
244  const std::string& machineVersion,
245  const std::string& softwareType,
246  const std::string& softwareVersion)
247  : id(id), hostname(hostname),
248  machineType(machineType), machineVersion(machineVersion),
249  softwareType(softwareType), softwareVersion(softwareVersion) {
250 }
251 
253 }
254 
255 const std::string& HostInfo::getId() const {
256  return this->id;
257 }
258 
259 const std::string& HostInfo::getHostname() const {
260  return this->hostname;
261 }
262 
263 const std::string& HostInfo::getMachineType() const {
264  return this->machineType;
265 }
266 
267 const std::string& HostInfo::getMachineVersion() const {
268  return this->machineVersion;
269 }
270 
271 const std::string& HostInfo::getSoftwareType() const {
272  return this->softwareType;
273 }
274 
275 const std::string& HostInfo::getSoftwareVersion() const {
276  return this->softwareVersion;
277 }
278 
279 }
280 }
unsigned int getPid() const
Definition: Model.cpp:143
boost::posix_time::ptime tryCurrentProcessStartTime()
Definition: Model.cpp:107
const std::string & getMachineType() const
Definition: Model.cpp:263
HostInfo(const std::string &id=currentCompatibleHostId(), const std::string &hostname=tryCurrentHostname(), const std::string &machineType=tryCurrentMachineType(), const std::string &machineVersion=tryCurrentMachineVersion(), const std::string &softwareType=tryCurrentSoftwareType(), const std::string &softwareVersion=tryCurrentSoftwareVersion())
Definition: Model.cpp:241
std::string softwareVersion
Definition: Model.h:184
boost::posix_time::ptime startTime
Definition: Model.h:131
const std::string & getKind() const
Definition: Model.cpp:60
std::string tryCurrentMachineType()
Definition: Model.cpp:201
std::vector< std::string > arguments
Definition: Model.h:130
std::string tryCurrentExecutingUser()
Definition: Model.cpp:119
std::string currentCompatibleHostId()
Definition: Model.cpp:179
const std::string & getHostname() const
Definition: Model.cpp:259
const std::string & getType() const
Definition: Model.cpp:76
std::string softwareType
Definition: Model.h:183
std::string tryCurrentSoftwareVersion()
Definition: Model.cpp:231
const std::set< std::string > & getTransportURLs() const
Definition: Model.cpp:80
const std::string & getProgramName() const
Definition: Model.cpp:147
const rsc::misc::UUID & getParentId() const
Definition: Model.cpp:68
std::set< std::string > transportURLs
Definition: Model.h:80
const std::string & getSoftwareType() const
Definition: Model.cpp:271
std::vector< std::string > tryCurrentCommandlineArguments()
Definition: Model.cpp:97
const std::vector< std::string > & getArguments() const
Definition: Model.cpp:151
const std::string & getRSBVersion() const
Definition: Model.cpp:159
const std::string & getExecutingUser() const
Definition: Model.cpp:163
std::string machineType
Definition: Model.h:181
const std::string & getMachineVersion() const
Definition: Model.cpp:267
const Scope & getScope() const
Definition: Model.cpp:72
std::string tryCurrentMachineVersion()
Definition: Model.cpp:211
std::string tryCurrentHostId()
Definition: Model.cpp:169
const std::string & getSoftwareVersion() const
Definition: Model.cpp:275
const std::string & getId() const
Definition: Model.cpp:255
std::string machineVersion
Definition: Model.h:182
std::string tryCurrentSoftwareType()
Definition: Model.cpp:221
std::string tryCurrentProgramName()
Definition: Model.cpp:87
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
const boost::posix_time::ptime & getStartTime() const
Definition: Model.cpp:155
ProcessInfo(unsigned int pid=rsc::os::currentProcessId(), const std::string &programName=tryCurrentProgramName(), const std::vector< std::string > &arguments=tryCurrentCommandlineArguments(), const boost::posix_time::ptime &startTime=tryCurrentProcessStartTime(), const std::string &rsbVersion=rsb::Version::string()+"-"+rsb::Version::buildId(), const std::string &executingUser=tryCurrentExecutingUser())
Definition: Model.cpp:129
std::string tryCurrentHostname()
Definition: Model.cpp:191
const rsc::misc::UUID & getId() const
Definition: Model.cpp:64