📄 provisionserver.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 ProvisionServer_cxx_Version = "$Id: ProvisionServer.cxx,v 1.94.2.2 2003/02/04 22:12:19 bko Exp $";//Standard Include#include <cerrno>#include <csignal>#include <unistd.h>#include <cstring>#include <cstdio>#include <algorithm>//User defined#include "VEnvVar.hxx"#include "VNetworkException.hxx"#include "VIoException.hxx"#include "VThreadPool.hxx"#include "VCondition.h"#include "NetworkAddress.h"#include "Vpp_def.h"#include "VPs_def.h"#include "cpLog.h"#include "PServerContainer.hxx"#include "LocalScopeAllocator.hxx"#include "HeartbeatTxThread.hxx"#include "HeartbeatRxThread.hxx"#include "ProvisionServer.hxx"#include "vocalconfig.h"#include "PSSecret.hxx"#include "FileStat.hxx"#include "VMarshalServerGroupContainer.hxx"#include "Tcp_ClientSocket.hxx"using Vocal::IO::FileStat;// initialize global static varsstatic const int vppOkGLBL = 1;static const int vppFailGLBL = 0;// intitalize static member varsProvisionServer* ProvisionServer::_instance = NULL;// _clientListLock and _featureListLock not used// pthread_mutex_t ProvisionServer::_clientListLock;// pthread_mutex_t ProvisionServer::_featureListLock;// Use VMutex instead of pthread_mutex directlyVMutex ProvisionServer::_MutexconnectionMapLock;VMutex ProvisionServer::_MutextimerLock;string ProvisionServer::SYS_CONF(VSYS_CONF);string ProvisionServer::FEATURES(VFEATURES);string ProvisionServer::ACCOUNTS(VACCOUNTS);string ProvisionServer::CALLEDLIST(VCALLEDLIST);string ProvisionServer::CALLINGLIST(VCALLINGLIST);// how long to wait in the thread dispatch loop -- gives time to let// a thread mark a connection as being serviced.const int ProvisionServer::THREAD_DELAY(10000);// how long to wait before we abort a sync (we assume it failed if it takes// longer than this)const int ProvisionServer::FAIL_SLEEP_TIME(600);#if defined(__SUNPRO_CC)extern "C" {void ProvisionServer_sigHupHandler(int sigNo){ cerr << "sigHupHandler" << endl;}void ProvisionServer_sigChld(int sigNo){ cerr << "sigChld" << endl;}void ProvisionServer_sigTerm(int sigNo){ cerr << "sigTerm" << endl;}}#else#define ProvisionServer_sigHupHandler ProvisionServer::sigHupHandler#define ProvisionServer_sigChld ProvisionServer::sigChld#define ProvisionServer_sigTerm ProvisionServer::sigTerm#endif/* In all cases while calling the redundant server, remember that there are 4 possible returns -- ERR, which means we are now in an invalid state (unlikely to happen, since we assert on most of these), OK, which means all is well and that we should send an ok message to the twin, FAIL, which means it failed, again send a message about this to twin, and NMSG, which means that we should send no message to the other server (usually when the transition results in no change in state and no action*/// signal handlersvoidProvisionServer::sigTerm(int signo){ cerr << "sigTermHandler" << endl; // Nothing for now...}voidProvisionServer::sigChld(int signo){ cerr << "sigChldHandler" << endl; // Nothing for now...}voidProvisionServer::sigHupHandler(int sigNo){ cerr << "sigHupHandler" << endl; // Nothing for now...}ProvisionServer&ProvisionServer::instance(){ if (!_instance) { cpLog(LOG_ERR, "Called instance() when none had been constructed"); assert(0); } return *_instance;}ProvisionServer&ProvisionServer::instance(int nbins, int nthreads, bool requireTls, bool useTls, const string& etcPath, const string& mcasthost, int mcastport, const string& hostname, int port){ if (!_instance) { if(VEnvVar::VLOG_LEVEL >= 0 && VEnvVar::VLOG_LEVEL <= LAST_PRIORITY) { cpLogSetPriority(VEnvVar::VLOG_LEVEL); } else { cpLogSetPriority(LOG_DEBUG); cpLog(LOG_ERR, "Invalid log string chosen. setting log level to LOG_DEBUG."); } cpLog(LOG_ERR, "Starting pserver..."); string certPath, keyPath, passwdPath, aclPath, readPath, writePath; certPath = etcPath + "/cert.pem"; keyPath = etcPath + "/key.pem"; passwdPath = etcPath + "/vocal.passwd"; aclPath = etcPath + "/vocal.acl"; readPath = etcPath + "/vocal.secret"; writePath = etcPath + "/vocal.writesecret"; _instance = new ProvisionServer(nbins, nthreads, requireTls, useTls, certPath.c_str(), keyPath.c_str(), passwdPath.c_str(), aclPath.c_str(), readPath.c_str(), writePath.c_str()); cpLog(LOG_DEBUG, "Created instance for %s\n", _instance->className().c_str()); } if (hostname != "") { cpLog(LOG_DEBUG, "Server has been started in redundant mode"); _instance->startHeartbeatThreads(hostname, port, mcasthost, mcastport); _instance->_copy.initialize(hostname, port, useTls); _instance->setSyncTimer(); } else { cpLog(LOG_DEBUG, "Server has been started in non-redundant mode"); } return *_instance;}voidProvisionServer::startHeartbeatThreads(const string &hostname, int port, const string &mcasthost, int mcastport){ cpLog(LOG_ERR, "In start threads, using multicast host %s and port %d", mcasthost.c_str(), mcastport); // create and start all the heartbeat threads int localPort = VEnvVar::VPS_PORT.value(); cpLog(LOG_ALERT, "Creating heartbeat trasnsmit thread %d", localPort); _itsHeartbeatTxThread = new HeartbeatTxThread(localPort, 500, mcasthost.c_str(), mcastport); _itsHeartbeatRxThread = new HeartbeatRxThread(PServerContainer::instance(), mcasthost.c_str(), mcastport); _itsHouseKeepingThread = new HouseKeepingThread(PServerContainer::instance(), 500); char buf[256]; sprintf(buf, "%s:%d", hostname.c_str(), port); string remote = buf; PServerContainer::instance().AddServer(Data(remote)); _itsHeartbeatTxThread->run(); _itsHeartbeatRxThread->run(); _itsHouseKeepingThread->run();}ProvisionServer::ProvisionServer(int nbins, int nthreads, bool requireTls, bool useTls, const char* certPath, const char* keyPath, const char* passwdPath, const char* aclPath, const char* readPath, const char* writePath) : _nbins(nbins), _nthreads(nthreads), myRequireTls(requireTls), myUseTls(useTls), myCertPath(certPath), myKeyPath(keyPath), myPasswdFile( passwdPath ), myAclFile( aclPath ), myFileOkTls( true ) { FileStat certStat(certPath); if(!certStat.isFile()) { if(!requireTls) { cpLog(LOG_WARNING, "missing certificate file %s, disabling TLS", certPath); myFileOkTls = false; } else { cpLog(LOG_ERR, "missing certificate file %s", certPath); exit(-1); } } FileStat keyStat(keyPath); if(!keyStat.isFile()) { if(!requireTls) { cpLog(LOG_WARNING, "missing key file %s, disabling TLS", keyPath); myFileOkTls = false; } else { cpLog(LOG_ERR, "missing key file %s", keyPath); exit(-1); } } PSSecret::create(readPath, writePath); // verify that the cert/key files exist#if defined(__linux__) if (::atexit(ProvisionServer::destroy) < 0) { cpLog(LOG_ALERT, "Failed to register with atexit()"); }#endif cpLog(LOG_DEBUG, "Creating %d threads for pServer", _nthreads); _threadPool = new VThreadPool(_nthreads); signal(SIGHUP, ProvisionServer_sigHupHandler); signal(SIGCHLD, ProvisionServer_sigChld); // xxx -- why are we preventing sigterm???// signal(SIGTERM, ProvisionServer_sigTerm); timerPending = false; // seed random number generator (used by timer to decide when to try // to sync) srand((int)getpid() + time(NULL)); _dataStore = new FileDataStore(_nbins, getRoot()); sendRegisterUpdate();}voidProvisionServer::run(){ //Loop through for Incoming messages cpLog(LOG_ERR, "Pserver successfully started -- Running"); for (;;) { // get a message, accept it, add it to connection map if (_serverSocket.getServerConn().isReadReady()) { LockedConnection conn; _serverSocket.accept(conn); _MutexconnectionMapLock.lock(); _connectionMap[conn.getConnId()] = conn; _MutexconnectionMapLock.unlock(); } // for each active connection with data, dispatch a thread to // handle it, unless it is already being handled // Connection map lock (cannot close a connection till released) _MutexconnectionMapLock.lock(); for (ConnectionMap::iterator itr = _connectionMap.begin(); itr != _connectionMap.end(); itr++) { assert( (*itr).first == (*itr).second.getConnId()); LockedConnection& conn = (*itr).second; if (conn.isLive() && conn.notServiced() && conn.isReadReady()) { // set the connection as being serviced and pass it to a thread conn.beingServiced(); cpLog(LOG_DEBUG, "servicing connection"); cpLog(LOG_DEBUG, "Connection %d marked", conn.getConnId()); assert(conn.isLive()); assert(conn.isReadReady()); assert(!conn.notServiced()); assert(conn.getConnId() != 0); VFunctor functor(ProvisionServer::processRequestFunc, &conn); _threadPool->addFunctor(functor); } } _MutexconnectionMapLock.unlock(); // Connection map unlocked vusleep(THREAD_DELAY); }}ProvisionServer::~ProvisionServer(){ cpLog(LOG_ERR, "ProvisionServer destructor called"); _serverSocket.close();}voidProvisionServer::destroy(void){ delete ProvisionServer::_instance; ProvisionServer::_instance = NULL;}voidProvisionServer::processRequestFunc(void* con){ // a thread will process the data when it is assigned a request to process LockedConnection& conn = *(static_cast < LockedConnection* > (con)); ProvisionServer::instance().processRequest(conn);}voidProvisionServer::timerFunc(void* arg){ // sleep 5 to 30 seconds and send sync int sleep_time = 5 + ( (int) (25.0 * rand() / (RAND_MAX + 1.0)) ); cpLog(LOG_DEBUG, "Setting Sync Timer -- Sleeping %d", sleep_time); sleep(sleep_time); ProvisionServer::instance().initiateSync();}void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -