watch_events_internal.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding Library.
00003  Copyright (C) 2007  Daniele Rondina aka ge@@ru, 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:  watch_events_internal.c
00020  
00021  Description:  
00022  
00023  Version:  1.0
00024  Created:  09/18/07 11:33:33 CEST
00025  Revision:  none
00026  
00027  Author:   Daniele Rondina aka Ge@@ru (geaaru@gmail.com) 
00028  License:  GPL 2.0
00029 */
00030 
00035 #include <string.h>
00036 #include <stdlib.h>
00037 // easydbus includes
00038 #include "debug.h"
00039 #include "easydbus-core.h"
00040 #include "reply_internal.h"
00041 #include "monitor_internal.h"
00042 
00054 inline int
00055 easydbus_send_method_withReply_Blocking (struct EasyDbus_core *core,
00056                                          DBusMessage * msg, 
00057                                          struct EasyDbus_method *method, 
00058                                          int timeout) 
00059 {
00060    int ret_val = 0;
00061    DBusError err;
00062 
00063    dbus_connection_flush (core->conn);
00064    dbus_error_init (&err);
00065 
00066    DBusMessage *reply =
00067       dbus_connection_send_with_reply_and_block (core->conn,
00068                                                  msg, timeout, &err);
00069 
00070    dbus_connection_flush (core->conn);
00071 
00072    if (dbus_error_is_set (&err)) {
00073       EasyDbusDebug ("Received error reply: %s\n", err.message);
00074       easydbus_reply_build_error (method, err.message);
00075       ret_val = -1;
00076       goto exit_;
00077    }
00078 
00079    EasyDbusDebug ("REPLY: %s Message: member = %s\n"
00080                   "interface = %s\n"
00081                   "path = %s\n",
00082                   dbus_message_type_to_string (dbus_message_get_type
00083                                                (reply)),
00084                   dbus_message_get_member (reply),
00085                   dbus_message_get_interface (reply),
00086                   dbus_message_get_path (reply));
00087 
00088    if (easydbus_reply_build_struct (method, reply)) {
00089       ret_val = -1;
00090       goto exit_;
00091    }
00092 
00093    dbus_error_free (&err);
00094 
00095  exit_:
00096    /* see if is needed */
00097    if (reply != NULL)
00098       dbus_message_unref (reply);
00099    dbus_error_free (&err);
00100    return ret_val;
00101 }
00102 
00103 
00113 dbus_bool_t 
00114 easydbus_add_watch_callback (DBusWatch * watch, 
00115                              void *data) 
00116 {
00117    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00118    struct EasyDbus_watcher *watcher = NULL;
00119    int flags = 0, fd = 0;
00120 
00121    flags = dbus_watch_get_flags (watch);
00122 #if defined(__DBUS_VERSION__) && (__DBUS_VERSION__ <= 102)
00123    fd = dbus_watch_get_fd (watch);
00124 #else
00125    fd = dbus_watch_get_unix_fd (watch);
00126 #endif
00127 
00128    watcher = easydbus_watcher_get_from_watch (core->watcher,
00129                                               watch);
00130    if (watcher) {
00131       // if watcher is already on list
00132       EasyDbusDebug ("Enable already create watcher (%p %s) "
00133                      "with fd %d",
00134                      watch, 
00135                      (flags & DBUS_WATCH_READABLE ? "readable" :
00136                       ((flags & DBUS_WATCH_WRITABLE) ? "writable" : 
00137                         "error" )), fd);
00138       
00139       core->user_data.watcher_add_cb (watcher, 
00140                                       core->user_data.watcher_closure);
00141    } else {
00142       dbus_watch_set_data (watch, data, NULL);
00143       watcher = easydbus_watcher_create_skeleton ();
00144       // see how manage this exception
00145       if (watcher == NULL)
00146          return FALSE;
00147 
00148       watcher->watch = watch;
00149       easydbus_watcher_add (core, watcher);
00150 
00151       EasyDbusDebug ("Add %s watcher (%p %s) with fd %d", 
00152                      (flags & DBUS_WATCH_READABLE ? "readable" :
00153                       ((flags & DBUS_WATCH_WRITABLE) ? "writable" : 
00154                         "error" )), watch, 
00155                      dbus_watch_get_enabled (watch) ? "enable" : "not enable",
00156                      fd);
00157    }
00158    return TRUE;
00159 }
00160 
00167 void 
00168 easydbus_remove_watch_callback (DBusWatch * watch, 
00169                                 void *data) 
00170 {
00171    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00172    int flags = 0;
00173    
00174    flags = dbus_watch_get_flags (watch);
00175 
00176    if (easydbus_watcher_remove_dbus_watch (core, watch))
00177      EasyDbusDebug ("Error on remove watcher (%s) with fd "
00178                     "%d or already removed.",
00179                     (flags & DBUS_WATCH_READABLE ? "readable" :
00180                      ((flags & DBUS_WATCH_WRITABLE) ? "writable" : 
00181                        "error" )), 
00182 #if defined(__DBUS_VERSION__) && (__DBUS_VERSION__ <= 102)
00183                     dbus_watch_get_fd (watch));
00184 #else
00185                     dbus_watch_get_unix_fd (watch));
00186 #endif
00187    else
00188     EasyDbusDebug ("Remove Watcher (%s) with fd = %d",
00189                    (flags & DBUS_WATCH_READABLE ? "readable" :
00190                     ((flags & DBUS_WATCH_WRITABLE) ? "writable" : 
00191                       "error" )),
00192 #if defined(__DBUS_VERSION__) && (__DBUS_VERSION__ <= 102)
00193                    dbus_watch_get_fd (watch));
00194 #else
00195                   dbus_watch_get_unix_fd(watch));
00196 #endif
00197 }
00198 
00207 void 
00208 easydbus_toggle_watch_callback (DBusWatch * watch, 
00209                                 void *data) 
00210 {
00211    EasyDbusDebug ("Toggle callback");
00212    easydbus_remove_watch_callback (watch, data);
00213 }
00214 
00215 #ifdef EASYDBUS_TESTING
00216 
00225 void 
00226 easydbus_dispatch_status_callback (DBusConnection * conn, 
00227                                    DBusDispatchStatus status, 
00228                                    void *data) 
00229 {
00230    switch (status) {
00231       case DBUS_DISPATCH_COMPLETE:
00232          EasyDbusDebug ("Complete dispatch status");
00233          break;
00234       case DBUS_DISPATCH_DATA_REMAINS:
00235          EasyDbusDebug ("Complete data remains");
00236          break;
00237       case DBUS_DISPATCH_NEED_MEMORY:
00238       default:
00239          break;
00240    }
00241 }
00242 
00250 dbus_bool_t
00251 easydbus_add_timeout (DBusTimeout * timeout, void *data)
00252 {
00253    //struct EasyDbus_core *core =
00254    // (struct EasyDbus_core *) data;
00255 
00256    if (!dbus_timeout_get_enabled (timeout)) {
00257       EasyDbusDebug ("timeout isn't enable");
00258       return TRUE;
00259    }
00260    EasyDbusDebug ("timeout added");
00261    return TRUE;
00262 }
00263 
00271 void
00272 easydbus_remove_timeout (DBusTimeout * timeout, void *data)
00273 {
00274    EasyDbusDebug ("remove timeout");
00275 }
00276 
00284 void
00285 easydbus_toggle_timeout (DBusTimeout * timeout, void *data)
00286 {
00287    EasyDbusDebug ("toggle timeout");
00288 }
00289 
00297 void
00298 easydbus_wakeup_main_callback (void *data)
00299 {
00300 
00301    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00302 
00303    if (core == NULL) {
00304       EasyDbusDebug ("Error data == NULL");
00305       return;
00306    }
00307    EasyDbusDebug ("i'm here");
00308 }
00309 #endif
00310 
00311 // vim: ts=3 shiftwidth=3 expandtab

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