co::int32 main( co::Range<std::string const> args ) { if( args.isEmpty() ) throw lua::Exception( "lua.Launcher must receive a Lua script name as argument" ); std::string filename; if( !LuaState::findScript( LuaState::getL(), args.getFirst(), filename ) ) CORAL_THROW( lua::Exception, "launch script '" << args.getFirst() << "' could not be found in the path" ); lua_State* L = LuaState::getL(); LuaState::loadFile( L, filename ); args.popFirst(); co::int32 numArgs = static_cast<co::int32>( args.getSize() ); for( ; args; args.popFirst() ) LuaState::push( L, args.getFirst() ); LuaState::call( L, numArgs, 1 ); int resType = lua_type( L, -1 ); co::int32 res = static_cast<co::int32>( lua_tointeger( L, -1 ) ); lua_pop( L, 1 ); if( resType != LUA_TNIL && resType != LUA_TNUMBER ) CORAL_THROW( lua::Exception, "launch script '" << args.getFirst() << "' returned an illegal (non-numerical) value" ); return res; }
void defineIdentifier( const std::string& name ) { assertNotCreated(); if( !LexicalUtils::isValidIdentifier( name ) ) CORAL_THROW( IllegalNameException, "invalid identifier '" << name << "'" ); std::vector<std::string>::iterator it = std::find( _identifiers.begin(), _identifiers.end(), name ); if( it != _identifiers.end() ) CORAL_THROW( IllegalNameException, "invalid duplicate identifier '" << name << "'" ); _identifiers.push_back( name ); }
void validate() { // check for cyclic inheritance IInterface* base = _baseType; while( base ) { if( base == _myType ) CORAL_THROW( IllegalStateException, "cyclic inheritance detected'" ); base = base->getBaseType(); } _myType->updateSuperTypes(); // check for member clashes, considering all super types IInterface* type = _myType; Range<IInterface* const> superTypes = type->getSuperTypes(); while( 1 ) { Range<IMember* const> members = type->getMembers(); for( ; members; members.popFirst() ) checkForMemberClashes( members.getFirst() ); if( !superTypes ) break; type = superTypes.getFirst(); superTypes.popFirst(); } }
void Requestor::requestSynchCall( MemberOwner& owner, co::IMethod* method, co::Slice<co::Any> args, const co::Any& ret ) { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); InvocationDetails details( owner.instanceID, owner.facetID, method->getIndex(), owner.inheritanceDepth, true ); ParameterPusher& pusher = _marshaller.beginInvocation( _publicEndpoint, details ); pushParameters( method, args, pusher ); std::string msg; _marshaller.marshalInvocation( msg ); _handler->handleSynchRequest( msg, msg ); MessageType msgType = _demarshaller.demarshal( msg ); if( msgType == EXCEPTION ) raiseReturnedException( _demarshaller ); ParameterPuller& puller = _demarshaller.getOutput(); if( method->getReturnType() ) getReturn( puller, method->getReturnType(), ret ); co::TSlice<co::IParameter*> params = method->getParameters(); for( int i = 0; i < params.getSize(); i++ ) { if( params[i]->getIsOut() ) getReturn( puller, params[i]->getType(), args[i] ); } }
void Requestor::requestSetField( MemberOwner& owner, co::IField* field, const co::Any arg ) { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); InvocationDetails details( owner.instanceID, owner.facetID, field->getIndex(), owner.inheritanceDepth, false ); ParameterPusher& pusher = _marshaller.beginInvocation( _publicEndpoint, details ); co::IType* fieldType = field->getType(); if( fieldType->getKind() != co::TK_INTERFACE ) { pusher.pushValue( arg, fieldType ); } else { ReferenceType refType; getProviderInfo( arg.get<co::IService*>(), refType ); pusher.pushReference( refType ); } std::string msg; _marshaller.marshalInvocation( msg ); _handler->handleAsynchRequest( msg ); }
void Requestor::getReturn( ParameterPuller& puller, co::IType* type, const co::Any& ret ) { if( type->getKind() != co::TK_INTERFACE && type->getKind() != co::TK_EXCEPTION ) { puller.pullValue( type, ret ); return; } ReferenceType refType; puller.pullReference( refType ); co::IObject* instance; switch( refType.owner ) { case OWNER_RECEIVER: { instance = _instanceMan->getInstance( refType.instanceID )->getInstance(); if( !instance ) CORAL_THROW( rpc::RemotingException, "Server returned an invalid reference to a local instance" ); break; } case OWNER_SENDER: case OWNER_ANOTHER: Requestor* req = _manager->getOrCreateRequestor( refType.ownerEndpoint ); instance = req->getOrCreateProxy( refType.instanceID, refType.instanceType ); break; } co::TSlice<co::IPort*> ports = instance->getComponent()->getFacets(); co::IPort* port = ports[refType.facetIdx]; co::IService* service = instance->getServiceAt( port ); ret.put( service ); }
co::IObject* Requestor::requestPublicInstance( const std::string& key, const std::string& componentName ) { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); std::string msg; _marshaller.marshalLookup( _publicEndpoint, key, componentName, msg ); _handler->handleSynchRequest( msg, msg ); MessageType type = _demarshaller.demarshal( msg ); if( type == EXCEPTION ) raiseReturnedException( _demarshaller ); co::int32 instanceID = _demarshaller.getIntReturn(); if( instanceID == -1 ) return 0; // First, search if there isnt already a cp to the instanceID std::map<co::int32, ClientProxy*>::iterator it = _proxies.find( instanceID ); if( it != _proxies.end() ) return it->second; co::IComponent* component = co::cast<co::IComponent>( co::getType( componentName ) ); ClientProxy* cp = new ClientProxy( this, component, instanceID ); _proxies.insert( std::pair<co::int32, ClientProxy*>( instanceID, cp ) ); return cp; }
bool TypeLoader::findCSL( const std::string& typeName, std::string& fullPath, std::string& relativePath ) { std::string names[2]; int n = 0; // if the current namespace is not the root, first try a relative path if( _namespace && _namespace->getParentNamespace() ) { const std::string& fullNS = _namespace->getFullName(); names[n].reserve( fullNS.size() + typeName.size() + 5 ); names[n] = fullNS; names[n].push_back( '.' ); names[n].append( typeName ); OS::convertDotsToDirSeps( names[n] ); names[n].append( ".csl" ); ++n; } names[n].reserve( typeName.size() + 4 ); names[n] = typeName; OS::convertDotsToDirSeps( names[n] ); names[n].append( ".csl" ); ++n; bool succeeded = OS::searchFile2( getPaths(), Slice<std::string>( names, n ), fullPath, NULL, &relativePath ); if( !succeeded ) CORAL_THROW( co::Exception, "type '" << typeName << "' was not found in the path" ); return succeeded; }
void ModuleManager::syncModuleWithSystemState( IModule* module ) { try { switch( getSystem()->getState() ) { case SystemState_Initializing: // module is already initialized break; case SystemState_Integrating: case SystemState_Integrated: updateModule( module, ModuleState_Integrated ); break; case SystemState_IntegratingPresentation: case SystemState_Running: updateModule( module, ModuleState_PresentationIntegrated ); break; default: assert( false ); } } catch( std::exception& e ) { module->abort(); CORAL_THROW( ModuleLoadException, "exception raised by module '" << module->getNamespace()->getFullName() << "' during update: " << e.what() ); } }
void fillType() { if( _interfaces.empty() ) CORAL_THROW( MissingInputException, "missing component ports" ); _myType->addPorts( _interfaces ); _myType->sortPorts(); }
void definePort( const std::string& name, IInterface* type, bool isFacet ) { assertNotCreated(); if( !type ) CORAL_THROW( IllegalArgumentException, "illegal null type" ); if( !LexicalUtils::isValidIdentifier( name ) ) CORAL_THROW( IllegalNameException, "port name '" << name << "' is not a valid indentifier"); Port* port = new Port; port->setName( name ); port->setType( type ); port->setIsFacet( isFacet ); _interfaces.push_back( port ); }
void fillType() { if( _fields.empty() ) CORAL_THROW( MissingInputException, "missing struct contents" ); _myType->addMembers( _fields ); _myType->sortMembers( _myType ); }
void SQLiteStatement::handleErrorCode( int errorCode ) { if( errorCode != SQLITE_OK && errorCode != SQLITE_DONE ) { CORAL_THROW( co::Exception, "SQLite statement error " << sqlite3_errmsg( sqlite3_db_handle( _stmt ) ) ); } }
void onServiceChanged( ca::IServiceChanges* changes ) { if( nextException ) { const char* msg = nextException; nextException = NULL; CORAL_THROW( co::MissingServiceException, msg ); } }
void defineBaseType( IType* baseType ) { assertNotCreated(); if( _baseType && _baseType->getFullName() != "co.IService" ) CORAL_THROW( NotSupportedException, "multiple inheritance not supported" ); if( !baseType ) CORAL_THROW( IllegalArgumentException, "illegal null baseType" ); co::TypeKind kind = baseType->getKind(); if( kind != co::TK_INTERFACE ) CORAL_THROW( IllegalArgumentException, "illegal baseType (interface expected, got " << co::TK_STRINGS[kind] << ")" ); _baseType = static_cast<Interface*>( baseType ); _myType->setBaseType( _baseType ); }
IAnnotation* Loader::getAnnotationFrom( IObject* object ) { IAnnotation* annotation = object->getService<IAnnotation>(); if( !annotation ) CORAL_THROW( Exception, "component '" << object->getComponent()->getName() << "' does not provide an annotation" ); return annotation; }
void Library::assertIsLoaded() { if( !isLoaded() ) { std::string errorMsg; getLastErrorMessage( errorMsg ); CORAL_THROW( ModuleLoadException, "error loading library '" << _fileName << "': " << errorMsg ); } }
void ReflectorBase::checkNumArguments( co::IMethod* mi, size_t numArgs ) { assert( mi ); size_t expectedNumArgs = mi->getParameters().getSize(); if( numArgs < expectedNumArgs ) CORAL_THROW( co::MissingInputException, "method '" << mi->getName() << "' takes " << expectedNumArgs << " argument" << ( expectedNumArgs > 1 ? "s" : "" ) << ", but only " << numArgs << ( numArgs > 1 ? " were" : " was" ) << " passed" ); }
void Requestor::requestBarrierDown() { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); std::string msg; _marshaller.marshalBarrierDown( _publicEndpoint, msg ); _handler->handleAsynchRequest( msg ); }
void setSpace( ca::ISpace* space ) { if( !space ) { CORAL_THROW( co::IllegalArgumentException, "NULL space" ); } _space = space; initializeIds(); }
IType* TypeManager::loadTypeOrThrow( const std::string& fullName ) { TypeLoader loader( fullName, this ); IType* type = loader.loadType(); if( loader.getError() ) CORAL_THROW( TypeLoadException, "could not load type '" << fullName << "':\n" << *loader.getError() ); return type; }
void fillType() { if( _identifiers.empty() ) CORAL_THROW( MissingInputException, "missing enum identifiers" ); for( Range<std::string> r( _identifiers ); r; r.popFirst() ) { _myType->addIdentifier( r.getFirst() ); } }
void Requestor::requestLease( co::int32 instanceID, std::string lessee ) { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); Marshaller marshaller; // cannot use the member instance (conflicts with other marshalling) std::string msg; marshaller.marshalLease( lessee, instanceID, msg ); _handler->handleAsynchRequest( msg ); }
void validate() { Range<IPort* const> ports = _myType->getPorts(); IPort* lastPort = ports.getFirst(); for( ports.popFirst(); ports; ports.popFirst() ) { if( lastPort->getName() == ports.getFirst()->getName() ) CORAL_THROW( IllegalNameException, "duplicate port '" << lastPort->getName() << "' in component '" << _myType->getFullName() << "'" ); } }
IMethodBuilder* defineMethod( const std::string& name ) { assertNotCreated(); if( !LexicalUtils::isValidIdentifier( name ) ) CORAL_THROW( IllegalNameException, "method name '" << name << "' is not a valid identifier" ); MethodBuilder* methodBuilder = new MethodBuilder; methodBuilder->init( this, name ); return methodBuilder; }
void defineField( const std::string& name, IType* type, bool isReadOnly ) { assertNotCreated(); if( !type ) CORAL_THROW( IllegalArgumentException, "illegal null type" ); if( type->getKind() == TK_EXCEPTION || type->getKind() == TK_COMPONENT ) CORAL_THROW( IllegalArgumentException, ( type->getKind() == TK_EXCEPTION ? "exception" : "component" ) << "s are illegal as field types" ); // struct-specific checks if( _kind == TK_STRUCT ) { if( isReadOnly ) CORAL_THROW( IllegalArgumentException, "structs cannot have read-only fields" ); if( _type == type ) CORAL_THROW( IllegalArgumentException, "a struct cannot contain itself recursively" ); } if( !LexicalUtils::isValidIdentifier( name ) ) CORAL_THROW( IllegalNameException, "field name '" << name << "' is not a valid identifier" ); if( !LexicalUtils::isValidFieldName( name ) ) CORAL_THROW( IllegalNameException, "field names must start with a lowercase letter" ); Field* attr = new Field; attr->setType( type ); attr->setName( name ); attr->setIsReadOnly( isReadOnly ); _fields.push_back( attr ); }
void ComponentBase::checkValidPort( IPort* port ) { if( !port ) throw NoSuchPortException( "illegal null port" ); IComponent* myType = getComponent(); ICompositeType* owner = port->getOwner(); if( owner != static_cast<ICompositeType*>( myType ) ) CORAL_THROW( NoSuchPortException, "port '" << port->getName() << "' belongs to " << owner->getFullName() << ", not to " << myType->getFullName() ); assert( port->getIndex() < myType->getPorts().getSize() ); }
IArray* TypeManager::getArrayOf( IType* elementType ) { if( !elementType ) CORAL_THROW( IllegalArgumentException, "null element type" ); const std::string& elementTypeName = elementType->getName(); std::string arrayName; arrayName.reserve( elementTypeName.length() + 2 ); arrayName.append( elementTypeName ); arrayName.append( "[]" ); Namespace* ns = static_cast<Namespace*>( elementType->getNamespace() ); // try to locate an existing array of this type IType* existingArrayType = ns->getType( arrayName ); if( existingArrayType ) { assert( existingArrayType->getKind() == TK_ARRAY ); return static_cast<IArray*>( existingArrayType ); } // otherwise, try to create it TypeKind kind = elementType->getKind(); if( kind == TK_ARRAY ) CORAL_THROW( IllegalArgumentException, "arrays of arrays are illegal" ); if( !isData( kind ) ) CORAL_THROW( IllegalArgumentException, "arrays of " << kind << "s are illegal" ); RefPtr<ArrayType> arrayType = new ArrayType; arrayType->setType( TK_ARRAY, elementType->getName() + "[]", ns ); arrayType->setElementType( elementType ); ns->addType( arrayType.get() ); return arrayType.get(); }
ISQLiteStatement* SQLiteConnection::prepare( const std::string& sql ) { if( !_db ) throw co::Exception( "Database not connected. Cannot execute command" ); sqlite3_stmt* stmt; int resultCode = sqlite3_prepare_v2( _db, sql.c_str(), -1, &stmt, 0 ); if( resultCode != SQLITE_OK ) { CORAL_THROW( co::Exception, "Query Failed: " << sqlite3_errmsg( _db ) ); } return new SQLiteStatement( stmt ); }
void Requestor::requestAsynchCall( MemberOwner& owner, co::IMethod* method, co::Slice<co::Any> args ) { if( !_connected ) CORAL_THROW( RemotingException, "Trying to request with the node stopped"); InvocationDetails details( owner.instanceID, owner.facetID, method->getIndex(), owner.inheritanceDepth, false ); ParameterPusher& pusher = _marshaller.beginInvocation( _publicEndpoint, details ); pushParameters( method, args, pusher ); std::string msg; _marshaller.marshalInvocation( msg ); _handler->handleAsynchRequest( msg ); }