Close open files when a session expires or is terminated

Questions regarding the use of the C++ SDK for Server or Client development or integration into customer products ...

Moderator: uasdkcpp

Post Reply
drothe82
Sr. Member
Sr. Member
Posts: 12
Joined: 20 Oct 2015, 10:07

Close open files when a session expires or is terminated

Post by drothe82 »

Hi,

I am implementing an OPCUA server that exports a few files using the OPCUA FileType model.
Therefore, I derived a custom class from OpcUa::FileType, in order to have more control over file operations and file handles.
I would like the server to automatically close files when the session that was used to open the files, ends.
Looking for an appropriate callback, I found NodeManager::sessionClosed(Session*).
However, the documentation says that this callback is executed after the session was closed.

On the other hand, when calling FileType::Close(const ServiceContext& serviceContext, OpcUa_UInt32 FileHandle),
I find that this function expects that the session is not yet closed!
The session is found inside the serviceContext. FileType::Close returns Bad_InvalidArgument if the session is already closed.
I tested that the same method call to FileType::Close works, when the session is still active.

Is there any callback that I can use, that is executed when the session should be closed, but before the session actually becomes invalid?
Or is there any other way to close a file where the FileHandle is known, but the session no longer exists?
The selected session should really not be important, when I want to close a file on the server.

Best,
D. Rothe

User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Close open files when a session expires or is terminated

Post by Support Team »

Hi,

The class FileType in the server SDK implements the interface to OPC UA clients. It automatically handles the closing of the file if the session of a client is closed that has a file open.

In addition you can set an additional application timeout if you want to avoid that a client with a valid session keeps a file open too long. This is a SDK specific feature provided through FileType::setClientProcessingTimeout(). The timeout is reset with every client operation like Read, Writer or SetPosition. A file handle that is timed out will be closed at the next time the file is opened.

By default the FileType uses the class UaFile for all file access operations. The default behaviour of UaFile can be changed by overwriting FileType::createFileAccessObject. This allows to create a own implementation of UaFile with the application specific behaviour.

Therefore you should overwrite UaFile to control access to the file and overwrite FileType to provide your specialized UaFile in FileType::createFileAccessObject(). UaFile::close will be called automatically in the case of a Session close or if the application timeout is enforced.
Best regards
Unified Automation Support Team

drothe82
Sr. Member
Sr. Member
Posts: 12
Joined: 20 Oct 2015, 10:07

Re: Close open files when a session expires or is terminated

Post by drothe82 »

Hi,

thank you for explaining.
However, there still is a problem with OpcUa::FileType in SDK 1.6.2. I can see this in the UA Demo Server,
using two instances of UaExpert. Let's call them UaExpert1 and UaExpert2.

The UA Demo Server exports the file serverconfig.xml as FileType node under the browse path
/Objects/Demo/014_Files/serverconfig.xml

OpenCount goes up and down as I open and close the File from UaExpert1.
I can also see the changes in UaExpert2.
I leave the file opened in UaExpert1 and close the session.
Then, in UaExpert2, I still can see that OpenCount > 0.
Maybe you are right that the File is actually closed when I close the session, but then I would expect that OpenCount also gets updated.

Post Reply