diff options
author | Steve Huston <shuston@riverace.com> | 2004-01-01 21:01:01 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 2004-01-01 21:01:01 +0000 |
commit | 6649a1a542c18bd756757a2d58f3ea51eea0cc47 (patch) | |
tree | 15b88279a4187c88c37adabba8740a0165139299 /examples/APG/Naming | |
parent | fcf24b2de58a080db0617c641f337298614a944b (diff) | |
download | ATCD-6649a1a542c18bd756757a2d58f3ea51eea0cc47.tar.gz |
ChangeLogTag:Thu Jan 1 15:20:30 2004 Steve Huston <shuston@riverace.com>
Diffstat (limited to 'examples/APG/Naming')
30 files changed, 1172 insertions, 0 deletions
diff --git a/examples/APG/Naming/EMail.h b/examples/APG/Naming/EMail.h new file mode 100644 index 00000000000..fc38913a10a --- /dev/null +++ b/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/examples/APG/Naming/Graph.cpp b/examples/APG/Naming/Graph.cpp new file mode 100644 index 00000000000..cff8e37244e --- /dev/null +++ b/examples/APG/Naming/Graph.cpp @@ -0,0 +1,48 @@ +// $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 %s\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/examples/APG/Naming/Graph.h b/examples/APG/Naming/Graph.h new file mode 100644 index 00000000000..135b7d14f77 --- /dev/null +++ b/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/examples/APG/Naming/Graphable_Element.cpp b/examples/APG/Naming/Graphable_Element.cpp new file mode 100644 index 00000000000..6b5d6c755e6 --- /dev/null +++ b/examples/APG/Naming/Graphable_Element.cpp @@ -0,0 +1,13 @@ +// $Id$ + +#include "Graphable_Element.h" + +#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) +template class _List_base<Graphable_Element, allocator<Graphable_Element> >; +template void list<Graphable_Element, allocator<Graphable_Element> >::sort(void); +template void list<Graphable_Element, allocator<Graphable_Element> >::merge(list<Graphable_Element, allocator<Graphable_Element> > &); +#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) +pragma instantiate _List_base<Graphable_Element, allocator<Graphable_Element> >; +pragma instantiate list<Graphable_Element, allocator<Graphable_Element> >::sort(void); +pragma instantiate list<Graphable_Element, allocator<Graphable_Element> >::merge(list<Graphable_Element, allocator<Graphable_Element> > &); +#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */ diff --git a/examples/APG/Naming/Graphable_Element.h b/examples/APG/Naming/Graphable_Element.h new file mode 100644 index 00000000000..cecc691f6f8 --- /dev/null +++ b/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) + { + return this->when_; + } + + inline float temp (void) + { + return this->temp_; + } + // Listing 2 + + // Listing 3 code/ch21 + inline bool operator< (Graphable_Element &other) + { + 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/examples/APG/Naming/Makefile b/examples/APG/Naming/Makefile new file mode 100644 index 00000000000..d3316532b97 --- /dev/null +++ b/examples/APG/Naming/Makefile @@ -0,0 +1,39 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +MAKEFILE=Makefile + +MKLIST = Makefile.netlocal \ + Makefile.netlocal_reader \ + Makefile.nodelocal \ + Makefile.nodelocal_shared \ + Makefile.nodelocal_shared_reader + +## Ensure that Makefiles in MKLIST are executed in sequence during a +## parallel build because we share some files between multiple libraries. +## Also, the AIX tempinc directory will get confused. +.NOTPARALLEL: + +## Makefile.*.mkfile is a dummy target which will cause +## $(MAKE) -f Makefile.* to be invoked, then it cleans +## up tempinc when needed for AIX Visual Age C++. +%.mkfile: % + @echo $(MAKE) -f $< $(MKFILE_TARGET) + @$(MAKE) -f $< $(MKFILE_TARGET) + -@$(RM) -rf tempinc + +# This rule invokes make again with the list of .mkfile targets as a +# parameter. For example, if the all target is being made, make is invoked +# as follows: +# +# make -f Makefile MKFILE_TARGET=all Makefile.singles + +all clean depend realclean: +ifneq ($(MKLIST),) + @echo $(MAKE) -f $(MAKEFILE) MKFILE_TARGET=$@ $(addsuffix .mkfile, $(MKLIST)) + @$(MAKE) -f $(MAKEFILE) MKFILE_TARGET=$@ $(addsuffix .mkfile, $(MKLIST)) +endif + +# DO NOT DELETE THIS LINE -- g++dep uses it. +# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. diff --git a/examples/APG/Naming/Makefile.netlocal b/examples/APG/Naming/Makefile.netlocal new file mode 100644 index 00000000000..f296f08301d --- /dev/null +++ b/examples/APG/Naming/Makefile.netlocal @@ -0,0 +1,14 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +BIN = Netlocal + +SRC = Netlocal.cpp Temperature_Monitor2.cpp + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU diff --git a/examples/APG/Naming/Makefile.netlocal_reader b/examples/APG/Naming/Makefile.netlocal_reader new file mode 100644 index 00000000000..1baecddb132 --- /dev/null +++ b/examples/APG/Naming/Makefile.netlocal_reader @@ -0,0 +1,14 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +BIN = Netlocal_reader + +SRC = Netlocal_reader.cpp Graph.cpp Temperature_Grapher.cpp + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU diff --git a/examples/APG/Naming/Makefile.nodelocal b/examples/APG/Naming/Makefile.nodelocal new file mode 100644 index 00000000000..1bf769dab9c --- /dev/null +++ b/examples/APG/Naming/Makefile.nodelocal @@ -0,0 +1,14 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +BIN = Nodelocal + +SRC = Nodelocal.cpp Temperature_Monitor.cpp + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU diff --git a/examples/APG/Naming/Makefile.nodelocal_shared b/examples/APG/Naming/Makefile.nodelocal_shared new file mode 100644 index 00000000000..719f7344755 --- /dev/null +++ b/examples/APG/Naming/Makefile.nodelocal_shared @@ -0,0 +1,14 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +BIN = Nodelocal_shared + +SRC = Nodelocal_shared.cpp Temperature_Monitor2.cpp + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU diff --git a/examples/APG/Naming/Makefile.nodelocal_shared_reader b/examples/APG/Naming/Makefile.nodelocal_shared_reader new file mode 100644 index 00000000000..2629d6ddd9f --- /dev/null +++ b/examples/APG/Naming/Makefile.nodelocal_shared_reader @@ -0,0 +1,17 @@ +# $Id$ +# +# Copyright 2003 Addison-Wesley Inc. All Rights Reserved. + +BIN = Nodelocal_shared_reader + +SRC = Nodelocal_shared_reader.cpp \ + Graph.cpp \ + Temperature_Grapher.cpp \ + Temperature_Monitor.cpp + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU diff --git a/examples/APG/Naming/Name_Binding.h b/examples/APG/Naming/Name_Binding.h new file mode 100644 index 00000000000..e170803dffa --- /dev/null +++ b/examples/APG/Naming/Name_Binding.h @@ -0,0 +1,58 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef NAME_BINDING_H +#define NAME_BINDING_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_; + delete this->type_; + } + + 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_; + const char *type_; +}; + +typedef auto_ptr<Name_Binding> Name_Binding_Ptr; +// Listing 1 + +#endif /* NAME_BINDING_H */ diff --git a/examples/APG/Naming/Naming_Context.h b/examples/APG/Naming/Naming_Context.h new file mode 100644 index 00000000000..7c2b245cba4 --- /dev/null +++ b/examples/APG/Naming/Naming_Context.h @@ -0,0 +1,67 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef NAMING_CONTEXT_H +#define NAMING_CONTEXT_H + +#include "ace/Naming_Context.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/examples/APG/Naming/Netlocal.cpp b/examples/APG/Naming/Netlocal.cpp new file mode 100644 index 00000000000..f1799ab3882 --- /dev/null +++ b/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/examples/APG/Naming/Netlocal_reader.cpp b/examples/APG/Naming/Netlocal_reader.cpp new file mode 100644 index 00000000000..f77724aabbe --- /dev/null +++ b/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/examples/APG/Naming/Nodelocal.cpp b/examples/APG/Naming/Nodelocal.cpp new file mode 100644 index 00000000000..c8a2d862634 --- /dev/null +++ b/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 + char *naming_options_argv[] = { argv[0] }; + name_options->parse_args + (sizeof(naming_options_argv) / sizeof(char*), + 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/examples/APG/Naming/Nodelocal_shared.cpp b/examples/APG/Naming/Nodelocal_shared.cpp new file mode 100644 index 00000000000..34ea87aab2d --- /dev/null +++ b/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/examples/APG/Naming/Nodelocal_shared_reader.cpp b/examples/APG/Naming/Nodelocal_shared_reader.cpp new file mode 100644 index 00000000000..7385f09a496 --- /dev/null +++ b/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/examples/APG/Naming/Temperature_Grapher.cpp b/examples/APG/Naming/Temperature_Grapher.cpp new file mode 100644 index 00000000000..6d47e1f8bf6 --- /dev/null +++ b/examples/APG/Naming/Temperature_Grapher.cpp @@ -0,0 +1,80 @@ +// $Id$ + +#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/examples/APG/Naming/Temperature_Grapher.h b/examples/APG/Naming/Temperature_Grapher.h new file mode 100644 index 00000000000..610dac6a578 --- /dev/null +++ b/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/examples/APG/Naming/Temperature_Grapher_Options.h b/examples/APG/Naming/Temperature_Grapher_Options.h new file mode 100644 index 00000000000..9e36a0551fd --- /dev/null +++ b/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, char ** 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/examples/APG/Naming/Temperature_Monitor.cpp b/examples/APG/Naming/Temperature_Monitor.cpp new file mode 100644 index 00000000000..561f045a553 --- /dev/null +++ b/examples/APG/Naming/Temperature_Monitor.cpp @@ -0,0 +1,131 @@ +// $Id$ + +#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/examples/APG/Naming/Temperature_Monitor.h b/examples/APG/Naming/Temperature_Monitor.h new file mode 100644 index 00000000000..3b85b100fa1 --- /dev/null +++ b/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/examples/APG/Naming/Temperature_Monitor2.cpp b/examples/APG/Naming/Temperature_Monitor2.cpp new file mode 100644 index 00000000000..bb84b8c4fc3 --- /dev/null +++ b/examples/APG/Naming/Temperature_Monitor2.cpp @@ -0,0 +1,142 @@ +// $Id$ + +#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() +{ + 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; + + 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() +{ + 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/examples/APG/Naming/Temperature_Monitor2.h b/examples/APG/Naming/Temperature_Monitor2.h new file mode 100644 index 00000000000..f09623f15a7 --- /dev/null +++ b/examples/APG/Naming/Temperature_Monitor2.h @@ -0,0 +1,35 @@ +// $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(); + + protected: + void record_temperature(float temp); + void record_history(float temp); + void record_failure(); + 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/examples/APG/Naming/Temperature_Monitor_Options.h b/examples/APG/Naming/Temperature_Monitor_Options.h new file mode 100644 index 00000000000..2dc6469788f --- /dev/null +++ b/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, char *[]) + { } + + 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/examples/APG/Naming/Thermometer.h b/examples/APG/Naming/Thermometer.h new file mode 100644 index 00000000000..609167874e7 --- /dev/null +++ b/examples/APG/Naming/Thermometer.h @@ -0,0 +1,47 @@ +/* -*- C++ -*- */ +// $Id$ + +#ifndef THERMOMETER_H +#define THERMOMETER_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/examples/APG/Naming/naming.mpc b/examples/APG/Naming/naming.mpc new file mode 100644 index 00000000000..1a86f720979 --- /dev/null +++ b/examples/APG/Naming/naming.mpc @@ -0,0 +1,42 @@ +project(Netlocal) : aceexe { + exename = Netlocal + Source_Files { + Netlocal.cpp + Temperature_Monitor2.cpp + } +} + +project(Netlocal Reader) : aceexe { + exename = Netlocal_reader + Source_Files { + Netlocal_reader.cpp + Graph.cpp + Temperature_Grapher.cpp + } +} + +project(Nodelocal) : aceexe { + exename = Netlocal + Source_Files { + Nodelocal.cpp + Temperature_Monitor.cpp + } +} + +project(Nodelocal Shared) : aceexe { + exename = Nodelocal_shared + Source_Files { + Nodelocal_shared.cpp + Temperature_Monitor2.cpp + } +} + +project(Nodelocal Shared Reader) : aceexe { + exename = Nodelocal_shared_reader + Source_Files { + Nodelocal_shared_reader.cpp + Graph.cpp + Temperature_Grapher.cpp + Temperature_Monitor.cpp + } +} diff --git a/examples/APG/Naming/naming.mwc b/examples/APG/Naming/naming.mwc new file mode 100644 index 00000000000..cdef75bfed2 --- /dev/null +++ b/examples/APG/Naming/naming.mwc @@ -0,0 +1,3 @@ +workspace { + naming.mpc +} diff --git a/examples/APG/Naming/svc.conf b/examples/APG/Naming/svc.conf new file mode 100644 index 00000000000..550ef992177 --- /dev/null +++ b/examples/APG/Naming/svc.conf @@ -0,0 +1 @@ +dynamic Name_Server Service_Object * netsvcs:_make_ACE_Name_Acceptor() "-p 20012" |