util/qos/address/AddressBase.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      AddressBase.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Holding connect address and callback address string including protocol
00006 Version:   $Id: AddressBase.cpp 14417 2005-12-22 12:13:15Z ruff $
00007 ------------------------------------------------------------------------------*/
00008 
00018 #include <util/qos/address/AddressBase.h>
00019 #include <util/lexical_cast.h>
00020 #include <util/Global.h>
00021 
00022 using namespace std;
00023 using namespace org::xmlBlaster::util;
00024 
00025 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace address {
00026 
00027 const int    DEFAULT_port               = 3412;
00028 const string DEFAULT_type               = Global::getDefaultProtocol(); //"SOCKET";
00029 const string DEFAULT_version            = "1.0";
00030 const long   DEFAULT_collectTime        = 0;
00031 const int    DEFAULT_burstModeMaxEntries= 1;
00032 const long   DEFAULT_burstModeMaxBytes  = -1L;
00033 const bool   DEFAULT_oneway             = false;
00034 const bool   DEFAULT_dispatcherActive   = true;
00035 const string DEFAULT_compressType       = "";
00036 const long   DEFAULT_minSize            = 0L;
00037 const bool   DEFAULT_ptpAllowed         = true;
00038 const string DEFAULT_sessionId          = "unknown";
00039 const bool   DEFAULT_useForSubjectQueue = true;
00040 string DEFAULT_dispatchPlugin     = "";
00041 string ATTRIBUTE_TAG = "attribute";
00042 
00043 
00044 AddressBase::AddressBase(Global& global, const string& rootTag)
00045    : ReferenceCounterBase(), global_(global), log_(global.getLog("org.xmlBlaster.util.qos")),
00046       attributes_()
00047 {
00048 
00049    defaultPingInterval_ = 0L;
00050    defaultRetries_      = 0;
00051    defaultDelay_        = 0L;
00052 
00053    // set the defaults here ...
00054    ME                   = "AddressBase";
00055    nodeId_              = "";
00056    maxEntries_          = 0;
00057    address_             = "";
00058    hostname_            = "";
00059    isHardcodedHostname_ = false;
00060    type_                = DEFAULT_type;
00061    port_                = DEFAULT_port;
00062    version_             = DEFAULT_version;
00063    collectTime_         = DEFAULT_collectTime;
00064    burstModeMaxEntries_ = DEFAULT_burstModeMaxEntries;
00065    burstModeMaxBytes_   = DEFAULT_burstModeMaxBytes;
00066    pingInterval_        = defaultPingInterval_;
00067    retries_             = defaultRetries_;
00068    delay_               = defaultDelay_;
00069    oneway_              = DEFAULT_oneway;
00070    dispatcherActive_    = DEFAULT_dispatcherActive;
00071    compressType_        = DEFAULT_compressType;
00072    minSize_             = DEFAULT_minSize;
00073    ptpAllowed_          = DEFAULT_ptpAllowed;
00074    sessionId_           = DEFAULT_sessionId;
00075    useForSubjectQueue_  = DEFAULT_useForSubjectQueue;
00076    dispatchPlugin_      = DEFAULT_dispatchPlugin;
00077    setRootTag(rootTag);
00078 }
00079 
00080 AddressBase::AddressBase(const AddressBase& addr)
00081    : global_(addr.global_), log_(addr.log_),
00082       attributes_()
00083 {
00084    copy(addr);
00085 }
00086 
00087 AddressBase& AddressBase::operator =(const AddressBase& addr)
00088 {
00089    copy(addr);
00090    return *this;
00091 }
00092 
00093 
00094 AddressBase::~AddressBase()
00095 {
00096 }
00097 
00098 void AddressBase::copy(const AddressBase& addr)
00099 {
00100    port_                = addr.port_;
00101    ME                   = addr.ME;
00102    rootTag_             = addr.rootTag_;
00103    address_             = addr.address_;
00104    hostname_            = addr.hostname_;
00105    isHardcodedHostname_ = addr.isHardcodedHostname_;
00106    type_                = addr.type_;
00107    version_             = addr.version_;
00108    collectTime_         = addr.collectTime_;
00109    pingInterval_        = addr.pingInterval_;
00110    retries_             = addr.retries_;
00111    delay_               = addr.delay_;
00112    oneway_              = addr.oneway_;
00113    dispatcherActive_    = addr.dispatcherActive_;
00114    compressType_        = addr.compressType_;
00115    minSize_             = addr.minSize_;
00116    ptpAllowed_          = addr.ptpAllowed_;
00117    sessionId_           = addr.sessionId_;
00118    useForSubjectQueue_  = addr.useForSubjectQueue_;
00119    dispatchPlugin_      = addr.dispatchPlugin_;
00120    nodeId_              = addr.nodeId_;
00121    maxEntries_          = addr.maxEntries_;
00122    defaultPingInterval_ = addr.defaultPingInterval_;
00123    defaultRetries_      = addr.defaultRetries_;
00124    defaultDelay_        = addr.defaultDelay_;
00125    attributes_          = addr.attributes_;
00126 }
00127 
00131 string AddressBase::getName()
00132 {
00133    return getHostname() + string(":") + lexical_cast<std::string>(getPort());
00134 }
00135 
00139 bool AddressBase::isSameAddress(AddressBase& other)
00140 {
00141    string oa = other.getRawAddress();
00142    if ( (oa!="") && (oa == getRawAddress()) ) return true;
00143    string oh = other.getHostname();
00144    int op = other.getPort();
00145    if ( (op>0) && (op==getPort()) && (oh!="") && (oh==getHostname()))
00146       return true;
00147    return false;
00148 }
00149 
00153 string AddressBase::getSettings() const
00154 {
00155    string ret = string("type=") + type_ + string(" oneway=") + lexical_cast<std::string>(oneway_) + string(" burstMode.collectTime=") + lexical_cast<std::string>(getCollectTime());
00156    return ret;
00157 }
00158 
00162 void AddressBase::setType(const string& type)
00163 {
00164    type_ = type;
00165 }
00166 
00170 void AddressBase::setVersion(const string& version)
00171 {
00172    version_ = version;
00173 }
00174 
00179 void AddressBase::setHostname(const string& host)
00180 {
00181    initHostname(host);
00182    isHardcodedHostname_ = true;
00183 }
00184 
00189 bool AddressBase::isHardcodedHostname()
00190 {
00191    return isHardcodedHostname_;
00192 }
00193 
00197 bool AddressBase::hasHostname() {
00198    return hostname_ != "";
00199 }
00200 
00204 string AddressBase::getHostname() const
00205 {
00206    if (hostname_ == "") {
00207       hostname_ = global_.getBootstrapHostname();
00208       address_  = ""; // reset cache
00209    }
00210    return hostname_;
00211 }
00212 
00217 void AddressBase::setPort(int port)
00218 {
00219    port_    = port;
00220    address_ = ""; // reset cache
00221 }
00222 
00223 int AddressBase::getPort() const
00224 {
00225    return port_;
00226 }
00227 
00233 void AddressBase::setAddress(const string& address)
00234 {
00235    address_ = address;
00236 }
00237 
00238 // TODO: Protocol abstraction from plugin
00239 string AddressBase::getRawAddress() const
00240 {
00241    if (address_ == "") {
00242       string schema = (getType() == Constants::SOCKET) ? "socket" : "http";
00243       address_ = schema + "://" + getHostname();  // socket://192.168.1.1:7607
00244       if (getPort() > 0)
00245          address_ += ":" + lexical_cast<std::string>(getPort());
00246    }
00247    return address_;
00248 }
00249 
00254 string AddressBase::getType() const
00255 {
00256    return type_;
00257 }
00258 
00263 string AddressBase::getVersion() const
00264 {
00265    return version_;
00266 }
00267 
00275 string AddressBase::getOnExhaust() const
00276 {
00277    return Constants::ONEXHAUST_KILL_SESSION; // in future possibly Constants.ONEXHAUST_KILL_CALLBACK
00278 }
00279 
00283 bool AddressBase::getOnExhaustKillSession() const
00284 {
00285    return getOnExhaust() == Constants::ONEXHAUST_KILL_SESSION;
00286 }
00287 
00292 long AddressBase::getCollectTime() const
00293 {
00294    return collectTime_;
00295 }
00296 
00301 void AddressBase::setCollectTime(long collectTime)
00302 {
00303    if (collectTime < 0) collectTime_ = 0;
00304    else collectTime_ = collectTime;
00305 }
00306 
00307 int AddressBase::getBurstModeMaxEntries() const
00308 {
00309    return burstModeMaxEntries_;
00310 }
00311 
00312 void AddressBase::setBurstModeMaxEntries(int burstModeMaxEntries)
00313 {
00314    if (burstModeMaxEntries < -1) {
00315       burstModeMaxEntries_ = 1;
00316    }
00317    else if (burstModeMaxEntries == 0) {
00318       log_.warn(ME, string("<burstMode maxEntries='") + lexical_cast<std::string>(burstModeMaxEntries) + string("'> is not supported and may cause strange behavior"));
00319       burstModeMaxEntries_ = burstModeMaxEntries;
00320    }
00321    else {
00322       burstModeMaxEntries_ = burstModeMaxEntries;
00323    }
00324 }
00325 
00326 long AddressBase::getBurstModeMaxBytes() const
00327 {
00328    return burstModeMaxBytes_;
00329 }
00330 
00331 void AddressBase::setBurstModeMaxBytes(long burstModeMaxBytes)
00332 {
00333    if (burstModeMaxBytes < -1) burstModeMaxBytes_ = -1;
00334    else burstModeMaxBytes_ = burstModeMaxBytes;
00335 }
00336 
00341 long AddressBase::getPingInterval() const
00342 {
00343    return pingInterval_;
00344 }
00345 
00350 void AddressBase::setPingInterval(long pingInterval)
00351 {
00352    if (pingInterval <= 0) pingInterval_ = 0;
00353    else if (pingInterval < 10) {
00354       log_.warn(ME, string("pingInterval=") + lexical_cast<std::string>(pingInterval) + string(" msec is too short, setting it to 10 millis"));
00355       pingInterval_ = 10;
00356    }
00357    else pingInterval_ = pingInterval;
00358 }
00359 
00364 int AddressBase::getRetries() const
00365 {
00366    return retries_;
00367 }
00368 
00373 void AddressBase::setRetries(int retries)
00374 {
00375    if (retries < -1) retries_ = -1;
00376    else retries_ = retries;
00377 }
00378 
00383 long AddressBase::getDelay() const
00384 {
00385    return delay_;
00386 }
00387 
00391 void AddressBase::setDelay(long delay)
00392 {
00393    if (delay < 0) delay_ = 0;
00394    else delay_ = delay;
00395 }
00396 
00402 bool AddressBase::oneway() const
00403 {
00404    return oneway_;
00405 }
00406 
00412 void AddressBase::setOneway(bool oneway)
00413 {
00414    oneway_ = oneway;
00415 }
00416 
00417 bool AddressBase::isDispatcherActive() const
00418 {
00419    return dispatcherActive_;
00420 }
00421 
00422 void AddressBase::setDispatcherActive(bool dispatcherActive)
00423 {
00424    dispatcherActive_ = dispatcherActive;
00425 }
00426 
00430 void AddressBase::setPtpAllowed(bool ptpAllowed)
00431 {
00432    ptpAllowed_ = ptpAllowed;
00433 }
00434 
00438 bool AddressBase::isPtpAllowed()
00439 {
00440    return ptpAllowed_;
00441 }
00442 
00443 void AddressBase::setCompressType(const string& compressType)
00444 {
00445    compressType_ = compressType;
00446 
00447    // TODO !!!
00448    if (compressType != "")
00449       log_.warn(ME, "Compression of messages is not yet supported");
00450 }
00451 
00456 string AddressBase::getSecretSessionId() const
00457 {
00458    return sessionId_;
00459 }
00460 
00462 void AddressBase::setSecretSessionId(const string& sessionId)
00463 {
00464    sessionId_ = sessionId;
00465 }
00466 
00471 string AddressBase::getCompressType() const
00472 {
00473    return compressType_;
00474 }
00475 
00482 long AddressBase::getMinSize() const
00483 {
00484    return minSize_;
00485 }
00486 
00493 void AddressBase::setMinSize(long minSize)
00494 {
00495    minSize_ = minSize;
00496 }
00497 
00513 void AddressBase::setDispatchPlugin(const string& dispatchPlugin)
00514 {
00515    dispatchPlugin_ = dispatchPlugin;
00516 }
00517 
00521 string AddressBase::getDispatchPlugin() const
00522 {
00523    return dispatchPlugin_;
00524 }
00525 
00526 void AddressBase::addAttribute(const ClientProperty& attribute)
00527 {
00528    attributes_.insert(ClientPropertyMap::value_type(attribute.getName(), attribute));   
00529 }
00530 
00531 const AddressBase::ClientPropertyMap& AddressBase::getAttributes() const
00532 {
00533    return attributes_;
00534 }
00535 
00536 string AddressBase::dumpAttributes(const string& extraOffset, bool clearText) const
00537 {
00538    string ret = "";
00539    QosData::ClientPropertyMap::const_iterator iter = attributes_.begin();
00540    while (iter != attributes_.end()) {
00541       const ClientProperty& cp = (*iter).second;
00542       ret += cp.toXml(extraOffset, clearText, ATTRIBUTE_TAG);
00543       iter++;
00544    }
00545    return ret;
00546 }
00547 
00548 
00556 string AddressBase::toXml(const string& extraOffset) const
00557 {
00558    //if (log_.call()) log_.call(ME, "::toXml");
00559    string ret;
00560    string offset = Constants::OFFSET + extraOffset;
00561    string offset2 = offset + Constants::INDENT;
00562 
00563    // <address type='SOCKET' bootstrapHostname='127.0.0.1' dispatchPlugin='undef'> ...
00564    ret += offset + string("<") + rootTag_  + string(" type='") + getType() + string("'");
00565    if ( (getVersion()!="") && (getVersion()!=DEFAULT_version))
00566       ret += string(" version='") + getVersion() + string("'");
00567    if (getHostname() != "")
00568       ret += string(" bootstrapHostname='") + getHostname() + string("'");
00569    if (DEFAULT_port != getPort())
00570        ret += string(" bootstrapPort='") + lexical_cast<std::string>(getPort()) + string("'");
00571    if (DEFAULT_sessionId != getSecretSessionId())
00572        ret += string(" sessionId='") + getSecretSessionId() + string("'");
00573    if (defaultPingInterval_ != getPingInterval())
00574        ret += string(" pingInterval='") + lexical_cast<std::string>(getPingInterval()) + string("'");
00575    if (defaultRetries_ != getRetries())
00576        ret += string(" retries='") + lexical_cast<std::string>(getRetries()) + string("'");
00577    if (defaultDelay_ != getDelay())
00578        ret += string(" delay='") + lexical_cast<std::string>(getDelay()) + string("'");
00579    if (DEFAULT_oneway != oneway()) {
00580        ret += string(" oneway='") + lexical_cast<std::string>(oneway()) + string("'");
00581    }
00582    if (DEFAULT_dispatcherActive != isDispatcherActive()) {
00583        ret += string(" dispatcherActive='") + lexical_cast<std::string>(isDispatcherActive()) + string("'");
00584    }
00585    if (DEFAULT_useForSubjectQueue != useForSubjectQueue_) {
00586        ret += string(" useForSubjectQueue='") + lexical_cast<std::string>(useForSubjectQueue_) + string("'");
00587    }
00588    if (DEFAULT_dispatchPlugin != dispatchPlugin_)
00589        ret += string(" dispatchPlugin='") + dispatchPlugin_ + string("'");
00590    ret += string(">");
00591    if (getRawAddress() != "")
00592       ret += offset + string("   ") + getRawAddress();
00593    if (getCollectTime() != DEFAULT_collectTime || getBurstModeMaxEntries() != DEFAULT_burstModeMaxEntries || getBurstModeMaxBytes() != DEFAULT_burstModeMaxBytes) {
00594       ret += offset2 + string("<burstMode");
00595       if (getCollectTime() != DEFAULT_collectTime)
00596          ret += string(" collectTime='") + lexical_cast<std::string>(getCollectTime()) + string("'");
00597       if (getBurstModeMaxEntries() != DEFAULT_burstModeMaxEntries)
00598          ret += string(" maxEntries='") + lexical_cast<std::string>(getBurstModeMaxEntries()) + string("'");
00599       if (getBurstModeMaxBytes() != DEFAULT_burstModeMaxBytes)
00600          ret += string(" maxBytes='") + lexical_cast<std::string>(getBurstModeMaxBytes()) + string("'");
00601       ret += string("/>");
00602    }
00603    if (getCompressType() != DEFAULT_compressType)
00604       ret += offset2 + string("<compress type='") + getCompressType() + string("' minSize='") + lexical_cast<std::string>(getMinSize()) + string("'/>");
00605    if (ptpAllowed_ != DEFAULT_ptpAllowed) {
00606       ret += offset2 + string("<ptp>") + lexical_cast<std::string>(ptpAllowed_) + string("</ptp>");
00607    }
00608    ret += dumpAttributes(offset2);
00609    ret += offset + string("</") + rootTag_ + string(">");
00610    return ret;
00611 }
00612 
00613 }}}}} // namespaces
00614 
00615