CCA
TickConverter.cpp
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2014 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #include "TickConverter.h"
00028 
00029 #include <rsb/converter/SerializationException.h>
00030 #include <rsb/converter/ProtocolBufferConverter.h>
00031 
00032 #include <rst/cbse/Tick.pb.h>
00033 
00034 using namespace std;
00035 using namespace nemo;
00036 
00037 namespace cca {
00038 namespace timing {
00039 namespace converters {
00040 
00041 TickConverter::TickConverter() :
00042                 rsb::converter::Converter<string>(
00043                         rsc::runtime::typeName<rst::cbse::Tick>(),
00044                         RSB_TYPE_TAG(cca::timing::Tick)), converter() {
00045     converter = boost::shared_ptr<rsb::converter::Converter<string> >(
00046             new rsb::converter::ProtocolBufferConverter<rst::cbse::Tick>);
00047 }
00048 
00049 TickConverter::~TickConverter() {
00050 }
00051 
00052 string TickConverter::getWireSchema() const {
00053     return converter->getWireSchema();
00054 }
00055 
00056 string TickConverter::serialize(const rsb::AnnotatedData &data, string &wire) {
00057 
00058     // Cast to original domain type
00059     boost::shared_ptr<cca::timing::Tick> domain = boost::static_pointer_cast<
00060             cca::timing::Tick>(data.second);
00061 
00062     // Fill protocol buffer object
00063     rst::cbse::Tick proto;
00064 
00065     //    1. reserve()
00066     //    2. std::copy(begin, begin+n) // oder memcpy
00067     proto.set_sequence_number(domain->getSequenceNumber());
00068     proto.mutable_timestamp()->set_time(domain->getTimeStamp());
00069     proto.mutable_virtual_timestamp()->set_time(domain->getVirtualTimeStamp());
00070     proto.mutable_timestep()->set_time(domain->getTimeStep());
00071     proto.mutable_virtual_timestep()->set_time(domain->getVirtualTimeStep());
00072 
00073     // Use embedded ProtoBuf converter for serialization to wire
00074     return converter->serialize(
00075             make_pair(rsc::runtime::typeName<rst::cbse::Tick>(),
00076                     boost::shared_ptr<void>(&proto, rsc::misc::NullDeleter())),
00077             wire);
00078 }
00079 
00080 rsb::AnnotatedData TickConverter::deserialize(const std::string &wireType,
00081         const std::string &wire) {
00082 
00083     // Deserialize and cast to specific ProtoBuf type
00084     boost::shared_ptr<rst::cbse::Tick> proto = boost::static_pointer_cast<
00085             rst::cbse::Tick>(converter->deserialize(wireType, wire).second);
00086 
00087     // Read domain data from ProtoBuf
00088     TickPtr domain(new cca::timing::Tick(proto->sequence_number()));
00089     domain->setTimeStamp(proto->timestamp().time());
00090     domain->setVirtualTimeStamp(proto->virtual_timestamp().time());
00091     domain->setTimeStep(proto->timestep().time());
00092     domain->setVirtualTimeStep(proto->virtual_timestep().time());
00093 
00094     return rsb::AnnotatedData(getDataType(), domain);
00095 }
00096 
00097 }
00098 }
00099 }