📄 psinterface.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 PSInterface_cxx_Version = "$Id: PSInterface.cxx,v 1.11 2002/09/26 01:14:02 bko Exp $";#include <stdlib.h>#include <stdio.h>#include <strstream.h>#include <unistd.h>#include <errno.h>#include "PSInterface.hxx"#include "VNetworkException.hxx"#include "VIoException.hxx"#include "cpLog.h"const int PSInterface::NUM_TRIES = 10;const int PSInterface::TIME_DELAY = 10;PSInterface::PSInterface(const char* primhost, const int primport, const char* althost, const int altport, const char* password) : _primary(primhost, primport, password), _secondary(althost, altport, password){ cpLog(LOG_DEBUG, "Primary Serve is %s:%d, Secondary: %s:%d", primhost, primport, althost, altport); // start by trying to talk to primary _interface = &_primary; _interface->setUse(true);}voidPSInterface::swapServers(string error){ cpLog(LOG_ERR, "Failed to talk to pserver: %s", error.c_str()); cpLog(LOG_ERR, "swapping pservers"); // check which is being used, and swap 'em if (_primary.inUse()) { _primary.setUse(false); _interface = &_secondary; _secondary.setUse(true); } else { _secondary.setUse(false); _interface = &_primary; _primary.setUse(true); }}// for most of these, I emulate a blocking socket by forever trying first// one server, then the otherstringPSInterface::readItem(const string& group, const string &name)throw (VException&){ int numTries = 0; string dRet; while (1) { while (numTries < NUM_TRIES) { try { string dRet = _interface->readItem(group, name); return dRet; } catch (VNetworkException& e) { swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return dRet;}voidPSInterface::getList(const string& group, vector < Data > &retList)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->getList(group, retList); return ; } catch (VNetworkException& e) { swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::writeItem(const string& group, const string &name, const string &contents)throw (VNetworkException&){ int numTries = 0; string dRet; while (1) { while (numTries < NUM_TRIES) { try { _interface->writeItem(group, name, contents); return ; } catch (VNetworkException& e) { swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::deleteSubscriber(const string& subscriberName)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->deleteSubscriber(subscriberName); return ; } catch (VNetworkException& e) { swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::registerForItem(const string &group, const string &name, const string &hostName)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->registerForItem(group, name, hostName); return ; } catch (VNetworkException& e) { cpLog(LOG_DEBUG, "register item failed"); swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::registerForGroup(const string &group, const string &hostName)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->registerForGroup(group, hostName); return ; } catch (VNetworkException& e) { cpLog(LOG_DEBUG, "register group failed"); swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::unregisterForItem(const string &group, const string &name, const string &hostName)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->unregisterForItem(group, name, hostName); return ; } catch (VNetworkException& e) { cpLog(LOG_DEBUG, "unregister item failed"); swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } return ;}voidPSInterface::unregisterForGroup(const string &group, const string &hostName)throw (VNetworkException&){ int numTries = 0; while (1) { while (numTries < NUM_TRIES) { try { _interface->unregisterForGroup(group, hostName); return ; } catch (VNetworkException& e) { cpLog(LOG_DEBUG, "unregister group failed"); swapServers(e.getDescription()); } numTries++; } cpLog(LOG_ERR, "Both Servers are dead. Sleeping."); sleep(TIME_DELAY); numTries = 0; } 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 + -