gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated (f4aa746 -> 1586ccd)


From: gnunet
Subject: [taler-anastasis] branch master updated (f4aa746 -> 1586ccd)
Date: Fri, 09 Oct 2020 12:03:41 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a change to branch master
in repository anastasis.

    from f4aa746  force
     new 5433de1  fixes
     new ba4c24b  fixes
     new 525c82e  fix
     new eb4d305  fix
     new 644a9f0  fix changing continent
     new bed5c82  fix
     new 5afb68f  enumerate states
     new 1586ccd  rework add_authentication

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 contrib/Makefile.am                      |   1 +
 contrib/{redux.de.json => redux.us.json} |  15 +-
 src/include/anastasis_redux.h            |  72 ++++--
 src/lib/Makefile.am                      |   2 +
 src/lib/anastasis_api_backup_redux.c     | 239 ++++++++----------
 src/lib/anastasis_api_recovery_redux.c   |  38 +--
 src/lib/anastasis_api_redux.c            | 420 +++++++++++++++++--------------
 7 files changed, 423 insertions(+), 364 deletions(-)
 copy contrib/{redux.de.json => redux.us.json} (68%)

diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index ce6ed23..c82cb24 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -12,5 +12,6 @@ EXTRA_DIST = \
 pkgdata_DATA = \
   redux.ch.json \
   redux.de.json \
+  redux.us.json \
   redux.countries.json \
   provider-list.json
diff --git a/contrib/redux.de.json b/contrib/redux.us.json
similarity index 68%
copy from contrib/redux.de.json
copy to contrib/redux.us.json
index 7acf0ac..00f4afb 100644
--- a/contrib/redux.de.json
+++ b/contrib/redux.us.json
@@ -4,11 +4,9 @@
            "type": "string",
            "name": "full_name",
                "label": "Full name",
-           "label_i18n":{
-                                               "de_DE": "Vollstaendiger Name",
-                                               "de_CH": "Vollstaendiger Name",
-                                               "fr": "Nom complet",
-                                               "en": "Full name"
+               "label_i18n":{
+                                               "de_DE":"Vollstaendiger Name",
+                                               "de_CH":"Vollstaendiger Name"
                                         },
            "widget": "anastasis_gtk_ia_full_name"
        },
@@ -16,12 +14,7 @@
            "type": "date",
            "name": "birthdate",
                "label": "Birthdate",
