221 lines
6.5 KiB
C++
221 lines
6.5 KiB
C++
/*
|
|
* File: TransferMessage.cc
|
|
* Author: jgaebler
|
|
*
|
|
* Created on May 10, 2012, 10:47 AM
|
|
*/
|
|
|
|
#include "TransferMessage.h"
|
|
|
|
#include "Moversight.h"
|
|
#include "mt/msg/MulticastMessage.h"
|
|
#include "common/Exception.h"
|
|
#include "common/time/VirtualLogicalTime.h"
|
|
#include "common/transport/MoversightMessageFactory.h"
|
|
#include "common/transport/msg/MoversightMessage.h"
|
|
|
|
#include "shared_pointer.h"
|
|
#include "byte_array_destination.h"
|
|
#include "byte_array_source.h"
|
|
#include "serializer_byte_array.h"
|
|
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Constructor
|
|
*/
|
|
TransferMessage::TransferMessage() : message(NULL) {
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor
|
|
* @param m The message to transfer
|
|
* @param t The reception time of the message to transfer.
|
|
*/
|
|
TransferMessage::TransferMessage(MulticastMessage * m, VirtualLogicalTime const & t) : message(m), time(t) {
|
|
}
|
|
|
|
/**
|
|
* @brief Copy constructor
|
|
* @param orig The instance to copy
|
|
*/
|
|
TransferMessage::TransferMessage(const TransferMessage & orig) : message(orig.message), time(orig.time) {
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor
|
|
* @note The referencing message is not deleted during destruction!!
|
|
*/
|
|
TransferMessage::~TransferMessage() {
|
|
}
|
|
|
|
/**
|
|
* @brief Permits access to the message to transfer.
|
|
* @return The message to transfer.
|
|
* @throws NullPointerException if no message set
|
|
*/
|
|
MulticastMessage *
|
|
TransferMessage::getMessage() {
|
|
|
|
if (message != NULL) {
|
|
return message;
|
|
}
|
|
|
|
throw NullPointerException("no message set (message == NULL)");
|
|
}
|
|
|
|
/**
|
|
* @brief Sets the message to transfer. The message have to be created using the
|
|
* dup() method. The stored message is deleted, on deleting the owning
|
|
* instance.
|
|
* @param m The message to set.
|
|
* @throws NullPointerException if the given message NULL
|
|
*/
|
|
void
|
|
TransferMessage::setMessage(MulticastMessage * m) {
|
|
|
|
if (m != NULL) {
|
|
message = m;
|
|
}
|
|
else {
|
|
throw NullPointerException("no message to set");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Permits access to the reception time of the message to transfer.
|
|
* @return The reception time of the message.
|
|
*/
|
|
VirtualLogicalTime const &
|
|
TransferMessage::getTime() const {
|
|
return time;
|
|
}
|
|
|
|
/**
|
|
* @brief Sets the reception time.
|
|
* @param t The time to set.
|
|
*/
|
|
void
|
|
TransferMessage::setTime(VirtualLogicalTime const & t) {
|
|
time = t;
|
|
}
|
|
|
|
/**
|
|
* @brief Assignment operator
|
|
* @param rhs The right hand value
|
|
* @return A reference to the local instance.
|
|
*/
|
|
TransferMessage &
|
|
TransferMessage::operator=(TransferMessage const & rhs) {
|
|
|
|
if (this != &rhs) {
|
|
|
|
message = rhs.message;
|
|
time = rhs.time;
|
|
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Serializes the message to the given byte array.
|
|
* @param buffer Buffer to serialize to. The byte array is resized to the size of the message after the serialization.
|
|
* @return True if serialization was successful. False if the serialization failed.
|
|
*/
|
|
bool
|
|
TransferMessage::serialize(ByteArray &buffer) {
|
|
|
|
if(message == NULL){
|
|
return false;
|
|
}//end if
|
|
|
|
//create the serializer
|
|
SerializerByteArray serializer(buffer);
|
|
//add the time
|
|
serializer << time;
|
|
|
|
//error occurred?
|
|
if (serializer.failed()) return false;
|
|
|
|
//serializes the message type
|
|
int type = message->getType();
|
|
serializer << type;
|
|
|
|
//error occurred?
|
|
if (serializer.failed()) return false;
|
|
|
|
//add the serialized size of the message
|
|
int serializeMessageSize = message->calcSerializedArraySize();
|
|
serializer << serializeMessageSize;
|
|
|
|
//serialize the message
|
|
#if OMNETPP
|
|
serializer << message;
|
|
|
|
return !serializer.failed();
|
|
|
|
#else
|
|
|
|
ByteArray messageBuffer;
|
|
bool messageSerializationSuccesfully = message->serialize(messageBuffer);
|
|
//serialize the size of the buffer
|
|
size_t messageBufferSize = messageBuffer.size();
|
|
serializer << messageBufferSize;
|
|
//add the message
|
|
serializer << messageBuffer;
|
|
//buffer.insert(buffer.end(), messageBuffer.begin(), messageBuffer.end());
|
|
return !serializer.failed() && messageSerializationSuccesfully;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Deserializes the transfermessage.
|
|
* @param buffer Buffer to deserialize from.
|
|
* @param size Size of buffer to deserialize from.
|
|
* @return True if serialization was successful. False if the given buffer was to small or is not exactly the same size as the serialized message.
|
|
* @note The buffer has to point to the payload and not to the header of the network message.
|
|
*/
|
|
bool
|
|
TransferMessage::deserialize(char const *buffer, int size) {
|
|
|
|
#if OMNETPP
|
|
ByteArray array(buffer, buffer + size);
|
|
#else
|
|
ByteArray array(buffer, size);
|
|
#endif
|
|
SerializerByteArray serializer(array);
|
|
|
|
//read the time
|
|
serializer >> time;
|
|
|
|
//read the message type
|
|
int type;
|
|
serializer >> type;
|
|
|
|
//not failed? continue with the message
|
|
if (!serializer.failed()) {
|
|
|
|
//retrieve the serialized size of the message
|
|
int size;
|
|
serializer >> size;
|
|
|
|
MoversightMessageFactory factory;
|
|
//SharedPointer<MoversightMessage> received = factory.createMessage(type, (buffer + serializer.getSerializedArraySize()), size);
|
|
message = dynamic_cast<MulticastMessage *>(factory.createMessage(type, (buffer + serializer.getSerializedArraySize()), size).data())->dup();
|
|
//message = dynamic_cast<MulticastMessage *>(received.data())->dup();
|
|
std::cout<<"check message deserialize"<<endl;
|
|
//received.reset();
|
|
|
|
}//end if
|
|
|
|
return !serializer.failed();
|
|
|
|
}
|
|
}
|
|
}
|