Kea 2.5.8
user_chk::UserFile Class Reference

Provides a UserDataSource implementation for JSON text files. More...

#include <user_file.h>

+ Inheritance diagram for user_chk::UserFile:

Public Member Functions

 UserFile (const std::string &fname)
 Constructor.
 
virtual ~UserFile ()
 Destructor. / The destructor does call the close method.
 
virtual void close ()
 Closes the underlying file.
 
virtual bool isOpen () const
 Returns true if the file is open.
 
UserPtr makeUser (const std::string &user_string)
 Creates a new User instance from JSON text.
 
virtual void open ()
 Opens the input file for reading.
 
virtual UserPtr readNextUser ()
 Fetches the next user from the file.
 
- Public Member Functions inherited from user_chk::UserDataSource
 UserDataSource ()
 Constructor.
 
virtual ~UserDataSource ()
 Virtual Destructor.
 
virtual void close ()=0
 Closes that data source.
 
virtual bool isOpen () const =0
 Returns true if the data source is open.
 
virtual void open ()=0
 Opens the data source.
 
virtual UserPtr readNextUser ()=0
 Fetches the next user from the data source.
 

Static Public Attributes

static const size_t USER_ENTRY_MAX_LEN = 4096
 Maximum length of a single user entry.
 

Detailed Description

Provides a UserDataSource implementation for JSON text files.

This class allows a text file of JSON entries to be treated as a source of User entries. The format of the file is one user entry per line, where each line contains a JSON string as follows:

{ "type" : "<user type>", "id" : "<user_id>" (options) }

where:

<user_type> text label of the id type: "HW_ADDR" or "DUID" <user_id> the user's id as a string of hex digits with or without colons (':') as a delimiter (options) zero or more string elements as name-value pairs, separated by commas: "opt1" : "val1", "other_opt", "77" ...

Each entry must have a valid entry for "type" and a valid entry or "id".

If an entry contains duplicate option names, that option will be assigned the last value found. This is typical JSON behavior. Currently, only string option values (i.e. enclosed in quotes) are supported.

Example file entries might look like this:

{ "type" : "HW_ADDR", "id" : "01AC00F03344", "opt1" : "true" }
{ "type" : "DUID", "id" : "225060de0a0b", "opt1" : "false" }

Definition at line 58 of file user_file.h.

Constructor & Destructor Documentation

◆ UserFile()

user_chk::UserFile::UserFile ( const std::string &  fname)

Constructor.

Create a UserFile for the given file name without opening the file.

Parameters
fnamepathname to the input file.
Exceptions
UserFileErrorif given file name is empty.

Definition at line 18 of file user_file.cc.

References isc_throw.

◆ ~UserFile()

user_chk::UserFile::~UserFile ( )
virtual

Destructor. / The destructor does call the close method.

Definition at line 24 of file user_file.cc.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ close()

void user_chk::UserFile::close ( )
virtual

Closes the underlying file.

Method is exception safe.

Implements user_chk::UserDataSource.

Definition at line 142 of file user_file.cc.

Referenced by ~UserFile().

◆ isOpen()

bool user_chk::UserFile::isOpen ( ) const
virtual

Returns true if the file is open.

Returns
True if the underlying file is open, false otherwise.

Implements user_chk::UserDataSource.

Definition at line 137 of file user_file.cc.

Referenced by open(), and readNextUser().

◆ makeUser()

UserPtr user_chk::UserFile::makeUser ( const std::string &  user_string)

Creates a new User instance from JSON text.

Parameters
user_stringstring the JSON text for a user entry.
Returns
A pointer to the newly created User instance.
Exceptions
UserFileErrorif the entry is invalid.

Definition at line 65 of file user_file.cc.

References isc::data::Element::fromJSON(), isc_throw, user_chk::UserId::lookupType(), isc::data::Element::string, and isc::Exception::what().

Referenced by readNextUser().

+ Here is the call graph for this function:

◆ open()

void user_chk::UserFile::open ( )
virtual

Opens the input file for reading.

Upon successful completion, the file is opened and positioned to start reading from the beginning of the file.

Exceptions
UserFileErrorif the file cannot be opened.

Implements user_chk::UserDataSource.

Definition at line 29 of file user_file.cc.

References isc_throw, and isOpen().

+ Here is the call graph for this function:

◆ readNextUser()

UserPtr user_chk::UserFile::readNextUser ( )
virtual

Fetches the next user from the file.

Reads the next user entry from the file and attempts to create a new User from the text therein. If there is no more data to be read it returns an empty UserPtr.

Returns
A UserPtr pointing to the new User or an empty pointer on EOF.
Exceptions
UserFileErrorif an error occurs while reading.

Implements user_chk::UserDataSource.

Definition at line 43 of file user_file.cc.

References isc_throw, isOpen(), makeUser(), and USER_ENTRY_MAX_LEN.

+ Here is the call graph for this function:

Member Data Documentation

◆ USER_ENTRY_MAX_LEN

const size_t user_chk::UserFile::USER_ENTRY_MAX_LEN = 4096
static

Maximum length of a single user entry.

This value is somewhat arbitrary. 4K seems reasonably large. If it goes beyond this, then a flat file is not likely the way to go.

Definition at line 63 of file user_file.h.

Referenced by readNextUser().


The documentation for this class was generated from the following files: