util/MessageUnit.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      MessageUnit.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Holding a message
00006 -----------------------------------------------------------------------------*/
00007 
00008 #ifndef _UTIL_MESSAGEUNIT_C
00009 #define _UTIL_MESSAGEUNIT_C
00010 
00011 #if defined(_WIN32)
00012   #pragma warning(disable:4786)
00013 #endif
00014  
00015 #include <string>
00016 #include <vector>
00017 #include <util/Log.h>
00018 #include <util/MessageUnit.h>
00019 
00020 using namespace std;
00021 using namespace org::xmlBlaster::util;
00022 using namespace org::xmlBlaster::util::key;
00023 using namespace org::xmlBlaster::util::qos;
00024 using namespace org::xmlBlaster::client::qos;
00025 using namespace org::xmlBlaster::client::key;
00026 
00027 namespace org { namespace xmlBlaster { namespace util {
00028 
00029 MessageUnit::MessageUnit(const KeyData &key,
00030                          unsigned long len,
00031                          const unsigned char * content, 
00032                          const QosData &qos)
00033    : key_(key.getClone()),
00034      len_(len), content_(0),
00035      qos_(qos.getClone()),
00036      immutableSizeInBytes_(0)
00037 {
00038   if (len_ > 0) {
00039      content_ = new unsigned char[len_];
00040      memcpy(content_, content, len_);
00041   }
00042 }
00043 
00047 MessageUnit::MessageUnit(const KeyData &key,
00048                          const string &content, 
00049                          const QosData &qos)
00050    : key_(key.getClone()),
00051      len_(content.size()), content_(0),
00052      qos_(qos.getClone()),
00053      immutableSizeInBytes_(0)
00054 {
00055    if (len_ > 0) {
00056       content_ = new unsigned char[len_];
00057       memcpy(content_, content.c_str(), len_);
00058    }
00059 }
00060 
00061 
00065 MessageUnit::MessageUnit(const PublishKey& xmlKey,
00066                          const string &content, 
00067                          PublishQos& publishQos)
00068    : key_(xmlKey.getData().getClone()),
00069      len_(content.size()),
00070      content_(0),
00071      qos_(publishQos.getData().getClone()),
00072      immutableSizeInBytes_(0)
00073 {
00074    if (len_ > 0) {
00075       content_ = new unsigned char[len_];
00076       memcpy(content_, content.c_str(), len_);
00077    }
00078 }
00079 
00080 
00084 MessageUnit::MessageUnit(const KeyData &xmlKey,
00085                          const vector<unsigned char> &contentVec, 
00086                          const QosData &qos)
00087      : key_(xmlKey.getClone()),
00088        len_(contentVec.size()), content_(0),
00089        qos_(qos.getClone()),
00090        immutableSizeInBytes_(0)
00091 {
00092    if (len_ > 0) {
00093       content_ = new unsigned char[len_];
00094       for (unsigned int ii=0; ii<len_; ii++) {
00095          content_[ii] = contentVec[ii];
00096       }
00097    }
00098 }
00099 
00100 
00104 MessageUnit::MessageUnit(const PublishKey &xmlKey,
00105                          const vector<unsigned char> &contentVec, 
00106                          PublishQos& publishQos)
00107    : key_(xmlKey.getData().getClone()),
00108      len_(contentVec.size()), content_(0),
00109      qos_(publishQos.getData().getClone()),
00110      immutableSizeInBytes_(0)
00111 {
00112    if (len_ > 0) {
00113       content_ = new unsigned char[len_];
00114       for (unsigned int ii=0; ii<len_; ii++) {
00115          content_[ii] = contentVec[ii];
00116       }
00117    }
00118 }
00119 
00123 MessageUnit::MessageUnit(const MessageUnit& rhs) 
00124    : key_(rhs.getKeyRef()),
00125      len_(rhs.getContentLen()),
00126      content_(0),
00127      qos_(rhs.getQosRef()),
00128      immutableSizeInBytes_(rhs.getSizeInBytes())
00129 {
00130    if (len_ > 0) {
00131       content_ = new unsigned char[len_];
00132       memcpy(content_, rhs.getContent(), len_);
00133    }
00134 }
00135 
00139 MessageUnit& MessageUnit::operator=(const MessageUnit& rhs) 
00140 {
00141    if (this != &rhs) {
00142       key_ = rhs.getKeyRef();
00143       len_ = rhs.getContentLen();
00144       if (len_ > 0) {
00145          content_ = new unsigned char[len_];
00146          memcpy(content_, rhs.getContent(), len_);
00147       }
00148       qos_ = rhs.getQosRef();
00149       immutableSizeInBytes_ = rhs.getSizeInBytes();
00150    }
00151    return *this;
00152 }
00153 
00154 MessageUnit::~MessageUnit() 
00155 {
00156   if (content_ != 0)
00157      delete [] content_;
00158 }
00159 
00160 const KeyData& MessageUnit::getKey() const
00161 {
00162    return *key_;
00163 }
00164 
00165 const KeyDataRef MessageUnit::getKeyRef() const
00166 {
00167    return key_;
00168 }
00169 
00170 vector<unsigned char> MessageUnit::getContentVec() const 
00171 {
00172   vector<unsigned char> vec;
00173   if (len_ > 0) {
00174      vec.reserve(len_);
00175      for (unsigned int ii=0; ii<len_; ii++) {
00176        vec.push_back(content_[ii]);
00177      }
00178   }
00179   return vec;
00180 }
00181 
00182 size_t MessageUnit::getSizeInBytes() const
00183 {
00184    if (immutableSizeInBytes_ > 0) return immutableSizeInBytes_;
00185    // See org.xmlBlaster.engine.MsgUnitWrapper.java
00186    immutableSizeInBytes_ = 306 + len_ + key_->toXml().size() + qos_->toXml().size();
00187    return immutableSizeInBytes_;
00188 }
00189 
00190 std::string MessageUnit::getContentStr() const {
00191    if (len_ < 1) {
00192       return "";
00193    }
00194    return std::string(reinterpret_cast<const char *>(content_), static_cast<std::string::size_type>(len_));
00195 }
00196 
00197 const QosData& MessageUnit::getQos() const
00198 {
00199    return *qos_;
00200 }
00201 
00202 const QosDataRef MessageUnit::getQosRef() const
00203 {
00204    return qos_;
00205 }
00206 
00207 string MessageUnit::toXml(const string &extraOffset) const
00208 {
00209    string ret;
00210    string offset = Constants::OFFSET + extraOffset;
00211 
00212    ret += offset + "<MessageUnit>";
00213    ret += getKey().toXml(Constants::INDENT+extraOffset);
00214    if (len_ > 0) {
00215       ret += offset + " <content>" + getContentStr() + "</content>";
00216    }
00217    ret += getQos().toXml(Constants::INDENT+extraOffset);
00218    ret += offset + "</MessageUnit>";
00219    return ret;
00220 }
00221 
00222 }}} // namespace
00223 #endif