summaryrefslogtreecommitdiff
path: root/examples/Registry/test_registry_update.cpp
diff options
context:
space:
mode:
authorirfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1996-10-29 06:56:17 +0000
committerirfan <irfan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1996-10-29 06:56:17 +0000
commit1d10be0da172c415a8cb0c641d79b6b4bf0a0eb6 (patch)
treefb602f0e222db85d18facffc981a535df1836a2c /examples/Registry/test_registry_update.cpp
parent32c6d18662a87c0a906b1b98547663a3e63687a2 (diff)
downloadATCD-1d10be0da172c415a8cb0c641d79b6b4bf0a0eb6.tar.gz
test programs for ACE Registry
Diffstat (limited to 'examples/Registry/test_registry_update.cpp')
-rw-r--r--examples/Registry/test_registry_update.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/examples/Registry/test_registry_update.cpp b/examples/Registry/test_registry_update.cpp
new file mode 100644
index 00000000000..7c3612f2daa
--- /dev/null
+++ b/examples/Registry/test_registry_update.cpp
@@ -0,0 +1,145 @@
+// Suppose this application belonged to AcmeSoft. AcmeSoft wants to
+// keep track of the number of times this application is
+// executed. They want two counters: one counts the number of
+// executions per machine, the other keeps track of the number of
+// executions per user.
+//
+// This application uses the ACE_Registry class to create and update
+// entries in the LOCAL_MACHINE and CURRENT_USER predefined registries
+// to store the counters.
+//
+// Note that this application will not work with remote registries
+// if used with the CURRENT_USER predefined registry.
+
+#include "ace/Registry.h"
+#include "ace/Log_Msg.h"
+
+// Name for application's naming context
+static ACE_Registry::Name application_context_name;
+
+// Name for instance counter.
+static ACE_Registry::Name counter_name;
+
+// Protypes
+static int update_counter (HKEY predefined,
+ u_long &current_counter);
+static void setup_names ();
+
+int
+main(int argc, char *argv[])
+{
+ int result;
+ u_long current_counter = 0;
+
+ // initialize name
+ setup_names ();
+
+ // Update counter per user
+ result = ::update_counter (HKEY_CURRENT_USER,
+ current_counter);
+ if (result == 0)
+ {
+ cout << "User counter: " << current_counter << endl;
+
+ // Update counter per machine
+ result = ::update_counter (HKEY_LOCAL_MACHINE,
+ current_counter);
+ if (result == 0)
+ cout << "Machine counter: " << current_counter << endl;
+ }
+
+ if (result != 0)
+ ACE_DEBUG ((LM_DEBUG, "test failed\n"));
+ else
+ ACE_DEBUG ((LM_DEBUG, "test succeeded\n"));
+
+ return 0;
+}
+
+static int
+update_counter (HKEY predefined,
+ u_long &current_counter)
+{
+ int result;
+ ACE_Registry::Naming_Context parent_context;
+ ACE_Registry::Naming_Context application_context;
+
+ // Connect to predefined entry
+ result = ACE_Predefined_Naming_Contexts::connect (parent_context,
+ predefined);
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
+
+ // Find application context name
+ result = parent_context.resolve_context (application_context_name,
+ application_context);
+
+ if (result != 0)
+ // Failed to find: create a new context
+ result = parent_context.bind_new_context (application_context_name,
+ application_context);
+
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve_context failed"), -1);
+
+ // Counter
+ u_long counter = 0;
+ // Represent counter as an ACE_Registry::Object
+ ACE_Registry::Object object ((void *) &counter,
+ sizeof counter,
+ REG_DWORD);
+ // Find counter
+ result = application_context.resolve (counter_name,
+ object);
+
+ if (result != 0)
+ // Failed to find: create new binding for object
+ {
+ counter = 1;
+ result = application_context.bind (counter_name,
+ object);
+ }
+ else
+ // Counter was found
+ {
+ // increment counter
+ counter++;
+ // Update
+ result = application_context.rebind (counter_name,
+ object);
+ }
+
+ if (result != 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve failed"), -1);
+ else
+ current_counter = counter;
+
+ return 0;
+}
+
+
+static void
+setup_names ()
+{
+ // Stupid implementation of STL is broken
+ /*
+ ::application_context_name.reserve (4);
+ ::application_context_name [0].id_ = "Software";
+ ::application_context_name [1].id_ = "AcmeSoft";
+ ::application_context_name [2].id_ = "AcmeApplication";
+ ::application_context_name [3].id_ = "1.0";
+
+ ::counter_name.reserve (1);
+ ::counter_name [0].id_ = "Instance Counter";
+ */
+
+ ACE_Registry::Name_Component component;
+
+ component.id_ = "Software", ::application_context_name.push_back (component);
+ component.id_ = "AcmeSoft", ::application_context_name.push_back (component);
+ component.id_ = "AcmeApplication", ::application_context_name.push_back (component);
+ component.id_ = "1.0", ::application_context_name.push_back (component);
+
+ component.id_ = "Instance Counter", ::counter_name.push_back (component);
+}
+