FileTopicReader.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5*
6* ArmarX is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* ArmarX is distributed in the hope that it will be useful, but
11* WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*
18* @package ArmarX
19* @author Mirko Waechter( mirko.waechter at kit dot edu)
20* @date 2016
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24#include "FileTopicReader.h"
25
26#include <fstream>
27
30
31namespace armarx
32{
33
34 FileTopicReader::FileTopicReader(std::istream* stream) : stream(stream)
35 {
36 }
37
38 FileTopicReader::FileTopicReader(std::filesystem::path path)
39 {
40 std::ifstream* i = new std::ifstream(path.string().c_str());
41 if (!i->is_open())
42 {
43 ARMARX_ERROR_S << "Could not open '" << path.string() << "'!";
44 }
45 stream = i;
46 }
47
49 {
50 if (!filepath.empty())
51 {
52 delete stream;
53 }
54 }
55
56 bool
58 {
59 std::string line;
60 if (!std::getline(*stream, line))
61 {
62 return false;
63 }
64 JSONObjectPtr json = new JSONObject();
65 json->fromString(line);
66 auto payloadEncoded = json->getString("data");
67 data.topicName = json->getString("topic");
68 std::string payloadStr = TopicUtil::Decode64(payloadEncoded);
69 data.inParams.assign(payloadStr.begin(), payloadStr.end());
70 data.operationName = json->getString("op");
71 data.timestamp = IceUtil::Time::microSecondsDouble(json->getDouble("t"));
72 return true;
73 }
74
75 bool
77 {
78 //Seeking is not supported
79 return false;
80 }
81
82 IceUtil::Time
84 {
85 //Finding length of replay file is not supported
86 return IceUtil::Time::microSeconds(0);
87 }
88
89 std::vector<std::string>
91 {
92 return std::vector<std::string>();
93 }
94
95} // namespace armarx
std::string timestamp()
bool seekTo(IceUtil::Time timestamp) override
std::vector< std::string > getReplayTopics() override
IceUtil::Time getReplayLength() override
bool read(TopicUtil::TopicData &data) override
FileTopicReader(std::istream *stream)
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
std::string Decode64(const std::string &val)
Definition TopicUtil.cpp:44
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34