Main Page · Modules · All Classes · Class Hierarchy
MPTypes.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MPTypes.hpp"
23 
24 #include <ml/MACollectedData.hpp>
25 #include <network/MAStreamPacketCoder.hpp>
26 #include <types/MAHead.hpp>
27 #include <types/MARobotState.hpp>
28 #include <types/MATorso.hpp>
29 
30 #include <MCBinaryData.hpp>
31 #include <MCLog.hpp>
32 #include <MCZipper.hpp>
33 
34 #include <MEImage.hpp>
35 
36 #include <qjsonobject.h>
37 #include <qstringlist.h>
38 #include <qtextstream.h>
39 #include <qthread.h>
40 
41 #include <boost/scoped_ptr.hpp>
42 
43 namespace
44 {
45 // Backward compatibility for old sessions
46 bool BackwardCompatibility = false;
47 }
48 
50 {
51  MEImage Image;
52 
53  if (Image.Decompress(binary_data))
54  return Image.ExportRawImageData();
55 
56  return nullptr;
57 }
58 
59 
60 QJsonObject* MPConvertCollectedDataToJson(MACollectedData& collected_data)
61 {
62  QJsonObject* Object = new QJsonObject;
63  MC::BinaryDataSPtr BinaryData(collected_data.Encode());
64 
65  Object->insert("objectType", QString("objects.")+QString::fromStdString(collected_data.GetName()));
66  Object->insert("sessionId", collected_data.GetSessionID());
67  Object->insert("collectionId", collected_data.GetCollectionID());
68  Object->insert("data", QString::fromStdString(BinaryData->ToBase64()));
69  return Object;
70 }
71 
72 
74 {
75  QJsonObject* QueryObject = new QJsonObject;
76  QJsonObject Object;
77  MC::BinaryDataSPtr BinaryData(collected_data.Encode());
78 
79  Object["sessionId"] = collected_data.GetSessionID();
80  Object["collectionId"] = collected_data.GetCollectionID();
81  QueryObject->insert("objectType", QString("objects.")+QString::fromStdString(collected_data.GetName()));
82  QueryObject->insert("query", Object);
83  return QueryObject;
84 }
85 
86 
88 {
89  MC::BinaryDataSPtr BinaryData(new MCBinaryData);
90 
91  BinaryData->FromBase64(json_object.value("data").toString().toStdString());
92  return MACollectedData::Decode(*BinaryData);
93 }
94 
95 
96 MPLogEntryMap::MPLogEntryMap() : MSData("MPLogEntryMap"), MP::LogEntryMap()
97 {
98 }
99 
100 
101 MPCursorPosition::MPCursorPosition() : MSData("MPCursorPosition"), Position(0)
102 {
103 }
104 
105 
107 {
108 }
109 
110 
111 MCBinaryData* MPRecord::DecodeJpegImage() const
112 {
113  if (!Image.get())
114  return nullptr;
115 
116  return MPDecodeImage(*Image);
117 }
118 
120 
122 {
123 }
124 
125 
126 const MP::RecordPtrList& MPRecordsContainer::GetRecords() const
127 {
128  return Records;
129 }
130 
131 
132 const MP::PacketPtrList& MPRecordsContainer::GetPackets() const
133 {
134  return Packets;
135 }
136 
137 
139 {
140  return Records.empty();
141 }
142 
143 
145 {
146  Records.clear();
147  Packets.clear();
148 }
149 
150 
152 {
153  boost::scoped_ptr<MAStreamPacketCoder> Coder(MAStreamPacketCoder::Decode(packet));
154  MA::IntIntSortedMap RecordIDsTimestamps;
155 
156  if (Coder.get() == nullptr)
157  {
158  MC_WARNING("Cannot decode packet into robot state record (packet size: %d)", packet.GetSize());
159  return RecordIDsTimestamps;
160  }
161  // Clone the packet
162  MCBinaryData* Packet = new MCBinaryData;
163 
164  Packet->Clone(packet);
165  Packets.push_back(MP::PacketPtr(Packet));
166  // Note: With low vector sizes, no performance difference between the iterating and [] usage
167  MA::BinaryDataSPtrList RobotStates = Coder->GetRobotStates();
168  MA::BinaryDataSPtrList Images = Coder->GetImages();
169  MA::BinaryDataSPtrList LogMessages = Coder->GetLogMessages();
170  MA::BinaryDataSPtrList Sounds = Coder->GetSounds();
171 
172  for (int i = 0; i < Coder->GetPacketCapacity(); ++i)
173  {
174  MPRecord* Record = new MPRecord;
175 
176  Record->ID = -1;
177  if (Images[i])
178  {
179  MCBinaryData* ImageData = new MCBinaryData;
180 
181  ImageData->Clone(*Images[i]);
182  Record->Image = MP::ImagePtr(ImageData);
183  }
184  if (RobotStates[i])
185  {
186  // This is left for backward compatibility of old saved sessions (it may lead to crashes)
187  if (BackwardCompatibility)
188  {
189  MCBinaryData FinalPacket;
190 
191  MCZipper::DecompressQuickLz(*RobotStates[i], FinalPacket, true);
192  *RobotStates[i] = FinalPacket;
193  }
194  if (RobotStateVariableMap.empty())
195  {
196  Record->RobotState = MP::RobotStatePtr(MARobotState::Decode(*RobotStates[i]));
197  if (Record->RobotState.get() != nullptr)
198  {
199  RobotStateVariableMap = Record->RobotState->GetDecodedVariableMap();
200  } else {
201  // Check if the packet can be decoded to enable the backward compatibility mode
202  MCBinaryData FinalPacket;
203 
204  MCZipper::DecompressQuickLz(*RobotStates[i], FinalPacket, true);
205  Record->RobotState = MP::RobotStatePtr(MARobotState::Decode(FinalPacket));
206  *RobotStates[i] = FinalPacket;
207  if (Record->RobotState.get() != nullptr)
208  {
209  RobotStateVariableMap = Record->RobotState->GetDecodedVariableMap();
210  BackwardCompatibility = true;
211  }
212  }
213  } else {
214  Record->RobotState = MP::RobotStatePtr(MARobotState::Decode(*RobotStates[i], RobotStateVariableMap));
215  }
216  if (Record->RobotState)
217  {
218  int Milliseconds = Record->RobotState->Torso->Timestamp.ToMilliseconds();
219 
220  Record->ID = Record->RobotState->Torso->CycleID;
221  RecordIDsTimestamps.insert(std::pair<int, int>(Record->ID, Milliseconds));
222  MCBinaryData* RobotStateData = new MCBinaryData;
223 
224  RobotStateData->Clone(*RobotStates[i]);
225  Record->RobotStateBinaryData = MC::BinaryDataSPtr(RobotStateData);
226  }
227  }
228  if (LogMessages[i])
229  {
230  QStringList* Messages = new QStringList;
231 
232  // Decode log messages
233  QByteArray LogMessageData((const char*)LogMessages[i]->GetData(), LogMessages[i]->GetSize());
234  QTextStream Stream(&LogMessageData);
235  QString Line;
236 
237  do
238  {
239  Line = Stream.readLine();
240  if (!Line.isNull())
241  {
242  *Messages += Line;
243  }
244  } while (!Line.isNull());
245  Record->LogMessages = MP::LogMessagesPtr(Messages);
246  }
247  if (Sounds[i])
248  {
249  MCBinaryData* SoundData = new MCBinaryData;
250 
251  SoundData->Clone(*Sounds[i]);
252  Record->SoundData = MC::BinaryDataSPtr(SoundData);
253  }
254  Records.push_back(MP::RecordPtr(Record));
255  }
256  return RecordIDsTimestamps;
257 }
258 
259 
261 {
262 }
263 
264 
266 {
267 }
268 
269 
271 {
272 }
273 
274 
275 MPTimestampList::MPTimestampList() : MSData("MPTimestampList"), MC::IntList()
276 {
277 }
const MP::PacketPtrList & GetPackets() const
Get the packets.
Definition: MPTypes.cpp:132
Collected data with portable storage functions.
MA::IntIntSortedMap AddDataFromPacket(MCBinaryData &packet)
Turn a packet content into robot state records.
Definition: MPTypes.cpp:151
std::string GetName() const
Get name.
const MP::RecordPtrList & GetRecords() const
Get the records.
Definition: MPTypes.cpp:126
MPRecord()
Struct constructor.
Definition: MPTypes.cpp:106
MP::RobotStatePtr RobotState
Stored robot state.
Definition: MPTypes.hpp:197
MP::ImagePtr Image
Stored image.
Definition: MPTypes.hpp:201
Binary data class.
bool IsEmpty() const
Whenever the container is empty.
Definition: MPTypes.cpp:138
int ID
Record ID.
Definition: MPTypes.hpp:195
MC::BinaryDataSPtr RobotStateBinaryData
Stored robot state binary data.
Definition: MPTypes.hpp:199
MCBinaryData * MPDecodeImage(const MCBinaryData &binary_data)
Decode a jpeg image from AIBO.
Definition: MPTypes.cpp:49
MP::PacketPtrList Packets
Packet list.
Definition: MPTypes.hpp:274
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
int GetCollectionID()
Get collection ID.
MPTimestampList()
Struct constructor.
Definition: MPTypes.cpp:275
MP::RecordPtrList Records
Record list.
Definition: MPTypes.hpp:272
MC::BinaryDataSPtr SoundData
Stored sound data.
Definition: MPTypes.hpp:205
MCBinaryData * ExportRawImageData() const
Export the raw image data.
Definition: MEImage.cpp:396
MPStreamMarkers()
Struct constructor.
Definition: MPTypes.cpp:270
void Clone(const MCBinaryData &other)
Clone an other binary data instance.
static void DecompressQuickLz(const MCBinaryData &input_buffer, MCBinaryData &output_buffer, bool without_header=false)
Decompress data with QuickLZ algorithm.
Definition: MCZipper.cpp:106
void Clear()
Clear the container.
Definition: MPTypes.cpp:144
QJsonObject * MPConvertCollectedDataToJsonQuery(MACollectedData &collected_data)
Convert the collected data in JSON object for query.
Definition: MPTypes.cpp:73
Image.
Definition: MEImage.hpp:112
static MACollectedData * Decode(const MCBinaryData &data)
Decode from binary data.
MCBinaryData * Encode() const
Encode into binary data.
QJsonObject * MPConvertCollectedDataToJson(MACollectedData &collected_data)
Convert the collected data in JSON object.
Definition: MPTypes.cpp:60
MPMissingPackets()
Struct constructor.
Definition: MPTypes.cpp:260
Definition: MPContext.hpp:37
static std::string RobotStateVariableMap
Cached variable map for robot state decoding.
Definition: MPTypes.hpp:266
Base class for the data exchange of the elements.
Definition: MSData.hpp:54
MPRecoveredPackets()
Struct constructor.
Definition: MPTypes.cpp:265
MPCursorPosition()
Struct constructor.
Definition: MPTypes.cpp:101
int GetSessionID()
Get session ID.
MPRecordsContainer()
Struct constructor.
Definition: MPTypes.cpp:121
MP::LogMessagesPtr LogMessages
Stored log messages.
Definition: MPTypes.hpp:203
static MARobotState * Decode(const MCBinaryData &data, const std::string &variable_map="")
Decode from binary data form.
bool Decompress(const MCBinaryData &data)
Decompress image data.
Definition: MEImage.cpp:449
Robot state record struct.
Definition: MPTypes.hpp:187
MPLogEntryMap()
Struct constructor.
Definition: MPTypes.cpp:96
MACollectedData * MPConvertJsonToCollectedData(QJsonObject &json_object)
Convert a JSON object back to collected data.
Definition: MPTypes.cpp:87
int GetSize() const
Get binary data size.