RSB  0.12.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 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  : kind(kind), id(id), parentId(parentId), scope(scope), type(type) {
53 }
54 
56 }
57 
58 const std::string& ParticipantInfo::getKind() const {
59  return this->kind;
60 }
61 
62 const rsc::misc::UUID& ParticipantInfo::getId() const {
63  return this->id;
64 }
65 
66 const rsc::misc::UUID& ParticipantInfo::getParentId() const {
67  return this->parentId;
68 }
69 
71  return this->scope;
72 }
73 
74 const std::string& ParticipantInfo::getType() const {
75  return this->type;
76 }
77 
78 // ProcessInfo
79 
80 std::string tryCurrentProgramName() {
81  try {
82  return rsc::os::currentProgramName();
83  } catch (const std::exception& e) {
84  RSCERROR(logger, boost::str(boost::format("tryCurrentProgramName failed: %1%")
85  % e.what()));
86  return "<unknown program>";
87  }
88 }
89 
90 std::vector<std::string> tryCurrentCommandlineArguments() {
91  try {
92  return rsc::os::currentCommandlineArguments();
93  } catch (const std::exception& e) {
94  RSCERROR(logger, boost::str(boost::format("tryCurrentCommandlineArguments failed: %1%")
95  % e.what()));
96  return std::vector<std::string>();
97  }
98 }
99 
100 boost::posix_time::ptime tryCurrentProcessStartTime() {
101  try {
102  return rsc::os::currentProcessStartTime();
103  } catch (const std::exception& e) {
104  RSCERROR(logger, boost::str(boost::format("tryCurrentProcessStartTime failed: %1%")
105  % e.what()));
106  static boost::posix_time::ptime firstCall
107  = boost::posix_time::microsec_clock::universal_time();
108  return firstCall;
109  }
110 }
111 
112 std::string tryCurrentExecutingUser() {
113  try {
114  return rsc::os::currentExecutingUser();
115  } catch (const std::exception& e) {
116  RSCERROR(logger, boost::str(boost::format("tryCurrentExecutingUser failed: %1%")
117  % e.what()));
118  return "";
119  }
120 }
121 
122 ProcessInfo::ProcessInfo(unsigned int pid,
123  const std::string& programName,
124  const std::vector<std::string>& arguments,
125  const boost::posix_time::ptime& startTime,
126  const std::string& rsbVersion,
127  const std::string& executingUser)
128  : pid(pid), programName(programName), arguments(arguments),
129  startTime(startTime), rsbVersion(rsbVersion),
130  executingUser(executingUser) {
131 }
132 
134 }
135 
136 unsigned int ProcessInfo::getPid() const {
137  return this->pid;
138 }
139 
140 const std::string& ProcessInfo::getProgramName() const {
141  return this->programName;
142 }
143 
144 const std::vector<std::string>& ProcessInfo::getArguments() const {
145  return this->arguments;
146 }
147 
148 const boost::posix_time::ptime& ProcessInfo::getStartTime() const {
149  return this->startTime;
150 }
151 
152 const std::string& ProcessInfo::getRSBVersion() const {
153  return this->rsbVersion;
154 }
155 
156 const std::string& ProcessInfo::getExecutingUser() const {
157  return this->executingUser;
158 }
159 
160 // HostInfo
161 
162 std::string tryCurrentHostId() {
163  try {
164  return rsc::os::currentHostId();
165  } catch (const std::exception& e) {
166  RSCERROR(logger, boost::str(boost::format("tryCurrentHostId failed: %1%")
167  % e.what()));
168  return "";
169  }
170 }
171 
172 std::string currentCompatibleHostId() {
173  // For compatibility with languages that have no easy way of
174  // calling C API functions, use hostname instead of calling a host
175  // id C function provided by respective operating system.
176  std::string softwareType = tryCurrentSoftwareType();
177  if (softwareType == "win32" || softwareType == "darwin") {
178  return tryCurrentHostname();
179  } else {
180  return tryCurrentHostId();
181  }
182 }
183 
184 std::string tryCurrentHostname() {
185  try {
186  return rsc::os::currentHostname();
187  } catch (const std::exception& e) {
188  RSCERROR(logger, boost::str(boost::format("tryCurrentHostname failed: %1%")
189  % e.what()));
190  return "<unknown host>";
191  }
192 }
193 
194 std::string tryCurrentMachineType() {
195  try {
196  return rsc::os::currentMachineType();
197  } catch (const std::exception& e) {
198  RSCERROR(logger, boost::str(boost::format("tryCurrentMachineType failed: %1%")
199  % e.what()));
200  return "";
201  }
202 }
203 
205  try {
206  return rsc::os::currentMachineVersion();
207  } catch (const std::exception& e) {
208  RSCERROR(logger, boost::str(boost::format("tryCurrentMachineVersion failed: %1%")
209  % e.what()));
210  return "";
211  }
212 }
213 
214 std::string tryCurrentSoftwareType() {
215  try {
216  return rsc::os::currentSoftwareType();
217  } catch (const std::exception& e) {
218  RSCERROR(logger, boost::str(boost::format("tryCurrentSoftwareType failed: %1%")
219  % e.what()));
220  return "";
221  }
222 }
223 
225  try {
226  return rsc::os::currentSoftwareVersion();
227  } catch (const std::exception& e) {
228  RSCERROR(logger, boost::str(boost::format("tryCurrentSoftwareVersion failed: %1%")
229  % e.what()));
230  return "";
231  }
232 }
233 
234 HostInfo::HostInfo(const std::string& id,
235  const std::string& hostname,
236  const std::string& machineType,
237  const std::string& machineVersion,
238  const std::string& softwareType,
239  const std::string& softwareVersion)
240  : id(id), hostname(hostname),
241  machineType(machineType), machineVersion(machineVersion),
242  softwareType(softwareType), softwareVersion(softwareVersion) {
243 }
244 
246 }
247 
248 const std::string& HostInfo::getId() const {
249  return this->id;
250 }
251 
252 const std::string& HostInfo::getHostname() const {
253  return this->hostname;
254 }
255 
256 const std::string& HostInfo::getMachineType() const {
257  return this->machineType;
258 }
259 
260 const std::string& HostInfo::getMachineVersion() const {
261  return this->machineVersion;
262 }
263 
264 const std::string& HostInfo::getSoftwareType() const {
265  return this->softwareType;
266 }
267 
268 const std::string& HostInfo::getSoftwareVersion() const {
269  return this->softwareVersion;
270 }
271 
272 }
273 }
unsigned int getPid() const
Definition: Model.cpp:136
boost::posix_time::ptime tryCurrentProcessStartTime()
Definition: Model.cpp:100
const std::string & getMachineType() const
Definition: Model.cpp:256
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:234
std::string softwareVersion
Definition: Model.h:179
boost::posix_time::ptime startTime
Definition: Model.h:126
const std::string & getKind() const
Definition: Model.cpp:58
std::string tryCurrentMachineType()
Definition: Model.cpp:194
std::vector< std::string > arguments
Definition: Model.h:125
std::string tryCurrentExecutingUser()
Definition: Model.cpp:112
std::string currentCompatibleHostId()
Definition: Model.cpp:172
const std::string & getHostname() const
Definition: Model.cpp:252
const std::string & getType() const
Definition: Model.cpp:74
std::string softwareType
Definition: Model.h:178
std::string tryCurrentSoftwareVersion()
Definition: Model.cpp:224
const std::string & getProgramName() const
Definition: Model.cpp:140
const rsc::misc::UUID & getParentId() const
Definition: Model.cpp:66
const std::string & getSoftwareType() const
Definition: Model.cpp:264
std::vector< std::string > tryCurrentCommandlineArguments()
Definition: Model.cpp:90
const std::vector< std::string > & getArguments() const
Definition: Model.cpp:144
const std::string & getRSBVersion() const
Definition: Model.cpp:152
const std::string & getExecutingUser() const
Definition: Model.cpp:156
std::string machineType
Definition: Model.h:176
const std::string & getMachineVersion() const
Definition: Model.cpp:260
const Scope & getScope() const
Definition: Model.cpp:70
std::string tryCurrentMachineVersion()
Definition: Model.cpp:204
std::string tryCurrentHostId()
Definition: Model.cpp:162
const std::string & getSoftwareVersion() const
Definition: Model.cpp:268
const std::string & getId() const
Definition: Model.cpp:248
std::string machineVersion
Definition: Model.h:177
std::string tryCurrentSoftwareType()
Definition: Model.cpp:214
std::string tryCurrentProgramName()
Definition: Model.cpp:80
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
ParticipantInfo(const std::string &kind, const rsc::misc::UUID &id, const rsc::misc::UUID &parentId, const Scope &scope, const std::string &type)
Definition: Model.cpp:47
const boost::posix_time::ptime & getStartTime() const
Definition: Model.cpp:148
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:122
std::string tryCurrentHostname()
Definition: Model.cpp:184
const rsc::misc::UUID & getId() const
Definition: Model.cpp:62