acquire_name.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding Library.
00003  Copyright (C) 2007  Daniele Rondina aka Ge@@eu, geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019  Filename:  acquire_name.c
00020  
00021  Description:  
00022  
00023  Version:  1.0
00024  Created:  09/25/07 15:31:55 CEST
00025  Revision:  
00026  `0 - created (Ge@@ru)
00027   1 - Added doxygen comments - 10/23/07 (Ge@@ru)
00028  
00029  Author:   Daniele Rondina aka Ge@@ru , geaaru@gmail.com 
00030  License:  GPL 2.0
00031 */
00032 
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <stdio.h>
00040 // easydbus includes
00041 #include "easydbus-core.h"
00042 #include "acquire_internal.h"
00043 #include "debug.h"
00044 #include "utils.h"
00045 
00054 inline int
00055 easydbus_set_request_flags (int easydbus_flags)
00056 {
00057    int dbus_flags = 0;
00058 
00059    if (easydbus_flags & EASYDBUS_CONN_ALLOW_REPLACEMENT) {
00060       EasyDbusDebug ("Set ALLOW_REPLACEMENT flag");
00061       dbus_flags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
00062    }
00063    if (easydbus_flags & EASYDBUS_CONN_REPLACE_EXISTING) {
00064       EasyDbusDebug ("Set REPLACE_EXISTING flag");
00065       dbus_flags |= DBUS_NAME_FLAG_REPLACE_EXISTING;
00066    }
00067    if (easydbus_flags & EASYDBUS_CONN_DO_NOT_QUEUE) {
00068       EasyDbusDebug ("Set DO_NOT_QUEUE flag");
00069       dbus_flags |= DBUS_NAME_FLAG_DO_NOT_QUEUE;
00070    }
00071 
00072    return dbus_flags;
00073 }
00074 
00089 inline int
00090 easydbus_acquire_name (struct EasyDbus_core *core,
00091                        char *serviceName, int flags, 
00092                        DBusError * err)
00093 {
00094    int result = -1;
00095    int dbus_flags = 0;
00096 
00097    EasyDbusDebug ("Request name");
00098 
00099    dbus_flags = easydbus_set_request_flags (flags);
00100 
00101    /* TODO: manage return values correctly */
00102    /* request for set service name */
00103    result = dbus_bus_request_name
00104       (core->conn, serviceName, dbus_flags, err);
00105 
00106    if (dbus_error_is_set (err)) {
00107       EasyDbusDebug ("Error %s from "
00108                      "dbus_bus_request_name: %s",
00109                      err->name, err->message);
00110       return -1;
00111    }
00112 
00113    switch (result) {
00114       case DBUS_REQUEST_NAME_REPLY_EXISTS:
00115          EasyDbusDebug ("Owner already exist");
00116          result = EASYDBUS_CONN_NAME_SERVICE_EXISTS;
00117          break;
00118       case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
00119          EasyDbusDebug ("Service is primary onwner");
00120          result = EASYDBUS_CONN_NAME_SERVICE_PRIMARY;
00121          break;
00122       case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
00123          EasyDbusDebug ("Service in queue");
00124          result = EASYDBUS_CONN_NAME_SERVICE_IN_QUEUE;
00125          break;
00126       case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
00127          EasyDbusDebug ("Service already owner");
00128          result = EASYDBUS_CONN_NAME_SERVICE_PRIMARY;
00129          break;
00130       default:
00131          EasyDbusDebug ("Error! Returned value not managed");
00132          return -1;
00133          break;
00134    }
00135 
00136    return result;
00137 }
00138 
00151 char *
00152 easydbus_found_new_name (struct EasyDbus_core *core,
00153                          char *oldName, int flags, 
00154                          DBusError * err)
00155 {
00156    char *service_name = NULL;
00157    int string_length = 0, string_length_max = 0;
00158    int i, result = -1;
00159    char buffer[5];
00160 
00161    // 1 --> for \0 and 3 --> for 3 char number.
00162    string_length_max = strlen (oldName) + 1 + 3;
00163    string_length = strlen (oldName) + 1;
00164    service_name = (char *) malloc (string_length_max);
00165    if (!service_name)
00166       return NULL;
00167    memset (service_name, 0, string_length_max);
00168    memcpy (service_name, oldName, string_length);
00169 
00170    EasyDbusDebug ("Check %s service name", oldName);
00171    for (i = 1; i < EASYDBUS_RENAMED_MAX_TEST; i++) {
00172       memset (buffer, 0, 5);
00173       snprintf (buffer, 5, "%d", i);
00174       memcpy (&service_name[string_length - 1],
00175               buffer, strlen (buffer) + 1);
00176       EasyDbusDebug ("Check %s service name", service_name);
00177       if (!dbus_bus_name_has_owner (core->conn, service_name, err)) {
00178          result = easydbus_acquire_name (core, service_name, 
00179                                          flags, err);
00180          if (result == -1)
00181             goto error;
00182          return service_name;
00183       }
00184    }
00185 
00186  error:
00187 
00188    if (service_name != NULL)
00189       free (service_name);
00190 
00191    return NULL;
00192 }
00193 
00194 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:17 2008 for EasyDbus-0.2 by  doxygen 1.5.4