Click here to Skip to main content
15,885,933 members
Articles / Programming Languages / C++

Remote SOF - An OSGI-like modularization framework for C++ supporting distributed software modules

Rate me:
Please Sign up or sign in to vote.
4.58/5 (8 votes)
13 Jul 2009BSD10 min read 31.3K   15  
The article describes the usage of a modularization framework called Remote SOF supporting distributed software modules.
#include "CORBANamingServiceImpl.h"

using namespace sof::framework::remote::corba::namingservice;


CORBANamingServiceImpl::CORBANamingServiceImpl() {
}

CORBANamingServiceImpl::~CORBANamingServiceImpl() {
	;
}

CORBANamingServiceImpl::CORBANamingServiceImpl( CosNaming::NamingContext_var ctx ) {
	this -> naming_context = ctx;
}

CORBANamingServiceImpl::CORBANamingServiceImpl( CORBA::ORB_var orb ) {
	this -> orb = orb;
	// resolve the naming service
        CORBA::Object_var nsobj =
        	orb->resolve_initial_references ("NameService");
        if ( CORBA::is_nil( nsobj ) ) {
            cerr << "can't resolve NameService\n";
            exit( 1 );
        }

        // narrow the root naming context
        naming_context = CosNaming::NamingContext::_narrow ( nsobj );
	
}

list<CORBA::Object_var> CORBANamingServiceImpl::findObjects( const string& path ) {
	list<CORBA::Object_var> l;
	list<string> names = getPathElements( path );
	CosNaming::NamingContext_var ctx = getNamingContext( names, false );
	CosNaming::BindingIterator_var it;
	CosNaming::BindingList_var bl;
	const CORBA::ULong CHUNK = 100;
	ctx->list( CHUNK, bl, it );

	for( CORBA::ULong i = 0; i < bl->length(); i++ ) {
		CORBA::Object_var obj = ctx->resolve( bl[i].binding_name );
		if ( !( obj->_is_a("IDL:omg.org/CosNaming/NamingContext:1.0") ) ) {
			l.push_back( obj );
		}
		CosNaming::NamingContext_var tempCtx = CosNaming::NamingContext::_narrow( obj );
	}

	return l;
}

void CORBANamingServiceImpl::bindObject( const string& path, const string& objectName,
		CORBA::Object_var object ) {
	list<string> names = getPathElements( path );
	CosNaming::NamingContext_var ctx = getNamingContext( names, true );
	CosNaming::Name name;
	name.length(1);
	CORBA::String_var str = objectName.data();
	name[0].id = str;
	ctx->rebind( name, object );	
}

void CORBANamingServiceImpl::unbindObject( const string& path, const string& objectName ) 
{
	list<string> names = getPathElements( path );
	CosNaming::NamingContext_var ctx = getNamingContext( names, true );
	CosNaming::Name name;
	name.length(1);
	CORBA::String_var str = objectName.data();
	name[0].id = str;
	ctx->unbind( name );	
}


list<string> CORBANamingServiceImpl::getPathElements( const string& path ) {

	list<string> l;
	string temp = path;
	int range = temp.find( "/" );
	while( range >= 0 ) {
		string substr = temp.substr( 0, range ); //end-1 );
		temp.erase( 0, range+1 );
		l.push_back( substr );
		range = temp.find( "/" );
	}

	l.push_back( temp );
	return l;
}

CosNaming::NamingContext_var CORBANamingServiceImpl::getNamingContext( list<string> names, bool force ) {

	list<string>::iterator pos;

	CosNaming::Name name;
	name.length(1);

	CosNaming::NamingContext_var tempContext = naming_context;

	for ( pos = names.begin(); pos != names.end(); pos++ ) {
		CORBA::String_var str = (*pos).c_str();
		name[0].id = str;
		try {
			tempContext = CosNaming::NamingContext::_narrow( tempContext->resolve( name ) );
		} catch( const CosNaming::NamingContext::NotFound & e ) {
			if ( force ) {
				tempContext = tempContext->bind_new_context( name );
			} else {
				throw e;
			}
		}
	}
	return tempContext;
}






By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions