11 #include "ace/INET_Addr.h" 13 #include "ace/Default_Constants.h" 14 #include "ace/Mem_Map.h" 15 #include "ace/OS_NS_fcntl.h" 16 #include "ace/OS_NS_unistd.h" 17 #include "ace/OS_NS_sys_socket.h" 18 #include "ace/OS_NS_Thread.h" 19 #include "ace/OS_NS_sys_time.h" 20 #include "ace/High_Res_Timer.h" 27 #include "madara/Version.h" 30 __DATE__ +
" at " + __TIME__;
38 unsigned char version_buffer[4] = {0,0,0,0};
39 uint32_t * return_value = (uint32_t *) version_buffer;
40 unsigned int major, minor, revision;
41 char version_delimiter;
43 std::stringstream buffer;
46 buffer << str_version;
51 buffer >> version_delimiter;
56 buffer >> version_delimiter;
63 version_buffer[1] = (
unsigned char)major;
64 version_buffer[2] = (
unsigned char)minor;
65 version_buffer[3] = (
unsigned char)revision;
73 std::stringstream new_version;
74 unsigned char * version_ptr = (
unsigned char *)&version;
75 unsigned int major, minor, revision;
77 major = (
unsigned int)version_ptr[1];
78 minor = (
unsigned int)version_ptr[2];
79 revision = (
unsigned int)version_ptr[3];
85 new_version << revision;
87 return new_version.str ();
95 return input.substr (prefix.size ());
103 for (std::string::iterator cur = input.begin ();
104 cur != input.end (); ++cur)
105 *cur = toupper (*cur);
114 for (std::string::iterator cur = input.begin ();
115 cur != input.end (); ++cur)
129 for (std::string::iterator cur = input.begin ();
130 cur != input.end (); ++cur)
131 *cur = tolower (*cur);
141 std::string::iterator cur = input.begin ();
144 for (std::string::iterator eval = cur;
145 eval != input.end (); ++eval)
148 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
164 if (cur != input.end ())
167 if (*cur !=
' ' && *cur !=
'\t' && *cur !=
'\n' && *cur !=
'\r')
172 if (cur != input.end ())
173 input.erase (cur, input.end ());
183 std::string::iterator cur = input.begin ();
185 for (std::string::iterator eval = cur;
186 eval != input.end (); ++eval)
189 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
197 if (cur != input.end ())
198 input.erase (cur, input.end ());
211 std::string::iterator cur = input.begin ();
213 for (std::string::iterator eval = cur;
214 eval != input.end (); ++eval)
217 if (*eval != unwanted)
225 if (cur != input.end ())
226 input.erase (cur, input.end ());
238 size_t replacements = 0;
240 if (old_phrase !=
"")
242 for (std::string::size_type i = source.find (old_phrase, 0);
243 i != source.npos; i = source.find (old_phrase, i))
245 source.replace (i, old_phrase.size (), new_phrase);
246 i += new_phrase.size ();
261 std::stringstream source, dest;
263 std::vector <std::string> splitters;
264 splitters.resize (2);
271 while (std::getline (source, cur))
273 std::vector <std::string> tokens;
274 std::vector <std::string> pivots;
275 tokenizer (cur, splitters, tokens, pivots);
292 const ::std::vector< std::string> & splitters,
293 ::std::vector< std::string> & tokens,
294 ::std::vector< std::string> & pivots)
296 std::string::size_type last = 0;
297 std::string::size_type cur = 0;
301 for (; cur < input.size (); ++cur)
303 for (std::string::size_type i = 0; i < splitters.size (); ++i)
306 if (splitters[i].size () > 0)
309 if (input[cur] == splitters[i][0])
311 std::string::size_type checker = cur;
312 std::string::size_type j = 1;
314 checker < input.size () && j < splitters[i].size () &&
315 input[checker] == splitters[i][j];
319 if (j == splitters[i].size ())
322 pivots.push_back (input.substr (cur, j));
324 if (cur - last >= splitters[i].size () - 1)
325 tokens.push_back (input.substr (last, cur - last));
327 tokens.push_back (
"");
341 tokens.push_back (input.substr (last, cur - last));
351 std::string::size_type delim = key.rfind (
':');
352 delim = delim == key.npos ? key.rfind (
'@') : delim;
355 if (delim == key.npos)
364 host = key.substr (0, delim);
365 port = key.substr (delim + 1, key.size () - delim);
389 std::stringstream port_stream;
390 port_stream << u_port;
398 std::string & host,
unsigned short & port,
bool increase_until_bound)
404 "utility::bind_to_ephemeral_port:" \
405 " creating ACE_INET_Addr\n");
407 ACE_INET_Addr addr (port);
408 char hostname[HOST_NAME_MAX + 1] =
"";
411 "utility::bind_to_ephemeral_port:" \
412 " getting hostname from ACE_INET_Addr\n");
414 addr.get_host_name (hostname, HOST_NAME_MAX);
418 "utility::bind_to_ephemeral_port:" \
419 " hostname equals %s\n", hostname);
422 "utility::bind_to_ephemeral_port:" \
423 " calling ACE_OS::socket_init\n");
425 ACE_OS::socket_init (2, 2);
427 for ( ; port < 65535; ++port, addr.set (port))
431 "utility::bind_to_ephemeral_port:" \
432 " attempting open on socket %d\n", port);
434 if (socket.open (addr, 2, 0, 0) != -1)
438 if (!increase_until_bound)
443 "utility::bind_to_ephemeral_port:" \
444 " unable to bind to any ephemeral port. Check firewall\n");
453 std::stringstream buffer;
455 std::ifstream file (filename.c_str ());
460 std::getline (file, line);
467 while (std::getline (file, line))
468 buffer <<
"\n" << line;
474 "utility::file_to_string:" \
475 " failed to open file: %s\n", filename.c_str());
478 return buffer.str ();
485 std::string::size_type start = 0;
486 for (std::string::size_type i = 0; i < name.size (); ++i)
489 if (name[i] ==
'/' || name[i] ==
'\\')
493 if (i != name.size () - 1)
499 return name.substr (0, start);
505 std::string::size_type start = 0;
506 for (std::string::size_type i = 0; i < name.size (); ++i)
509 if (name[i] ==
'/' || name[i] ==
'\\')
513 if (i != name.size () - 1)
519 return name.substr (start, name.size () - start);
526 std::stringstream buffer;
528 for (
size_t i = 0; i < source.size (); ++i)
531 if (source[i] ==
'$' && i + 3 < source.size ())
533 char * value =
get_var (source, i+2, i);
540 return buffer.str ();
546 size_t cur,
size_t & end)
548 for (end = cur; end < source.size (); ++end)
550 if (source[end] ==
')' || source[end] ==
'}')
552 return getenv (source.substr (cur, end - cur).c_str ());
556 return getenv (source.substr (cur,source.size () - cur).c_str ());
564 #define REPLACE_THIS '/' 565 #define REPLACE_WITH '\\' 567 #define REPLACE_THIS '\\' 568 #define REPLACE_WITH '/' 573 for (std::string::iterator i = target.begin (); i != target.end(); ++i)
596 if (
x.c[sizeof (
unsigned short) - 1] == 1)
614 unsigned char c[
sizeof (
unsigned short)];
630 value = ((value << 8) & 0xFF00FF00FF00FF00ULL )
631 | ((value >> 8) & 0x00FF00FF00FF00FFULL );
632 value = ((value << 16) & 0xFFFF0000FFFF0000ULL )
633 | ((value >> 16) & 0x0000FFFF0000FFFFULL );
634 return (value << 32) | (value >> 32);
649 value = ((value << 8) & 0xFF00FF00FF00FF00ULL )
650 | ((value >> 8) & 0x00FF00FF00FF00FFULL );
651 value = ((value << 16) & 0xFFFF0000FFFF0000ULL )
652 | ((value >> 16) & 0x0000FFFF0000FFFFULL );
653 return (value << 32) | ((value >> 32) & 0xFFFFFFFFULL);
669 value = ((value << 8) & 0xFF00FF00 ) |
670 ((value >> 8) & 0xFF00FF );
671 return (value << 16) | (value >> 16);
686 value = ((value << 8) & 0xFF00FF00) |
687 ((value >> 8) & 0xFF00FF );
688 return (value << 16) | ((value >> 16) & 0xFFFF);
703 return ((value << 8) & 0xFFFF) | (value >> 8);
718 return ((value << 8) & 0xFFFF) | (value >> 8);
734 memcpy(&value, &orig,
sizeof(value));
739 memcpy (&result, &value,
sizeof(result));
748 void *& buffer,
size_t & size,
bool add_zero_char)
755 ACE_Mem_Map mapped_file;
757 ret_value = mapped_file.map (filename.c_str ());
758 temp_buffer = mapped_file.addr ();
759 size = mapped_file.size ();
764 buffer =
new unsigned char [size];
765 memcpy (buffer, temp_buffer, size);
769 unsigned char * zeroed = (
unsigned char *)buffer;
770 zeroed[size - 1] = 0;
780 void * buffer,
size_t size)
786 ACE_HANDLE file_handle = ACE_OS::open (filename.c_str (),
787 O_RDWR | O_CREAT | O_TRUNC,
788 ACE_DEFAULT_FILE_PERMS);
791 "Files::write_file : beginning write of %" PRIu64
" bytes\n", size);
793 if (file_handle != ACE_INVALID_HANDLE)
796 actual = ACE_OS::write (file_handle, buffer, size);
797 ACE_OS::close (file_handle);
807 ACE_Time_Value tv = ACE_High_Res_Timer::gettimeofday ();
808 int64_t timeofday (tv.sec () * 1000000000);
809 timeofday += tv.usec () * 1000;
816 bool set_seed_to_time)
819 if (set_seed_to_time)
821 unsigned int new_seed (
get_time ());
826 double position_in_range = ((double)rand()) / ((
double)RAND_MAX);
829 return (position_in_range * (ceiling - floor)) + floor;
831 return (position_in_range * (floor - ceiling)) + ceiling;
836 bool set_seed_to_time)
838 double random =
rand_double (floor, ceiling, set_seed_to_time);
845 int change = input >= 0 ? 1 : -1;
846 int64_t left = input;
847 int64_t right = input + change;
849 if (input - left < -input + right)
858 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday ();
859 ACE_Time_Value actual_time;
860 actual_time.set (sleep_time);
864 ACE_Time_Value end = ACE_High_Res_Timer::gettimeofday ();
866 double time_taken = (double) end.sec ();
867 time_taken += (double) end.usec () / 1000000;
875 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday ();
876 ACE_Time_Value earliest = current + sleep_time;
878 while (current < earliest)
881 current = ACE_High_Res_Timer::gettimeofday ();
884 return ACE_High_Res_Timer::gettimeofday () - current;
888 double value1,
double value2,
double epsilon)
890 return std::abs (value1 - value2) < epsilon;
896 if (FILE * file = fopen (filename.c_str(),
"r"))
910 unsigned int size = 0;
911 if (FILE * file = fopen (filename.c_str(),
"r"))
913 fseek (file, 0L, SEEK_END);
928 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday ();
929 ACE_Time_Value max_wait, sleep_time, next_epoch;
930 ACE_Time_Value poll_frequency, last = current;
937 max_wait = current + max_wait;
940 next_epoch = current + poll_frequency;
950 "utility::wait_true:" \
951 " variable returned %s\n",
954 current = ACE_High_Res_Timer::gettimeofday ();
961 "utility::wait_true:" \
962 " current is %" PRIu64
".%" PRIu64
" and max is %" PRIu64
".%" PRIu64
" (poll freq is %f)\n",
963 current.sec (), current.usec (), max_wait.sec (), max_wait.usec (),
967 "utility::wait_true:" \
968 " last value didn't result in success\n");
974 if (current < next_epoch)
976 sleep_time = next_epoch - current;
980 next_epoch = next_epoch + poll_frequency;
986 "utility::wait_true:" \
987 " waiting on %s\n", variable.c_str ());
989 last_value = knowledge.
get (ref, settings);
992 "utility::wait_true:" \
993 " completed eval to get %s\n",
994 last_value.to_string ().c_str ());
997 current = ACE_High_Res_Timer::gettimeofday ();
1001 if (current >= max_wait)
1004 "utility::wait_true:" \
1005 " Evaluate did not succeed. Timeout occurred\n",
1022 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday ();
1023 ACE_Time_Value max_wait, sleep_time, next_epoch;
1024 ACE_Time_Value poll_frequency, last = current;
1031 max_wait = current + max_wait;
1034 next_epoch = current + poll_frequency;
1044 "utility::wait_false:" \
1045 " variable returned %s\n",
1046 last_value.to_string ().c_str ());
1048 current = ACE_High_Res_Timer::gettimeofday ();
1051 while (!last_value.to_integer () &&
1055 "utility::wait_false:" \
1056 " current is %d.%d and max is %d.%d (poll freq is %f)\n",
1057 current.sec (), current.usec (), max_wait.sec (), max_wait.usec (),
1061 "utility::wait_false:" 1062 " last value didn't result in success\n");
1068 if (current < next_epoch)
1070 sleep_time = next_epoch - current;
1074 next_epoch = next_epoch + poll_frequency;
1080 "utility::wait_false:" 1081 " waiting on %s\n", variable.c_str ());
1086 "utility::wait_false:" 1087 " completed eval to get %s\n",
1088 last_value.to_string ().c_str ());
1091 current = ACE_High_Res_Timer::gettimeofday ();
1095 if (current >= max_wait)
1098 "utility::wait_false:" 1099 " Evaluate did not succeed. Timeout occurred\n",
1100 last_value.to_string ().c_str ());
1107 return last_value.is_true ();
This class encapsulates an entry in a KnowledgeBase.
bool is_true(void) const
Checks to see if the record is true.
double max_wait_time
Maximum time to wait for an expression to become true (in seconds)
MADARA_Export int64_t rand_int(int64_t floor=0, int64_t ceiling=RAND_MAX, bool set_seed_to_time=true)
Returns a random integer between a floor and ceiling.
MADARA_Export bool wait_false(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
Waits on a knowledge record to be false without needing KaRL language.
MADARA_Export int bind_to_ephemeral_port(ACE_SOCK_Dgram &socket, std::string &host, unsigned short &port, bool increase_until_bound=true)
Binds to an ephemeral port.
madara::knowledge::KnowledgeRecord KnowledgeRecord
MADARA_Export std::string get_version(void)
Gets the MADARA version number.
MADARA_Export int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char=false)
Reads a file into a provided void pointer.
MADARA_Export int64_t get_time(void)
Returns a time of day in nanoseconds.
std::string pre_print_statement
Statement to print before evaluations.
unsigned short us
unsigned short value
MADARA_Export std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
MADARA_Export unsigned int file_size(const std::string &filename)
Returns the size of a file.
MADARA_Export bool wait_true(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
Waits on a knowledge record to be true without needing KaRL language.
unsigned char c[sizeof(unsigned short)]
unsigned char array value
MADARA_Export std::string & lower(std::string &input)
Converts the string to lower.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
MADARA_Export uint32_t get_uint_version(void)
Gets the MADARA version number.
MADARA_Export int split_hostport_identifier(const std::string &key, std::string &host, std::string &port)
Splits a key of host:port into a corresponding host and port.
MADARA_Export double sleep(double sleep_time)
Sleeps for a certain amount of time.
Optimized reference to a variable within the knowledge base.
MADARA_Export int64_t nearest_int(double input)
Rounds a double to the nearest integer.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
std::string post_print_statement
Statement to print after evaluations.
MADARA_Export std::string & upper(std::string &input)
Converts the string to upper.
MADARA_Export std::string extract_path(const std::string &name)
Extracts the path of a filename.
bool is_little
indicates whether this is little endian (modified by constructor)
MADARA_Export double rand_double(double floor=0.0, double ceiling=1.0, bool set_seed_to_time=true)
Returns a random double between floor and ceiling.
MADARA_Export uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
INTERNAL USE: Checks for and converting to network long (htonl)
MADARA_Export ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
EndiannessChecker()
Constructor.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
This class provides a distributed knowledge base to users.
MADARA_Export std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
double poll_frequency
Frequency to poll an expression for truth (in seconds)
MADARA_Export std::string & strip_white_space(std::string &input)
Strips all whitespace characters from a string.
Integer to_integer(void) const
converts the value to an integer
MADARA_Export int merge_hostport_identifier(std::string &key, const std::string &host, const std::string &port)
Merges a host and port into a host:port key.
MADARA_Export std::string & strip_extra_white_space(std::string &input)
Strips whitespace from front and end of string and also condenses multiple whitespace into a single s...
MADARA_Export std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
MADARA_Export std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
EndiannessChecker endianness
checks for endianness of architecture on library load
MADARA_Export std::string expand_envs(const std::string &source)
Expands environment variables referenced in the string.
Provides functions and classes for the distributed knowledge base.
MADARA_Export std::string file_to_string(const std::string &filename)
Reads a file into a string.
MADARA_Export bool file_exists(const std::string &filename)
Checks if a file exists.
MADARA_Export size_t string_replace(std::string &source, const std::string &old_phrase, const std::string &new_phrase, bool replace_all=true)
Replaces an old phrase with a new phrase within a string.
MADARA_Export std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
Encapsulates settings for a wait statement.
MADARA_Export void tokenizer(const std::string &input, const ::std::vector< std::string > &splitters,::std::vector< std::string > &tokens,::std::vector< std::string > &pivot_list)
Splits an input string into tokens.
union EndiannessChecker::@18 x
by using a union, we can reference the bytes of the short
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
MADARA_Export bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
char * get_var(const std::string &source, size_t cur, size_t &end)
Helper function for madara::utility::expand_envs which retrieves an environment variable.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
void print(unsigned int level=0) const
Prints all knowledge variables and values in the context.
MADARA_Export std::string & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
MADARA_Export std::string clean_dir_name(const std::string &target)
Substitutes the appropriate directory delimiter, which may help with portability between operating sy...