gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 01/02: fix stringify


From: gnunet
Subject: [taler-anastasis] 01/02: fix stringify
Date: Wed, 14 Oct 2020 14:51:43 +0200

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

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

commit b4175354de3025c86bc66996edba9e973d21ef0b
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Wed Oct 14 14:51:13 2020 +0200

    fix stringify
---
 src/include/anastasis_redux.h          |  13 +--
 src/lib/anastasis_api_backup_redux.c   |  39 ++++++---
 src/lib/anastasis_api_recovery_redux.c |  64 ++++++++++++--
 src/lib/anastasis_api_redux.c          | 153 ++++++++++++++++++++-------------
 4 files changed, 182 insertions(+), 87 deletions(-)

diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index 84346bf..7a973ac 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -192,15 +192,16 @@ typedef void
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
+ * @param ctx the CURL context used to connect to the backend
  * @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
- * @return failure state or new state
  */
 void
-ANASTASIS_backup_action (json_t *state,
+ANASTASIS_backup_action (struct GNUNET_CURL_Context *ctx,
+                         json_t *state,
                          const char *action,
                          const json_t *arguments,
                          ANASTASIS_ActionCallback cb,
@@ -208,11 +209,12 @@ ANASTASIS_backup_action (json_t *state,
 
 
 /**
- * Operates on a backup state depending on given #ANASTASIS_BackupState
- * and #ANASTASIS_BackupAction. The new #ANASTASIS_BackupState is returned
+ * Operates on a recovery state depending on given #ANASTASIS_RecoveryState
+ * and #ANASTASIS_RecoveryAction. The new #ANASTASIS_RecoveryState is returned
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
+ * @param ctx the CURL context used to connect to the backend
  * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
@@ -220,7 +222,8 @@ ANASTASIS_backup_action (json_t *state,
  * @param cb_cls closure for @a cb
  */
 void
-ANASTASIS_recovery_action (json_t *state,
+ANASTASIS_recovery_action (struct GNUNET_CURL_Context *ctx,
+                           json_t *state,
                            const char *action,
                            const json_t *arguments,
                            ANASTASIS_ActionCallback cb,
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index aadaa4f..2cd29ee 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -442,7 +442,8 @@ done_authentication (json_t *state,
                                            "policies",
                                            pb.policies));
   set_state (state,
-             STATE_STRING[PoliciesReviewingState]);
+             ANASTASIS_backup_state_to_string (
+               ANASTASIS_BACKUP_STATE_PoliciesReviewingState));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -511,15 +512,16 @@ add_policy (json_t *state,
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
+ * @param ctx the CURL context used to connect to the backend
  * @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
- * @return failure state or new state
  */
 void
-ANASTASIS_backup_action (json_t *state,
+ANASTASIS_backup_action (struct GNUNET_CURL_Context *ctx,
+                         json_t *state,
                          const char *action,
                          const json_t *arguments,
                          ANASTASIS_ActionCallback cb,
@@ -527,34 +529,36 @@ ANASTASIS_backup_action (json_t *state,
 {
   struct Dispatcher
   {
-    REDUX_STATE backup_state;
+    enum ANASTASIS_BackupState backup_state;
     const char *backup_action;
     DispatchHandler fun;
   } dispatchers[] = {
     {
-      AuthenticationsEditingState,
+      ANASTASIS_BACKUP_STATE_AuthenticationsEditingState,
       "add_authentication",
       &add_authentication
     },
     {
-      AuthenticationsEditingState,
+      ANASTASIS_BACKUP_STATE_AuthenticationsEditingState,
       "delete_authentication",
       &del_authentication
     },
     {
-      AuthenticationsEditingState,
+      ANASTASIS_BACKUP_STATE_AuthenticationsEditingState,
       "next",
       &done_authentication
     },
     {
-      AuthenticationsEditingState,
+      ANASTASIS_BACKUP_STATE_AuthenticationsEditingState,
       "back",
       &ANASTASIS_back_generic_decrement_,
     },
-    { NoState, NULL, NULL }
+    { ANASTASIS_BACKUP_STATE_ERROR, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
                                                       "backup_state"));
+  enum ANASTASIS_BackupState bs;
+
   if (NULL == s)
   {
     GNUNET_break (0);
@@ -562,15 +566,24 @@ ANASTASIS_backup_action (json_t *state,
         ANASTASIS_EC_INVALID, // FIXME: Define correct error code
         NULL);
   }
-  // FIXME: convert s to enum, if fails: call cb with error!
+  bs = ANASTASIS_backup_state_from_string (s);
+  if (ANASTASIS_BACKUP_STATE_ERROR == bs)
+  {
+    cb (cb_cls,
+        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+        NULL);
+    return;
+  }
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s,
-                       STATE_STRING[dispatchers[i].backup_state])) &&
+    if ( (bs == dispatchers[i].backup_state) &&
          (0 == strcmp (action,
                        dispatchers[i].backup_action)) )
     {
-      dispatchers[i].fun (state, arguments, cb, cb_cls);
+      dispatchers[i].fun (state,
+                          arguments,
+                          cb,
+                          cb_cls);
       return;
     }
   }
diff --git a/src/lib/anastasis_api_recovery_redux.c 
b/src/lib/anastasis_api_recovery_redux.c
index 6dfe14a..d682fc2 100644
--- a/src/lib/anastasis_api_recovery_redux.c
+++ b/src/lib/anastasis_api_recovery_redux.c
@@ -25,6 +25,41 @@
 #include <jansson.h>
 #include "anastasis_redux.h"
 #include "anastasis_error_codes.h"
+#include "anastasis_api_redux.h"
+
+
+#define GENERATE_STRING(STRING) #STRING,
+static const char *recovery_strings[] = {
+  ANASTASIS_RECOVERY_STATES (GENERATE_STRING)
+};
+#undef GENERATE_STRING
+
+
+enum ANASTASIS_RecoveryState
+ANASTASIS_recovery_state_from_string (const char *state_string)
+{
+  for (enum ANASTASIS_RecoveryState i = 0;
+       i < sizeof (recovery_strings) / sizeof(*recovery_strings);
+       i++)
+    if (0 == strcmp (state_string,
+                     recovery_strings[i]))
+      return i;
+  return ANASTASIS_RECOVERY_STATE_ERROR;
+}
+
+
+const char *
+ANASTASIS_recovery_state_to_string (enum ANASTASIS_RecoveryState rs)
+{
+  if ( (rs < 0) ||
+       (rs >= sizeof (recovery_strings) / sizeof(*recovery_strings)) )
+  {
+    GNUNET_break_op (0);
+    return NULL;
+  }
+  return recovery_strings[rs];
+}
+
 
 /**
  * Callback function FIXME: Description.
@@ -93,8 +128,10 @@ ANASTASIS_recovery_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
 
   initial_state = json_pack ("{s:s, s:o}",
                              "recovery_state",
-                             STATE_STRING[ContinentSelectionState],
-                             "continents", continents);
+                             ANASTASIS_recovery_state_to_string (
+                               ANASTASIS_RECOVERY_STATE_CONTINENT_SELECTION),
+                             "continents",
+                             continents);
   return initial_state;
 }
 
@@ -105,12 +142,12 @@ 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 ctx the CURL context used to connect to the backend
  * @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
- * @return failure state or new state
  */
 void
 ANASTASIS_recovery_action (json_t *state,
@@ -121,14 +158,16 @@ ANASTASIS_recovery_action (json_t *state,
 {
   struct Dispatcher
   {
-    REDUX_STATE recovery_state;
+    enum ANASTASIS_RecoveryState recovery_state;
     const char *recovery_action;
     DispatchHandler fun;
   } dispatchers[] = {
-    { NoState, NULL, NULL }
+    { ANASTASIS_RECOVERY_STATE_ERROR, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
                                                       "recovery_state"));
+  enum ANASTASIS_RecoveryState rs;
+
   if (NULL == s)
   {
     GNUNET_break (0);
@@ -136,12 +175,23 @@ ANASTASIS_recovery_action (json_t *state,
         ANASTASIS_EC_INVALID, // FIXME: Define correct error code
         NULL);
   }
+  rs = ANASTASIS_recovery_state_from_string (s);
+  if (ANASTASIS_RECOVERY_STATE_ERROR == rs)
+  {
+    cb (cb_cls,
+        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+        NULL);
+    return;
+  }
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
-    if ( (0 == strcmp (s, STATE_STRING[dispatchers[i].recovery_state])) &&
+    if ( (rs == dispatchers[i].recovery_state) &&
          (0 == strcmp (action, dispatchers[i].recovery_action)) )
     {
-      dispatchers[i].fun (state, arguments, cb, cb_cls);
+      dispatchers[i].fun (state,
+                          arguments,
+                          cb,
+                          cb_cls);
       return;
     }
   }
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index dab178c..afc897f 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -28,6 +28,40 @@
 #include <taler/taler_json_lib.h>
 #include "anastasis_api_redux.h"
 
+
+#define GENERATE_STRING(STRING) #STRING,
+static const char *generic_strings[] = {
+  ANASTASIS_GENERIC_STATES (GENERATE_STRING)
+};
+#undef GENERATE_STRING
+
+
+enum ANASTASIS_GenericState
+ANASTASIS_generic_state_from_string (const char *state_string)
+{
+  for (enum ANASTASIS_GenericState i = 0;
+       i < sizeof (generic_strings) / sizeof(*generic_strings);
+       i++)
+    if (0 == strcmp (state_string,
+                     generic_strings[i]))
+      return i;
+  return ANASTASIS_GENERIC_STATE_ERROR;
+}
+
+
+const char *
+ANASTASIS_generic_state_to_string (enum ANASTASIS_GenericState gs)
+{
+  if ( (gs < 0) ||
+       (gs >= sizeof (generic_strings) / sizeof(*generic_strings)) )
+  {
+    GNUNET_break_op (0);
+    return NULL;
+  }
+  return generic_strings[gs];
+}
+
+
 /**
  * Server information
  */
@@ -268,25 +302,6 @@ config_cb (void *cls,
 }
 
 
-/**
- * Returns the integer value to a string value of a state.
- * -1 if state unknown.
- *
- * @param state_string
- * @return 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
  *
@@ -427,17 +442,23 @@ select_continent (json_t *state,
       json_array_append_new (countries, country);
   }
 
-  json_object_set_new (state,
-                       s_mode,
-                       json_string (STATE_STRING[CountrySelectionState]));
+  GNUNET_assert (0 ==
+                 json_object_set_new (
+                   state,
+                   s_mode,
+                   json_string (
+                     ANASTASIS_backup_state_to_string (
+                       ANASTASIS_GENERIC_STATE_COUNTRY_SELECTION))));
 
-  json_object_set_new (state,
-                       "selected_continent",
-                       continent);
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      "selected_continent",
+                                      continent));
 
-  json_object_set_new (state,
-                       "countries",
-                       countries);
+  GNUNET_assert (0 ==
+                 json_object_set_new (state,
+                                      "countries",
+                                      countries));
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
@@ -578,14 +599,15 @@ unselect_country (json_t *state,
                   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[CountrySelectionState])));
+                 json_object_set_new (
+                   state,
+                   s_mode,
+                   json_string (
+                     ANASTASIS_generic_state_to_string (
+                       ANASTASIS_GENERIC_STATE_COUNTRY_SELECTION))));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -602,10 +624,12 @@ unselect_continent (json_t *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])));
+                 json_object_set_new (
+                   state,
+                   s_mode,
+                   json_string (
+                     ANASTASIS_generic_state_to_string (
+                       ANASTASIS_GENERIC_STATE_CONTINENT_SELECTION))));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -626,11 +650,14 @@ enter_user_attributes (json_t *state,
   GNUNET_assert (NULL != s_mode);
   json_t *auth_providers = json_object_get (arguments,
                                             "authentication_providers");
-  // GNUNET_assert (NULL != auth_providers);
 
-  json_object_set_new (state,
-                       s_mode,
-                       json_string 
(STATE_STRING[AuthenticationsEditingState]));
+  if (0 == strcmp (s_mode, "backup_state"))
+    json_object_set_new (state,
+                         s_mode,
+                         json_string (
+                           ANASTASIS_backup_state_to_string (
+                             
ANASTASIS_BACKUP_STATE_AuthenticationsEditingState)));
+  /** else FIXME: do something for recovery mode*/
 
   json_object_set_new (state,
                        "identity_attributes",
@@ -656,13 +683,16 @@ ANASTASIS_back_generic_decrement_ (json_t *state,
   const char*state_string = json_string_value (json_object_get (state,
                                                                 s_mode));
   GNUNET_assert (NULL != state_string); // FIXME: call cb with error!
-  enum ANASTASIS_BackupState state_index = get_enum_from_string (state_string);
+  enum ANASTASIS_BackupState state_index = ANASTASIS_generic_state_from_string 
(
+    state_string);
   GNUNET_assert (state_index > 0);
   state_index = state_index - 1;
-  GNUNET_assert (0 == json_object_set_new (state,
-                                           s_mode,
-                                           json_string (
-                                             STATE_STRING[state_index])));
+  GNUNET_assert (0 ==
+                 json_object_set_new (
+                   state,
+                   s_mode,
+                   json_string (
+                     ANASTASIS_generic_state_to_string (state_index))));
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -705,56 +735,56 @@ ANASTASIS_redux_action (const json_t *state,
       &select_continent
     },
     {
-      CountrySelectionState,
+      ANASTASIS_GENERIC_STATE_COUNTRY_SELECTION,
       "unselect_continent",
       &unselect_continent
     },
     {
-      CountrySelectionState,
+      ANASTASIS_GENERIC_STATE_COUNTRY_SELECTION,
       "select_continent",
       &select_continent
     },
     {
-      CountrySelectionState,
+      ANASTASIS_GENERIC_STATE_COUNTRY_SELECTION,
       "select_country",
       &select_country
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "unselect_country",
       &unselect_country
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "select_country",
       &select_country
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "back",
       &unselect_country
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "select_continent",
       &select_continent
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "unselect_continent",
       &unselect_continent
     },
     {
-      UserAttributesCollectionState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "enter_user_attributes",
       &enter_user_attributes
     },
     {
-      AuthenticationsEditingState,
+      ANASTASIS_GENERIC_STATE_USER_ATTRIBUTES_COLLECTION,
       "back",
       &ANASTASIS_back_generic_decrement_
     },
-    { NoState, NULL, NULL }
+    { ANASTASIS_GENERIC_STATE_ERROR, NULL, NULL }
   };
   bool recovery_mode = false;
   const char *s = json_string_value (json_object_get (state,
@@ -783,9 +813,6 @@ ANASTASIS_redux_action (const json_t *state,
         NULL);
     return;
   }
-  if (NULL != ctx)
-    curl_ctx = ctx; // FIXME: pass as argument to 'fun'. NO GLOBALS!
-
   {
     json_t *new_state;
 
@@ -807,7 +834,8 @@ ANASTASIS_redux_action (const json_t *state,
     }
     if (recovery_mode)
     {
-      ANASTASIS_recovery_action (new_state, // FIXME: pass ctx!
+      ANASTASIS_recovery_action (ctx,
+                                 new_state,
                                  action,
                                  arguments,
                                  cb,
@@ -815,7 +843,8 @@ ANASTASIS_redux_action (const json_t *state,
     }
     else
     {
-      ANASTASIS_backup_action (new_state,
+      ANASTASIS_backup_action (ctx,
+                               new_state,
                                action,
                                arguments,
                                cb,

-- 
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]