1 #ifndef _MADARA_THREADSAFECONTEXT_INL_ 2 #define _MADARA_THREADSAFECONTEXT_INL_ 23 return read_file (variable, filename, settings);
56 KnowledgeMap::iterator found;
58 MADARA_GUARD_TYPE guard (
mutex_);
63 found =
map_.find (cur_key);
67 found =
map_.find (key);
70 if (found !=
map_.end ())
72 return &found->second;
84 MADARA_GUARD_TYPE guard (
mutex_);
97 KnowledgeMap::const_iterator found;
99 MADARA_GUARD_TYPE guard (
mutex_);
104 found =
map_.find (cur_key);
108 found =
map_.find (key);
111 if (found !=
map_.end ())
113 return &found->second;
125 MADARA_GUARD_TYPE guard (
mutex_);
139 MADARA_GUARD_TYPE guard (
mutex_);
154 MADARA_GUARD_TYPE guard (
mutex_);
180 return set (variable, std::forward<T>(value), settings);
190 MADARA_GUARD_TYPE guard (
mutex_);
192 return set_unsafe(variable, std::forward<T>(value), settings);
205 return set (variable, value, size, settings);
217 MADARA_GUARD_TYPE guard (
mutex_);
229 template<
typename... Args>
285 const char * value,
size_t size,
289 return set_xml (variable, value, size, settings);
295 const char * value,
size_t size,
299 return set_text (variable, value, size, settings);
305 const unsigned char * value,
size_t size,
309 return set_jpeg (variable, value, size, settings);
315 const unsigned char * value,
size_t size,
319 return set_file (variable, value, size, settings);
326 size_t index, T&& value,
330 return set_index (variable, index, std::forward<T>(value), settings);
340 MADARA_GUARD_TYPE guard (
mutex_);
373 std::forward<T>(value), settings);
387 return inc (variable, settings);
396 return dec (variable, settings);
404 MADARA_GUARD_TYPE guard (
mutex_);
423 #ifndef _MADARA_NO_KARL_ 430 MADARA_GUARD_TYPE guard (
mutex_);
435 #endif // _MADARA_NO_KARL_ 446 MADARA_GUARD_TYPE guard (
mutex_);
451 key_ptr = &key_actual;
457 KnowledgeMap::iterator record =
map_.find (*key_ptr);
458 found = record !=
map_.end ();
462 record->second.clear_value ();
479 MADARA_GUARD_TYPE guard (
mutex_);
484 key_ptr = &key_actual;
490 result =
map_.erase (*key_ptr) == 1;
508 MADARA_GUARD_TYPE guard (
mutex_);
513 key_ptr = &key_actual;
522 KnowledgeMap::const_iterator found =
map_.find (*key_ptr);
525 if (found !=
map_.end ())
541 MADARA_GUARD_TYPE guard (
mutex_);
566 MADARA_GUARD_TYPE guard (
mutex_);
586 MADARA_GUARD_TYPE guard (
mutex_);
591 key_ptr = &key_actual;
604 if (record.
clock < clock)
606 record.
clock = clock;
625 MADARA_GUARD_TYPE guard (
mutex_);
630 key_ptr = &key_actual;
650 MADARA_GUARD_TYPE guard (
mutex_);
659 MADARA_GUARD_TYPE guard (
mutex_);
666 MADARA_GUARD_TYPE guard (
mutex_);
674 MADARA_GUARD_TYPE guard (
mutex_);
687 MADARA_GUARD_TYPE guard (
mutex_);
692 key_ptr = &key_actual;
702 KnowledgeMap::const_iterator found =
map_.find (*key_ptr);
705 if (found !=
map_.end ())
707 return found->second.clock;
719 mutex_.MADARA_LOCK_LOCK ();
726 mutex_.MADARA_LOCK_UNLOCK ();
733 const std::string & statement,
unsigned int level)
const 744 MADARA_GUARD_TYPE guard (
mutex_);
753 for (madara::knowledge::KnowledgeMap::iterator i =
map_.begin ();
754 i !=
map_.end (); ++i)
756 i->second.reset_value ();
761 changed_.MADARA_CONDITION_NOTIFY_ONE ();
772 MADARA_GUARD_TYPE guard (
mutex_);
779 mutex_.MADARA_LOCK_UNLOCK ();
804 MADARA_GUARD_TYPE guard (
mutex_);
837 MADARA_GUARD_TYPE guard (
mutex_);
875 changed_.MADARA_CONDITION_NOTIFY_ALL ();
895 MADARA_GUARD_TYPE guard (
mutex_);
902 MADARA_GUARD_TYPE guard (
mutex_);
903 std::stringstream result;
905 result <<
changed_map_.size () <<
" modifications ready to send:\n";
907 for (KnowledgeRecords::const_iterator i =
changed_map_.begin ();
910 if (i->second->is_binary_file_type ())
916 result <<
"Double: ";
920 result <<
"Double array: ";
924 result <<
"Integer: ";
928 result <<
"Integer array: ";
930 else if (i->second->is_string_type ())
932 result <<
"String: ";
936 result <<
"Unknown: ";
939 result << i->first <<
" = " << i->second->to_string () <<
"\n";
942 return result.str ();
950 MADARA_GUARD_TYPE guard (
mutex_);
958 MADARA_GUARD_TYPE guard (
mutex_);
965 "ThreadSafeContext::save_modifieds:" \
966 " changed_map.size=%d, snapshot.size=%d\n",
969 for (KnowledgeRecords::const_iterator i =
changed_map_.begin ();
973 "ThreadSafeContext::save_modifieds:" \
974 " snapshot[%d].name=%s\n",
975 cur, i->first.c_str ());
977 snapshot[cur].set_name (i->first);
978 snapshot[cur].record_ = i->second;
988 MADARA_GUARD_TYPE guard (
mutex_);
990 for (VariableReferences::const_iterator i = modifieds.begin ();
991 i != modifieds.end (); ++i)
994 KnowledgeRecords::value_type (i->name_.get_ptr (), i->record_));
1002 MADARA_GUARD_TYPE guard (
mutex_);
1011 MADARA_GUARD_TYPE guard (
mutex_);
1021 MADARA_GUARD_TYPE guard (
mutex_);
1028 for (madara::knowledge::KnowledgeMap::iterator i =
map_.begin ();
1032 if (i->first.size () > 0 && i->first[0] !=
'.')
1054 MADARA_GUARD_TYPE guard (
mutex_);
1062 MADARA_GUARD_TYPE guard (
mutex_);
1074 MADARA_GUARD_TYPE guard (
mutex_);
1075 changed_.MADARA_CONDITION_NOTIFY_ONE ();
1078 changed_.MADARA_CONDITION_NOTIFY_ONE ();
1102 #endif // _MADARA_THREADSAFECONTEXT_INL_ This class encapsulates an entry in a KnowledgeBase.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false) ...
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
madara::knowledge::KnowledgeMap map_
Hash table containing variable names and values.
int set_index_unsafe(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of an array index to a value.
KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a value at a specified index within a knowledge array.
uint64_t set_clock(uint64_t clock)
Atomically sets the lamport clock.
utility::ScopedArray< const char > name_
potential string value of the node (size int)
int set_index_unsafe_impl(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
uint32_t quality
priority of the update
MADARA_CONDITION_TYPE changed_
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
madara::knowledge::KnowledgeRecord KnowledgeRecord
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
const knowledge::KnowledgeRecords & get_modifieds(void) const
Retrieves a list of modified variables.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
void lock(void) const
Locks the mutex on this context.
bool signal_changes
Toggle whether to signal changes have happened.
bool delete_expression(const std::string &expression)
Deletes the expression from the interpreter cache.
int set_unsafe(const VariableReference &variable, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of a variable to the specific value.
void set_level(int level)
Sets the maximum logging detail level.
void signal(bool lock=true) const
Signals that this thread is done with the context.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
Provides knowledge logging services to files and terminals.
void mark_to_checkpoint(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock for the purposes of checkpointing (even if it...
int get_log_level(void)
Gets the log level.
Optimized reference to a variable within the knowledge base.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
A multi-threaded logger for logging to one or more destinations.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
void apply_modified(void)
Changes all global variables to modified at current clock.
int get_level(void)
Gets the maximum logging detail level.
int status(void) const
returns the status of the record.
const knowledge::KnowledgeRecords & get_local_modified(void) const
Retrieves a list of modified local variables.
int set_xml(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
T * get_ptr(void)
get the underlying pointer
uint64_t clock_increment
Default clock increment.
void add_logger(const std::string &filename)
Adds a file to the logger.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
void unlock(void) const
Unlocks the mutex on this context.
madara::knowledge::KnowledgeRecord * with(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int set_unsafe_impl(const VariableReference &variable, const KnowledgeUpdateSettings &settings, Args &&...args)
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
knowledge::KnowledgeRecords local_changed_map_
void add_file(const std::string &filename)
Adds a file to the logger.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
void set_modified(void)
sets the status to modified
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
uint64_t inc_clock(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the Lamport clock and returns the new clock time (intended for sending knowledg...
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
std::vector< VariableReference > VariableReferences
a vector of variable references
int set_text(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
void mark_to_checkpoint_unsafe(const std::string &key, madara::knowledge::KnowledgeRecord &record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock for the purposes of checkpointing.
knowledge::KnowledgeRecords changed_map_
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
logger::Logger * logger_
Logger for printing.
const char * get_name(void) const
Returns the name of the variable.
uint64_t get_clock(void) const
Atomically gets the Lamport clock.
uint32_t write_quality
write priority for any local updates
void mark_to_send(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock, and queues it to send, even if it is a local...
knowledge::KnowledgeRecord * record_
Reference to knowledge record.
void reset_checkpoint(void) const
Reset all checkpoint variables in the modified lists.
void mark_to_send_unsafe(const std::string &key, madara::knowledge::KnowledgeRecord &record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock, and queues it to send, even if it is a local that woul...
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
bool track_local_changes
Toggle for checkpointing support.
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
Settings for applying knowledge updates.
void mark_and_signal(const char *name, knowledge::KnowledgeRecord *record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
method for marking a record modified and signaling changes
madara::expression::Interpreter * interpreter_
KaRL interpreter.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
bool treat_locals_as_globals
Toggle whether updates to local variables are treated as global variables that should be sent over th...
uint64_t clock
last modification time
void set_log_level(int level)
Sets the log level.
void reset_modified(void)
Reset all variables to be unmodified.