ernie.c

Go to the documentation of this file.
00001 /*
00002  * File Name:  ernie.c
00003  */
00004 
00005 /**
00006  * Copyright (C) 2009 iRex Technologies B.V.
00007  * All rights reserved.
00008  */
00009 
00010 #include "config.h"
00011 
00012 #define LOGGING_ON 1
00013 
00014 // system include files, between < >
00015 #include <glib.h>
00016 #include <gtk/gtk.h>
00017 #include <signal.h>
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 
00023 #include <liberipc/eripc.h>
00024 #include <liberipc/eripc_support.h>
00025 
00026 #include "log.h"
00027 
00028 #define DBUS_APPL_NAME       "ernie"
00029 #define DBUS_SERVICE         "com.irexnet." DBUS_APPL_NAME
00030 #define DBUS_PATH            "/com/irexnet/" DBUS_APPL_NAME
00031 #define DBUS_INTERFACE       "com.irexnet." DBUS_APPL_NAME
00032 
00033 #define DBUS_SERVICE_BERT   "com.irexnet.bert"
00034 
00035 #define MAXCOUNT 100
00036 #define DELAY_MS 100
00037 
00038 int activated_handler = -1;
00039 static eripc_client_context_t *eripcClient = NULL;
00040 
00041 
00042 static void on_ping (eripc_context_t *context, const eripc_event_info_t *info, void *user_data);
00043 static void on_pong (eripc_context_t *context, const eripc_event_info_t *info, gpointer data);
00044 
00045 static eripc_callback_function_t service_functions[] = {
00046         { on_ping,         "callernie",           NULL,             0 },
00047         { NULL }  // end of list
00048     };
00049 
00050 
00051 gboolean ipc_set_services()
00052 {
00053     eripcClient = eripc_client_context_new(
00054                     DBUS_APPL_NAME,
00055                     "1.0",
00056                     DBUS_SERVICE,
00057                     DBUS_PATH,
00058                     DBUS_INTERFACE,
00059                     service_functions);
00060 
00061     return TRUE;
00062 }
00063 
00064 
00065 void ipc_unset_services()
00066 {
00067     eripc_client_context_free(eripcClient, service_functions);
00068 }
00069 
00070 
00071 void ipc_send_ping(void *callback_handler, 
00072                    void *callback_data)
00073 {
00074     eripc_error_t retval;
00075 
00076     retval = eripc_send_varargs(eripcClient->context, 
00077                                callback_handler,
00078                                callback_data,
00079                                ERIPC_BUS_SESSION,
00080                                DBUS_SERVICE_BERT,
00081                                "callbert",
00082                                ERIPC_TYPE_STRING, "greets from ernie",
00083                                ERIPC_TYPE_INVALID);
00084     
00085     if (retval != ERIPC_ERROR_SUCCESS) 
00086     {
00087         ERRORPRINTF("Error launching eripc handler: %s", eripc_error_string(retval));
00088         return;
00089     }
00090 
00091     LOGPRINTF("Sent ping");
00092 }
00093 
00094 
00095 static void on_pong (eripc_context_t *context,
00096                     const eripc_event_info_t *info,
00097                     gpointer data)
00098 {
00099     if (info->event_type != ERIPC_EVENT_REPLY)
00100     {
00101         WARNPRINTF("invalid event: %d", info->event_type);
00102     }
00103     else
00104     {
00105         LOGPRINTF("Pong from BERT [%d]", (gint) data);
00106     }
00107 }
00108 
00109 
00110 static void on_ping (eripc_context_t *context, 
00111                     const eripc_event_info_t *info, 
00112                     void *user_data)
00113 {
00114     gint32 wait = g_random_int_range(1000, 100*1000);
00115     
00116     LOGPRINTF("Ping from BERT, return Pong in [%d] ms", wait/1000);
00117     g_usleep(wait);
00118     
00119     eripc_reply_bool(context, info->message_id, TRUE);
00120 }
00121 
00122 
00123 static gboolean on_second(gpointer data)
00124 {
00125     static gint count = 0;
00126     
00127     ipc_send_ping(on_pong, (gpointer)count++);
00128     
00129     if (count>MAXCOUNT)
00130     {
00131         gtk_main_quit();
00132         // don't call again
00133         return FALSE;
00134     }
00135     
00136     return TRUE;
00137 }
00138 
00139 
00140 int main(int argc, char *argv[])
00141 {
00142     // init gdk (for windowing)
00143     g_type_init();
00144     gtk_init(&argc, &argv);
00145     
00146     LOGPRINTF("Starting...");
00147 
00148     // prepare IPC, system (micro) and display
00149     ipc_set_services();
00150         
00151     LOGPRINTF("Running...");
00152     
00153     gint32 interval = g_random_int_range(100, 200);
00154     g_timeout_add(interval, on_second, NULL);
00155     
00156     // run the main loop
00157     LOGPRINTF("before gtk_main");
00158     gtk_main();
00159     LOGPRINTF("after gtk_main");
00160     
00161     ipc_unset_services();    
00162     
00163     LOGPRINTF("...Exit");
00164     
00165     return 0;
00166 }
Generated by  doxygen 1.6.2-20100208