Skip to content

Commit

Permalink
msg v0.0.16. GetEpisodes.srv. Implement services: episode/get, stream…
Browse files Browse the repository at this point in the history
…/get,add,delete.
  • Loading branch information
mpavezb committed Jul 4, 2018
1 parent 7ed8bc0 commit 9dfe498
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 26 deletions.
2 changes: 1 addition & 1 deletion include/ltm/db/impl/stream_collection_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ namespace ltm {

template<class StreamType>
bool StreamCollectionManager<StreamType>::ltm_insert(const StreamType &stream, MetadataPtr metadata) {

// add common metadata for streams
metadata->append("uid", (int) stream.meta.uid);
metadata->append("episode_uid", (int) stream.meta.episode);
Expand All @@ -206,6 +205,7 @@ namespace ltm {
metadata->append("start", _start);
metadata->append("end", _end);

// insert
_coll->insert(stream, metadata);
// todo: insert into cache
ROS_INFO_STREAM(_log_prefix << "Inserting stream (" << stream.meta.uid << ") into collection "
Expand Down
41 changes: 29 additions & 12 deletions include/ltm/plugin/impl/stream_ros_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LTM_PLUGIN_STREAM_ROS_IMPL_HXX

#include <ltm/plugin/stream_ros.h>
#include <ltm/util/util.h>

namespace ltm {
namespace plugin {
Expand Down Expand Up @@ -46,29 +47,45 @@ namespace ltm {

template<class StreamType, class StreamSrv>
bool StreamROS<StreamType, StreamSrv>::add_service(StreamSrvRequest &req, StreamSrvResponse &res) {
this->ltm_insert(req.msg, this->make_metadata(req.msg));
typename std::vector<StreamType>::const_iterator it;
for (it = req.msgs.begin(); it != req.msgs.end(); ++it) {
this->ltm_insert(*it, this->make_metadata(*it));
}
return true;
}

template<class StreamType, class StreamSrv>
bool StreamROS<StreamType, StreamSrv>::get_service(StreamSrvRequest &req, StreamSrvResponse &res) {
ROS_INFO_STREAM(this->_log_prefix << "Retrieving stream (" << req.uid << ") from collection '" << this->ltm_get_collection_name() << "'");
StreamWithMetadataPtr stream_ptr;
if (!this->ltm_get(req.uid, stream_ptr)) {
ROS_ERROR_STREAM(this->_log_prefix << "Stream with uid '" << req.uid << "' not found.");
res.succeeded = (uint8_t) false;
return true;
ROS_INFO_STREAM(this->_log_prefix << "Retrieving streams from collection '" << this->ltm_get_collection_name() << "': " << ltm::util::vector_to_str(req.uids));
res.msgs.clear();

std::vector<uint32_t> visited, not_found;
std::vector<uint32_t>::const_iterator it;
for (it = req.uids.begin(); it != req.uids.end(); ++it) {
// do not seek repeated msgs
if (visited.end() != std::find(visited.begin(), visited.end(), *it)) continue;
visited.push_back(*it);

StreamWithMetadataPtr stream_ptr;
if (!this->ltm_get(*it, stream_ptr)) {
not_found.push_back(*it);
continue;
}
res.msgs.push_back(*stream_ptr);
}
res.msg = *stream_ptr;
res.succeeded = (uint8_t) true;
ROS_WARN_STREAM_COND(not_found.size() > 0, this->_log_prefix
<< "GET: The following requested streams were not found: " << ltm::util::vector_to_str(not_found));
return true;
}

template<class StreamType, class StreamSrv>
bool StreamROS<StreamType, StreamSrv>::delete_service(StreamSrvRequest &req, StreamSrvResponse &res) {
ROS_INFO_STREAM(this->_log_prefix << "Removing (" << req.uid << ") from collection '" << this->ltm_get_collection_name() << "'");
res.succeeded = (uint8_t) this->ltm_remove(req.uid);
return res.succeeded;
ROS_INFO_STREAM(this->_log_prefix << "Deleting streams from collection '" << this->ltm_get_collection_name() << "': " << ltm::util::vector_to_str(req.uids));
std::vector<uint32_t>::const_iterator it;
for (it = req.uids.begin(); it != req.uids.end(); ++it) {
this->ltm_remove(*it);
}
return true;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion include/ltm/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>
#include <set>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>

namespace ltm {
Expand All @@ -17,6 +17,10 @@ namespace ltm {

std::string vector_to_str(const std::vector<std::string> &array);

std::string vector_to_str(const std::vector<int> &array);

std::string vector_to_str(const std::vector<uint32_t> &array);

}
}

Expand Down
31 changes: 21 additions & 10 deletions src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ltm/util/parameter_server_wrapper.h>
#include <ltm/util/util.h>
#include <ltm/server.h>
#include <boost/scoped_ptr.hpp>

Expand Down Expand Up @@ -57,16 +58,26 @@ namespace ltm {
// ==========================================================

bool Server::get_episodes_service(ltm::GetEpisodes::Request &req, ltm::GetEpisodes::Response &res) {
// ROS_INFO_STREAM(_log_prefix << "GET: Retrieving episode with uid: " << req.uids);
// EpisodeWithMetadataPtr ep_ptr;
// if (!_db->get(req.uid, ep_ptr)) {
// ROS_ERROR_STREAM(_log_prefix << "GET: Episode with uid '" << req.uid << "' not found.");
// res.succeeded = (uint8_t) false;
// return true;
// }
// res.episodes = *ep_ptr;
// res.succeeded = (uint8_t) true;
// return true;
ROS_INFO_STREAM(_log_prefix << "GET: Retrieving episodes with uids: " << ltm::util::vector_to_str(req.uids));
res.not_found.clear();
res.episodes.clear();
std::vector<uint32_t> visited;
std::vector<uint32_t>::const_iterator it;
for (it = req.uids.begin(); it != req.uids.end(); ++it) {
// do not seek repeated episodes
if (visited.end() != std::find(visited.begin(), visited.end(), *it)) continue;
visited.push_back(*it);

EpisodeWithMetadataPtr ep_ptr;
if (!_db->get(*it, ep_ptr)) {
res.not_found.push_back(*it);
continue;
}
res.episodes.push_back(*ep_ptr);
}
ROS_WARN_STREAM_COND(res.not_found.size() > 0, _log_prefix
<< "GET: The following requested episodes were not found: " << ltm::util::vector_to_str(res.not_found));
return true;
}

bool Server::query_server_service(ltm::QueryServer::Request &req, ltm::QueryServer::Response &res) {
Expand Down
27 changes: 27 additions & 0 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,36 @@ namespace ltm {
namespace util {

std::string vector_to_str(const std::vector<std::string> &array) {
if (array.empty()) return "[]";
std::vector<std::string>::const_iterator it;
std::stringstream ss;
ss << "[";
for (it = array.begin(); it != array.end(); ++it) {
ss << "'" << *it << "', ";
}
ss.seekp(-2, ss.cur);
ss << "]";
return ss.str();
}

std::string vector_to_str(const std::vector<int> &array) {
if (array.empty()) return "[]";
std::vector<int>::const_iterator it;
std::ostringstream ss;
ss << "[";
for (it = array.begin(); it != array.end(); ++it) {
ss << *it << ", ";
}
ss.seekp(-2, ss.cur);
ss << "]";
return ss.str();
}

std::string vector_to_str(const std::vector<uint32_t> &array) {
if (array.empty()) return "[]";
std::vector<uint32_t>::const_iterator it;
std::ostringstream ss;
ss << "[";
for (it = array.begin(); it != array.end(); ++it) {
ss << *it << ", ";
}
Expand Down
4 changes: 2 additions & 2 deletions srv/GetEpisodes.srv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# target uids
uint32[] uids
---
bool succeeded
ltm/Episode[] episodes
ltm/Episode[] episodes
uint32[] not_found

0 comments on commit 9dfe498

Please sign in to comment.