-           "label_i18n":{
-                                               "de_DE":"Geburtsdatum",
-                                               "de_CH":"Geburtsdatum",
-                                               "fr":"Date de naissance",
-                                               "en":"Birthdate"
-                                       },
+           "label_i18n":{"de_CH":"Geburtsdatum"},
            "widget": "anastasis_gtk_ia_birthdate"
        },
        {
diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index 4adc474..7da95ae 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -27,21 +27,50 @@
 #include <gnunet/gnunet_util_lib.h>
 #include "anastasis_error_codes.h"
 
+#define FOREACH_STATE(REDUX_STATE) \
+  REDUX_STATE (ContinentSelectionState)   \
+  REDUX_STATE (CountrySelectionState)  \
+  REDUX_STATE (UserAttributesCollectionState)   \
+  REDUX_STATE (AuthenticationsEditingState)  \
+  REDUX_STATE (PoliciesReviewingState)   \
+  REDUX_STATE (BackupPayingState)  \
+  REDUX_STATE (NoState) \
+
+#define GENERATE_ENUM(ENUM) ENUM,
+#define GENERATE_STRING(STRING) #STRING,
+
+typedef enum
+{
+  FOREACH_STATE (GENERATE_ENUM)
+} REDUX_STATE;
+
+static const char *STATE_STRING[] = {
+  FOREACH_STATE (GENERATE_STRING)
+};
 
 /**
- * JSON containing all supported countries and additional data.
+ * JSON containing country specific identity attributes to ask the user for.
  */
-extern json_t *redux_countries;
+extern const json_t *redux_id_attr;
 
 /**
- * JSON containing country specific identity attributes to ask the user for.
+ * JSON containing anastasis providers.
  */
-extern json_t *redux_id_attr;
+extern const json_t *provider_list;
 
 /**
- * JSON containing anastasis providers.
+ * Currency of chosen country
  */
-extern json_t *provider_list;
+extern const char *currency;
+
+
+/**
+ * Returns the integer value to a string value of a state.
+ *
+ * @param state_string
+ * @return int
+ */
+int get_enum_from_string (const char *state_string);
 
 
 /**
@@ -73,11 +102,11 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg);
 
 /**
  * Function to load json containing all countries.
- * Returns #GNUNET_OK if operation succeeded, else #GNUNET_SYSERR.
+ * Returns the countries.
  *
- * @return int
+ * @return json_t *
  */
-int
+json_t *
 redux_countries_init ();
 
 
@@ -103,7 +132,7 @@ typedef void
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param state input state
+ * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
@@ -111,7 +140,7 @@ typedef void
  * @return failure state or new state
  */
 void
-ANASTASIS_backup_action (const json_t *state,
+ANASTASIS_backup_action (json_t *state,
                          const char *action,
                          const json_t *arguments,
                          ANASTASIS_ActionCallback cb,
@@ -124,14 +153,14 @@ ANASTASIS_backup_action (const json_t *state,
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param state input state
+ * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
  * @param cb_cls closure for @a cb
  */
 void
-ANASTASIS_recovery_action (const json_t *state,
+ANASTASIS_recovery_action (json_t *state,
                            const char *action,
                            const json_t *arguments,
                            ANASTASIS_ActionCallback cb,
@@ -146,7 +175,7 @@ ANASTASIS_recovery_action (const json_t *state,
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param state input state
+ * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
@@ -161,4 +190,19 @@ ANASTASIS_redux_action (const json_t *state,
                         void *cb_cls);
 
 
+/**
+ * Function which is called when we need to go a state backwards.
+ * Sets state to previous state.
+ *
+ * @param[in,out] state input/output state (to be modified)
+ * @param arguments normally unused
+ * @param cb callback
+ * @param cb_cls closure
+ */
+void
+state_back (json_t *state,
+            const json_t *arguments,
+            ANASTASIS_ActionCallback cb,
+            void *cb_cls);
+
 #endif  /* _ANASTASIS_REDUX_H */
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index ae4c20c..04b092b 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -108,6 +108,8 @@ libanastasisredux_la_LIBADD = \
   -lgnunetjson \
   -lgnunetcurl \
   -lgnunetutil \
+  -ltalerutil \
+  -ltalerjson \
   -ljansson \
   $(XLIB)
 
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index e83e0d7..d6b1369 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -23,6 +23,8 @@
 
 #include <platform.h>
 #include <jansson.h>
+#include <taler/taler_json_lib.h>
+#include <taler/taler_util.h>
 #include "anastasis_redux.h"
 #include "anastasis_error_codes.h"
 
@@ -35,12 +37,23 @@
  *  @param cb_cls FIXME: Description
  */
 typedef void
-(*DispatchHandler)(const json_t *state,
+(*DispatchHandler)(json_t *state,
                    const json_t *arguments,
                    ANASTASIS_ActionCallback cb,
                    void *cb_cls);
 
 
+static void
+set_state (json_t *state,
+           const char *new_backup_state)
+{
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      "backup_state",
+                                      json_string (new_backup_state)));
+}
+
+
 /**
  * Returns an initial ANASTASIS backup state.
  *
@@ -52,15 +65,13 @@ ANASTASIS_backup_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
   json_t *initial_state;
   json_t *temp_country;
   size_t index;
-  json_t *continents = json_array ();
   json_t *root;
+  json_t *continents = json_array ();
+  json_t *redux_countries = redux_countries_init ();
 
-  if (GNUNET_SYSERR == redux_countries_init ())
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
+  GNUNET_assert (NULL != redux_countries);
   root = json_object_get (redux_countries, "countries");
+  GNUNET_assert (NULL != root);
 
   json_array_foreach (root, index, temp_country)
   {
@@ -83,15 +94,16 @@ ANASTASIS_backup_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
   }
 
   initial_state = json_pack ("{s:s, s:o}",
-                             "backup-state", "ReduxInitialState",
+                             "backup_state",
+                             STATE_STRING[ContinentSelectionState],
                              "continents", continents);
   GNUNET_assert (NULL != initial_state);
   return initial_state;
 }
 
-
+/** FIXME: Implement just one "back" action
 static void
-edit_user_attributes (const json_t *state,
+edit_user_attributes (json_t *state,
                       const json_t *arguments,
                       ANASTASIS_ActionCallback cb,
                       void *cb_cls)
@@ -132,98 +144,29 @@ edit_user_attributes (const json_t *state,
       new_state);
   json_decref (new_state);
 }
+*/
 
 
 static void
-add_authentication (const json_t *state,
+add_authentication (json_t *state,
                     const json_t *arguments,
                     ANASTASIS_ActionCallback cb,
                     void *cb_cls)
 {
-  json_t *new_state;
-  json_t *methods = json_object_get (arguments, "authentication_methods");
-
-  GNUNET_assert (NULL != methods);
-  new_state = json_deep_copy (state);
-  GNUNET_assert (NULL != new_state);
-  json_object_set (new_state,
-                   "authentication_methods",
-                   methods);
-
-  {
-    /** FIXME: RUBBISH :)
-    // initialize policy
-    size_t index_m; // index methods array
-    size_t index_up; // index used_providers array
-    json_t *method;
-    json_t *used_providers = json_array ();
-    json_t *policy = json_array ();
-
-    // simply distribute the methods
-    json_array_foreach (methods, index_m, method) // FIXME: add individual, 
remove loop!
-    {
-      size_t index_p; // index providers array
-      json_t *provider;
-      json_t *used_provider;
-      json_t *policy_element = json_object ();
-      json_t *provider_arr = json_object_get (method, "providers");
-
-      json_array_foreach (provider_arr, index_p, provider)
-      {
-        json_t *method_cpy = json_deep_copy (method);
-
-        json_object_del (method_cpy, "providers");
-        json_object_set_new (policy_element, "method", method_cpy);
-
-        bool used = false;
-        json_array_foreach (used_providers, index_up, used_provider)
-        {
-          // first, check if provider already is used for a method
-          if (json_equal (provider, used_provider))
-          {
-            used = true;
-            break;
-          }
-        }
-        if (! used)
-        {
-          // if provider is not used for a method, choose it
-          json_array_append (used_providers, provider);
-          json_object_set_new (policy_element, "provider", provider);
-          break;
-        }
-        // if all providers are used once, begin with the first again
-        if (json_array_size (provider_arr) == index_p - 1)
-        {
-          json_array_clear (used_providers);
-          provider = json_array_get (provider_arr, 0);
-          json_array_append (used_providers, provider);
-          json_object_set_new (policy_element, "provider", provider);
-        }
-      }
-      json_array_append_new (policy, policy_element);
-    }
-    json_object_set_new (new_state,
-                         "initial_policy",
-                         policy);
-    */}
+  json_t *method = json_object_get (arguments, "authentication_method");
+  GNUNET_assert (NULL != method);
+  json_t *auth_method_arr = json_object_get (state, "authentication_methods");
+  if (NULL == auth_method_arr)
+    auth_method_arr = json_array ();
+  GNUNET_assert (NULL != auth_method_arr);
+  GNUNET_assert (0 == json_array_append_new (auth_method_arr, method));
+  GNUNET_assert (0 == json_object_set (state,
+                                       "authentication_methods",
+                                       auth_method_arr));
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (methods);
-  json_decref (new_state);
-}
-
-
-static void
-set_state (json_t *state,
-           const char *new_backup_state)
-{
-  GNUNET_assert (0 ==
-                 json_object_set_new (state,
-                                      "backup-state",
-                                      json_string (new_backup_state)));
+      state);
 }
 
 
@@ -260,7 +203,7 @@ eval_provider_selection (struct PolicyBuilder *pb,
             prov_sel,
             sizeof (unsigned int) * pb->req_methods);
     pb->best_diversity = curr_diversity;
-    ob->best_cost = curr_cost;
+    pb->best_cost = curr_cost;
   }
 }
 
@@ -280,7 +223,7 @@ provider_candidate (struct PolicyBuilder *pb,
                                                     "method"));
   method_providers = json_object_get (pb->providers,
                                       method_name);
-  num_prov = json_array_size (method_providers);
+  int num_prov = json_array_size (method_providers);
   for (unsigned int j = 0; j<num_prov; j++)
   {
     prov_sel[i] = j;
@@ -301,27 +244,57 @@ static void
 go_with (struct PolicyBuilder *pb,
          const unsigned int *m_idx)
 {
+  struct TALER_Amount recovery_cost;
   unsigned int best_sel[pb->req_methods];
   unsigned int prov_sel[pb->req_methods];
 
   pb->best_diversity = 0;
   pb->best_sel = best_sel;
   pb->m_idx = m_idx;
-  provider_candidate (&pb,
+  provider_candidate (pb,
                       prov_sel,
                       0);
 
-  for (unsigned int i = 0; i<pb->req_methods)
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_amount_get_zero (currency,
+                                        &recovery_cost));
+  json_t *method_arr = json_array ();
+  for (unsigned int i = 0; i<pb->req_methods; i++)
   {
+    json_t *method_obj = json_array_get (pb->methods,
+                                         m_idx[i]);
+    const char *method_name = json_string_value (json_object_get (method_obj,
+                                                                  "method"));
+    json_t *method_providers = json_object_get (pb->providers,
+                                                method_name);
     json_t *provider_obj = json_array_get (method_providers,
                                            best_sel[i]);
     const char *provider_name = json_string_value (json_object_get (
                                                      provider_obj,
                                                      "provider"));
-    json_object_pack ("{s:i, s:s}",
-                      "authentication_method", m_idx[i],
-                      "provider", provider_name);
+    json_t *policy_method = json_pack ("{s:i, s:s}",
+                                       "authentication_method", m_idx[i],
+                                       "provider", provider_name);
+    json_array_append_new (method_arr, policy_method);
+    struct TALER_Amount method_cost;
+    GNUNET_assert (GNUNET_OK ==
+                   TALER_string_to_amount (
+                     json_string_value (json_object_get (provider_obj,
+                                                         "method_cost")),
+                     &method_cost));
+    if (0 >
+        TALER_amount_add (&recovery_cost,
+                          &recovery_cost,
+                          &method_cost))
+    {
+      GNUNET_break (0);
+      return;
+    }
   }
+  json_t *policy = json_pack ("{s:i, s:i}",
+                              "recovery_cost",
+                              TALER_JSON_from_amount (&recovery_cost),
+                              "methods", method_arr);
 }
 
 
@@ -334,7 +307,7 @@ method_candidate (struct PolicyBuilder *pb,
 
   if (i > 0)
     start = m_idx[i - 1];
-  for (unsigned int j = start; j<pb->num_methods; j++)
+  for (unsigned int j = start; j < pb->num_methods; j++)
   {
     m_idx[i] = j;
     if (i == pb->req_methods)
@@ -354,7 +327,7 @@ method_candidate (struct PolicyBuilder *pb,
 
 
 static void
-done_authentication (const json_t *state,
+done_authentication (json_t *state,
                      const json_t *arguments,
                      ANASTASIS_ActionCallback cb,
                      void *cb_cls)
@@ -365,8 +338,8 @@ done_authentication (const json_t *state,
                                   "authentication_providers");
   pb.methods = json_object_get (state,
                                 "authentication_methods");
-  pb.policies = json_array_new ();
-  pb.num_methods = json_array_size (methods);
+  pb.policies = json_array ();
+  pb.num_methods = json_array_size (pb.methods);
   switch (pb.num_methods)
   {
   case 0:
@@ -374,7 +347,7 @@ done_authentication (const json_t *state,
     break;
   case 1:
   case 2:
-    pb.req_methods = num_methods;
+    pb.req_methods = pb.num_methods;
     break;
   case 3:
   case 4:
@@ -385,7 +358,7 @@ done_authentication (const json_t *state,
     break;
   }
   {
-    unsigned int m_idx[req_methods];
+    unsigned int m_idx[pb.req_methods];
 
     method_candidate (&pb,
                       m_idx,
@@ -396,13 +369,11 @@ done_authentication (const json_t *state,
 
 
   }
-
-
   json_object_set_new (state,
                        "policies",
-                       policies);
+                       pb.policies);
   set_state (state,
-             "ReduxAuthenticationAddedState");
+             STATE_STRING[PoliciesReviewingState]);
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -410,7 +381,17 @@ done_authentication (const json_t *state,
 
 
 static void
-add_policy (const json_t *state,
+del_authentication (json_t *state,
+                    const json_t *arguments,
+                    ANASTASIS_ActionCallback cb,
+                    void *cb_cls)
+{
+
+}
+
+
+static void
+add_policy (json_t *state,
             const json_t *arguments,
             ANASTASIS_ActionCallback cb,
             void *cb_cls)
@@ -441,46 +422,29 @@ ANASTASIS_backup_action (json_t *state,
 {
   struct Dispatcher
   {
-    const char *backup_state;
+    REDUX_STATE backup_state;
     const char *backup_action;
     DispatchHandler fun;
   } dispatchers[] = {
     {
-      "ReduxUserAttributesAddedState",
-      "adding_authentication",
+      AuthenticationsEditingState,
+      "add_authentication",
       &add_authentication
     },
-    #if 0
     {
-      "ReduxUserAttributesAddedState",
-      "deleting_authentication",
+      AuthenticationsEditingState,
+      "delete_authentication",
       &del_authentication
     },
-    #endif
     {
-      "ReduxUserAttributesAddedState",
-      "next", // done_authentication",
+      AuthenticationsEditingState,
+      "next",
       &done_authentication
     },
-    {
-      "ReduxUserAttributesAddedState",
-      "editing_user_attributes",
-      &edit_user_attributes
-    },
-    {
-      "ReduxAuthenticationAddedState",
-      "adding_policy",
-      &add_policy
-    },
-    {
-      "ReduxAuthenticationAddedState",
-      "back", // editing_user_attributes",
-      &edit_user_attributes
-    },
-    { NULL, NULL, NULL }
+    { NoState, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
-                                                      "backup-state"));
+                                                      "backup_state"));
   if (NULL == s)
   {
     GNUNET_break (0);
@@ -490,7 +454,7 @@ ANASTASIS_backup_action (json_t *state,
   }
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s, dispatchers[i].backup_state)) &&
+    if ( (0 == strcmp (s, STATE_STRING[dispatchers[i].backup_state])) &&
          (0 == strcmp (action, dispatchers[i].backup_action)) )
     {
       dispatchers[i].fun (state, arguments, cb, cb_cls);
@@ -501,5 +465,4 @@ ANASTASIS_backup_action (json_t *state,
   cb (cb_cls,
       ANASTASIS_EC_INVALID, // FIXME: Define correct error code
       NULL);
-
 }
diff --git a/src/lib/anastasis_api_recovery_redux.c 
b/src/lib/anastasis_api_recovery_redux.c
index 459168a..6dfe14a 100644
--- a/src/lib/anastasis_api_recovery_redux.c
+++ b/src/lib/anastasis_api_recovery_redux.c
@@ -35,12 +35,23 @@
  *  @param cb_cls FIXME: Description
  */
 typedef void
-(*DispatchHandler)(const json_t *state,
+(*DispatchHandler)(json_t *state,
                    const json_t *arguments,
                    ANASTASIS_ActionCallback cb,
                    void *cb_cls);
 
 
+static void
+set_state (json_t *state,
+           const char *new_recovery_state)
+{
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      "recovery_state",
+                                      json_string (new_recovery_state)));
+}
+
+
 /**
  * Returns an initial ANASTASIS recovery state.
  *
@@ -52,15 +63,13 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
   json_t *initial_state;
   json_t *temp_country;
   size_t index;
-  json_t *continents = json_array ();
   json_t *root;
+  json_t *continents = json_array ();
+  json_t *redux_countries = redux_countries_init ();
 
-  if (GNUNET_SYSERR == redux_countries_init ())
-  {
-    GNUNET_break (0);
-    return NULL;
-  }
+  GNUNET_assert (NULL != redux_countries);
   root = json_object_get (redux_countries, "countries");
+  GNUNET_assert (NULL != root);
 
   json_array_foreach (root, index, temp_country)
   {
@@ -83,7 +92,8 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
   }
 
   initial_state = json_pack ("{s:s, s:o}",
-                             "recovery-state", "ReduxInitialRecoveryState",
+                             "recovery_state",
+                             STATE_STRING[ContinentSelectionState],
                              "continents", continents);
   return initial_state;
 }
@@ -95,7 +105,7 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param state input state
+ * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
@@ -103,7 +113,7 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
  * @return failure state or new state
  */
 void
-ANASTASIS_recovery_action (const json_t *state,
+ANASTASIS_recovery_action (json_t *state,
                            const char *action,
                            const json_t *arguments,
                            ANASTASIS_ActionCallback cb,
@@ -111,14 +121,14 @@ ANASTASIS_recovery_action (const json_t *state,
 {
   struct Dispatcher
   {
-    const char *recovery_state;
+    REDUX_STATE recovery_state;
     const char *recovery_action;
     DispatchHandler fun;
   } dispatchers[] = {
-    { NULL, NULL, NULL }
+    { NoState, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
-                                                      "recovery-state"));
+                                                      "recovery_state"));
   if (NULL == s)
   {
     GNUNET_break (0);
@@ -128,7 +138,7 @@ ANASTASIS_recovery_action (const json_t *state,
   }
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s, dispatchers[i].recovery_state)) &&
+    if ( (0 == strcmp (s, STATE_STRING[dispatchers[i].recovery_state])) &&
          (0 == strcmp (action, dispatchers[i].recovery_action)) )
     {
       dispatchers[i].fun (state, arguments, cb, cb_cls);
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index 9245a54..09e28a0 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -26,25 +26,21 @@
 #include "anastasis_redux.h"
 #include "anastasis_error_codes.h"
 
-/**
- * JSON containing all supported countries and additional data.
- */
-json_t *redux_countries;
 
 /**
  * JSON containing country specific identity attributes to ask the user for.
  */
-json_t *redux_id_attr;
+const json_t *redux_id_attr;
 
 /**
  * JSON containing anastasis providers.
  */
-json_t *provider_list;
+const json_t *provider_list;
 
 /**
- * backup or recovery mode
+ * Currency of chosen country
  */
-static char *s_mode;
+const char *currency;
 
 
 /**
@@ -56,23 +52,60 @@ static char *s_mode;
  *  @param cb_cls FIXME: Description
  */
 typedef void
-(*DispatchHandler)(const json_t *state,
+(*DispatchHandler)(json_t *state,
                    const json_t *arguments,
                    ANASTASIS_ActionCallback cb,
                    void *cb_cls);
 
 
 /**
- * Function to load json containing all countries.
- * Returns #GNUNET_OK if operation succeeded, else #GNUNET_SYSERR.
+ * Returns the integer value to a string value of a state.
+ * -1 if state unknown.
  *
+ * @param state_string
  * @return int
  */
-int
+int get_enum_from_string (const char *state_string)
+{
+  for (unsigned int i = ContinentSelectionState; i != NoState; i++)
+  {
+    if (0 == strcmp (state_string, STATE_STRING[i]))
+      return i;
+  }
+  return -1;
+}
+
+
+/**
+ * Extract the mode of a state from json
+ *
+ * @param state the state to operate on
+ * @return char*
+ */
+static
+char *get_state_mode (const json_t *state)
+{
+  if (json_object_get (state, "backup_state"))
+    return "backup_state";
+  else if (json_object_get (state, "recovery_state"))
+    return "recovery_state";
+  else
+    return NULL;
+}
+
+
+/**
+ * Function to load json containing all countries.
+ * Returns the countries.
+ *
+ * @return json_t *
+ */
+json_t *
 redux_countries_init ()
 {
   char *dn;
   json_error_t error;
+  json_t *redux_countries;
 
   {
     char *path;
@@ -81,7 +114,7 @@ redux_countries_init ()
     if (NULL == path)
     {
       GNUNET_break (0);
-      return GNUNET_SYSERR;
+      return NULL;
     }
     GNUNET_asprintf (&dn,
                      "%s/share/anastasis/redux.countries.json",
@@ -99,10 +132,10 @@ redux_countries_init ()
                 error.column,
                 error.position);
     GNUNET_free (dn);
-    return GNUNET_SYSERR;
+    return NULL;
   }
   GNUNET_free (dn);
-  return GNUNET_OK;
+  return redux_countries;
 }
 
 
@@ -154,40 +187,24 @@ redux_id_attr_init (const char*country_code)
 
 
 static void
-select_continent (const json_t *state,
+select_continent (json_t *state,
                   const json_t *arguments,
                   ANASTASIS_ActionCallback cb,
                   void *cb_cls)
 {
-  json_t *new_state;
   size_t index;
   json_t *country;
+  json_t *redux_countries = redux_countries_init ();
   json_t *root = json_object_get (redux_countries, "countries");
   json_t *countries = json_array ();
   json_t *continent = json_object_get (arguments, "continent");
 
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == continent)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
+  GNUNET_assert (NULL != root);
+  GNUNET_assert (NULL != countries);
+  GNUNET_assert (NULL != continent);
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
 
   json_array_foreach (root, index, country)
   {
@@ -198,156 +215,122 @@ select_continent (const json_t *state,
       json_array_append_new (countries, country);
   }
 
-  json_object_set (new_state,
-                   s_mode,
-                   json_string ("ReduxContinentSelectedState"));
+  json_object_set_new (state,
+                       s_mode,
+                       json_string (STATE_STRING[CountrySelectionState]));
 
-  json_object_set_new (new_state,
+  json_object_set_new (state,
                        "selected_continent",
                        continent);
 
-  json_object_set_new (new_state,
+  json_object_set_new (state,
                        "countries",
                        countries);
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
+      state);
 }
 
 
 static void
-select_country (const json_t *state,
+select_country (json_t *state,
                 const json_t *arguments,
                 ANASTASIS_ActionCallback cb,
                 void *cb_cls)
 {
-  json_t *new_state;
   json_t *root;
   json_t *country = json_object_get (arguments, "country_code");
 
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == country)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (GNUNET_SYSERR ==
-      redux_id_attr_init (json_string_value (country)))
-  {
-    GNUNET_break (0);
-    return;
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
+  GNUNET_assert (NULL != country);
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
+  currency = json_string_value (json_object_get (arguments,
+                                                 "currency"));
+  GNUNET_assert (NULL != currency);
+  GNUNET_assert (GNUNET_SYSERR !=
+                 redux_id_attr_init (json_string_value (country)));
   root = json_object_get (redux_id_attr, "required_attributes");
+  GNUNET_assert (NULL != root);
+  json_object_set_new (state,
+                       s_mode,
+                       json_string (
+                         STATE_STRING[UserAttributesCollectionState]));
 
-  json_object_set (new_state,
-                   s_mode,
-                   json_string ("ReduxCountrySelectedState"));
-
-  json_object_set_new (new_state,
+  json_object_set_new (state,
                        "selected_country",
                        country);
 
-  json_object_set_new (new_state,
+  json_object_set_new (state,
+                       "currency",
+                       json_string (currency));
+
+  json_object_set_new (state,
                        "required_attributes",
                        root);
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
+      state);
 }
 
 
 static void
-change_country (const json_t *state,
-                const json_t *arguments,
-                ANASTASIS_ActionCallback cb,
-                void *cb_cls)
+unselect_country (json_t *state,
+                  const json_t *arguments,
+                  ANASTASIS_ActionCallback cb,
+                  void *cb_cls)
 {
-  json_t *new_state;
-  json_t *root;
-  json_t *country = json_object_get (arguments, "country_code");
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == country)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (GNUNET_SYSERR ==
-      redux_id_attr_init (json_string_value (country)))
-  {
-    GNUNET_break (0);
-    return;
-  }
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  root = json_object_get (redux_id_attr, "required_attributes");
-
-
-  json_object_set (new_state,
-                   s_mode,
-                   json_string ("ReduxContinentSelectedState"));
-
-  json_object_set_new (new_state,
-                       "selected_country",
-                       country);
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      s_mode,
+                                      json_string (
+                                        STATE_STRING[CountrySelectionState])));
+  cb (cb_cls,
+      ANASTASIS_EC_NONE,
+      state);
+}
 
-  json_object_set_new (new_state,
-                       "required_attributes",
-                       root);
 
+static void
+unselect_continent (json_t *state,
+                    const json_t *arguments,
+                    ANASTASIS_ActionCallback cb,
+                    void *cb_cls)
+{
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      s_mode,
+                                      json_string (
+                                        
STATE_STRING[ContinentSelectionState])));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
+      state);
 }
 
 
 static void
-enter_user_attributes (const json_t *state,
+enter_user_attributes (json_t *state,
                        const json_t *arguments,
                        ANASTASIS_ActionCallback cb,
                        void *cb_cls)
 {
-  json_t *new_state;
-  json_t *attributes = json_object_get (arguments, "identity");
   json_t *root;
   char *dn;
   json_error_t error;
+  json_t *attributes = json_object_get (arguments, "identity_attributes");
 
+  GNUNET_assert (NULL != attributes);
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
   {
     char *path;
 
@@ -375,48 +358,62 @@ enter_user_attributes (const json_t *state,
     GNUNET_free (dn);
     return;
   }
-
-  if (NULL == state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-  if (NULL == attributes)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  new_state = json_deep_copy (state);
-  if (NULL == new_state)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
   root = json_object_get (provider_list, "anastasis-provider");
-
-  json_object_set_new (new_state,
+  GNUNET_assert (NULL != root);
+  json_object_set_new (state,
                        s_mode,
-                       json_string ("ReduxUserAttributesAddedState"));
+                       json_string 
(STATE_STRING[AuthenticationsEditingState]));
 
-  json_object_set_new (new_state,
+  json_object_set_new (state,
                        "identity_attributes",
                        attributes);
 
-  json_object_set_new (new_state,
+  json_object_set_new (state,
                        "provider-list",
                        root);
+  cb (cb_cls,
+      ANASTASIS_EC_NONE,
+      state);
+}
 
+
+/**
+ * Function which is called when we need to go a state backwards.
+ * Sets state to previous state.
+ *
+ * @param[in,out] state input/output state (to be modified)
+ * @param arguments normally unused
+ * @param cb callback
+ * @param cb_cls closure
+ */
+void
+state_back (json_t *state,
+            const json_t *arguments,
+            ANASTASIS_ActionCallback cb,
+            void *cb_cls)
+{
+  GNUNET_assert (NULL != state);
+  const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
+  const char*state_string = json_string_value (json_object_get (state,
+                                                                s_mode));
+  GNUNET_assert (NULL != state_string);
+  int state_index = get_enum_from_string (state_string);
+  GNUNET_assert (GNUNET_SYSERR != state_index);
+
+  if (state_index == BackupPayingState + 1) // offset for recovery states
+  {
+    state_index = UserAttributesCollectionState;
+  }
+  else
+    state_index = state_index - 1;
+  GNUNET_assert (0 == json_object_set_new (state,
+                                           s_mode,
+                                           json_string (
+                                             STATE_STRING[state_index])));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (new_state);
+      state);
 }
 
 
@@ -444,41 +441,77 @@ ANASTASIS_redux_action (const json_t *state,
 {
   struct Dispatcher
   {
-    const char *redux_state;
+    REDUX_STATE redux_state;
     const char *redux_action;
     DispatchHandler fun;
   } dispatchers[] = {
     {
-      "ReduxInitialState",
-      "selection_continent",
+      ContinentSelectionState,
+      "select_continent",
+      &select_continent
+    },
+    {
+      CountrySelectionState,
+      "unselect_continent",
+      &unselect_continent
+    },
+    {
+      CountrySelectionState,
+      "select_continent",
       &select_continent
     },
     {
-      "ReduxContinentSelectedState",
-      "selection_country",
+      CountrySelectionState,
+      "select_country",
+      &select_country
+    },
+    {
+      UserAttributesCollectionState,
+      "unselect_country",
+      &unselect_country
+    },
+    {
+      UserAttributesCollectionState,
+      "select_country",
       &select_country
     },
     {
-      "ReduxCountrySelectedState",
-      "entering_user_attributes",
+      UserAttributesCollectionState,
+      "back",
+      &unselect_country
+    },
+    {
+      UserAttributesCollectionState,
+      "select_continent",
+      &select_continent
+    },
+    {
+      UserAttributesCollectionState,
+      "unselect_continent",
+      &unselect_continent
+    },
+    {
+      UserAttributesCollectionState,
+      "enter_user_attributes",
       &enter_user_attributes
     },
     {
-      "ReduxUserAttributesAddedState",
-      "changing_country",
-      &change_country
+      AuthenticationsEditingState,
+      "back",
+      &state_back
     },
-    { NULL, NULL, NULL }
+    { NoState, NULL, NULL }
   };
+  GNUNET_assert (NULL != state);
   json_t *new_state;
-  const char *s_mode = "backup-state";
+  const char *s_mode = "backup_state";
   const char *s = json_string_value (json_object_get (state,
-                                                      "backup-state"));
+                                                      "backup_state"));
   if (NULL == s)
   {
     s = json_string_value (json_object_get (state,
-                                            "recovery-state"));
-    s_mode = "recovery-state";
+                                            "recovery_state"));
+    s_mode = "recovery_state";
     if (NULL == s)
     {
       GNUNET_break (0);
@@ -491,7 +524,7 @@ ANASTASIS_redux_action (const json_t *state,
   GNUNET_assert (NULL != new_state);
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s, dispatchers[i].redux_state)) &&
+    if ( (0 == strcmp (s, STATE_STRING[dispatchers[i].redux_state])) &&
          (0 == strcmp (action, dispatchers[i].redux_action)) )
     {
       dispatchers[i].fun (new_state, arguments, cb, cb_cls);
@@ -499,17 +532,30 @@ ANASTASIS_redux_action (const json_t *state,
       return;
     }
   }
-  if (0 == strcmp (s_mode, "backup-state"))
+  if (0 == strcmp (s_mode, "backup_state"))
+  {
     ANASTASIS_backup_action (new_state,
                              action,
                              arguments,
                              cb,
                              NULL);
-  if (0 == strcmp (s_mode, "recovery-state"))
+    json_decref (new_state);
+    return;
+  }
+
+  if (0 == strcmp (s_mode, "recovery_state"))
+  {
     ANASTASIS_recovery_action (new_state,
                                action,
                                arguments,
                                cb,
                                NULL);
+    json_decref (new_state);
+    return;
+  }
   json_decref (new_state);
+  GNUNET_break (0);
+  cb (cb_cls,
+      ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+      NULL);
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]