util/cluster/NodeInfo.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      NodeInfo.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Holding information about the current node.
00006 ------------------------------------------------------------------------------*/
00007 
00008 #include <util/cluster/NodeInfo.h>
00009 #include <util/Global.h>
00010 
00011 namespace org { namespace xmlBlaster { namespace util { namespace cluster {
00012 
00013 using namespace std;
00014 using namespace org::xmlBlaster::util;
00015 using namespace org::xmlBlaster::util::qos::address;
00016 using namespace org::xmlBlaster::util::cluster;
00017 
00023 //typedef map<string, CallbackAddress> CbAddressMap;
00024 //typedef map<string, Address>         AddressMap;
00025 //typedef map<string, NodeId>          NodeMap;
00026 
00027 NodeInfo::NodeInfo(Global& global, NodeId nodeId)
00028    : ME(string("NodeInfo.") + nodeId.toString()), global_(global), nodeId_(nodeId),
00029      tmpAddress_(global), tmpCbAddress_(global), cbAddressMap_(), addressMap_(),
00030      backupNodeMap_()
00031 {
00032    init();
00033 }
00034 
00035 NodeInfo::NodeInfo(const NodeInfo& info)
00036    : ME(info.ME), global_(info.global_), nodeId_(info.nodeId_),
00037      tmpAddress_(info.tmpAddress_), tmpCbAddress_(info.tmpCbAddress_),
00038      cbAddressMap_(info.cbAddressMap_), addressMap_(info.addressMap_),
00039      backupNodeMap_(info.backupNodeMap_)
00040 {
00041    copy(info);
00042 }
00043 
00044 NodeInfo& NodeInfo::operator=(const NodeInfo& info)
00045 {
00046    copy(info);
00047    return *this;
00048 }
00049 
00053 string NodeInfo::getId() const
00054 {
00055   return nodeId_.getId();
00056 }
00057 
00061    NodeId NodeInfo::getNodeId() const
00062    {
00063      return nodeId_;
00064    }
00065 
00069    void NodeInfo::setNodeId(NodeId nodeId)
00070    {
00071       nodeId_ = nodeId;
00072    }
00073 
00078    AddressBaseRef NodeInfo::getAddress() const
00079    {
00080       if (addressMap_.empty()) return new Address(global_);
00081       return (*(addressMap_.begin())).second;
00082    }
00083 
00089    void NodeInfo::addAddress(const AddressBaseRef& address)
00090    {
00091       addressMap_.insert(AddressMap::value_type(address->getRawAddress(), address));
00092    }
00093 
00097    AddressMap NodeInfo::getAddressMap() const
00098    {
00099       return addressMap_;
00100    }
00101 
00105    bool NodeInfo::contains(const AddressBaseRef& other)
00106    {
00107       if (addressMap_.empty()) return false;
00108       return (addressMap_.find(other->getRawAddress()) != addressMap_.end());
00109    }
00110 
00115    AddressBaseRef NodeInfo::getCbAddress()
00116    {
00117       if (cbAddressMap_.empty()) {
00118          addCbAddress(new CallbackAddress(global_));
00119       }
00120       return (*(cbAddressMap_.begin())).second;
00121    }
00122 
00126    AddressMap NodeInfo::getCbAddressMap() const
00127    {
00128       return cbAddressMap_;
00129    }
00130 
00134    void NodeInfo::addCbAddress(const AddressBaseRef& cbAddress)
00135    {
00136       cbAddressMap_.insert(AddressMap::value_type(cbAddress->getRawAddress(),cbAddress));
00137    }
00138 
00144    bool NodeInfo::isNameService() const
00145    {
00146       return nameService_;
00147    }
00148 
00154    void NodeInfo::setNameService(bool nameService)
00155    {
00156       nameService_ = nameService;
00157    }
00158 
00163    NodeMap NodeInfo::getBackupnodeMap() const
00164    {
00165       return backupNodeMap_;
00166    }
00167 
00171    void NodeInfo::addBackupnode(const NodeId& backupId)
00172    {
00173       backupNodeMap_.insert(NodeMap::value_type(backupId.getId(), backupId));
00174    }
00175 
00180 /*
00181    bool startElement(String uri, String localName, String name, StringBuffer character, Attributes attrs) {
00182       // glob.getLog("cluster").info(ME, "startElement: name=" + name + " character='" + character.toString() + "'");
00183 
00184       if (name.equalsIgnoreCase("info")) {
00185          return true;
00186       }
00187 
00188       if (inAddress) { // delegate internal tags
00189          if (tmpAddress == null) return false;
00190          tmpAddress.startElement(uri, localName, name, character, attrs);
00191          return true;
00192       }
00193       if (name.equalsIgnoreCase("address")) {
00194          inAddress = true;
00195          tmpAddress = new Address(glob, "", getId());
00196          tmpAddress.startElement(uri, localName, name, character, attrs);
00197          return true;
00198       }
00199 
00200       if (name.equalsIgnoreCase("callback")) {
00201          inCallback = true;
00202          tmpCbAddress = new CallbackAddress(glob);
00203          tmpCbAddress.startElement(uri, localName, name, character, attrs);
00204          return true;
00205       }
00206 
00207       if (name.equalsIgnoreCase("backupnode")) {
00208          inBackupnode = true;
00209          return true;
00210       }
00211       if (inBackupnode && name.equalsIgnoreCase("clusternode")) {
00212          if (attrs != null) {
00213             String tmp = attrs.getValue("id");
00214             if (tmp == null) {
00215                glob.getLog("org.xmlBlaster.cluster").error(ME, "<backupnode><clusternode> attribute 'id' is missing, ignoring message");
00216                throw RuntimeException("NodeParser: <backupnode><clusternode> attribute 'id' is missing, ignoring message");
00217             }
00218             addBackupnode(new NodeId(tmp.trim()));
00219          }
00220          return true;
00221       }
00222 
00223       return false;
00224    }
00225 */
00226 
00230 /*
00231    public final void endElement(String uri, String localName, String name, StringBuffer character) {
00232       if (inAddress) { // delegate address internal tags
00233          tmpAddress.endElement(uri, localName, name, character);
00234          if (name.equalsIgnoreCase("address")) {
00235             inAddress = false;
00236             addAddress(tmpAddress);
00237          }
00238          return;
00239       }
00240 
00241       if (inCallback) { // delegate address internal tags
00242          tmpCbAddress.endElement(uri, localName, name, character);
00243          if (name.equalsIgnoreCase("callback")) {
00244             inCallback = false;
00245             addCbAddress(tmpCbAddress);
00246          }
00247          return;
00248       }
00249 
00250       if (name.equalsIgnoreCase("backupnode")) {
00251          inBackupnode = false;
00252          character.setLength(0);
00253          return;
00254       }
00255 
00256       character.setLength(0);
00257       return;
00258    }
00259 */
00260 
00265    string NodeInfo::toXml(const string& extraOffset)
00266    {
00267       string ret;
00268       string offset = "\n   ";
00269       offset += extraOffset;
00270 
00271       ret += offset + "<info>";
00272       if (!addressMap_.empty()) {
00273          AddressMap::iterator iter = addressMap_.begin();
00274          while (iter != addressMap_.end()) {
00275             const AddressBaseRef& info = (*iter).second;
00276             ret += info->toXml(extraOffset + "   ");
00277             iter++;
00278          }
00279       }
00280  
00281       if (!cbAddressMap_.empty()) {
00282          AddressMap::iterator iter = cbAddressMap_.begin();
00283          while (iter != cbAddressMap_.end()) {
00284             const AddressBaseRef &info = (*iter).second;
00285             ret +=  info->toXml(extraOffset + "   ");
00286             iter++;
00287          }
00288       }
00289 
00290       if (!backupNodeMap_.empty()) {
00291          NodeMap::iterator iter = backupNodeMap_.begin();
00292          ret += offset + "   <backupnode>";
00293          while (iter != backupNodeMap_.end()) {
00294             NodeId info = (*iter).second;
00295             ret += offset + "      <clusternode id='" + info.getId() + "'/>";
00296          }
00297          ret += offset + "   </backupnode>";
00298       }
00299       ret += offset + "</info>";
00300       return ret;
00301    }
00302 
00303 }}}}
00304