Author: sandervanderburg
Date: Sat Oct 16 12:43:25 2010
New Revision: 24316
URL: https://svn.nixos.org/websvn/nix/?rev=24316&sc=1

Log:
Separated disnix-client interface and operation

Added:
   disnix/disnix/trunk/src/dbus-service/disnix-client-operation.c
   disnix/disnix/trunk/src/dbus-service/disnix-client-operation.h
   disnix/disnix/trunk/src/dbus-service/operation.h
Replaced:
   disnix/disnix/trunk/src/dbus-service/disnix-client.c
Modified:
   disnix/disnix/trunk/src/dbus-service/Makefile.am
   disnix/disnix/trunk/src/dbus-service/disnix-service-main.c

Modified: disnix/disnix/trunk/src/dbus-service/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/dbus-service/Makefile.am    Sat Oct 16 12:13:54 
2010        (r24315)
+++ disnix/disnix/trunk/src/dbus-service/Makefile.am    Sat Oct 16 12:43:25 
2010        (r24316)
@@ -1,11 +1,11 @@
 AM_CPPFLAGS=-ggdb -DLOCALSTATEDIR=\"$(localstatedir)\"
 
 bin_PROGRAMS = disnix-service disnix-client
-noinst_HEADERS = methods.h signals.h disnix-gtype-def.h disnix-instance.h
+noinst_HEADERS = methods.h signals.h disnix-gtype-def.h disnix-instance.h 
operation.h disnix-client-operation.h
 
 disnix_service_SOURCES = methods.c signals.c disnix-service.c 
disnix-service-main.c 
 
-disnix_client_SOURCES = disnix-marshal.c disnix-client.c
+disnix_client_SOURCES = disnix-marshal.c disnix-client-operation.c 
disnix-client.c
 
 BUILT_SOURCES = disnix-service.h disnix-client.h disnix-marshal.h 
disnix-marshal.c
 

