Kea 2.7.6
|
Provides a UserDataSource implementation for JSON text files. More...
#include <user_file.h>
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. | |
Static Public Attributes | |
static const size_t | USER_ENTRY_MAX_LEN = 4096 |
Maximum length of a single user entry. | |
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:
Definition at line 58 of file user_file.h.
user_chk::UserFile::UserFile | ( | const std::string & | fname | ) |
Constructor.
Create a UserFile for the given file name without opening the file.
fname | pathname to the input file. |
UserFileError | if given file name is empty. |
Definition at line 18 of file user_file.cc.
|
virtual |
Destructor. / The destructor does call the close method.
Definition at line 24 of file user_file.cc.
References close().
|
virtual |
Closes the underlying file.
Method is exception safe.
Implements user_chk::UserDataSource.
Definition at line 141 of file user_file.cc.
Referenced by ~UserFile().
|
virtual |
Returns true if the file is open.
Implements user_chk::UserDataSource.
Definition at line 136 of file user_file.cc.
Referenced by open(), and readNextUser().
UserPtr user_chk::UserFile::makeUser | ( | const std::string & | user_string | ) |
Creates a new User instance from JSON text.
user_string | string the JSON text for a user entry. |
UserFileError | if 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().
|
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.
UserFileError | if the file cannot be opened. |
Implements user_chk::UserDataSource.
Definition at line 29 of file user_file.cc.
References isc_throw, and isOpen().
|
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.
UserFileError | if 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.
|
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().