コード例 #1
0
/**
   take in a configuration xml document and a system 
   context and initialize the transport
*/
int 
RpcHttpClientTransport::init( 
    const DOMNode* config, 
    RefCountedPtr<SysContext>& ctx )
{
	REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent( StdLogger::getRegistryName()), _logger);
    ASSERT_D(_logger != NULL);
	
    // parsing for proxy server
    if ( config->getNodeType() == DOMNode::ELEMENT_NODE )
    {
        const DOMElement* configElem = (const DOMElement*)config;
        // look for the listening port setting
        String val;
        Mapping attrs;

        DOMNodeList* nodes = DomUtils::getNodeList( configElem, RPC_PROXY_NAME );
        if ( DomUtils::getNodeValue( (const DOMElement*)nodes->item( 0 ), &val, &attrs ) )
        {
            Mapping::const_iterator it = attrs.find( RPC_PROXY_PORTATTR );
            if( it != attrs.end() )
            {
                _proxy = val;
                _proxyPort = StringUtils::toInt( (*it).second );
            }
            else
            {
                CBLOGERR(_logger,
                         NTEXT("RpcHttpClientTransport::init: can't find attributes to configure Proxy Port"));
            }
        }
    }

    return 0;
}
コード例 #2
0
int 
SerSerializationMgr::postInit( 
    const DOMNode* config, 
    RefCountedPtr<SysContext>& ctx )
{
    if ( _logger == NULL )
    {
        REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent(StdLogger::getRegistryName()), _logger);
    }

    if ( _paths == NULL )
    {
        REFCOUNTED_CAST(iSysComponent, SysPathMgr, ctx->getComponent(SysPathMgr::getRegistryName()), _paths);
    }

    return 0;

}
コード例 #3
0
void
SysComponentLoader::updateComponentContext(
    RefCountedPtr<SysContext> &ctx,
    const DOMDocument* doc )
{

    std::vector<DOMProcessingInstruction*> pis;
    DomUtils::findProcessingInstructions( doc, TARGET_NAME, pis );

    for ( std::vector<DOMProcessingInstruction*>::const_iterator it = pis.begin();
            it != pis.end(); it++ )
    {
        // check to see if the processing instruction has the
        // following attributes:
        // "component-name" - name of the class to create
        // "component-config" - path of the config tree for the component
        //
        Mapping attrs;
        if ( DomUtils::getAttributes( (*it), attrs ) )
        {
            String cmpName;
            attrs.getIfAvailable(CMP_NAME_ATTR, cmpName);

            RefCountedPtr<iSysComponent> cmp = ctx->getComponent( cmpName );
            if ( cmp != NULL )
            {
                // locate it's configuration and call it to initialize
                String xmlIsland;
                attrs.getIfAvailable(CMP_CFG_ATTR, xmlIsland);

                if ( xmlIsland.length() > 0 )
                {
                    const DOMNode* theNode = (const DOMNode*)doc->getDocumentElement();

                    DOMElement* xmlNode = NULL;
                    DomUtils::selectSingleNode( theNode, xmlIsland, (DOMNode**)&xmlNode );

                    cmp->update( xmlNode );
                }
            }
        }
    }
}
コード例 #4
0
int 
StdFileImpl::init( 
    const DOMNode* config, 
    RefCountedPtr<SysContext>& ctx )
{
    int res = -1;
    
    if ( (config != NULL) && (config->getNodeType() == DOMNode::ELEMENT_NODE) )
    {
        const DOMElement* configElem = (const DOMElement*)config;
        
        String val;
        
        // Get the log level setting [optional]
        DOMNodeList* levels = DomUtils::getNodeList( configElem, LOG_LEVEL );
        if ( levels != NULL )
        {
            for ( XMLSize_t i = 0, sz = levels->getLength(); i < sz; i++ )
            {
                if ( DomUtils::getNodeValue( (DOMElement*)levels->item( i ), &val ) )
                {
                    int level = StringUtils::toInt( val );
                    if ( level >= StdLogger::LOGC_ALL )
                    {
                        _logAll = true;
                    }
                    else
                    {
                        _levels.insert( level );
                    }
                }
            }
        }
        
        // get the log file path [required]
        DOMElement* logFileElement = NULL;
        String logFileName;
        if ( DomUtils::selectSingleNode( configElem, FILE_NAME, (DOMNode**)&logFileElement ) )
        {
            if ( DomUtils::getNodeValue( logFileElement, &val ) )
            {	
				RefCountedPtr<SysPathMgr> paths;
				REFCOUNTED_CAST(iSysComponent, SysPathMgr, ctx->getComponent(SysPathMgr::getRegistryName()), paths);
                if ( paths != NULL )
                {
                    logFileName = paths->getPath( val );
                }
                else
                {
                    logFileName = val;
                }

                DOMElement* numFilesElement = NULL;
                DOMElement* sizeFileElement = NULL;

                int numLogs = 0;
                _SizePerLog = 0;
                if ( DomUtils::selectSingleNode( configElem, NUM_FILES, (DOMNode**)&numFilesElement ) )
                {
                    if (DomUtils::getNodeValue( numFilesElement, &val ) )
                    {
                        numLogs = StringUtils::toInt(val);
                    }
                    else 
                    {
                        numLogs = 1;
                    }
                }
                if ( DomUtils::selectSingleNode( configElem, FILE_SIZE, (DOMNode**)&sizeFileElement ) )
                {
                    if (DomUtils::getNodeValue( sizeFileElement, &val ) )
                    {
                        _SizePerLog = StringUtils::toLong(val);
                    }				
                }
                
                if ( (numLogs <= 0) || (_SizePerLog <= 0) )
                {
#ifdef __GNUG__
                    _logFile.open( logFileName.c_str(), std::ios::out | std::ios::app );
#else
                    _logFile.open( logFileName.c_str(), std::ios_base::out | std::ios_base::app );
#endif
                    _logFileNames.push_back( logFileName );
                    _logIndex = 0;
                    _SizePerLog = 0;
                    res = 0;
                }
                else
                {
                    int i;
                    if ( numLogs == 1 )
                    {
                        _logFileNames.push_back( logFileName );
                    }
                    else
                    {
                        for ( i = 1; i <= numLogs; i++ )
                        {
                            _logFileNames.push_back( logFileName + StringUtils::toString(i)+ NTEXT(".txt") );
                        }
                    }

                    FileAttributes fattrs;
                    if ( !FileUtils::getAttributes( _logFileNames[0].c_str(), fattrs ) )
                    {
#ifdef __GNUG__
                        _logFile.open(_logFileNames[0].c_str(), std::ios::out | std::ios::app );
#else
                        _logFile.open(_logFileNames[0].c_str(), std::ios_base::out | std::ios_base::app );
#endif
                        _logIndex = 0;
                        res = 0;
                    }
                    else 
                    {
                        int useindex = 0;
                        struct tm filetime = fattrs.getModifyTime();
                        time_t currtime = ::time(NULL);
                        double besttime = ::difftime( currtime, mktime(&filetime) );
                        for (i = 1; i < numLogs; i++)
                        {
                            if ( FileUtils::getAttributes( _logFileNames[i].c_str(), fattrs ) )
                            {
                                filetime = fattrs.getModifyTime();
                                double thisdiff = ::difftime( currtime, ::mktime(&filetime) );
                                if ( thisdiff < besttime )
                                {
                                    besttime = thisdiff;
                                    useindex = i;
                                }
                            }
                        }

                        FileUtils::getAttributes( _logFileNames[useindex].c_str(), fattrs );
                        if ( (int) fattrs.getFileSize() < _SizePerLog )
                        {
#ifdef __GNUG__
                            _logFile.open(_logFileNames[useindex].c_str(), std::ios::out | std::ios::app );
#else
                            _logFile.open(_logFileNames[useindex].c_str(), std::ios_base::out | std::ios_base::app );
#endif
                            _logIndex = useindex;
                            res = 0;
                        }
                        else
                        {
                            if ( (useindex + 1) >= (int)_logFileNames.size() )
                            {
                                useindex = 0;
                            }
                            else
                            {
                                useindex += 1;
                            }
#ifdef __GNUG__
                            _logFile.open(_logFileNames[useindex].c_str(), std::ios::out | std::ios::trunc );
#else
                            _logFile.open(_logFileNames[useindex].c_str(), std::ios_base::out | std::ios_base::trunc );
#endif
                            _logIndex = useindex;
                            res = 0;
                            
                        }
                    }
                    
                }
            }
        }
    }
    
    return res;
}
コード例 #5
0
ファイル: SysPathMgr.cpp プロジェクト: CSanchezAustin/cslib
int 
SysPathMgr::init( 
            const DOMNode* config, 
            RefCountedPtr<SysContext>& ctx )
{
    int res = -1;

    REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent( StdLogger::getRegistryName()), _logger);

	// I expect the config node to be an element node
    if ( ( config != NULL ) && ( config->getNodeType() == DOMNode::ELEMENT_NODE ) )
    {
        // set the root to be the current directory by default
        String root(NTEXT("."));

        Mapping rootAttrs;
        if ( DomUtils::getNodeValue( (const DOMElement*)config, NULL, &rootAttrs ) )
        {
            rootAttrs.getIfAvailable(SYS_PATH_ROOTATTR, root);
        }

        // resolve whatever we have in the root and then verify a file does not exist
        // in place of the intended directory
        _root = FileUtils::resolve( root );

        FileAttributes fattrs;
        if ( !FileUtils::getAttributes( _root, fattrs ) )
        {
            if (!FileUtils::mkdirs( _root ))
            {
                CBLOGERR(_logger, NTEXT("SysPathMgr::init: could not create directory '") + _root + NTEXT("'"));
                return -1;
            }

            // update the file attributes with another call to get info in the directory
            FileUtils::getAttributes( _root, fattrs );
        }

        if (!fattrs.isDirectory())
        {
            CBLOGERR(_logger, NTEXT("SysPathMgr::init: found a file where a directory '") + _root + NTEXT("' was expected"));
            return -1;
        }

        // put a special key with the root attribute in the map
    	_paths[ SYS_PATH_IND + SYS_PATH_ROOTATTR ] = _root;

        // ok to this point, no errors
        res = 0;

        // iterate through the list of child elements whose tag is SYS_PATH_ENTRY
        // and get the name attribute and value. If all is found properly, then
        // construct a counter whose value is retrieved from the config node
        DOMNodeList* children = DomUtils::getNodeList( (const DOMElement*)config, SYS_PATH_ENTRY );
        if ( children != NULL )
        {
            for ( XMLSize_t i = 0, sz = children->getLength() ;
                  i < sz; i++ )
            {
                DOMNode* child = children->item(i);
                if ( (child != NULL) && (child->getNodeType() == DOMNode::ELEMENT_NODE) )
                {
                    String value;
                    Mapping attrs;

                    bool found = DomUtils::getNodeValue( (const DOMElement*)child, &value, &attrs );
                    if ( found )
                    {
                        res = 0;

                        String tagName;
                        attrs.getIfAvailable(SYS_PATH_TAGATTR, tagName);

                        if ( tagName.length() > 0 )
                        {
                            add( tagName, value );
                        }
                    }
                }
            }
        }
    }

    return res;
}
コード例 #6
0
/**
   take in a configuration xml document and a system
   context and initialize the transport
*/
int
RpcHttpTransport::init(
    const DOMNode* config,
    RefCountedPtr<SysContext>& ctx,
    iRpcServer* masterServer)
{
    int res = -1;

    ASSERT_D(status() == keRpcNoState);

    REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent( StdLogger::getRegistryName()), _logger);

    RefCountedPtr<ThdPool> pool;
    REFCOUNTED_CAST(iSysComponent, ThdPool, ctx->getComponent( ThdPool::getRegistryName()), pool);
    ASSERT_D(pool != NULL);

    // inititalize socket environment
    if (( config != NULL ) && ( config->getNodeType() == DOMNode::ELEMENT_NODE ))
    {
        const DOMElement* configElem = (const DOMElement*)config;

        String val;
        Mapping attrs;

        // first configure the server attributes
        DOMNodeList* nodes = DomUtils::getNodeList( configElem, RPC_LISTEN_PORT );
        if ( DomUtils::getNodeValue( (const DOMElement*)nodes->item( 0 ), &val, &attrs ) )
        {
            Mapping::const_iterator it = attrs.find( RPC_BACKLOG_ATTR );
            if( it != attrs.end() )
            {
                _port = StringUtils::toInt( val );
                _backlog = StringUtils::toInt( (*it).second );
            }
            else
            {
                CBLOGERR(_logger,
                         NTEXT("RpcHttpClientTransport::init: can't find attributes to server listener, using defaults"));
            }
        }


        // now configure the client attributes
        attrs.clear();
        nodes = DomUtils::getNodeList( configElem, RPC_PROXY_NAME );
        if ( DomUtils::getNodeValue( (const DOMElement*)nodes->item( 0 ), &val, &attrs ) )
        {
            Mapping::const_iterator it = attrs.find( RPC_PROXY_PORTATTR );
            if( it != attrs.end() )
            {
                _proxy = val;
                _proxyPort = StringUtils::toInt( (*it).second );
            }
            else
            {
                CBLOGERR(_logger,
                         NTEXT("RpcHttpClientTransport::init: can't find attributes to configure Proxy Port"));
            }
        }

        attrs.clear();
        nodes = DomUtils::getNodeList( configElem, RPC_CLIENT_RETRIES );
        if ( DomUtils::getNodeValue( (const DOMElement*)nodes->item( 0 ), &val, &attrs ) )
        {
            Mapping::const_iterator it = attrs.find( RPC_CLIENT_TOATTR );
            if( it != attrs.end() )
            {
                _retries = StringUtils::toInt( val );
                _sleepInterval = StringUtils::toInt( (*it).second );
            }
            else
            {
                CBLOGERR(_logger,
                         NTEXT("RpcHttpClientTransport::init: can't find attributes to configure client communications parameters"));
            }
        }

        // setup the rpc address for this server
        String address;
        address = Net::getHostName();
        address += COLON;
        address += StringUtils::toString( _port );

        _transportAddress.setTransport( RPC_HTTP_NAME );
        _transportAddress.setAddress( address );

        // all is well set the initial state
        res = 0;
        _state = keRpcInitted;
    }

    if ( res == 0 )
    {
        RefCountedPtr<MyThdFn> wfn(new MyThdFn( *this, &RpcHttpTransport::myWorkerFunction ));
        pool->add( 1, (RefCountedPtr<iRunnable> &)wfn );

        // inititalize MasterServer Pointer
        ASSERT_D(masterServer != NULL);
        _masterServer = masterServer;
        _triggerEvent.reset();
        _startedEvent.reset();
    }

    return res;
}