Added: disnix/disnix/trunk/src/dbus-service/disnix-client-operation.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/dbus-service/disnix-client-operation.c      Sat Oct 
16 12:43:25 2010        (r24316)
@@ -0,0 +1,259 @@
+/*
+ * 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 "disnix-client-operation.h"
+#include <stdlib.h>
+#include "disnix-client.h"
+#include "disnix-marshal.h"
+
+/* Signal handlers */
+
+static void disnix_finish_signal_handler(DBusGProxy *proxy, const gint pid, 
gpointer user_data)
+{
+    gint my_pid = *((gint*)user_data);
+
+    g_printerr("Received finish signal from pid: %d\n", pid);
+
+    /* Stop the main loop if our job is done */
+    if(pid == my_pid)
+       exit(0);    
+}
+
+static void disnix_success_signal_handler(DBusGProxy *proxy, const gint pid, 
gchar **derivation, gpointer user_data)
+{
+    unsigned int i;
+    gint my_pid = *((gint*)user_data);
+    
+    g_printerr("Received success signal from pid: %d\n", pid);
+    
+    for(i = 0; i < g_strv_length(derivation); i++)
+       g_print("%s", derivation[i]);
+       
+    /* Stop the main loop if our job is done */
+    if(pid == my_pid)
+       exit(0);
+}
+
+static void disnix_failure_signal_handler(DBusGProxy *proxy, const gint pid, 
gpointer user_data)
+{
+    gint my_pid = *((gint*)user_data);
+    
+    g_printerr("Received failure signal from pid: %d\n", pid);
+    
+    /* Stop the main loop if our job is done */
+    if(pid == my_pid)
+       exit(1);
+}
+
+int run_disnix_client(Operation operation, gchar **derivation, gboolean 
session_bus, char *profile, gboolean delete_old, gchar **arguments, char *type)
+{
+    /* The GObject representing a D-Bus connection. */
+    DBusGConnection *bus;
+    
+    /* Proxy object representing the D-Bus service object. */
+    DBusGProxy *remote_object;
+
+    /* GMainLoop object */
+    GMainLoop *mainloop;
+    
+    /* Captures the results of D-Bus operations */
+    GError *error = NULL;    
+    gint reply;
+    
+    /* Other declarations */
+    gint pid;
+    
+    /* If no operation is specified we should quit */
+    if(operation == OP_NONE)
+    {
+       g_printerr("No operation is specified!\n");
+       return 1;
+    }
+    
+    /* Initialize the GType/GObject system. */
+    g_type_init();
+
+    /* Create main loop */
+    mainloop = g_main_loop_new(NULL, FALSE);
+    if(mainloop == NULL)
+    {
+       g_printerr("Cannot create main loop.\n");
+       g_strfreev(derivation);
+       g_strfreev(arguments);
+       return 1;
+    }
+
+    /* Connect to the session/system bus */
+    
+    if(session_bus)
+    {
+       g_printerr("Connecting to the session bus.\n");
+       bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+    }
+    else
+    {
+       g_printerr("Connecting to the system bus.\n");
+       bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+    }
+    
+    if(!bus)
+    {
+        g_printerr("Cannot connect to session/system bus! Reason: %s\n", 
error->message);
+       g_strfreev(derivation);
+       g_strfreev(arguments);
+        return 1;
+    }
+    
+    /* Create the proxy object that will be used to access the object */
+    
+    g_printerr("Creating a Glib proxy object.\n");
+    remote_object = dbus_g_proxy_new_for_name (bus,
+                                              "org.nixos.disnix.Disnix", /* 
name */
+                                              "/org/nixos/disnix/Disnix", /* 
Object path */
+                                              "org.nixos.disnix.Disnix"); /* 
Interface */
+    if(remote_object == NULL)
+    {
+        g_printerr("Cannot create the proxy object! Reason: %s\n", 
error->message);
+       g_strfreev(derivation);
+       g_strfreev(arguments);
+        return 1;
+    }
+
+    /* Register marshaller for the return values of the success signal */
+    dbus_g_object_register_marshaller(marshal_VOID__INT_BOXED, G_TYPE_NONE, 
G_TYPE_INT, G_TYPE_STRV, G_TYPE_INVALID);
+    
+    /* Register the signatures for the signal handlers */
+
+    g_printerr("Add the argument signatures for the signal handler\n");
+    dbus_g_proxy_add_signal(remote_object, "finish", G_TYPE_INT, 
G_TYPE_INVALID);
+    dbus_g_proxy_add_signal(remote_object, "success", G_TYPE_INT, G_TYPE_STRV, 
G_TYPE_INVALID);
+    dbus_g_proxy_add_signal(remote_object, "failure", G_TYPE_INT, 
G_TYPE_INVALID);
+
+    g_printerr("Register D-Bus signal handlers\n");
+    dbus_g_proxy_connect_signal(remote_object, "finish", 
G_CALLBACK(disnix_finish_signal_handler), &pid, NULL);
+    dbus_g_proxy_connect_signal(remote_object, "success", 
G_CALLBACK(disnix_success_signal_handler), &pid, NULL);
+    dbus_g_proxy_connect_signal(remote_object, "failure", 
G_CALLBACK(disnix_failure_signal_handler), &pid, NULL);
+
+    /* Receive a PID for the job we want to execute */
+    org_nixos_disnix_Disnix_get_job_id(remote_object, &pid, &error);
+    g_printerr("Assigned PID: %d\n", pid);
+
+    /* Execute operation */
+    g_printerr("Executing operation.\n");
+    
+    switch(operation)
+    {
+       case OP_IMPORT:
+           if(derivation[0] == NULL)
+           {
+               g_printerr("ERROR: A Nix store component has to be 
specified!\n");
+               g_strfreev(derivation);
+               g_strfreev(arguments);
+               return 1;
+           }
+           else
+               org_nixos_disnix_Disnix_import(remote_object, pid, 
derivation[0], &error);
+           break;
+       case OP_EXPORT:
+           org_nixos_disnix_Disnix_export(remote_object, pid, (const gchar**) 
derivation, &error);
+           break;
+       case OP_PRINT_INVALID:
+           org_nixos_disnix_Disnix_print_invalid(remote_object, pid, (const 
gchar**) derivation, &error);
+           break;
+       case OP_REALISE:
+           org_nixos_disnix_Disnix_realise(remote_object, pid, (const gchar**) 
derivation, &error);
+           break;
+       case OP_SET:
+           org_nixos_disnix_Disnix_set(remote_object, pid, profile, 
derivation[0], &error);
+           break;
+       case OP_QUERY_INSTALLED:
+           org_nixos_disnix_Disnix_query_installed(remote_object, pid, 
profile, &error);
+           break;
+       case OP_QUERY_REQUISITES:
+           org_nixos_disnix_Disnix_query_requisites(remote_object, pid, (const 
gchar**) derivation, &error);
+           break;
+       case OP_COLLECT_GARBAGE:
+           org_nixos_disnix_Disnix_collect_garbage(remote_object, pid, 
delete_old, &error);
+           break;
+       case OP_ACTIVATE:
+           if(type == NULL)
+           {
+               g_printerr("ERROR: A type must be specified!\n");
+               g_strfreev(derivation);
+               g_strfreev(arguments);
+               return 1;
+           }
+           else if(derivation[0] == NULL)
+           {
+               g_printerr("ERROR: A Nix store component has to be 
specified!\n");
+               g_strfreev(derivation);
+               g_strfreev(arguments);
+               return 1;
+           }
+           else
+               org_nixos_disnix_Disnix_activate(remote_object, pid, 
derivation[0], type, (const gchar**) arguments, &error);
+           break;
+       case OP_DEACTIVATE:
+           if(type == NULL)
+           {
+               g_printerr("ERROR: A type must be specified!\n");
+               g_strfreev(derivation);
+               g_strfreev(arguments);
+               return 1;
+           }
+           else if(derivation[0] == NULL)
+           {
+               g_printerr("ERROR: A Nix store component has to be 
specified!\n");
+               g_strfreev(derivation);
+               g_strfreev(arguments);
+               return 1;
+           }
+           else
+               org_nixos_disnix_Disnix_deactivate(remote_object, pid, 
derivation[0], type, (const gchar**) arguments, &error);
+           break;
+       case OP_LOCK:
+           org_nixos_disnix_Disnix_lock(remote_object, pid, profile, &error);
+           break;
+       case OP_UNLOCK:
+           org_nixos_disnix_Disnix_unlock(remote_object, pid, profile, &error);
+           break;
+       case OP_NONE:
+           g_printerr("ERROR: No operation specified!\n");         
+           g_strfreev(derivation);
+           g_strfreev(arguments);
+           return 1;
+    }
+
+    if(error != NULL) 
+    {
+        g_printerr("Error while executing the operation! Reason: %s\n", 
error->message);
+       g_strfreev(derivation);
+       g_strfreev(arguments);
+       return 1;
+    }
+    
+    /* Run loop and wait for signals */
+    g_main_loop_run(mainloop);
+    
+    /* Operation is finished */
+    g_strfreev(derivation);
+    g_strfreev(arguments);
+    
+    return EXIT_FAILURE;
+}

Added: disnix/disnix/trunk/src/dbus-service/disnix-client-operation.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/dbus-service/disnix-client-operation.h      Sat Oct 
16 12:43:25 2010        (r24316)
@@ -0,0 +1,40 @@
+/*
+ * 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 __DISNIX_CLIENT_OPERATION_H
+#define __DISNIX_CLIENT_OPERATION_H
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include "operation.h"
+
+/**
+ * Runs the Disnix client
+ *
+ * @param operation Operation number to execute
+ * @param derivation List of paths to Nix store components
+ * @param session_bus Indicates whether to use the session bus of D-Bus
+ * @param profile Identifier of a distributed profile
+ * @param delete_old Indicates whether to delete old profile generations
+ * @param arguments List of activation arguments in key=value format
+ * @param type Type of the service
+ * @return 0 if the operation succeeds, else a non-zero exit value
+ */
+int run_disnix_client(Operation operation, gchar **derivation, gboolean 
session_bus, char *profile, gboolean delete_old, gchar **arguments, char *type);
+
+#endif

Added: disnix/disnix/trunk/src/dbus-service/disnix-client.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/dbus-service/disnix-client.c        Sat Oct 16 
12:43:25 2010        (r24316)
@@ -0,0 +1,180 @@
+/*
+ * 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 <stdio.h>
+#define _GNU_SOURCE
+#include <getopt.h>
+#include <glib.h>
+#include "operation.h"
+#include "disnix-client-operation.h"
+
+static void print_usage()
+{
+    /* Print the usage */
+    fprintf(stderr, "Usage:\n");
+    fprintf(stderr, "disnix-client [--session-bus] operation\n\n");
+    
+    fprintf(stderr, "Operations:\n");
+    fprintf(stderr, "--import [--localfile|--remotefile] derivations\n");
+    fprintf(stderr, "--export [--localfile|--remotefile] derivations\n");
+    fprintf(stderr, "--print-invalid derivations\n");
+    fprintf(stderr, "{-r|--realise} derivations\n");
+    fprintf(stderr, "--set [{-p|--profile} name] derivation\n");
+    fprintf(stderr, "{-q|--query-installed} [{-p|--profile} name]\n");
+    fprintf(stderr, "--query-requisites derivations\n");
+    fprintf(stderr, "--collect-garbage [{-d|--delete-old}]\n");
+    fprintf(stderr, "--activate --type type --arguments arguments 
derivation\n");
+    fprintf(stderr, "--deactivate --type type --arguments arguments 
derivation\n");
+    fprintf(stderr, "--lock [{-p|--profile} profile]\n");
+    fprintf(stderr, "--unlock [{-p|--profile} profile]\n");
+    fprintf(stderr, "{-h|--help}\n");
+}
+
+int main(int argc, char *argv[])
+{
+    /* Declarations */
+    int c, option_index = 0;
+    struct option long_options[] =
+    {
+       {"import", no_argument, 0, 'I'},
+       {"export", no_argument, 0, 'E'},
+       {"print-invalid", no_argument, 0, 'P'},
+       {"realise", no_argument, 0, 'r'},
+       {"set", no_argument, 0, 'S'},
+       {"query-installed", no_argument, 0, 'q'},
+       {"query-requisites", no_argument, 0, 'Q'},
+       {"collect-garbage", no_argument, 0, 'C'},
+       {"activate", no_argument, 0, 'A'},
+       {"deactivate", no_argument, 0, 'D'},
+       {"lock", no_argument, 0, 'L'},
+       {"unlock", no_argument, 0, 'U'},
+       {"help", no_argument, 0, 'h'},
+       {"target", required_argument, 0, 't'},
+       {"localfile", no_argument, 0, 'l'},
+       {"remotefile", no_argument, 0, 'R'},
+       {"profile", required_argument, 0, 'p'},
+       {"delete-old", no_argument, 0, 'd'},
+       {"type", required_argument, 0, 'T'},
+       {"arguments", required_argument, 0, 'a'},
+       {"session-bus", no_argument, 0, 'b'},
+       {0, 0, 0, 0}
+    };
+
+    /* Option value declarations */
+    Operation operation = OP_NONE;
+    char *target, *profile = "default", *type = NULL;
+    gchar **derivation = NULL, **arguments = NULL;
+    unsigned int derivation_size = 0, arguments_size = 0;
+    int localfile = FALSE, remotefile = TRUE;
+    int delete_old = FALSE, session_bus = FALSE;
+    
+    /* Parse command-line options */
+    while((c = getopt_long(argc, argv, "rqt:p:dh", long_options, 
&option_index)) != -1)
+    {
+       switch(c)
+       {           
+           case 'I':
+               operation = OP_IMPORT;
+               break;
+           case 'E':
+               operation = OP_EXPORT;          
+               break;
+           case 'P':
+               operation = OP_PRINT_INVALID;           
+               break;
+           case 'r':
+               operation = OP_REALISE;
+               break;
+           case 'S':
+               operation = OP_SET;
+               break;
+           case 'q':
+               operation = OP_QUERY_INSTALLED;
+               break;
+           case 'Q':
+               operation = OP_QUERY_REQUISITES;
+               break;
+           case 'C':
+               operation = OP_COLLECT_GARBAGE;
+               break;
+           case 'A':
+               operation = OP_ACTIVATE;
+               break;
+           case 'D':
+               operation = OP_DEACTIVATE;
+               break;
+           case 'L':
+               operation = OP_LOCK;
+               break;
+           case 'U':
+               operation = OP_UNLOCK;
+               break;
+           case 't':
+               target = optarg;
+               break;
+           case 'l':
+               localfile = TRUE;
+               remotefile = FALSE;
+               break;
+           case 'R':
+               localfile = FALSE;
+               remotefile = TRUE;
+               break;
+           case 'p':
+               profile = optarg;
+               break;
+           case 'd':
+               delete_old = TRUE;
+               break;
+           case 'T':
+               type = optarg;
+               break;
+           case 'a':
+               arguments = (gchar**)g_realloc(arguments, (arguments_size + 1) 
* sizeof(gchar*));
+               arguments[arguments_size] = g_strdup(optarg);
+               arguments_size++;
+               break;
+           case 'b':
+               session_bus = TRUE;
+               break;
+           case 'h':
+               print_usage();
+               return 0;
+       }
+    }
+    
+    /* Validate non-options */
+    while(optind < argc)
+    {
+       derivation = g_realloc(derivation, (derivation_size + 1) * 
sizeof(gchar*));
+       derivation[derivation_size] = g_strdup(argv[optind]);
+       derivation_size++;      
+       optind++;
+    }
+    
+    derivation = g_realloc(derivation, (derivation_size + 1) * sizeof(gchar*));
+    derivation[derivation_size] = NULL;
+    
+    /* Add NULL termination to the arguments vector */
+    arguments = g_realloc(arguments, (arguments_size + 1) * sizeof(gchar*));
+    arguments[arguments_size] = NULL;
+    
+    /* Execute Disnix client */
+    return run_disnix_client(operation, derivation, session_bus, profile, 
delete_old, arguments, type);
+}

Modified: disnix/disnix/trunk/src/dbus-service/disnix-service-main.c
==============================================================================
--- disnix/disnix/trunk/src/dbus-service/disnix-service-main.c  Sat Oct 16 
12:13:54 2010        (r24315)
+++ disnix/disnix/trunk/src/dbus-service/disnix-service-main.c  Sat Oct 16 
12:43:25 2010        (r24316)
@@ -17,21 +17,21 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
  */
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <getopt.h>
+#define _GNU_SOURCE
 #define TRUE 1
 #define FALSE 0
 
 static void print_usage()
 {
-    printf("Usage:\n");
-    printf("disnix-service [options]\n\n");
+    fprintf(stderr, "Usage:\n");
+    fprintf(stderr, "disnix-service [options]\n\n");
     
-    printf("Options:\n");
-    printf("--activation-modules-dir  Directory where the activation modules 
can be found\n");
-    printf("--session-bus             Register the Disnix service on the 
session bus instead of the system bus (useful for testing)\n");
-    printf("--help                    Shows the usage of this command to the 
user\n");
+    fprintf(stderr, "Options:\n");
+    fprintf(stderr, "--activation-modules-dir  Directory where the activation 
modules can be found\n");
+    fprintf(stderr, "--session-bus             Register the Disnix service on 
the session bus instead of the system bus (useful for testing)\n");
+    fprintf(stderr, "--help                    Shows the usage of this command 
to the user\n");
 }
 
 int main(int argc, char *argv[])

Added: disnix/disnix/trunk/src/dbus-service/operation.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/dbus-service/operation.h    Sat Oct 16 12:43:25 
2010        (r24316)
@@ -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 __OPERATION_H
+#define __OPERATION_H
+
+/**
+ * Enumeration of possible Disnix service operations
+ */
+
+typedef enum
+{
+    OP_NONE,
+    OP_IMPORT,
+    OP_EXPORT,
+    OP_PRINT_INVALID,
+    OP_REALISE,
+    OP_SET,
+    OP_QUERY_INSTALLED,
+    OP_QUERY_REQUISITES,
+    OP_COLLECT_GARBAGE,
+    OP_ACTIVATE,
+    OP_DEACTIVATE,
+    OP_LOCK,
+    OP_UNLOCK,
+}
+Operation;
+
+#endif
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to