📄 registermanager.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const RegisterManager_cxx_Version = "$Id: RegisterManager.cxx,v 1.14 2002/06/05 23:27:54 bko Exp $";#include <iostream.h>#include <stdio.h>#include <algorithm>// #include "LockGuard.hxx"#include "cpLog.h"#include "Sptr.hxx"#include "NetworkAddress.h"#include "Tcp_ClientSocket.hxx"#include "Vpp_def.h"#include "RegisterManager.hxx"RegisterManager::RegisterManager(){}voidRegisterManager::registerItemCat(const string &groupAndItem, const string &hostWithPort){ //LockGuard guard(_lock); LockHelper lockAid( this->_aMutex ); ClientHostList &hosts = _itemMap[groupAndItem]; // find it. If not there, add it ClientHostList::iterator itr = find(hosts.begin(), hosts.end(), hostWithPort); if (itr == hosts.end()) { hosts.push_back(hostWithPort); }}voidRegisterManager::registerItem(const string &group, const string &name, const string &hostWithPort){ LockHelper lockAid( this->_aMutex ); string index = group + "|" + name; ClientHostList &hosts = _itemMap[index]; // find it. If not there, add it ClientHostList::iterator itr = find(hosts.begin(), hosts.end(), hostWithPort); if (itr == hosts.end()) { hosts.push_back(hostWithPort); }}voidRegisterManager::unregisterItem(const string &group, const string &name, const string &hostWithPort){ LockHelper lockAid( this->_aMutex ); string index = group + "|" + name; ClientHostList &hosts = _itemMap[index]; // find it. If there, delete it ClientHostList::iterator itr = find(hosts.begin(), hosts.end(), hostWithPort); if (itr != hosts.end()) { hosts.erase(itr); }}voidRegisterManager::registerGroup(const string &group, const string &hostWithPort){ LockHelper lockAid( this->_aMutex ); ClientHostList &hosts = _groupMap[group]; // find it. If not there, add it ClientHostList::iterator itr = find(hosts.begin(), hosts.end(), hostWithPort); if (itr == hosts.end()) { hosts.push_back(hostWithPort); }}voidRegisterManager::unregisterGroup(const string &group, const string &hostWithPort){ LockHelper lockAid( this->_aMutex ); ClientHostList &hosts = _groupMap[group]; // find it. If there, delete it ClientHostList::iterator itr = find(hosts.begin(), hosts.end(), hostWithPort); if (itr != hosts.end()) { hosts.erase(itr); }}voidRegisterManager::modifyItem(const string &group, const string &name, bool delFlag){ LockHelper lockAid( this->_aMutex ); string index = group + "|" + name; ClientHostList &hosts = _itemMap[index]; ClientHostList sent; ClientHostList::iterator itr; // look for group+name matches for (itr = hosts.begin(); itr != hosts.end(); itr++) { sent.push_back(*itr); if (delFlag) { sendItemDelete(group, name, *itr); } else { sendItemModify(group, name, *itr); } } // now get the list of servers registered for the group. Make sure we // don't send twice. ClientHostList &hosts2 = _groupMap[group]; // only send if we haven't already sent an update for (itr = hosts2.begin(); itr != hosts2.end(); itr++) { // look for it in the sent list. If not there, send ClientHostList::iterator finder = find(sent.begin(), sent.end(), *itr); if (finder == sent.end()) { if (delFlag) { sendGroupDelete(group, name, *itr); } else { sendGroupModify(group, name, *itr); } } }}void RegisterManager::deleteItem(const string &group, const string &name){ modifyItem(group, name, true);}voidRegisterManager::getList(string &list){ // this is ugly and inneficent... list = ""; list += "++ITEM++,"; ClientHostMap::iterator itr; ClientHostList::iterator itr2; for (itr = _itemMap.begin(); itr != _itemMap.end(); itr++) { for (itr2 = ((*itr).second).begin(); itr2 != ((*itr).second).end(); itr2++) { list += (*itr).first; list += ","; list += (*itr2); list += ","; } } list += "++GROUP++,"; for (itr = _groupMap.begin(); itr != _groupMap.end(); itr++) { for (itr2 = ((*itr).second).begin(); itr2 != ((*itr).second).end(); itr2++) { list += (*itr).first; list += ","; list += (*itr2); list += ","; } } cpLog(LOG_DEBUG, "List being sent is (%s)", list.c_str()); return ;}// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// THESE SHOULD ALL BE CALLED FROM INSIDE A LOCKED FUNCTION!// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!voidRegisterManager::sendItemModify(const string &group, const string &name, const string &host){ try { NetworkAddress na(host); // if no port was specified (which should be trapped), return if (na.getPort() == -1) { return ; } Sptr < TcpClientSocket > clientSocket = 0; clientSocket = new TcpClientSocket(na); clientSocket->connect(); char buf[MAXLINE]; sprintf(buf, "%s %s %s\n\n", UPDATEITEM_REQ, group.c_str(), name.c_str()); string wData(buf); clientSocket->getConn().writeData(wData); } catch (VException &e) { cpLog(LOG_ALERT, "Failed to send update to %s for item (%s,%s)", host.c_str(), group.c_str(), name.c_str()); return ; } cpLog(LOG_DEBUG, "Sent item modify update for (%s,%s) to (%s)", group.c_str(), name.c_str(), host.c_str()); return ;}voidRegisterManager::sendItemDelete(const string &group, const string &name, const string &host){ try { NetworkAddress na(host); // if no port was specified (which should be trapped), return if (na.getPort() == -1) { return ; } Sptr < TcpClientSocket > clientSocket = 0; clientSocket = new TcpClientSocket(na); clientSocket->connect(); char buf[MAXLINE]; sprintf(buf, "%s %s %s\n\n", DELITEM_REQ, group.c_str(), name.c_str()); string wData(buf); clientSocket->getConn().writeData(wData); } catch (VException &e) { cpLog(LOG_ALERT, "Failed to send update to %s for item (%s,%s)", host.c_str(), group.c_str(), name.c_str()); return ; } cpLog(LOG_DEBUG, "Sent item delete update for (%s,%s) to (%s)", group.c_str(), name.c_str(), host.c_str()); return ;}voidRegisterManager::sendGroupModify(const string &group, const string &name, const string &host){ try { NetworkAddress na(host); // if no port was specified (which should be trapped), return if (na.getPort() == -1) { return ; } Sptr < TcpClientSocket > clientSocket = 0; clientSocket = new TcpClientSocket(na); clientSocket->connect(); char buf[MAXLINE]; sprintf(buf, "%s %s %s\n\n", UPDATEGROUP_REQ, group.c_str(), name.c_str()); string wData(buf); clientSocket->getConn().writeData(wData); } catch (VException &e) { cpLog(LOG_ALERT, "Failed to send update to %s for item (%s,%s)", host.c_str(), group.c_str(), name.c_str()); return ; } cpLog(LOG_DEBUG, "Sent group modify update for (%s,%s) to (%s)", group.c_str(), name.c_str(), host.c_str()); return ;}voidRegisterManager::sendGroupDelete(const string &group, const string &name, const string &host){ try { NetworkAddress na(host); // if no port was specified (which should be trapped), return if (na.getPort() == -1) { return ; } Sptr < TcpClientSocket > clientSocket = 0; clientSocket = new TcpClientSocket(na); clientSocket->connect(); char buf[MAXLINE]; sprintf(buf, "%s %s %s\n\n", DELGROUP_REQ, group.c_str(), name.c_str()); string wData(buf); clientSocket->getConn().writeData(wData); } catch (VException &e) { cpLog(LOG_ALERT, "Failed to send update to %s for item (%s,%s)", host.c_str(), group.c_str(), name.c_str()); return ; } cpLog(LOG_DEBUG, "Sent group delete update for (%s,%s) to (%s)", group.c_str(), name.c_str(), host.c_str()); return ;}/* Local Variables: *//* c-file-style: "stroustrup" *//* indent-tabs-mode: nil *//* c-file-offsets: ((access-label . -) (inclass . ++)) *//* c-basic-offset: 4 *//* End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -