mongodb_log_trimesh.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * mongodb_log_trimesh.cpp - MongoDB Logger for Triangle Meshes
4  *
5  * Created: Wed Dec 15 18:16:03 2010
6  * Copyright 2010 Tim Niemueller [www.niemueller.de]
7  * 2010 Carnegie Mellon University
8  * 2010 Intel Labs Pittsburgh
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <ros/ros.h>
26 #include <mongo/client/dbclient.h>
27 
28 #include <rviz_intel/TriangleMesh.h>
29 #include <mongodb_store/util.h>
30 
31 
32 using namespace mongo;
33 
34 DBClientConnection *mongodb_conn;
35 std::string collection;
36 
37 unsigned int in_counter;
38 unsigned int out_counter;
39 unsigned int qsize;
40 unsigned int drop_counter;
41 
42 static pthread_mutex_t in_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
43 static pthread_mutex_t out_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static pthread_mutex_t drop_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_mutex_t qsize_mutex = PTHREAD_MUTEX_INITIALIZER;
46 
47 void msg_callback(const rviz_intel::TriangleMesh::ConstPtr& msg)
48 {
49  BSONObjBuilder document;
50 
51  Date_t stamp = msg->header.stamp.sec * 1000.0 + msg->header.stamp.nsec / 1000000.0;
52  document.append("header", BSON( "seq" << msg->header.seq
53  << "stamp" << stamp
54  << "frame_id" << msg->header.frame_id));
55 
56  BSONArrayBuilder pointsb(document.subarrayStart("points"));
57  std::vector<geometry_msgs::Point32>::const_iterator p;
58  for (p = msg->points.begin(); p != msg->points.end(); ++p) {
59  pointsb.append(BSON( "x" << p->x
60  << "y" << p->y
61  << "z" << p->z));
62  }
63  pointsb.doneFast();
64 
65  BSONArrayBuilder normalsb(document.subarrayStart("normals"));
66  for (p = msg->normals.begin(); p != msg->normals.end(); ++p) {
67  normalsb.append(BSON( "x" << p->x
68  << "y" << p->y
69  << "z" << p->z));
70  }
71  normalsb.doneFast();
72 
73  BSONArrayBuilder colorsb(document.subarrayStart("colors"));
74  std::vector<uint32_t>::const_iterator u;
75  for (u = msg->colors.begin(); u != msg->colors.end(); ++u) {
76  colorsb.append(*u);
77  }
78  colorsb.doneFast();
79 
80  BSONArrayBuilder color_indsb(document.subarrayStart("color_inds"));
81  for (u = msg->color_inds.begin(); u != msg->color_inds.end(); ++u) {
82  color_indsb.append(*u);
83  }
84  color_indsb.doneFast();
85 
86  BSONArrayBuilder trianglesb(document.subarrayStart("triangles"));
87  std::vector<rviz_intel::Triangle>::const_iterator t;
88  for (t = msg->triangles.begin(); t != msg->triangles.end(); ++t) {
89  trianglesb.append(BSON( "i" << t->i
90  << "j" << t->j
91  << "k" << t->k));
92  }
93  trianglesb.doneFast();
94  document.append("sending_node", msg->sending_node);
95 
96  mongodb_store::add_meta_for_msg<rviz_intel::TriangleMesh>(msg, document);
97  mongodb_conn->insert(collection, document.obj());
98 
99  // If we'd get access to the message queue this could be more useful
100  // https://code.ros.org/trac/ros/ticket/744
101  pthread_mutex_lock(&in_counter_mutex);
102  ++in_counter;
103  pthread_mutex_unlock(&in_counter_mutex);
104  pthread_mutex_lock(&out_counter_mutex);
105  ++out_counter;
106  pthread_mutex_unlock(&out_counter_mutex);
107 }
108 
110 {
111  unsigned int l_in_counter, l_out_counter, l_drop_counter, l_qsize;
112 
113  pthread_mutex_lock(&in_counter_mutex);
114  l_in_counter = in_counter; in_counter = 0;
115  pthread_mutex_unlock(&in_counter_mutex);
116 
117  pthread_mutex_lock(&out_counter_mutex);
118  l_out_counter = out_counter; out_counter = 0;
119  pthread_mutex_unlock(&out_counter_mutex);
120 
121  pthread_mutex_lock(&drop_counter_mutex);
122  l_drop_counter = drop_counter; drop_counter = 0;
123  pthread_mutex_unlock(&drop_counter_mutex);
124 
125  pthread_mutex_lock(&qsize_mutex);
126  l_qsize = qsize; qsize = 0;
127  pthread_mutex_unlock(&qsize_mutex);
128 
129  printf("%u:%u:%u:%u\n", l_in_counter, l_out_counter, l_drop_counter, l_qsize);
130  fflush(stdout);
131 }
132 
133 
134 int
135 main(int argc, char **argv)
136 {
137  std::string topic = "", mongodb = "localhost", nodename = "";
138  collection = "";
139 
141 
142  int c;
143  while ((c = getopt(argc, argv, "t:m:n:c:")) != -1) {
144  if ((c == '?') || (c == ':')) {
145  printf("Usage: %s -t topic -m mongodb -n nodename -c collection\n", argv[0]);
146  exit(-1);
147  } else if (c == 't') {
148  topic = optarg;
149  } else if (c == 'm') {
150  mongodb = optarg;
151  } else if (c == 'n') {
152  nodename = optarg;
153  } else if (c == 'c') {
154  collection = optarg;
155  }
156  }
157 
158  if (topic == "") {
159  printf("No topic given.\n");
160  exit(-2);
161  } else if (nodename == "") {
162  printf("No node name given.\n");
163  exit(-2);
164  }
165 
166  ros::init(argc, argv, nodename);
167  ros::NodeHandle n;
168 
169  std::string errmsg;
170  mongodb_conn = new DBClientConnection(/* auto reconnect*/ true);
171  if (! mongodb_conn->connect(mongodb, errmsg)) {
172  ROS_ERROR("Failed to connect to MongoDB: %s", errmsg.c_str());
173  return -1;
174  }
175 
176  ros::Subscriber sub = n.subscribe<rviz_intel::TriangleMesh>(topic, 1000, msg_callback);
177  ros::Timer count_print_timer = n.createTimer(ros::Duration(5, 0), print_count);
178 
179  ros::spin();
180 
181  delete mongodb_conn;
182 
183  return 0;
184 }
static pthread_mutex_t out_counter_mutex
unsigned int drop_counter
static pthread_mutex_t qsize_mutex
unsigned int qsize
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
DBClientConnection * mongodb_conn
ROSCPP_DECL void spin(Spinner &spinner)
std::string topic
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
int main(int argc, char **argv)
void msg_callback(const rviz_intel::TriangleMesh::ConstPtr &msg)
static pthread_mutex_t drop_counter_mutex
static pthread_mutex_t in_counter_mutex
unsigned int in_counter
unsigned int out_counter
#define ROS_ERROR(...)
void print_count(const ros::TimerEvent &te)
std::string collection


mongodb_log
Author(s): Tim Niemueller
autogenerated on Sat Sep 7 2019 03:40:41