gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 01/02: fun for Dennis


From: gnunet
Subject: [taler-anastasis] 01/02: fun for Dennis
Date: Tue, 06 Oct 2020 12:18:40 +0200

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

grothoff pushed a commit to branch master
in repository anastasis.

commit d7da4b1796f909f0a60cf640c4de3b30f7107135
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Tue Oct 6 12:17:46 2020 +0200

    fun for Dennis
---
 src/lib/anastasis_api_backup_redux.c | 239 +++++++++++++++++++++++++++++++----
 src/lib/anastasis_api_redux.c        |  28 ++--
 2 files changed, 229 insertions(+), 38 deletions(-)

diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index e4a8b0f..13bc5b5 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -83,8 +83,9 @@ ANASTASIS_backup_start (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
   }
 
   initial_state = json_pack ("{s:s, s:o}",
-                             "backup-state", "ReduxInitialBackupState",
+                             "backup-state", "ReduxInitialState",
                              "continents", continents);
+  GNUNET_assert (NULL != initial_state);
   return initial_state;
 }
 
@@ -142,26 +143,9 @@ add_authentication (const json_t *state,
   json_t *new_state;
   json_t *methods = json_object_get (arguments, "authentication_methods");
 
+  GNUNET_assert (NULL != methods);
   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);
-  }
-  if (NULL == methods)
-  {
-    GNUNET_break (0);
-    cb (cb_cls,
-        ANASTASIS_EC_INVALID, // FIXME: Define correct error code
-        NULL);
-  }
-
-  json_object_set (new_state,
-                   "backup-state",
-                   json_string ("ReduxAuthenticationAddedState"));
-
+  GNUNET_assert (NULL != new_state);
   json_object_set (new_state,
                    "authentication_methods",
                    methods);
@@ -175,16 +159,18 @@ add_authentication (const json_t *state,
     json_t *policy = json_array ();
 
     // simply distribute the methods
-    json_array_foreach (methods, index_m, method)
+    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);
 
@@ -229,6 +215,199 @@ add_authentication (const json_t *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)));
+}
+
+
+struct PolicyBuilder
+{
+  json_t *providers;
+  json_t *methods;
+  json_t *policies;
+  unsigned int num_methods;
+  unsigned int req_methods;
+
+
+  const unsigned int *m_idx;
+
+  unsigned int *best_sel;
+
+  unsigned int best_diversity;
+  struct TALER_Amount best_cost;
+};
+
+
+static void
+eval_provider_selection (struct PolicyBuilder *pb,
+                         const unsigned int *prov_sel)
+{
+  bool better = true;
+  unsigned int curr_diversity = 0;
+  struct TALER_Amount curr_cost;
+
+
+  if (better)
+  {
+    memcpy (pb->best_sel,
+            prov_sel,
+            sizeof (unsigned int) * pb->req_methods);
+    pb->best_diversity = curr_diversity;
+    ob->best_cost = curr_cost;
+  }
+}
+
+
+static void
+provider_candidate (struct PolicyBuilder *pb,
+                    unsigned int *prov_sel,
+                    unsigned int i)
+{
+  json_t *method_obj;
+  const char *method_name;
+  json_t *method_providers;
+
+  method_obj = json_array_get (pb->methods,
+                               pb->m_idx[i]);
+  method_name = json_string_value (json_object_get (method_obj,
+                                                    "method"));
+  method_providers = json_object_get (pb->providers,
+                                      method_name);
+  num_prov = json_array_size (method_providers);
+  for (unsigned int j = 0; j<num_prov; j++)
+  {
+    prov_sel[i] = j;
+    if (i == num_prov)
+    {
+      eval_provider_selection (pb,
+                               prov_sel);
+      continue;
+    }
+    provider_candidate (pb,
+                        prov_sel,
+                        i + 1);
+  }
+}
+
+
+static void
+go_with (struct PolicyBuilder *pb,
+         const unsigned int *m_idx)
+{
+  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,
+                      prov_sel,
+                      0);
+
+  for (unsigned int i = 0; i<pb->req_methods)
+  {
+    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);
+  }
+}
+
+
+static void
+method_candidate (struct PolicyBuilder *pb,
+                  unsigned int *m_idx,
+                  unsigned int i)
+{
+  unsigned int start = 0;
+
+  if (i > 0)
+    start = m_idx[i - 1];
+  for (unsigned int j = start; j<pb->num_methods; j++)
+  {
+    m_idx[i] = j;
+    if (i == pb->req_methods)
+    {
+      go_with (pb,
+               m_idx);
+      continue;
+    }
+    else
+    {
+      method_candidate (pb,
+                        m_idx,
+                        i + 1);
+    }
+  }
+}
+
+
+static void
+done_authentication (const json_t *state,
+                     const json_t *arguments,
+                     ANASTASIS_ActionCallback cb,
+                     void *cb_cls)
+{
+  struct PolicyBuilder pb;
+
+  pb.providers = json_object_get (state,
+                                  "authentication_providers");
+  pb.methods = json_object_get (state,
+                                "authentication_methods");
+  pb.policies = json_array_new ();
+  pb.num_methods = json_array_size (methods);
+  switch (pb.num_methods)
+  {
+  case 0:
+    // FAIL
+    break;
+  case 1:
+  case 2:
+    pb.req_methods = num_methods;
+    break;
+  case 3:
+  case 4:
+    pb.req_methods = pb.num_methods - 1;
+    break;
+  default:
+    pb.req_methods = pb.num_methods - 2;
+    break;
+  }
+  {
+    unsigned int m_idx[req_methods];
+
+    method_candidate (&pb,
+                      m_idx,
+                      0);
+    bool feasible = false;
+
+    // select req_methods from num_methods.
+
+
+  }
+
+
+  json_object_set_new (state,
+                       "policies",
+                       policies);
+  set_state (state,
+             "ReduxAuthenticationAddedState");
+  cb (cb_cls,
+      ANASTASIS_EC_NONE,
+      state);
+}
+
+
 static void
 add_policy (const json_t *state,
             const json_t *arguments,
@@ -245,7 +424,7 @@ add_policy (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
@@ -253,7 +432,7 @@ add_policy (const json_t *state,
  * @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,
@@ -270,6 +449,18 @@ ANASTASIS_backup_action (const json_t *state,
       "adding_authentication",
       &add_authentication
     },
+    #if 0
+    {
+      "ReduxUserAttributesAddedState",
+      "deleting_authentication",
+      &del_authentication
+    },
+    #endif
+    {
+      "ReduxUserAttributesAddedState",
+      "next", // done_authentication",
+      &done_authentication
+    },
     {
       "ReduxUserAttributesAddedState",
       "editing_user_attributes",
@@ -282,7 +473,7 @@ ANASTASIS_backup_action (const json_t *state,
     },
     {
       "ReduxAuthenticationAddedState",
-      "editing_user_attributes",
+      "back", // editing_user_attributes",
       &edit_user_attributes
     },
     { NULL, NULL, NULL }
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index 3859729..9245a54 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -319,7 +319,7 @@ change_country (const json_t *state,
 
   json_object_set (new_state,
                    s_mode,
-                   json_string ("ReduxCountrySelectedState"));
+                   json_string ("ReduxContinentSelectedState"));
 
   json_object_set_new (new_state,
                        "selected_country",
@@ -401,9 +401,9 @@ enter_user_attributes (const json_t *state,
   }
   root = json_object_get (provider_list, "anastasis-provider");
 
-  json_object_set (new_state,
-                   s_mode,
-                   json_string ("ReduxUserAttributesAddedState"));
+  json_object_set_new (new_state,
+                       s_mode,
+                       json_string ("ReduxUserAttributesAddedState"));
 
   json_object_set_new (new_state,
                        "identity_attributes",
@@ -449,12 +449,7 @@ ANASTASIS_redux_action (const json_t *state,
     DispatchHandler fun;
   } dispatchers[] = {
     {
-      "ReduxInitialBackupState",
-      "selection_continent",
-      &select_continent
-    },
-    {
-      "ReduxInitialRecoveryState",
+      "ReduxInitialState",
       "selection_continent",
       &select_continent
     },
@@ -475,7 +470,8 @@ ANASTASIS_redux_action (const json_t *state,
     },
     { NULL, NULL, NULL }
   };
-  s_mode = "backup-state";
+  json_t *new_state;
+  const char *s_mode = "backup-state";
   const char *s = json_string_value (json_object_get (state,
                                                       "backup-state"));
   if (NULL == s)
@@ -491,25 +487,29 @@ ANASTASIS_redux_action (const json_t *state,
           NULL);
     }
   }
+  new_state = json_deep_copy (state);
+  GNUNET_assert (NULL != new_state);
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)
   {
     if ( (0 == strcmp (s, dispatchers[i].redux_state)) &&
          (0 == strcmp (action, dispatchers[i].redux_action)) )
     {
-      dispatchers[i].fun (state, arguments, cb, cb_cls);
+      dispatchers[i].fun (new_state, arguments, cb, cb_cls);
+      json_decref (new_state);
       return;
     }
   }
   if (0 == strcmp (s_mode, "backup-state"))
-    ANASTASIS_backup_action (state,
+    ANASTASIS_backup_action (new_state,
                              action,
                              arguments,
                              cb,
                              NULL);
   if (0 == strcmp (s_mode, "recovery-state"))
-    ANASTASIS_recovery_action (state,
+    ANASTASIS_recovery_action (new_state,
                                action,
                                arguments,
                                cb,
                                NULL);
+  json_decref (new_state);
 }

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