void ServerConnection::connectToRemoteServer( CLSID server_clsid )
{
	FRL_EXCEPT_GUARD();
	COSERVERINFO server_info;
	os::win32::com::zeroMemory( &server_info );

	server_info.pwszName = util::duplicateString( unicodeCompatibility( host_name ) );
	// setup requested interfaces
	MULTI_QI mq_result;
	os::win32::com::zeroMemory( &mq_result );

	mq_result.pIID = &IID_IOPCServer;
	mq_result.pItf = NULL;
	mq_result.hr   = S_OK;

	// call create instance
	if( FAILED( CoCreateInstanceEx( server_clsid,
		NULL,
		CLSCTX_REMOTE_SERVER,
		&server_info,
		1,
		&mq_result) ) )
	{
		FRL_THROW_S_CLASS( CreateServerObjectError );
	}

	// check QueryInterface result
	if( FAILED(mq_result.hr) )
		FRL_THROW_S_CLASS( QueryInterfaceError );

	// set pointer to server object
	ComPtr<IOPCServer> tmp( (IOPCServer*)mq_result.pItf );
	tmp.get()->Release(); // release tmp
	server.swap( tmp ); // equal: this->server->ptr = mq_result.pItf
}
void AddressSpace::addBranch( const String &fullPath )
{
	FRL_EXCEPT_GUARD();
	if( rootTag == NULL )
		FRL_THROW_S_CLASS( NotFinalConstruct );
	if( fullPath.empty() )
		FRL_THROW_S_CLASS( InvalidBranchName );
	size_t pos = fullPath.rfind( delimiter + delimiter );
	if ( pos != String::npos )
		FRL_THROW_S_CLASS( InvalidBranchName );
	pos = fullPath.rfind( delimiter );
	if( pos == String::npos )
	{
		Tag *added = rootTag->addBranch( fullPath );
		nameBranchCache.insert( std::pair< String, Tag*>( fullPath, added ) );
		return;
	}
	// last or first symbol in branch name == delimiter
	if( pos == fullPath.size()-1 || pos == 0 )
		FRL_THROW_S_CLASS( InvalidBranchName );
	String tmpPath = fullPath.substr( 0, pos );
	if ( !isExistBranch(tmpPath) )
	{
		addBranch( tmpPath );
	}	
	Tag *added = getBranch( tmpPath )->addBranch( fullPath );
	nameBranchCache.insert( std::pair< String, Tag*>( fullPath, added ) );
}
void Document::loadFileToString( const String& file_name_, String &buffer_ )
{
	FRL_EXCEPT_GUARD();
	if( ! fs::exists( file_name_ ) )
		FRL_THROW_S_CLASS( Document::FileNotFound );
	size_t length = static_cast< size_t >( fs::file_size( file_name_ ) );
	if( length == 0 )
		FRL_THROW_S_CLASS( Document::EmptyFile );
	if( ! buffer_.empty() )
		buffer_.clear();

	static const size_t buffer_size = 4096;
	stream_std::InFile in( file_name_.c_str(), std::ios::binary );
	if( ! in.is_open() )
		FRL_THROW_S_CLASS( Document::UnknownError );
	std::vector< Char > buf( buffer_size );

	buffer_.reserve( length );
	while( in )
	{
		in.read( &buf[ 0 ], buffer_size );
		buffer_.append( &buf[ 0 ], in.gcount() );
	}
	in.close();
}
Exemple #4
0
void ColorOut::SetAttributes( frl::Long attrib )
{
	FRL_EXCEPT_GUARD();
	#if( FRL_PLATFORM == FRL_PLATFORM_WIN32 )
		if( ! SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)( attrib ) ) )
			FRL_THROW_SYSAPI_EX( FRL_STR("Error on set console screen buffer information. ") );
	#endif
}
std::vector< String > ServerConnection::getLeafsList( const String& from_branch,
																			const String& name_filter,
																			const String& vendor_filter )
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	return getItemsList( OPC_BROWSE_FILTER_ITEMS, from_branch, name_filter, vendor_filter );
}
frl::Bool ServerConnection::testComplianceOPC_DA1()
{
	FRL_EXCEPT_GUARD();
	if( ! isInterfaceSupported( IID_IUnknown ) )
		return False;
	if( ! isInterfaceSupported( IID_IOPCServer ) )
		return False;
	return True;
}
frl::opc::GroupElem GroupManager::getGroup( String &name )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	GroupElemNamesMap::iterator it = names_map.find( name );
	if( it == names_map.end() )
		FRL_THROW_S_CLASS( NotExistGroup );
	return it->second;
}
frl::opc::GroupElem GroupManager::getGroup( OPCHANDLE handle )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	GroupElemHandlesMap::iterator it = handles_map.find( handle );
	if( it == handles_map.end() )
		FRL_THROW_S_CLASS( NotExistGroup );
	return it->second;
}
std::vector< GroupPtr > ServerConnection::getGoupList()
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	if( ! isConnected() )
		FRL_THROW_S_CLASS( NotConnected );
	std::vector<GroupPtr> vec_tmp( group_list.size() );
	std::transform( group_list.begin(), group_list.end(), vec_tmp.begin(), boost::mem_fn(&GroupList::value_type::second ) );
	return vec_tmp;
}
GroupElem GroupManager::addGroup( String &name )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	if( names_map.find( name ) != names_map.end() )
		FRL_THROW_S_CLASS( IsExistGroup );
	GroupElem new_group( new Group( name ) );
	insert( new_group );
	return new_group;
}
GroupPtr ServerConnection::getGroupByName( const String& group_name )
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	checkIsConnect();
	GroupList::iterator it = group_list.find( group_name );
	if( it == group_list.end() )
		FRL_THROW_S_CLASS( GroupNotExist );
	return it->second;
}
Exemple #12
0
void ColorOut::GetDefaultAttributes()
{
	FRL_EXCEPT_GUARD();
	#if( FRL_PLATFORM == FRL_PLATFORM_WIN32 )
		CONSOLE_SCREEN_BUFFER_INFO bi;
		if ( ! GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &bi ) )
			FRL_THROW_SYSAPI_EX( FRL_STR("Error on get console screen buffer information. ") );
		defaultAttributes = bi.wAttributes;
	#endif
}
void ServerConnection::internalRemoveGroup( const String& group_name, Bool force )
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	checkIsConnect();
	GroupList::iterator it = group_list.find( group_name );
	if( it == group_list.end() )
		FRL_THROW_S_CLASS( GroupNotExist );
	it->second->removeGroup( force );
	group_list.erase( it );
}
frl::String ServerConnection::getServerErrorString( HRESULT error_id )
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	checkIsConnect();
	ComPtr<IOPCCommon> comm; 
	getInterface( IID_IOPCCommon, comm );
	LPWSTR str_tmp;
	comm->GetErrorString( error_id, &str_tmp );
	String ret_str = similarCompatibility( str_tmp );
	os::win32::com::freeMemory( str_tmp );
	return ret_str;
}
Exemple #15
0
	ChannelPtr ChannelMgr::Create(ChannelType type, const Json::Value& jsonValue)
	{
		FRL_EXCEPT_GUARD();

		try
		{
			switch(type)
			{
			case OPC_DA2:
				{
					string_t name = jsonValue["name"].asString();
					string_t host = jsonValue["host"].asString();
					string_t server = jsonValue["server"].asString();

					return CreateOPC(name, host, server);
				}
			case MODBUS_RTU_NPORT:
				{
					string_t name = jsonValue["name"].asString();
					string_t server = jsonValue["server"].asString();
					int32_t port = jsonValue["port"].asInt();
					int32_t speed = jsonValue["speed"].asInt();
					int32_t timeout = jsonValue["timeout"].asInt();
					int32_t pause = jsonValue["pause"].asInt();

					boost::shared_ptr<Modbus::StreamRTU> stream = boost::make_shared<ModbusRTU_NPort>(server, port, speed, timeout);
					return CreateModbusRtu(stream, name, pause);
				}
			case MODBUS_RTU_SERIAL:
				{
					//string_t name = jsonValue["name"].asString();
					//boost::shared_ptr<Modbus::StreamRTU> stream( new ModbusRTU_Serial() );
					//Channel* channelPtr = new Modbus::ModbusRTU(stream, name);
					//Attach(channelPtr);
					//channelPtr->Init(name);
					//return channelPtr;
					FRL_THROW_CLASS( NotSupportedChannelType, "MODBUS_RTU_SERIAL not supported yet." );
					//return NULL;
				}
			default:
				{
					FRL_THROW_CLASS( NotSupportedChannelType, "Type is not recognized." );
				}
			}
		}
		catch(...)
		{
			FRL_THROW_S();
		}
	}
