⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 b2bcmdline.cxx

📁 Vovida 社区开源的 SIP 协议源码
💻 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 B2bCmdLine_cxx_Version =    "$Id: B2bCmdLine.cxx,v 1.3 2001/09/18 23:17:42 sprajpat Exp $";#include "global.h"#include <iostream>#include "B2bCmdLine.hxx"#include "Daemon.hxx"#include "cpLog.h"#if defined(WIN32)#define __GNU_LIBRARY__#define __STDC__ 1#include "getopt.h"#endif // defined(WIN32)///Sptr<B2bCmdLine> B2bCmdLine::myInstance = 0;string B2bCmdLine::USE_PROVISIONING = "use-provisioning";///const pair < const char*, const char* > B2bCmdLine::cmdLineOptionString[] =    {        pair < const char*, const char* > ("daemon", "0" ),        pair < const char*, const char* > ("debuglevel", "LOG_INFO" ),        pair < const char*, const char* > ("logfile", "" ),        pair < const char*, const char* > ("sipport", "5060" ),        pair < const char*, const char* > ("pshostname", "ps.private.vovida.com"),        pair < const char*, const char* > ("psremoteport", "6005" ),        pair < const char*, const char* > ("psbackuphostname", "ps.private.vovida.com"),        pair < const char*, const char* > ("psbackupremoteport", "6010" ),        pair < const char*, const char* > ("configFile", USE_PROVISIONING.c_str() )    };///Sptr<B2bCmdLine>B2bCmdLine::instance(    const int argc,    const char** argv,    const char* applicationUsage){    if (argc <= 1)    {        cerr << "Usage: " << argv[0] << " " << applicationUsage << endl;        exit( -1);    }    if (myInstance == 0 )    {        myInstance = new B2bCmdLine();        assert( myInstance != 0 );        myInstance->myAppUsage = string(argv[0]) + string(" ") + string(applicationUsage);        myInstance->setDefaultValues();        myInstance->parseB2bCmdLine( argc, argv );        // Run as a daemon        if (myInstance->getInt( "daemon" ) == 0)        {            if (Daemon() != 0 )            {                cerr << "Cannot start as daemon!" << endl;                exit( -1);            }        }        // Set the debug level        string debugLevel = B2bCmdLine::instance()->getString( "debuglevel" );        int priority = cpLogStrToPriority(debugLevel.c_str());        if (priority)        {            cpLog( LOG_ALERT, "set level to %s", cpLogPriorityToStr(priority));#ifdef WIN32_DEBUGGING	    cpLogSetPriority(priority, 0, 0);#else            cpLogSetPriority(priority);#endif        }        else        {            cpLog( LOG_ALERT, "set level to LOG_DEBUG");            cpLogSetPriority( LOG_DEBUG );        }        string logfile = myInstance-> getString( "logfile" );        if (logfile != "")        {            cpLog( LOG_ALERT, "setting log file to %s", logfile.c_str());            cpLogOpen(logfile.c_str());        }        myInstance->print(LOG_INFO);    }    return myInstance;}///voidB2bCmdLine::setDefaultValues(){    for (unsigned int i = 0;            i < sizeof(cmdLineOptionString) / sizeof(cmdLineOptionString[0]);            i++)    {        cmdLineOptionTable[B2bCmdLine::cmdLineOptionString[i].first] =                               B2bCmdLine::cmdLineOptionString[i].second;    }}///voidB2bCmdLine::parseB2bCmdLine( const int argc, const char**argv ){    int arg_count = 0;    int c = 0;#if defined(__svr4__)|| defined (__SUNPRO_CC)    struct option    {# if defined __STDC__ && __STDC__        const char *name;# else       const char *name;# endif         /* has_arg can't be an enum because some compilers complain about           type mismatches in all the code that assumes it is an int.  */        int has_arg;        int *flag;        int val;    };#endif    static struct option long_options[] =        {            { B2bCmdLine::cmdLineOptionString[0].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[1].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[2].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[3].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[4].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[5].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[6].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[7].first, 1, 0, 0},            { B2bCmdLine::cmdLineOptionString[8].first, 1, 0, 0},        };    while ( (c = getopt ((argc),                         const_cast < char** > (argv),                         "dv:f:p:c:") ) != EOF)    {        switch (c)        {            case 'd':            {                // daemon flag                cmdLineOptionTable[long_options[0].name] = "1";            }            break;            case 'f':            {                // filename flag                cmdLineOptionTable[long_options[2].name] = optarg;            }            break;            case 'c':            {                // config filename flag                cmdLineOptionTable[long_options[8].name] = optarg;            }            break;            case 'v':            {                // verbose (debug msg) flag                int priority =                    cpLogStrToPriority(optarg);                if (priority != -1)                {                    cmdLineOptionTable[long_options[1].name] = optarg;                }                else                {                    printf("Invalid log level: '%s'\n", optarg);                    exit( -1);                }            }            break;            case 'p':            {                // SipPort                cmdLineOptionTable[long_options[3].name] = optarg;            }            case '?':  // getopt_long already printed an error message            break;            default:            printf ("?? getopt returned character code 0%o ??\n", c);            // abort ();        }    }    if ((argc - optind) > 2)    {        printf("Too many arguments\n");        cerr << "Usage: " << myAppUsage << endl;        exit( -1);    }    if (optind < argc)    {        if(cmdLineOptionTable[long_options[8].name] != USE_PROVISIONING)        {            cerr << "Provisioning and config file options are mutually exclusive" << endl;            cerr << "Usage: " << myAppUsage.c_str() << endl;            exit( -1);        }        if ((argc - optind) <  2)        {            cerr << "Too few arguments" << endl;            cerr << "Usage: " << myAppUsage.c_str() << endl;            exit( -1);        }        cmdLineOptionTable[long_options[8].name] = B2bCmdLine::USE_PROVISIONING;        while (optind < argc)        {            // first required arg must be the host name/port.            const string& psName = argv[optind];            string::size_type start;            string::size_type myLen;            start = psName.find(":");            myLen = psName.length();            int arg_index = 0;            if (arg_count == 0)            {                arg_index = 4;            }            else if (arg_count == 1)            {                arg_index = 6;            }            // Set the provisioning server hostname info            cmdLineOptionTable[long_options[arg_index].name] =                (psName.substr(0, start)).c_str();            // Check to see if the port info is given...            if (start != string::npos )            {                // Overwrite default port info only if it's specified                cmdLineOptionTable[long_options[arg_index+1].name] =                    (psName.substr(start + 1, myLen)).c_str();            }            arg_count++;            optind++;        }    }}///Sptr<B2bCmdLine>B2bCmdLine::instance(){    assert(myInstance != 0);    return myInstance;}///const string&B2bCmdLine::getString(const string& cmdOption){    static string nullstring = "";    TableIter i = cmdLineOptionTable.find(cmdOption);    if ( i != cmdLineOptionTable.end() )        return i->second;    return nullstring;}///const intB2bCmdLine::getInt(const string& cmdOption){    TableIter i = cmdLineOptionTable.find(cmdOption);    int ret = 0;    if ( i != cmdLineOptionTable.end() )        ret = atoi(i->second.c_str());    return ret;}#ifdef WIN32_DEBUGGINGvoidB2bCmdLine::print(const int loglevel, const char* file, int line){    cpLog(loglevel, file, line, "************************************");    if(myInstance->getString( "configFile") == "false")    {        cpLog(loglevel, file, line, "pshostname: %s",              myInstance->getString( "pshostname" ).c_str());        cpLog(loglevel, file, line, "psremoteport: %d" ,               myInstance->getInt( "psremoteport" ));        cpLog(loglevel, file, line, "psbackuphostname: %s",              myInstance->getString( "psbackuphostname" ).c_str());        cpLog(loglevel, file, line, "psbackupremoteport: %d" ,              myInstance->getInt( "psbackupremoteport" ));    }    cpLog(loglevel, file, line, "daemon: %d" , myInstance->getInt( "daemon" ));    cpLog(loglevel, file, line, "debuglevel: %s" ,          myInstance->getString( "debuglevel" ).c_str());    cpLog(loglevel, file, line, "logfile: %s" , myInstance->getString( "logfile" ).c_str());    cpLog(loglevel, file, line, "compiled at: %s", __DATE__ " " __TIME__);    cpLog(loglevel, file, line, "************************************");}#elsevoidB2bCmdLine::print(const int loglevel){    cpLog(loglevel, "************************************");    if(myInstance->getString( "configFile") == "false")    {       cpLog(loglevel, "pshostname: %s",             myInstance->getString( "pshostname" ).c_str());       cpLog(loglevel, "psremoteport: %d" , myInstance->getInt( "psremoteport" ));       cpLog(loglevel, "psbackuphostname: %s",             myInstance->getString( "psbackuphostname" ).c_str());       cpLog(loglevel, "psbackupremoteport: %d" ,             myInstance->getInt( "psbackupremoteport" ));    }    cpLog(loglevel, "sipport: %d" , myInstance->getInt( "sipport" ));    cpLog(loglevel, "daemon: %d" , myInstance->getInt( "daemon" ));    cpLog(loglevel, "debuglevel: %s" ,          myInstance->getString( "debuglevel" ).c_str());    cpLog(loglevel, "logfile: %s" , myInstance->getString( "logfile" ).c_str());    cpLog(loglevel, "compiled at: %s", __DATE__ " " __TIME__);    cpLog(loglevel, "************************************");}#endif // WIN32_DEBUGGING// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -