Author: sandervanderburg
Date: Sat Oct 16 23:07:16 2010
New Revision: 24322
URL: https://svn.nixos.org/websvn/nix/?rev=24322&sc=1

Log:
Seperated the transition and locking stuff out the main module

Added:
   disnix/disnix/trunk/src/activate/locking.c
   disnix/disnix/trunk/src/activate/locking.h
   disnix/disnix/trunk/src/activate/transition.c
   disnix/disnix/trunk/src/activate/transition.h
Modified:
   disnix/disnix/trunk/src/activate/Makefile.am
   disnix/disnix/trunk/src/activate/main.c

Modified: disnix/disnix/trunk/src/activate/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/activate/Makefile.am        Sat Oct 16 17:22:39 
2010        (r24321)
+++ disnix/disnix/trunk/src/activate/Makefile.am        Sat Oct 16 23:07:16 
2010        (r24322)
@@ -1,7 +1,8 @@
 AM_CPPFLAGS = -ggdb -DLOCALSTATEDIR=\"$(localstatedir)\"
 
 bin_PROGRAMS = disnix-activate
+noinst_HEADERS = transition.h locking.h
 
-disnix_activate_SOURCES = main.c
+disnix_activate_SOURCES = transition.c locking.c main.c
 disnix_activate_LDADD = ../libmanifest/libmanifest.la ../libmain/libmain.la 
../libinterface/libinterface.la
 disnix_activate_CFLAGS = -I../libmanifest -I../libmain -I../libinterface

Added: disnix/disnix/trunk/src/activate/locking.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/activate/locking.c  Sat Oct 16 23:07:16 2010        
(r24322)
@@ -0,0 +1,111 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#include "locking.h"
+#include <distributionmapping.h>
+#include <client-interface.h>
+
+gboolean unlock(gchar *interface, GArray *distribution_array, gchar *profile)
+{
+    unsigned int i, running_processes = 0;
+    int exit_status = TRUE;
+    int status;
+    
+    /* For each locked machine, release the lock */
+    for(i = 0; i < distribution_array->len; i++)
+    {
+       DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
+       
+       status = exec_unlock(interface, item->target, profile);
+       
+       if(status == -1)
+       {
+           g_printerr("Error with forking unlock process!\n");
+           exit_status = FALSE;
+       }
+       else
+           running_processes++;
+    }
+    
+    /* Wait until every lock is released */
+    for(i = 0; i < running_processes; i++)
+    {
+       status = wait_to_finish(0);
+       
+       /* If a process fails, change the exit status */
+       if(status != 0)
+       {
+           g_printerr("Failed to release the lock!\n");
+           exit_status = FALSE;
+       }
+    }
+    
+    /* Return exit status */
+    return exit_status;
+}
+
+gboolean lock(gchar *interface, GArray *distribution_array, gchar *profile)
+{
+    unsigned int i;
+    GArray *try_array = g_array_new(FALSE, FALSE, sizeof(DistributionItem*));
+    GArray *lock_array = g_array_new(FALSE, FALSE, sizeof(DistributionItem*));
+    int exit_status = TRUE;
+    int status;
+    
+    /* For each machine acquire a lock */
+    for(i = 0; i < distribution_array->len; i++)
+    {
+       DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
+       
+       status = exec_lock(interface, item->target, profile);
+       
+       /* If a process fails, change the exit status */
+       if(status == -1)
+       {
+           g_printerr("Error with forking lock process!\n");
+           exit_status = FALSE;
+       }       
+       else
+           g_array_append_val(try_array, item);
+    }    
+    
+    /* Wait until every lock is acquired */
+    for(i = 0; i < try_array->len; i++)
+    {
+       status = wait_to_finish(0);
+       
+       /* If a process fails, change the exit status */
+       if(status != 0)
+       {
+           g_printerr("Failed to acquire a lock!\n");
+           exit_status = FALSE;
+       }
+    }
+    
+    /* If a lock fails then unlock every machine that is locked */
+    if(!exit_status)
+       unlock(interface, lock_array, profile);
+    
+    /* Cleanup */
+    g_array_free(try_array, TRUE);
+    g_array_free(lock_array, TRUE);
+    
+    /* Return exit status */
+    return exit_status;
+}

Added: disnix/disnix/trunk/src/activate/locking.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/activate/locking.h  Sat Oct 16 23:07:16 2010        
(r24322)
@@ -0,0 +1,45 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#ifndef __LOCKING_H
+#define __LOCKING_H
+#include <glib.h>
+
+/**
+ * Unlocks the given profile on each machine defined in the distribution array.
+ *
+ * @param interface Path to the client interface executable
+ * @param distribution_array Array with profiles distributed to the target 
machines
+ * @param profile Identifier of the distributed profile
+ * @return TRUE if the unlocking phase succeeds, else FALSE
+ */
+gboolean unlock(gchar *interface, GArray *distribution_array, gchar *profile);
+
+/**
+ * Locks the given profile on each machine defined in the distribution array.
+ * In case of a failure the lock is released again.
+ *
+ * @param interface Path to the client interface executable
+ * @param distribution_array Array with profiles distributed to the target 
machines
+ * @param profile Identifier of the distributed profile
+ * @return TRUE if the locking phase succeeds, else FALSE
+ */
+gboolean lock(gchar *interface, GArray *distribution_array, gchar *profile);
+
+#endif

Modified: disnix/disnix/trunk/src/activate/main.c
==============================================================================
--- disnix/disnix/trunk/src/activate/main.c     Sat Oct 16 17:22:39 2010        
(r24321)
+++ disnix/disnix/trunk/src/activate/main.c     Sat Oct 16 23:07:16 2010        
(r24322)
@@ -31,6 +31,8 @@
 #include <distributionmapping.h>
 #include <defaultoptions.h>
 #include <client-interface.h>
+#include "transition.h"
+#include "locking.h"
 
 static void print_usage()
 {
@@ -39,214 +41,6 @@
     fprintf(stderr, "disnix-activate {-h | --help}\n");
 }
 
-static int activate(GArray *union_list, ActivationMapping *mapping, gchar 
*interface)
-{
-    gint actual_mapping_index = activation_mapping_index(union_list, mapping);
-    ActivationMapping *actual_mapping = g_array_index(union_list, 
ActivationMapping*, actual_mapping_index);
-    
-    /* First activate all inter-dependencies */
-    if(actual_mapping->depends_on != NULL)
-    {
-       unsigned int i;
-       
-       for(i = 0; i < actual_mapping->depends_on->len; i++)
-       {
-           Dependency *dependency = g_array_index(actual_mapping->depends_on, 
Dependency*, i);
-           ActivationMapping lookup;
-           
-           lookup.service = dependency->service;
-           lookup.target = dependency->target;                     
-           
-           if(!activate(union_list, &lookup, interface))
-               return FALSE;
-       }
-    }
-    
-    /* Finally activate the service itself */
-    if(!actual_mapping->activated)
-    {
-       gchar **arguments = 
generate_activation_arguments(actual_mapping->target);
-       unsigned int arguments_size = g_strv_length(arguments);
-       gchar *target_interface = get_target_interface(actual_mapping);
-       int status, i;
-       
-       printf("Now activating service: %s of type: %s through: %s\n", 
actual_mapping->service, actual_mapping->type, target_interface);
-       printf("Using arguments: ");
-       
-       for(i = 0; i < arguments_size; i++)
-           printf("%s ", arguments[i]);    
-       
-       printf("\n");
-
-       status = wait_to_finish(exec_activate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
-       
-       g_strfreev(arguments);
-       
-       if(status != 0)
-           return FALSE;
-       else            
-           actual_mapping->activated = TRUE;
-    }
-    
-    return TRUE;
-}
-
-static int deactivate(GArray *union_list, ActivationMapping *mapping, gchar 
*interface)
-{
-    gint actual_mapping_index = activation_mapping_index(union_list, mapping);
-    ActivationMapping *actual_mapping = g_array_index(union_list, 
ActivationMapping*, actual_mapping_index);
-    GArray *interdependent_services = find_interdependent_mappings(union_list, 
actual_mapping);
-    
-    /* First deactivate all service which have an inter-dependency on this 
service */
-        
-    unsigned int i;
-       
-    for(i = 0; i < interdependent_services->len; i++)
-    {
-        ActivationMapping *dependency_mapping = 
g_array_index(interdependent_services, ActivationMapping*, i);
-        if(!deactivate(union_list, dependency_mapping, interface))
-           return FALSE;
-    }
-    
-    /* Finally deactivate the service itself */
-    if(actual_mapping->activated)
-    {
-       gchar **arguments = 
generate_activation_arguments(actual_mapping->target);
-       unsigned int arguments_size = g_strv_length(arguments);
-       gchar *target_interface = get_target_interface(actual_mapping);
-       int status, i;
-       
-       printf("Now deactivating service: %s of type: %s through: %s\n", 
actual_mapping->service, actual_mapping->type, target_interface);
-       printf("Using arguments: ");
-       
-       for(i = 0; i < arguments_size; i++)
-           printf("%s ", arguments[i]);
-       
-       printf("\n");
-       
-       status = wait_to_finish(exec_deactivate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
-               
-       g_free(arguments);
-
-       if(status != 0)
-           return FALSE;
-       else    
-           actual_mapping->activated = FALSE;
-    }
-    
-    return TRUE;
-}
-
-static int transition(GArray *list_new, GArray *list_old, char *interface)
-{
-    GArray *unio;
-    GArray *deactivate_list;
-    GArray *activate_list;
-    unsigned int i;        
-    
-    printf("new:\n");
-    print_activation_array(list_new);
-    
-    if(list_old != NULL)
-    {
-        printf("old:\n");
-        print_activation_array(list_old);
-
-        printf("intersect:\n");
-        GArray *intsect = intersect_activation_array(list_new, list_old);
-        print_activation_array(intsect);
-                           
-        printf("to deactivate:\n");
-        deactivate_list = substract_activation_array(list_old, intsect);
-        print_activation_array(deactivate_list);
-           
-        printf("to activate:\n");
-        activate_list = substract_activation_array(list_new, intsect);
-        print_activation_array(activate_list);
-
-        printf("union:\n");
-       unio = union_activation_array(list_old, list_new, intsect);
-       print_activation_array(unio);
-    }  
-    else
-    {      
-        unio = list_new;
-           
-        for(i = 0; i < unio->len; i++)
-        {
-           ActivationMapping *mapping = g_array_index(unio, 
ActivationMapping*, i);
-           mapping->activated = FALSE;
-       }
-           
-       deactivate_list = NULL;
-       activate_list = list_new;
-    }
-
-    printf("Deactivate:\n");
-       
-    if(deactivate_list != NULL)
-    {
-        for(i = 0; i < deactivate_list->len; i++)
-        {
-           ActivationMapping *mapping = g_array_index(deactivate_list, 
ActivationMapping*, i);
-                               
-           if(!deactivate(unio, mapping, interface))
-           {
-               unsigned int j;
-               printf("Deactivation failed! Doing a rollback...\n");
-               
-               for(j = 0; j < list_old->len; j++)
-               {
-                   ActivationMapping *mapping = g_array_index(unio, 
ActivationMapping*, j);
-                   
-                   if(!activate(unio, mapping, interface))
-                       printf("Rollback failed!\n");                           
    
-               }               
-               
-               return FALSE;
-           }
-       }
-    }
-       
-    printf("Activate:\n");
-               
-    for(i = 0; i < activate_list->len; i++)
-    {
-        ActivationMapping *mapping = g_array_index(activate_list, 
ActivationMapping*, i);
-           
-        if(!activate(unio, mapping, interface))
-       {
-           unsigned int j;
-           printf("Activation failed! Doing a rollback...\n");
-           
-           /* Deactivate the newly activated services */
-           for(j = 0; j < activate_list->len; j++)
-           {
-               ActivationMapping *mapping = g_array_index(activate_list, 
ActivationMapping*, j);
-           
-               if(!deactivate(unio, mapping, interface))
-                   printf("Rollback failed!\n");
-           }
-           
-           if(list_old != NULL)
-           {
-               /* Activate all services in the old configuration */
-               for(j = 0; j < list_old->len; j++)
-               {
-                   ActivationMapping *mapping = g_array_index(list_old, 
ActivationMapping*, j);
-               
-                   if(!activate(unio, mapping, interface))
-                       printf("Rollback failed!\n");
-               }
-           }
-           
-           return FALSE;
-       }
-    }
-    
-    return TRUE;
-}
-
 static int set_target_profiles(GArray *distribution_array, char *interface, 
char *profile)
 {
     unsigned int i;
@@ -323,94 +117,6 @@
     }
 }
 
-static int unlock(GArray *distribution_array, char *interface, char *profile)
-{
-    unsigned int i, running_processes = 0;
-    int exit_status = TRUE;
-    int status;
-    
-    /* For each locked machine, release the lock */
-    for(i = 0; i < distribution_array->len; i++)
-    {
-       DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
-       
-       status = exec_unlock(interface, item->target, profile);
-       
-       if(status == -1)
-       {
-           fprintf(stderr, "Error with forking unlock process!\n");
-           exit_status = FALSE;
-       }
-       else
-           running_processes++;
-    }
-    
-    /* Wait until every lock is released */
-    for(i = 0; i < running_processes; i++)
-    {
-       status = wait_to_finish(0);
-       
-       /* If a process fails, change the exit status */
-       if(status != 0)
-       {
-           g_printerr("Failed to release the lock!\n");
-           exit_status = FALSE;
-       }
-    }
-    
-    /* Return exit status */
-    return exit_status;
-}
-
-static int lock(GArray *distribution_array, char *interface, char *profile)
-{
-    unsigned int i;
-    GArray *try_array = g_array_new(FALSE, FALSE, sizeof(DistributionItem*));
-    GArray *lock_array = g_array_new(FALSE, FALSE, sizeof(DistributionItem*));
-    int exit_status = TRUE;
-    int status;
-    
-    /* For each machine acquire a lock */
-    for(i = 0; i < distribution_array->len; i++)
-    {
-       DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
-       
-       status = exec_lock(interface, item->target, profile);
-       
-       if(status == -1)
-       {
-           fprintf(stderr, "Error with forking lock process!\n");
-           exit_status = FALSE;
-       }       
-       else
-           g_array_append_val(try_array, item);
-    }    
-    
-    /* Wait until every lock is acquired */
-    for(i = 0; i < try_array->len; i++)
-    {
-       status = wait_to_finish(0);
-       
-       /* If a process fails, change the exit status */
-       if(status != 0)
-       {
-           g_printerr("Failed to acquire a lock!\n");
-           exit_status = FALSE;
-       }
-    }
-    
-    /* If a lock fails then unlock every machine that is locked */
-    if(!exit_status)
-       unlock(lock_array, interface, profile);
-    
-    /* Cleanup */
-    g_array_free(try_array, TRUE);
-    g_array_free(lock_array, TRUE);
-    
-    /* Return exit status */
-    return exit_status;
-}
-
 int main(int argc, char *argv[])
 {
     /* Declarations */
@@ -506,20 +212,20 @@
            list_old = NULL;
 
        /* Try to acquire a lock */
-       if(!lock(distribution_array, interface, profile))
+       if(!lock(interface, distribution_array, profile))
        {
            delete_distribution_array(distribution_array);
            return 1;
        }
        
        /* Execute transition */        
-       status = transition(list_new, list_old, interface);
+       status = transition(interface, list_new, list_old);
 
        /* If the transition failed, abort */
        if(!status)
        {
            /* Try to release the lock */
-           unlock(distribution_array, interface, profile);
+           unlock(interface, distribution_array, profile);
            
            delete_distribution_array(distribution_array);
            return 1;
@@ -530,7 +236,7 @@
        status = set_target_profiles(distribution_array, interface, profile);
 
        /* Try to release the lock */
-       unlock(distribution_array, interface, profile);
+       unlock(interface, distribution_array, profile);
        
        /* If setting the profiles failed, abort */
        if(!status)

Added: disnix/disnix/trunk/src/activate/transition.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/activate/transition.c       Sat Oct 16 23:07:16 
2010        (r24322)
@@ -0,0 +1,281 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#include "transition.h"
+#include <activationmapping.h>
+#include <client-interface.h>
+
+static gboolean activate(gchar *interface, GArray *union_array, 
ActivationMapping *mapping)
+{
+    /* Search for the location of the mapping in the union array */
+    gint actual_mapping_index = activation_mapping_index(union_array, mapping);
+    
+    /* Retrieve the mapping from the union array */
+    ActivationMapping *actual_mapping = g_array_index(union_array, 
ActivationMapping*, actual_mapping_index);
+    
+    /* First, activate all inter-dependency mappings */
+    if(actual_mapping->depends_on != NULL)
+    {
+       unsigned int i;
+       
+       for(i = 0; i < actual_mapping->depends_on->len; i++)
+       {
+           Dependency *dependency = g_array_index(actual_mapping->depends_on, 
Dependency*, i);
+           
+           ActivationMapping lookup;
+           lookup.service = dependency->service;
+           lookup.target = dependency->target;
+                   
+           if(!activate(interface, union_array, &lookup))
+               return FALSE; /* If the activation of an inter-dependency 
fails, abort */
+       }
+    }
+    
+    /* Finally, activate the mapping itself if it is not activated yet */
+    if(!actual_mapping->activated)
+    {
+       unsigned int i;
+       int status;
+       
+       /* Generate an array of key=value pairs from infrastructure properties 
*/
+       gchar **arguments = 
generate_activation_arguments(actual_mapping->target);
+       
+       /* Determine length of the activation arguments array */
+       unsigned int arguments_size = g_strv_length(arguments);
+       
+       /* Get the target interface property from the mapping */
+       gchar *target_interface = get_target_interface(actual_mapping);
+       
+       /* Print debug message */
+       g_print("Now activating service: %s of type: %s through: %s\n", 
actual_mapping->service, actual_mapping->type, target_interface);
+       g_print("Using arguments: ");
+       
+       for(i = 0; i < arguments_size; i++)
+           g_print("%s ", arguments[i]);    
+       
+       g_print("\n");
+
+       /* Execute the activation operation */
+       status = wait_to_finish(exec_activate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
+       
+       /* Cleanup */
+       g_strfreev(arguments);
+       
+       if(status != 0)
+           return FALSE; /* If the activation fails, abort */
+       else            
+           actual_mapping->activated = TRUE; /* Mark activation mapping as 
activated */
+    }
+    
+    return TRUE; /* The activation of the closure succeeded */
+}
+
+static gboolean deactivate(gchar *interface, GArray *union_array, 
ActivationMapping *mapping)
+{
+    /* Search for the location of the mapping in the union array */
+    gint actual_mapping_index = activation_mapping_index(union_array, mapping);
+    
+    /* Retrieve the mapping from the union array */
+    ActivationMapping *actual_mapping = g_array_index(union_array, 
ActivationMapping*, actual_mapping_index);
+    
+    /* Find all interdependent mapping on this mapping */
+    GArray *interdependent_mappings = 
find_interdependent_mappings(union_array, actual_mapping);
+    
+    /* First deactivate all mappings which have an inter-dependency on this 
mapping */
+    
+    unsigned int i;
+       
+    for(i = 0; i < interdependent_mappings->len; i++)
+    {
+        ActivationMapping *dependency_mapping = 
g_array_index(interdependent_mappings, ActivationMapping*, i);
+        if(!deactivate(interface, union_array, dependency_mapping))
+       {
+           g_array_free(interdependent_mappings, TRUE);
+           return FALSE; /* If the deactivation of an inter-dependency fails, 
abort */
+       }
+    }
+    
+    g_array_free(interdependent_mappings, TRUE);
+    
+    /* Finally deactivate the mapping itself */
+    if(actual_mapping->activated)
+    {
+       int status;
+       
+       /* Generate an array of key=value pairs from infrastructure properties 
*/
+       gchar **arguments = 
generate_activation_arguments(actual_mapping->target);
+       
+       /* Determine length of the activation arguments array */
+       unsigned int arguments_size = g_strv_length(arguments);
+       
+       /* Get the target interface property from the mapping */
+       gchar *target_interface = get_target_interface(actual_mapping);
+       
+       /* Print debug message */
+       g_print("Now deactivating service: %s of type: %s through: %s\n", 
actual_mapping->service, actual_mapping->type, target_interface);
+       g_print("Using arguments: ");
+       
+       for(i = 0; i < arguments_size; i++)
+           g_print("%s ", arguments[i]);
+       
+       g_print("\n");
+       
+       /* Execute the deactivation operation */
+       status = wait_to_finish(exec_deactivate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
+       
+       /* Cleanup */
+       g_free(arguments);
+
+       if(status != 0)
+           return FALSE; /* If the deactivation fails, abort */
+       else
+           actual_mapping->activated = FALSE; /* Mark activation mapping as 
deactivated */
+    }
+    
+    return TRUE; /* The deactivation of the closure succeeded */
+}
+
+gboolean transition(gchar *interface, GArray *new_activation_mappings, GArray 
*old_activation_mappings)
+{
+    GArray *union_array;
+    GArray *deactivation_array;
+    GArray *activation_array;
+    unsigned int i;
+    
+    /* Print configurations */
+    
+    g_print("New configuration mappings:\n");
+    print_activation_array(new_activation_mappings);
+    
+    if(old_activation_mappings != NULL)
+    {
+       GArray *intersection_array;
+       
+        g_print("Old configuration mappings:\n");
+        print_activation_array(old_activation_mappings);
+
+        g_print("Intersection between old and new mappings:\n");
+        intersection_array = 
intersect_activation_array(new_activation_mappings, old_activation_mappings);
+        print_activation_array(intersection_array);
+                           
+        g_print("Mapping closures to deactivate:\n");
+        deactivation_array = 
substract_activation_array(old_activation_mappings, intersection_array);
+        print_activation_array(deactivation_array);
+           
+        g_print("Mapping closures to activate:\n");
+        activation_array = substract_activation_array(new_activation_mappings, 
intersection_array);
+        print_activation_array(activation_array);
+
+        g_print("Union of old and new configuration mappings:\n");
+       union_array = union_activation_array(old_activation_mappings, 
new_activation_mappings, intersection_array);
+       print_activation_array(union_array);
+       
+       g_array_free(intersection_array, TRUE);
+    }  
+    else
+    {
+        union_array = new_activation_mappings;
+           
+        for(i = 0; i < union_array->len; i++)
+        {
+           ActivationMapping *mapping = g_array_index(union_array, 
ActivationMapping*, i);
+           mapping->activated = FALSE;
+       }
+           
+       deactivation_array = NULL;
+       activation_array = new_activation_mappings;
+    }
+
+    /* Execute deactivation process */
+    
+    g_print("Executing deactivation:\n");
+
+    /* Deactivate each mapping closure that is not in the new configuration */
+    
+    if(deactivation_array != NULL)
+    {  
+        for(i = 0; i < deactivation_array->len; i++)
+        {
+           ActivationMapping *mapping = g_array_index(deactivation_array, 
ActivationMapping*, i);
+                               
+           if(!deactivate(interface, union_array, mapping))
+           {
+               /* If the deactivation fails, perform a rollback */
+               
+               unsigned int j;
+               g_print("Deactivation failed! Doing a rollback...\n");
+               
+               for(j = 0; j < old_activation_mappings->len; j++)
+               {
+                   ActivationMapping *mapping = g_array_index(union_array, 
ActivationMapping*, j);
+                   
+                   if(!activate(interface, union_array, mapping))
+                       g_print("Rollback failed!\n");                          
    
+               }               
+               
+               return FALSE;
+           }
+       }
+    }
+
+    /* Execute activation process */
+    
+    g_print("Executing activation:\n");
+
+    /* Activate each mapping closure that is new in the new configuration */
+    
+    for(i = 0; i < activation_array->len; i++)
+    {
+        ActivationMapping *mapping = g_array_index(activation_array, 
ActivationMapping*, i);
+           
+        if(!activate(interface, union_array, mapping))
+       {
+           /* If the activation fails, perform a rollback */
+           
+           unsigned int j;
+           g_print("Activation failed! Doing a rollback...\n");
+           
+           /* Deactivate the newly activated services */
+           for(j = 0; j < activation_array->len; j++)
+           {
+               ActivationMapping *mapping = g_array_index(activation_array, 
ActivationMapping*, j);
+           
+               if(!deactivate(interface, union_array, mapping))
+                   g_print("Rollback failed!\n");
+           }
+           
+           if(old_activation_mappings != NULL)
+           {
+               /* Activate all services in the old configuration */
+               for(j = 0; j < old_activation_mappings->len; j++)
+               {
+                   ActivationMapping *mapping = 
g_array_index(old_activation_mappings, ActivationMapping*, j);
+               
+                   if(!activate(interface, union_array, mapping))
+                       g_print("Rollback failed!\n");
+               }
+           }
+           
+           return FALSE;
+       }
+    }
+    
+    /* Transition process succeeded */    
+    return TRUE;
+}

Added: disnix/disnix/trunk/src/activate/transition.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/activate/transition.h       Sat Oct 16 23:07:16 
2010        (r24322)
@@ -0,0 +1,35 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#ifndef __TRANSITION_H
+#define __TRANSITION_H
+#include <glib.h>
+
+/**
+ * Performs the transition phase, in which obsolete services are deactivated 
and
+ * new services are activated.
+ *
+ * @param interface Path to the client interface executable
+ * @param new_activation_mappings Array containing the activation mappings of 
the new configuration
+ * @param old_activation_mappings Array containing the activation mappings of 
the old configuration
+ * @return TRUE if the phase succeeds, else FALSE
+ */
+gboolean transition(gchar *interface, GArray *new_activation_mappings, GArray 
*old_activation_mappings);
+
+#endif
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to