diff options
author | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
---|---|---|
committer | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
commit | c44379cc7d9c7aa113989237ab0f56db12aa5219 (patch) | |
tree | 66a84b20d47f2269d8bdc6e0323f338763424d3a /ACE/examples/APG/Naming | |
parent | 3aff90f4a822fcf5d902bbfbcc9fa931d6191a8c (diff) | |
download | ATCD-c44379cc7d9c7aa113989237ab0f56db12aa5219.tar.gz |
Repo restructuring
Diffstat (limited to 'ACE/examples/APG/Naming')
25 files changed, 1200 insertions, 0 deletions
diff --git a/ACE/examples/APG/Naming/.cvsignore b/ACE/examples/APG/Naming/.cvsignore new file mode 100644 index 00000000000..e5fd579f85d --- /dev/null +++ b/ACE/examples/APG/Naming/.cvsignore @@ -0,0 +1,10 @@ +Netlocal +Netlocal +Netlocal_reader +Netlocal_reader +Nodelocal +Nodelocal +Nodelocal_shared +Nodelocal_shared +Nodelocal_shared_reader +Nodelocal_shared_reader diff --git a/ACE/examples/APG/Naming/EMail.h b/ACE/examples/APG/Naming/EMail.h new file mode 100644 index 00000000000..fc38913a10a --- /dev/null +++ b/ACE/examples/APG/Naming/EMail.h @@ -0,0 +1,28 @@ +// $Id$ + +#ifndef EMAIL_H +#define EMAIL_H + +#include "ace/Log_Msg.h" + +class EMail + { + public: + EMail() + { } + + int send (const char *to, + const char *from, + const char *subject, + const char *message) + { + ACE_DEBUG ((LM_ERROR, ACE_TEXT ("To:\t%s\n"), to)); + ACE_DEBUG ((LM_ERROR, ACE_TEXT ("From:\t%s\n"), from)); + ACE_DEBUG ((LM_ERROR, ACE_TEXT ("Subject:\t%s\n"), subject)); + ACE_DEBUG ((LM_ERROR, ACE_TEXT ("\n%s\n"), message)); + + return 0; + } + }; + +#endif /* EMAIL_H */ diff --git a/ACE/examples/APG/Naming/Graph.cpp b/ACE/examples/APG/Naming/Graph.cpp new file mode 100644 index 00000000000..62ca52afd0d --- /dev/null +++ b/ACE/examples/APG/Naming/Graph.cpp @@ -0,0 +1,47 @@ +// $Id$ + +#include "ace/Log_Msg.h" +#include "Graph.h" + +void Graph::graph (char *filename, Graphable_Element_List &data) +{ + data.sort (); + + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Save graph to %C\n"), filename)); + + char h[10][10]; + for (int n = 0 ; n < 10 ; ++n ) + { + for (int j = 0; j < 10; ++j ) + { + h[n][j] = ' '; + } + } + + int l[10]; + int k = 0; + for (Graphable_Element_List::iterator i = data.begin (); + i != data.end (); + ++i, ++k ) + { + l[k] = (*i).when (); + + int temp = (int)((*i).temp () - 80.0); + + for (int j = 0; j <= temp; ++j) + { + h[k][j] = '#'; + } + } + + for (int m = 0 ; m < 10 ; ++m) + { + ACE_DEBUG ((LM_INFO, ACE_TEXT ("%d "), l[m])); + + for (int j = 0; j < 10; ++j) + { + ACE_DEBUG ((LM_INFO, ACE_TEXT ("%c"), h[m][j])); + } + ACE_DEBUG ((LM_INFO, ACE_TEXT ("\n"))); + } +} diff --git a/ACE/examples/APG/Naming/Graph.h b/ACE/examples/APG/Naming/Graph.h new file mode 100644 index 00000000000..135b7d14f77 --- /dev/null +++ b/ACE/examples/APG/Naming/Graph.h @@ -0,0 +1,18 @@ +// $Id$ + +#ifndef GRAPH_H +#define GRAPH_H + +#include "Graphable_Element.h" + +class Graph + { + public: + Graph() + { + } + + void graph( char * filename, Graphable_Element_List & data ); + }; + +#endif /* GRAPH_H */ diff --git a/ACE/examples/APG/Naming/Graphable_Element.cpp b/ACE/examples/APG/Naming/Graphable_Element.cpp new file mode 100644 index 00000000000..c3ff6ee7b41 --- /dev/null +++ b/ACE/examples/APG/Naming/Graphable_Element.cpp @@ -0,0 +1,4 @@ +// $Id$ + +#include "Graphable_Element.h" + diff --git a/ACE/examples/APG/Naming/Graphable_Element.h b/ACE/examples/APG/Naming/Graphable_Element.h new file mode 100644 index 00000000000..42758934993 --- /dev/null +++ b/ACE/examples/APG/Naming/Graphable_Element.h @@ -0,0 +1,53 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef GRAPHABLE_ELEMENT_H +#define GRAPHABLE_ELEMENT_H + +#include "Name_Binding.h" +#include <list> + +// A helper class that knows how to sort two ACE_Name_Binding objects +// which contain temperature metrics. The value stored in the binding +// is expected to be of the format "time|temp". +// +// Listing 1 code/ch21 +class Graphable_Element : public Name_Binding +{ +public: + Graphable_Element (ACE_Name_Binding *entry) + : Name_Binding(entry) + { + sscanf (this->value (), "%d|%f", &this->when_, &this->temp_); + } + // Listing 1 + + // Listing 2 code/ch21 + inline int when (void) const + { + return this->when_; + } + + inline float temp (void) + { + return this->temp_; + } + // Listing 2 + + // Listing 3 code/ch21 + inline bool operator< (const Graphable_Element &other) const + { + return this->when () < other.when (); + } + // Listing 3 + + // Listing 4 code/ch21 +private: + int when_; + float temp_; +}; + +typedef std::list<Graphable_Element> Graphable_Element_List; +// Listing 4 + +#endif /* GRAPHABLE_ELEMENT_H */ diff --git a/ACE/examples/APG/Naming/Makefile.am b/ACE/examples/APG/Naming/Makefile.am new file mode 100644 index 00000000000..f73b1258348 --- /dev/null +++ b/ACE/examples/APG/Naming/Makefile.am @@ -0,0 +1,123 @@ +## Process this file with automake to create Makefile.in +## +## $Id$ +## +## This file was generated by MPC. Any changes made directly to +## this file will be lost the next time it is generated. +## +## MPC Command: +## /acebuilds/ACE_wrappers-repository/bin/mwc.pl -include /acebuilds/MPC/config -include /acebuilds/MPC/templates -feature_file /acebuilds/ACE_wrappers-repository/local.features -noreldefs -type automake -exclude build,Kokyu + +ACE_BUILDDIR = $(top_builddir) +ACE_ROOT = $(top_srcdir) + +noinst_PROGRAMS = + +## Makefile.Netlocal.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += Netlocal + +Netlocal_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Netlocal_SOURCES = \ + Netlocal.cpp \ + Temperature_Monitor2.cpp \ + Temperature_Monitor2.h + +Netlocal_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.Netlocal_Reader.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += Netlocal_reader + +Netlocal_reader_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Netlocal_reader_SOURCES = \ + Graph.cpp \ + Netlocal_reader.cpp \ + Temperature_Grapher.cpp \ + Graph.h \ + Temperature_Grapher.h + +Netlocal_reader_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.Nodelocal.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += Nodelocal + +Nodelocal_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Nodelocal_SOURCES = \ + Nodelocal.cpp \ + Temperature_Monitor.cpp \ + Temperature_Monitor.h + +Nodelocal_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.Nodelocal_Shared.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += Nodelocal_shared + +Nodelocal_shared_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Nodelocal_shared_SOURCES = \ + Nodelocal_shared.cpp \ + Temperature_Monitor2.cpp \ + Temperature_Monitor2.h + +Nodelocal_shared_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.Nodelocal_Shared_Reader.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += Nodelocal_shared_reader + +Nodelocal_shared_reader_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Nodelocal_shared_reader_SOURCES = \ + Graph.cpp \ + Nodelocal_shared_reader.cpp \ + Temperature_Grapher.cpp \ + Temperature_Monitor.cpp \ + Graph.h \ + Temperature_Grapher.h \ + Temperature_Monitor.h + +Nodelocal_shared_reader_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Clean up template repositories, etc. +clean-local: + -rm -f *~ *.bak *.rpo *.sym lib*.*_pure_* core core.* + -rm -f gcctemp.c gcctemp so_locations *.ics + -rm -rf cxx_repository ptrepository ti_files + -rm -rf templateregistry ir.out + -rm -rf ptrepository SunWS_cache Templates.DB diff --git a/ACE/examples/APG/Naming/Name_Binding.h b/ACE/examples/APG/Naming/Name_Binding.h new file mode 100644 index 00000000000..6c093a941f2 --- /dev/null +++ b/ACE/examples/APG/Naming/Name_Binding.h @@ -0,0 +1,61 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef NAME_BINDING_H +#define NAME_BINDING_H + +#include "ace/OS_NS_stdlib.h" +#include "ace/OS_NS_string.h" +#include "ace/Auto_Ptr.h" +#include "ace/Name_Space.h" + +// Listing 1 code/ch21 +class Name_Binding +{ +public: + Name_Binding (ACE_Name_Binding *entry) + { + this->name_ = entry->name_.char_rep (); + this->value_ = entry->value_.char_rep (); + this->type_ = ACE_OS::strdup (entry->type_); + } + + Name_Binding (const ACE_NS_WString &n, + const ACE_NS_WString &v, + const char *t) + { + this->name_ = n.char_rep (); + this->value_ = v.char_rep (); + this->type_ = ACE_OS::strdup (t); + } + + ~Name_Binding () + { + delete this->name_; + delete this->value_; + ACE_OS::free (const_cast<char*> (this->type_)); + this->type_ = 0; + } + + char *name (void) + { return this->name_; } + + char *value (void) + { return this->value_; } + + const char *type (void) + { return this->type_; } + + int int_value (void) + { return ACE_OS::atoi (this->value ()); } + +private: + char *name_; + char *value_; + char *type_; +}; + +typedef auto_ptr<Name_Binding> Name_Binding_Ptr; +// Listing 1 + +#endif /* NAME_BINDING_H */ diff --git a/ACE/examples/APG/Naming/Naming_Context.h b/ACE/examples/APG/Naming/Naming_Context.h new file mode 100644 index 00000000000..abcf793b2b6 --- /dev/null +++ b/ACE/examples/APG/Naming/Naming_Context.h @@ -0,0 +1,68 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef NAMING_CONTEXT_H +#define NAMING_CONTEXT_H + +#include "ace/Naming_Context.h" +#include "ace/OS_NS_stdio.h" +#include "Name_Binding.h" + +// Listing 1 code/ch21 +class Naming_Context : public ACE_Naming_Context +{ +public: + typedef ACE_Naming_Context inherited; + + int rebind (const char *name_in, + const char *value_in, + const char *type_in = "") + { + return this->inherited::rebind (name_in, value_in, type_in); + } + + int rebind (const char *name_in, + float value_in, + const char *type_in = "") + { + char buf[BUFSIZ]; + ACE_OS::sprintf (buf, "%2f", value_in); + return this->inherited::rebind (name_in, + (const char *)buf, + type_in); + } + + int rebind (const char *name_in, + int value_in, + const char *type_in = "") + { + char buf[BUFSIZ]; + ACE_OS::sprintf (buf, "%d", value_in ); + return this->inherited::rebind (name_in, + (const char *)buf, + type_in); + } + // Listing 1 + + // Listing 2 code/ch21 + Name_Binding *fetch (const char *name) + { + ACE_NS_WString value; + char *type; + + if (this->resolve (name, value, type) != 0 || + value.length () < 1) + { + return 0; + } + + Name_Binding *rval = + new Name_Binding (ACE_NS_WString (name), + value, + type); + return rval; + } +// Listing 2 +}; + +#endif /* NAMING_CONTEXT_H */ diff --git a/ACE/examples/APG/Naming/Netlocal.cpp b/ACE/examples/APG/Naming/Netlocal.cpp new file mode 100644 index 00000000000..f1799ab3882 --- /dev/null +++ b/ACE/examples/APG/Naming/Netlocal.cpp @@ -0,0 +1,40 @@ +// $Id$ + +#include "Naming_Context.h" +#include "Temperature_Monitor2.h" +#include "Temperature_Monitor_Options.h" + +// Listing 1 code/ch21 +int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) +{ + Temperature_Monitor_Options opt (argc, argv); + + Naming_Context process_context; + { + ACE_Name_Options *name_options = + process_context.name_options (); + name_options->context (ACE_Naming_Context::PROC_LOCAL); + ACE_TCHAR *nargv[] = { argv[0] }; + name_options->parse_args (sizeof(nargv) / sizeof(ACE_TCHAR*), + nargv); + process_context.open (name_options->context ()); + } + + Naming_Context shared_context; + { + ACE_Name_Options *name_options = + shared_context.name_options (); + name_options->process_name (argv[0]); + name_options->context (ACE_Naming_Context::NET_LOCAL); + shared_context.open (name_options->context ()); + } + + Temperature_Monitor2 temperature_monitor (opt, + process_context, + shared_context); + temperature_monitor.monitor (); + process_context.close (); + shared_context.close (); + return 0; +} +// Listing 1 diff --git a/ACE/examples/APG/Naming/Netlocal_reader.cpp b/ACE/examples/APG/Naming/Netlocal_reader.cpp new file mode 100644 index 00000000000..f77724aabbe --- /dev/null +++ b/ACE/examples/APG/Naming/Netlocal_reader.cpp @@ -0,0 +1,23 @@ +// $Id$ + +#include "Naming_Context.h" +#include "Temperature_Grapher.h" +#include "Temperature_Grapher_Options.h" + +// Listing 1 code/ch21 +int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) +{ + Temperature_Grapher_Options opt (argc, argv); + + Naming_Context naming_context; + ACE_Name_Options *name_options = naming_context.name_options (); + name_options->process_name (argv[0]); + name_options->context (ACE_Naming_Context::NET_LOCAL); + naming_context.open (name_options->context ()); + + Temperature_Grapher grapher (opt, naming_context); + grapher.monitor (); + naming_context.close (); + return 0; +} +// Listing 1 diff --git a/ACE/examples/APG/Naming/Nodelocal.cpp b/ACE/examples/APG/Naming/Nodelocal.cpp new file mode 100644 index 00000000000..ca37a27e374 --- /dev/null +++ b/ACE/examples/APG/Naming/Nodelocal.cpp @@ -0,0 +1,37 @@ +// $Id$ + +#include "Naming_Context.h" +#include "Temperature_Monitor.h" +#include "Temperature_Monitor_Options.h" + +// Listing 1 code/ch21 +int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) +{ + Temperature_Monitor_Options opt (argc, argv); + // Listing 1 + + // Listing 2 code/ch21 + Naming_Context naming_context; + + ACE_Name_Options *name_options = naming_context.name_options(); + // Listing 2 + + // Listing 3 code/ch21 + ACE_TCHAR *naming_options_argv[] = { argv[0] }; + name_options->parse_args + (sizeof(naming_options_argv) / sizeof(ACE_TCHAR*), + naming_options_argv); + name_options->context (ACE_Naming_Context::PROC_LOCAL); + naming_context.open (name_options->context ()); + // Listing 3 + + // Listing 4 code/ch21 + Temperature_Monitor temperature_monitor (opt, naming_context); + temperature_monitor.monitor (); + // Listing 4 + + // Listing 5 code/ch21 + naming_context.close (); + return 0; + // Listing 5 +} diff --git a/ACE/examples/APG/Naming/Nodelocal_shared.cpp b/ACE/examples/APG/Naming/Nodelocal_shared.cpp new file mode 100644 index 00000000000..34ea87aab2d --- /dev/null +++ b/ACE/examples/APG/Naming/Nodelocal_shared.cpp @@ -0,0 +1,43 @@ +// $Id$ + +#include "Naming_Context.h" +#include "Temperature_Monitor2.h" +#include "Temperature_Monitor_Options.h" + +// Listing 1 code/ch21 +int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) +{ + Temperature_Monitor_Options opt (argc, argv); + Naming_Context process_context; + { + ACE_Name_Options *name_options = + process_context.name_options (); + name_options->context (ACE_Naming_Context::PROC_LOCAL); + ACE_TCHAR *nargv[] = { argv[0] }; + name_options->parse_args (sizeof(nargv) / sizeof(ACE_TCHAR*) , + nargv); + process_context.open (name_options->context ()); + } + + Naming_Context shared_context; + { + ACE_Name_Options *name_options = + shared_context.name_options (); + name_options->process_name (argv[0]); + name_options->context (ACE_Naming_Context::NODE_LOCAL); + shared_context.open (name_options->context ()); + } + + Temperature_Monitor2 temperature_monitor (opt, + process_context, + shared_context); + temperature_monitor.monitor (); + + process_context.close (); + shared_context.close (); + + return 0; +} +// Listing 1 + + diff --git a/ACE/examples/APG/Naming/Nodelocal_shared_reader.cpp b/ACE/examples/APG/Naming/Nodelocal_shared_reader.cpp new file mode 100644 index 00000000000..7385f09a496 --- /dev/null +++ b/ACE/examples/APG/Naming/Nodelocal_shared_reader.cpp @@ -0,0 +1,23 @@ +// $Id$ + +#include "Naming_Context.h" +#include "Temperature_Grapher.h" +#include "Temperature_Grapher_Options.h" + +// Listing 1 code/ch21 +int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) +{ + Temperature_Grapher_Options opt (argc, argv); + + Naming_Context naming_context; + ACE_Name_Options *name_options = naming_context.name_options (); + name_options->process_name (argv[0]); + name_options->context (ACE_Naming_Context::NODE_LOCAL); + naming_context.open (name_options->context ()); + + Temperature_Grapher grapher (opt, naming_context); + grapher.monitor (); + naming_context.close (); + return 0; +} +// Listing 1 diff --git a/ACE/examples/APG/Naming/Temperature_Grapher.cpp b/ACE/examples/APG/Naming/Temperature_Grapher.cpp new file mode 100644 index 00000000000..e11367af4a0 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Grapher.cpp @@ -0,0 +1,81 @@ +// $Id$ + +#include "ace/OS_NS_unistd.h" +#include "ace/Log_Msg.h" + +#include "Graph.h" +#include "Graphable_Element.h" +#include "Temperature_Grapher.h" + +// Listing 1 code/ch21 +void Temperature_Grapher::monitor (void) +{ + for (;;) + { + this->update_graph (); + ACE_OS::sleep (this->opt_.poll_interval ()); + } +} +// Listing 1 + +// Listing 2 code/ch21 +void Temperature_Grapher::update_graph (void) +{ + Name_Binding_Ptr lastUpdate + (this->naming_context_.fetch ("lastUpdate")); + + if (!lastUpdate.get ()) + { + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("No data to graph\n"))); + return; + } + // Listing 2 + + // Listing 3 code/ch21 + Name_Binding_Ptr lastGraphed + (this->naming_context_.fetch ("lastGraphed")); + + if (lastGraphed.get () && + lastGraphed->int_value () == lastUpdate->int_value ()) + { + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Data already graphed\n"))); + return; + } + // Listing 3 + + // Listing 4 code/ch21 + ACE_BINDING_SET set; + if (this->naming_context_.list_name_entries + (set, "history[") != 0) + { + ACE_DEBUG ((LM_INFO, + ACE_TEXT ("There's nothing to graph\n"))); + return; + } + // Listing 4 + + // Listing 5 code/ch21 + Graphable_Element_List graphable; + ACE_BINDING_ITERATOR set_iterator (set); + for (ACE_Name_Binding *entry = 0; + set_iterator.next (entry) != 0; + set_iterator.advance ()) + { + Name_Binding binding (entry); + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s\t%s\t%s\n"), + binding.type (), + binding.name (), + binding.value ())); + + Graphable_Element *ge = new Graphable_Element (entry); + graphable.push_back (*ge); + } + // Listing 5 + + // Listing 6 code/ch21 + Graph g; + g.graph (lastUpdate->value (), graphable); + this->naming_context_.rebind ("lastGraphed", + lastUpdate->int_value ()); + // Listing 6 +} diff --git a/ACE/examples/APG/Naming/Temperature_Grapher.h b/ACE/examples/APG/Naming/Temperature_Grapher.h new file mode 100644 index 00000000000..610dac6a578 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Grapher.h @@ -0,0 +1,30 @@ +// $Id$ + +#ifndef TEMPERATURE_GRAPHER_H +#define TEMPERATURE_GRAPHER_H + +#include "Thermometer.h" +#include "Temperature_Grapher_Options.h" +#include "Naming_Context.h" + +class Temperature_Grapher + { + public: + Temperature_Grapher( Temperature_Grapher_Options & opt, + Naming_Context & naming_context ) + : opt_(opt), naming_context_(naming_context) + { + } + + void monitor(); + + protected: + void update_graph(); + + private: + Thermometer * thermometer_; + Temperature_Grapher_Options & opt_; + Naming_Context & naming_context_; + }; + +#endif /* TEMPERATURE_GRAPHER_H */ diff --git a/ACE/examples/APG/Naming/Temperature_Grapher_Options.h b/ACE/examples/APG/Naming/Temperature_Grapher_Options.h new file mode 100644 index 00000000000..e7a2dbe7c94 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Grapher_Options.h @@ -0,0 +1,21 @@ +// $Id$ + +#ifndef TEMPERATURE_GRAPHER_OPTIONS_H +#define TEMPERATURE_GRAPHER_OPTIONS_H + +class Temperature_Grapher_Options + { + public: + Temperature_Grapher_Options( int argc, ACE_TCHAR ** argv ) + { + ACE_UNUSED_ARG(argc); + ACE_UNUSED_ARG(argv); + } + + int poll_interval() + { + return 20; // every 20 seconds + } + }; + +#endif /* TEMPERATURE_GRAPHER_OPTIONS_H */ diff --git a/ACE/examples/APG/Naming/Temperature_Monitor.cpp b/ACE/examples/APG/Naming/Temperature_Monitor.cpp new file mode 100644 index 00000000000..8ac841b4108 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Monitor.cpp @@ -0,0 +1,133 @@ +// $Id$ + +#include "ace/OS_NS_time.h" +#include "ace/OS_NS_unistd.h" +#include "ace/Log_Msg.h" + +#include "Thermometer.h" +#include "Temperature_Monitor.h" +#include "EMail.h" + +// Listing 1 code/ch21 +Temperature_Monitor::Temperature_Monitor + (Temperature_Monitor_Options &opt, + Naming_Context &naming_context) + : opt_(opt), naming_context_(naming_context) +{ } +// Listing 1 + +// Listing 31 code/ch21 +void Temperature_Monitor::record_temperature (float temp) +{ + Name_Binding_Ptr current + (this->naming_context_.fetch ("current")); + if (current.get()) + { + this->naming_context_.rebind ("previous", + current->value ()); + } +// Listing 31 + +// Listing 32 code/ch21 + this->naming_context_.rebind ("current", temp); +// Listing 32 + +// Listing 33 code/ch21 + this->naming_context_.unbind ("lastReset"); + this->naming_context_.unbind ("resetCount"); +// Listing 33 +} + +// Listing 41 code/ch21 +void Temperature_Monitor::record_failure (void) +{ + Name_Binding_Ptr lastReset + (this->naming_context_.fetch ("lastReset")); + Name_Binding_Ptr resetCount + (this->naming_context_.fetch ("resetCount")); +// Listing 41 + +// Listing 42 code/ch21 + int now = ACE_OS::time (); + int lastResetTime; + if (lastReset.get ()) + { + lastResetTime = lastReset->int_value (); + } + else + { + this->naming_context_.rebind ("lastReset", now); + lastResetTime = now; + } + // Listing 42 + + // Listing 43 code/ch21 + if (now - lastResetTime > this->opt_.reset_interval ()) + { + this->reset_device (resetCount); + } + // Listing 43 +} + +// Listing 5 code/ch21 +void +Temperature_Monitor::reset_device (Name_Binding_Ptr &resetCount) +{ + int number_of_resets = 1; + if (resetCount.get ()) + { + number_of_resets = resetCount->int_value () + 1; + if (number_of_resets > this->opt_.excessive_resets ()) + { + // Exclude 5 + EMail notification; + + char message[BUFSIZ]; + ACE_OS::sprintf (message, + "Thermometer: %s\n" + "Reset Count: %d\n", + this->thermometer_->address(), + number_of_resets); + + notification.send (this->opt_.admin_email (), + this->opt_.email_from (), + "Excessive number of thermometer resets", + message); + // Exclude 5 + } + } + this->thermometer_->reset (); + this->naming_context_.rebind ("lastReset", + (int) ACE_OS::time ()); + this->naming_context_.rebind ("resetCount", + number_of_resets); +} +// Listing 5 + +// Listing 2 code/ch21 +void Temperature_Monitor::monitor (void) +{ + this->thermometer_ = + new Thermometer (this->opt_.thermometer_address ()); + + for(;;) + { + float temp = this->thermometer_->temperature (); + ACE_DEBUG ((LM_INFO, ACE_TEXT ("Read temperature %.2f\n"), + temp)); + + if (temp >= 0) + { + this->record_temperature (temp); + } + else + { + this->record_failure (); + } + + ACE_OS::sleep (this->opt_.poll_interval ()); + } + + delete this->thermometer_; +} +// Listing 2 diff --git a/ACE/examples/APG/Naming/Temperature_Monitor.h b/ACE/examples/APG/Naming/Temperature_Monitor.h new file mode 100644 index 00000000000..3b85b100fa1 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Monitor.h @@ -0,0 +1,29 @@ +// $Id$ + +#ifndef TEMPERATURE_MONITOR_H +#define TEMPERATURE_MONITOR_H + +#include "Thermometer.h" +#include "Temperature_Monitor_Options.h" +#include "Naming_Context.h" + +class Temperature_Monitor + { + public: + Temperature_Monitor( Temperature_Monitor_Options & opt, + Naming_Context & naming_context ); + + void monitor(); + + protected: + void record_temperature(float temp); + void record_failure(); + void reset_device(Name_Binding_Ptr & resetCount); + + private: + Thermometer * thermometer_; + Temperature_Monitor_Options & opt_; + Naming_Context & naming_context_; + }; + +#endif /* TEMPERATURE_MONITOR_H */ diff --git a/ACE/examples/APG/Naming/Temperature_Monitor2.cpp b/ACE/examples/APG/Naming/Temperature_Monitor2.cpp new file mode 100644 index 00000000000..b24f1c1f329 --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Monitor2.cpp @@ -0,0 +1,145 @@ +// $Id$ + +#include "ace/OS_NS_time.h" +#include "ace/OS_NS_unistd.h" +#include "ace/Log_Msg.h" + +#include "Thermometer.h" +#include "Temperature_Monitor2.h" +#include "EMail.h" + +// Listing 1 code/ch21 +void Temperature_Monitor2::record_temperature (float temp) +{ + Name_Binding_Ptr current + (this->naming_context_.fetch ("current")); + if (current.get ()) + { + this->naming_context_.rebind ("previous", + current->value ()); + } + + this->record_history (temp); + + this->naming_context_.unbind ("lastFailure"); + this->naming_context_.unbind ("lastReset"); + this->naming_context_.unbind ("resetCount"); +} +// Listing 1 + +// Listing 2 code/ch21 +void Temperature_Monitor2::record_history (float temp) +{ + int now = (int)ACE_OS::time (); + this->shared_context_.rebind ("lastUpdate", now); + + Name_Binding_Ptr counter + (this->shared_context_.fetch ("counter")); + int counterValue = counter.get () ? counter->int_value () : 0; + + char name[BUFSIZ]; + ACE_OS::sprintf (name, "history[%d]", counterValue); + + char value[BUFSIZ]; + ACE_OS::sprintf (value, "%d|%.2f", now, temp); + + this->shared_context_.rebind (name, value); + + ++counterValue; + counterValue %= this->opt_.history_size (); + this->shared_context_.rebind ("counter", counterValue); +} +// Listing 2 + +void Temperature_Monitor2::reset_device (Name_Binding_Ptr &resetCount) +{ + int number_of_resets = 1; + + if (resetCount.get ()) + { + number_of_resets = resetCount->int_value () + 1; + + if (number_of_resets > this->opt_.excessive_resets ()) + { + EMail notification; + + char message[BUFSIZ]; + ACE_OS::sprintf (message, + "Thermometer: %s\n" + "Reset Count: %d\n", + this->thermometer_->address (), + number_of_resets); + + notification.send (this->opt_.admin_email (), + this->opt_.email_from (), + "Excessive number of thermometer resets", + message); + } + } + + this->thermometer_->reset (); + + this->naming_context_.rebind ("lastReset", (int)ACE_OS::time ()); + this->naming_context_.rebind ("resetCount", number_of_resets); +} + +void Temperature_Monitor2::record_failure (void) +{ + Name_Binding_Ptr lastFailure (this->naming_context_.fetch ("lastFailure")); + Name_Binding_Ptr lastReset (this->naming_context_.fetch ("lastReset")); + Name_Binding_Ptr resetCount (this->naming_context_.fetch ("resetCount")); + + int now = ACE_OS::time (); + + int lastFailureTime; + int lastResetTime = 0; + + if (lastFailure.get ()) + { + lastFailureTime = lastFailure->int_value (); + } + else + { + this->naming_context_.rebind ("firstFailure", now); + this->naming_context_.rebind ("lastReset", now); + lastFailureTime = now; + lastResetTime = now; + } + + if (lastReset.get ()) + { + lastResetTime = lastReset->int_value (); + } + + if (now - lastResetTime > this->opt_.reset_interval ()) + { + this->reset_device (resetCount); + } + + this->naming_context_.rebind ("lastFailure", now); +} + +void Temperature_Monitor2::monitor (void) +{ + this->thermometer_ = new Thermometer (this->opt_.thermometer_address ()); + + for (;;) + { + float temp = this->thermometer_->temperature (); + + ACE_DEBUG ((LM_INFO, ACE_TEXT ("Read temperature %.2f\n"), temp)); + + if (temp >= 0) + { + this->record_temperature (temp); + } + else + { + this->record_failure (); + } + + ACE_OS::sleep (this->opt_.poll_interval ()); + } + + delete this->thermometer_; +} diff --git a/ACE/examples/APG/Naming/Temperature_Monitor2.h b/ACE/examples/APG/Naming/Temperature_Monitor2.h new file mode 100644 index 00000000000..b887e5c8e4c --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Monitor2.h @@ -0,0 +1,36 @@ +// $Id$ + +#ifndef TEMPERATURE_MONITOR_H +#define TEMPERATURE_MONITOR_H + +#include "Thermometer.h" +#include "Temperature_Monitor_Options.h" +#include "Naming_Context.h" + +class Temperature_Monitor2 +{ +public: + Temperature_Monitor2 (Temperature_Monitor_Options & opt, + Naming_Context & naming_context, + Naming_Context & shared_context) + : opt_(opt), + naming_context_(naming_context), + shared_context_(shared_context) + { } + + void monitor (void); + +protected: + void record_temperature (float temp); + void record_history (float temp); + void record_failure (void); + void reset_device (Name_Binding_Ptr & resetCount); + +private: + Thermometer *thermometer_; + Temperature_Monitor_Options &opt_; + Naming_Context &naming_context_; + Naming_Context &shared_context_; +}; + +#endif /* TEMPERATURE_MONITOR_H */ diff --git a/ACE/examples/APG/Naming/Temperature_Monitor_Options.h b/ACE/examples/APG/Naming/Temperature_Monitor_Options.h new file mode 100644 index 00000000000..95fb82faa9b --- /dev/null +++ b/ACE/examples/APG/Naming/Temperature_Monitor_Options.h @@ -0,0 +1,48 @@ +// $Id$ + +#ifndef TEMPERATURE_MONITOR_OPTIONS_H +#define TEMPERATURE_MONITOR_OPTIONS_H + +class Temperature_Monitor_Options + { + public: + Temperature_Monitor_Options (int, ACE_TCHAR *[]) + { } + + const char *thermometer_address (void) + { + return "serial:// s0/0x3e52"; + } + + int poll_interval (void) + { + return 10; // every 10 seconds + } + + int reset_interval (void) + { + return 60; // sixty seconds + } + + int excessive_resets (void) + { + return 5; // no response in 5 minutes + } + + const char *admin_email (void) + { + return "root@localhost"; + } + + const char *email_from (void) + { + return "temperature monitor"; + } + + int history_size() + { + return 10; + } + }; + +#endif /* TEMPERATURE_MONITOR_OPTIONS_H */ diff --git a/ACE/examples/APG/Naming/Thermometer.h b/ACE/examples/APG/Naming/Thermometer.h new file mode 100644 index 00000000000..cfdf1ca0f3d --- /dev/null +++ b/ACE/examples/APG/Naming/Thermometer.h @@ -0,0 +1,48 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef THERMOMETER_H +#define THERMOMETER_H + +#include "ace/OS_NS_stdlib.h" +#include "ace/Log_Msg.h" + +class Thermometer +{ +public: + Thermometer (const char *addr) + : addr_(addr), threshold_(5) + { } + + float temperature (void) + { + int success = ACE_OS::rand () % 10; + if (success < this->threshold_) + { + this->threshold_ = 7; + return -1.0; + } + + this->threshold_ = 3; + int itemp = 80 + ACE_OS::rand () % 10; // 80 <= t <= 90 + return (float)itemp; + } + + const char *address (void) + { + return this->addr_; + } + + void reset (void) + { + this->threshold_ = 4; + ACE_DEBUG ((LM_ERROR, ACE_TEXT ("Resetting thermometer %C\n"), + this->address ())); + } + +private: + const char *addr_; + int threshold_; +}; + +#endif /* THERMOMETER_H */ diff --git a/ACE/examples/APG/Naming/naming.mpc b/ACE/examples/APG/Naming/naming.mpc new file mode 100644 index 00000000000..fda823954fc --- /dev/null +++ b/ACE/examples/APG/Naming/naming.mpc @@ -0,0 +1,50 @@ +// -*- MPC -*- +// $Id$ + +project(Netlocal) : aceexe { + avoids += ace_for_tao + exename = Netlocal + Source_Files { + Netlocal.cpp + Temperature_Monitor2.cpp + } +} + +project(Netlocal Reader) : aceexe { + avoids += ace_for_tao + exename = Netlocal_reader + Source_Files { + Netlocal_reader.cpp + Graph.cpp + Temperature_Grapher.cpp + } +} + +project(Nodelocal) : aceexe { + avoids += ace_for_tao + exename = Nodelocal + Source_Files { + Nodelocal.cpp + Temperature_Monitor.cpp + } +} + +project(Nodelocal Shared) : aceexe { + avoids += ace_for_tao + exename = Nodelocal_shared + Source_Files { + Nodelocal_shared.cpp + Temperature_Monitor2.cpp + } +} + +project(Nodelocal Shared Reader) : aceexe { + avoids += ace_for_tao + exename = Nodelocal_shared_reader + Source_Files { + Nodelocal_shared_reader.cpp + Graph.cpp + Temperature_Grapher.cpp + Temperature_Monitor.cpp + } +} diff --git a/ACE/examples/APG/Naming/svc.conf b/ACE/examples/APG/Naming/svc.conf new file mode 100644 index 00000000000..550ef992177 --- /dev/null +++ b/ACE/examples/APG/Naming/svc.conf @@ -0,0 +1 @@ +dynamic Name_Server Service_Object * netsvcs:_make_ACE_Name_Acceptor() "-p 20012" |