void Document::loadFromCurrenttDir( const String& fileName_ )
{
	FRL_EXCEPT_GUARD();
	String currDir = io::fs::getCurrentDirectory();
	io::fs::addSlashToEndPath( currDir );
	fileName = currDir + fileName_;
	String buffer;
	loadFileToString( fileName, buffer );
	buffer.erase( std::remove_if( buffer.begin(), buffer.end(), private_::isCRLF ), buffer.end() ); // remove CRLF
	parseHeader( buffer );
	NodesList tmpMap = Parser::getSubNodes( buffer );
	if( tmpMap.size() > 1 )
		FRL_THROW_S_CLASS( Document::BrokenXML ); // must be one root node
	root = (*tmpMap.begin());
}
GroupPtr ServerConnection::addGroupAsyncIO2( const String& group_name )
{
	FRL_EXCEPT_GUARD();
	boost::mutex::scoped_lock guard( scope_guard );
	checkIsConnect();
	GroupList::iterator it = group_list.find( group_name );
	if( it != group_list.end() )
		FRL_THROW_S_CLASS( GroupAlreadyExist );
	AsyncIO2Group *new_gr = new AsyncIO2Group( group_name, server );
	new_gr->create();
	GroupPtr new_group( new_gr );
	GroupElemPair new_elem( group_name, new_group );
	group_list.insert( new_elem );
	return new_group; 
}
frl::Bool ServerConnection::testComplianceOPC_DA2()
{
	FRL_EXCEPT_GUARD();
	if( ! isInterfaceSupported( IID_IUnknown ) )
		return False;
	if( ! isInterfaceSupported( IID_IOPCServer ) )
		return False;
	if( ! isInterfaceSupported( IID_IOPCCommon ) )
		return False;
	if( ! isInterfaceSupported( IID_IConnectionPointContainer ) )
		return False;
	if( ! isInterfaceSupported( IID_IOPCItemProperties ) )
		return False;
	return True;
}
void GroupManager::renameGroup( const String &name, const String &to_name )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	GroupElemNamesMap::iterator end = names_map.end();
	GroupElemNamesMap::iterator it = names_map.find( to_name );
	if( it != end )
		FRL_THROW_S_CLASS( IsExistGroup );
	it = names_map.find( name );
	if( it == end )
		FRL_THROW_S_CLASS( NotExistGroup );
	GroupElem group = it->second;
	names_map.erase( it );
	group->setName( to_name );
	names_map.insert( std::pair< String, GroupElem >( name, group ) );
}
std::vector< GroupElem > GroupManager::getGroupEnum()
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	size_t size = names_map.size();
	if( size == 0 )
		FRL_THROW_S_CLASS( NotExistGroup );
	std::vector< GroupElem > vec;
	vec.reserve( size );
	GroupElemNamesMap::iterator end = names_map.end();
	for(	GroupElemNamesMap::iterator it = names_map.begin();
			it != end;
			++it )
	{
		vec.push_back( it->second );
	}
	return vec;
}
frl::opc::GroupElem GroupManager::cloneGroup( String &name, String &to_name )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	GroupElemNamesMap::iterator end = names_map.end();
	GroupElemNamesMap::iterator it = names_map.find( to_name );
	if( it != end )
		FRL_THROW_S_CLASS( IsExistGroup );

	it = names_map.find( name );
	if( it == end )
		FRL_THROW_S_CLASS( NotExistGroup );

	GroupElem new_group( it->second->clone() );
	new_group->setName( to_name );
	insert( new_group );
	return new_group;
}
bool GroupManager::removeGroup( String &name )
{
	boost::mutex::scoped_lock lock( guard );
	FRL_EXCEPT_GUARD();
	GroupElemNamesMap::iterator name_it = names_map.find( name );
	if( name_it == names_map.end() )
		FRL_THROW_S_CLASS( NotExistGroup );
	name_it->second->isDeleted( True );
	OPCHANDLE tmp_handle = name_it->second->getServerHandle();
	names_map.erase( name_it );
	
	GroupElemHandlesMap::iterator handle_it = handles_map.find( tmp_handle );
	if( handle_it == handles_map.end() )
		FRL_THROW_S_CLASS( NotExistGroup );
	Group *ptr = handle_it->second.get();
	ptr->AddRef();
	handles_map.erase( handle_it );
	return ptr->Release() == 0;
}
CLSID ServerConnection::getCLSID()
{
	FRL_EXCEPT_GUARD();
	CLSID server_clsid = GUID_NULL;
	#if( FRL_CHARACTER == FRL_CHARACTER_UNICODE )
		if( FAILED(CLSIDFromProgID( server_id.c_str(), &server_clsid)) )
		{
			if( UuidFromString( (unsigned short*)server_id.c_str(), &server_clsid) != RPC_S_OK )
				FRL_THROW_S_CLASS( NotResolveProgID );
		}
	#else
		if( FAILED(CLSIDFromProgID( string2wstring( server_id ).c_str(), &server_clsid)) )
		{
			if( UuidFromString( (unsigned char*)server_id.c_str(), &server_clsid) != RPC_S_OK )
				FRL_THROW_S_CLASS( NotResolveProgID );
		}	
	#endif // FRL_CHARACTER_UNICODE
	return server_clsid;
}
Tag* AddressSpace::addLeaf( const String &fullPath, Bool createPath )
{
	FRL_EXCEPT_GUARD();
	if( fullPath.empty() )
		FRL_THROW_S_CLASS( InvalidLeafName );
	size_t pos = fullPath.rfind( delimiter + delimiter );
	if ( pos != String::npos )
		FRL_THROW_S_CLASS( InvalidLeafName );
	pos = fullPath.rfind( delimiter );
	if( pos == 0 || pos == fullPath.length()-1 )
		FRL_THROW_S_CLASS( InvalidLeafName );
	if( pos == String::npos )
	{
		if ( rootTag->isExistTag( fullPath ) )
			FRL_THROW_S_CLASS( Tag::IsExistTag );
		rootTag->addLeaf( fullPath );
		Tag *added = rootTag->getLeaf( fullPath );
		nameLeafCache.insert( std::pair< String, Tag*>( fullPath, added ) );
		return added;
	}
	String fullBranchName = fullPath.substr(0, pos );

	try
	{
		//getBranch( fullBranchName )->addLeaf( fullPath );
		if ( !isExistBranch(fullBranchName) )
		{
			addBranch( fullBranchName );
		}
		
		getBranch( fullBranchName )->addLeaf( fullPath );
		Tag *added = getBranch( fullBranchName )->getLeaf( fullPath );
		nameLeafCache.insert( std::pair< String, Tag*>( fullPath, added ) );
		return added;
	}
	catch( Tag::NotExistTag& )
	{
		if( ! createPath )
			FRL_THROW_S_CLASS( Tag::NotExistTag );
	}
	return NULL;
}
void Document::parseHeader( String &buffer )
{
	FRL_EXCEPT_GUARD();
	if( buffer.empty() )
		FRL_THROW_S_CLASS( Document::EmptyFile );

	String tmpBuffer = buffer;
	size_t headerBegin = buffer.find( FRL_STR("<?xml") );
	if( headerBegin != String::npos )
	{
		headerBegin += 5;
		size_t headerEnd = buffer.find( FRL_STR("?>") );
		if( headerEnd == String::npos )
			FRL_THROW_S_CLASS( Document::BrokenXML );
		headerEnd -= headerBegin;
		if( headerBegin >= headerEnd )
			FRL_THROW_S_CLASS( Document::BrokenXML );
		String tmp = buffer.substr( headerBegin, headerEnd );
		headerEnd += (headerBegin + 2);
		buffer = buffer.substr( headerEnd, buffer.length() - 1 );
		removeSymbolsFromStart( tmp, FRL_STR(' ') );
		removeSymbolsFromEnd( tmp, FRL_STR(' ') );
		try
		{
			version = Parser::getProperty( tmp, FRL_STR("version") );
		}
		catch( ... )
		{
			FRL_THROW_S_CLASS( Document::BrokenXML );
		}
		try
		{
			encoding = Parser::getProperty( tmp, FRL_STR("encoding") );
		}
		catch( ... )
		{
			return;
		}
	}
}
std::vector< String > ServerConnection::getItemsList( OPCBROWSEFILTER type, const String& from_branch, const String& name_filter, const String& vendor_filter )
{
	FRL_EXCEPT_GUARD();
	checkIsConnect();
	ComPtr<IOPCBrowse> br_ptr;
	getInterface( IID_IOPCBrowse, br_ptr );
	LPWSTR item_id = util::duplicateString( unicodeCompatibility( from_branch ) );
	LPWSTR cont_pint = NULL;
	LPWSTR name_filter_str = util::duplicateString( unicodeCompatibility( name_filter ) );
	LPWSTR vendor_filter_str = util::duplicateString( unicodeCompatibility( vendor_filter ) );
	DWORD *property_ids = os::win32::com::allocMemory<DWORD>();
	DWORD count;
	OPCBROWSEELEMENT *browse_elements;
	BOOL m = FALSE;
	HRESULT val = br_ptr->Browse( item_id, &cont_pint, 0, type, name_filter_str, vendor_filter_str, FALSE, FALSE, 0, property_ids, &m, &count, &browse_elements );
	if( FAILED( val ) )
	{
		os::win32::com::freeMemory( property_ids );
		os::win32::com::freeMemory( item_id );
		os::win32::com::freeMemory( name_filter_str );
		os::win32::com::freeMemory( vendor_filter_str );
		FRL_THROW_OPC( val );
	}

	std::vector< String > ret_vec( count );
	for( DWORD i = 0; i < count; ++i )
	{
		ret_vec[i] = similarCompatibility( browse_elements[i].szName );
		os::win32::com::freeMemory( browse_elements[i].szName );
		os::win32::com::freeMemory( browse_elements[i].szItemID );
		os::win32::com::freeMemory( browse_elements[i].ItemProperties.pItemProperties );
	}
	os::win32::com::freeMemory( property_ids );
	os::win32::com::freeMemory( item_id );
	os::win32::com::freeMemory( name_filter_str );
	os::win32::com::freeMemory( vendor_filter_str );
	os::win32::com::freeMemory( browse_elements );
	return ret_vec;
}
Exemple #27
0
Event::Event() : handle( CreateEvent(NULL, FALSE, FALSE, NULL) )
{
	FRL_EXCEPT_GUARD();
	if( handle == NULL )
		FRL_THROW_S_CLASS( Event::InitializeError );
}
Exemple #28
0
ColorOut::ColorOut()
{
	FRL_EXCEPT_GUARD();
	GetDefaultAttributes();
}
void ServerConnection::checkIsConnect()
{
	FRL_EXCEPT_GUARD();
	if( ! isConnected() )
		FRL_THROW_S_CLASS( NotConnected );
}
void ServerConnection::removeGroupForce( const String& group_name )
{
	FRL_EXCEPT_GUARD();
	internalRemoveGroup( group_name, True );
}