client.c

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

client.c

Client to test various call message types against a provided server (server.c)

Client to test various call message types against a provided server (server.c)

//Compile with:
// gcc -o client client.c `pkg-config --cflags --libs eldbus ecore eina`
#include "Eldbus.h"
#include <Ecore.h>
#define BUS "org.Enlightenment"
#define PATH "/org/enlightenment"
#define INTERFACE "org.enlightenment.Test"
#define NTESTS 8
static int _client_log_dom = -1;
#define ERR(...) EINA_LOG_DOM_ERR(_client_log_dom, __VA_ARGS__)
static Eldbus_Connection *conn = NULL;
static Ecore_Timer *timeout = NULL;
static void
_on_alive(void *context EINA_UNUSED, const Eldbus_Message *msg EINA_UNUSED)
{
printf("Alive\n\n");
}
static void
_on_hello(void *context EINA_UNUSED, const Eldbus_Message *msg)
{
const char *txt;
if (eldbus_message_arguments_get(msg, "s", &txt))
printf("%s\n", txt);
}
#include <inttypes.h>
static struct expected
{
Eina_Bool b;
uint8_t y;
uint32_t u;
int32_t i;
int16_t n;
double d;
const char *s;
} expected = {
.b = EINA_TRUE,
.y = 0xAA,
.u = 0xFFFFFFFF,
.i = 0xFFFFFFFF,
.n = 0xFFFF,
.d = 3.1415926,
.s = "test",
};
static void
test(void)
{
static int n = 0;
n++;
if (n >= NTESTS)
{
printf("Passed in all tests\nExiting...\n");
eldbus_connection_unref(conn);
conn = NULL;
ecore_main_loop_quit();
}
else
printf("Passed in %d/%d tests\n", n, NTESTS);
}
static void
_on_send_bool(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
Eina_Bool b;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "b", &b))
{
ERR("Could not get entry contents");
return;
}
if (b != expected.b)
{
ERR("Bool value doesn't match");
return;
}
test();
}
static void
_on_send_byte(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
uint8_t y;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "y", &y))
{
ERR("Could not get entry contents");
return;
}
if (y != expected.y)
{
ERR("Byte value doesn't match expected value");
return;
}
test();
}
static void
_on_send_uint32(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
unsigned int u;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "u", &u))
{
ERR("Could not get entry contents");
return;
}
if (u != expected.u)
{
ERR("Uint32 value doesn't match expected value");
return;
}
test();
}
static void
_on_send_int32(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
int32_t i;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "i", &i))
{
ERR("Could not get entry contents");
return;
}
if (i != expected.i)
{
ERR("Int32 value doesn't match expected value");
return;
}
test();
}
static void
_on_send_int16(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
int16_t n;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "n", &n))
{
ERR("Could not get entry contents");
return;
}
if (n != expected.n)
{
ERR("Int16 value doesn't match expected value");
return;
}
test();
}
static void
_on_send_double(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
double d;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "d", &d))
{
ERR("Could not get entry contents");
return;
}
if (!EINA_DBL_EQ(d, expected.d))
{
ERR("Double value doesn't match expected value");
return;
}
test();
}
static void
_on_send_string(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
char *s;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "s", &s))
{
ERR("Could not get entry contents");
return;
}
if (strcmp(s, expected.s) != 0)
{
ERR("Uint32 value doesn't match expected value");
return;
}
test();
}
static void
_on_async_test(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname, *errmsg;
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
ERR("%s %s", errname, errmsg);
return;
}
test();
}
static void
on_name_owner_changed(void *data EINA_UNUSED, const char *bus, const char *old_id, const char *new_id EINA_UNUSED)
{
printf("Bus=%s | old=%s | new=%s\n", bus, old_id, new_id);
}
static Eina_Bool
finish(void *data EINA_UNUSED)
{
ERR("Timeout\nSome error happened or server is taking too much time to respond.");
ecore_main_loop_quit();
timeout = NULL;
return ECORE_CALLBACK_CANCEL;
}
int
main(void)
{
Eldbus_Object *obj;
Eldbus_Proxy *proxy;
eina_init();
_client_log_dom = eina_log_domain_register("client", EINA_COLOR_CYAN);
if (_client_log_dom < 0)
{
EINA_LOG_ERR("Unable to create 'client' log domain");
goto exit_eina;
}
ecore_init();
eldbus_init();
conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
obj = eldbus_object_get(conn, BUS, PATH);
proxy = eldbus_proxy_get(obj, INTERFACE);
eldbus_proxy_signal_handler_add(proxy, "Alive", _on_alive, NULL);
eldbus_proxy_signal_handler_add(proxy, "Hello", _on_hello, NULL);
eldbus_proxy_call(proxy, "SendBool", _on_send_bool, NULL, -1, "b",
expected.b);
eldbus_proxy_call(proxy, "SendByte", _on_send_byte, NULL, -1, "y",
expected.y);
eldbus_proxy_call(proxy, "SendUint32", _on_send_uint32, NULL, -1, "u",
expected.u);
eldbus_proxy_call(proxy, "SendInt32", _on_send_int32, NULL, -1, "i",
expected.i);
eldbus_proxy_call(proxy, "SendInt16", _on_send_int16, NULL, -1, "n",
expected.n);
eldbus_proxy_call(proxy, "SendDouble", _on_send_double, NULL, -1, "d",
expected.d);
eldbus_proxy_call(proxy, "SendString", _on_send_string, NULL, -1, "s",
expected.s);
eldbus_proxy_call(proxy, "AsyncTest", _on_async_test, NULL, -1, "");
eldbus_name_owner_changed_callback_add(conn, BUS, on_name_owner_changed,
conn, EINA_TRUE);
timeout = ecore_timer_add(30, finish, NULL);
ecore_main_loop_begin();
if (timeout)
ecore_timer_del(timeout);
if (conn)
eldbus_connection_unref(conn);
eldbus_shutdown();
ecore_shutdown();
eina_log_domain_unregister(_client_log_dom);
exit_eina:
eina_shutdown();
return 0;
}
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
ECORE_CALLBACK_CANCEL
#define ECORE_CALLBACK_CANCEL
Return value to remove a callback.
Definition: Ecore_Common.h:152
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
ecore_timer_del
void * ecore_timer_del(Ecore_Timer *timer)
Deletes the specified timer from the timer list.
Definition: ecore_timer.c:238
ecore_timer_add
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
Ecore_Timer
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3079
EINA_LOG_ERR
#define EINA_LOG_ERR(fmt,...)
Logs a message with level ERROR on the default domain with the specified format.
Definition: eina_log.h:376
eina_log_domain_unregister
EINA_API void eina_log_domain_unregister(int domain)
Forgets about a logging domain registered by eina_log_domain_register()
Definition: eina_log.c:1904
eina_log_domain_register
EINA_API int eina_log_domain_register(const char *name, const char *color)
Definition: eina_log.c:1885
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
EINA_DBL_EQ
#define EINA_DBL_EQ(a, b)
Safe comparison of double.
Definition: eina_util.h:100
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
eldbus_name_owner_changed_callback_add
void eldbus_name_owner_changed_callback_add(Eldbus_Connection *conn, const char *bus, Eldbus_Name_Owner_Changed_Cb cb, const void *cb_data, Eina_Bool allow_initial_call)
Add a callback to be called when unique id of a bus name changed.
Definition: eldbus_core.c:1397
eldbus_connection_unref
void eldbus_connection_unref(Eldbus_Connection *conn)
Decrement connection reference count.
Definition: eldbus_core.c:1306
eldbus_connection_get
Eldbus_Connection * eldbus_connection_get(Eldbus_Connection_Type type)
Establish a connection to bus and integrate it with the ecore main loop.
Definition: eldbus_core.c:1102
Eldbus_Message
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition: Eldbus.h:173
eldbus_shutdown
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition: eldbus_core.c:246
Eldbus_Pending
struct _Eldbus_Pending Eldbus_Pending
Represents a message that has been sent but has not yet reached its destination.
Definition: Eldbus.h:188
eldbus_init
EAPI int eldbus_init(void)
Initialize eldbus.
Definition: eldbus_core.c:128
eldbus_message_error_get
Eina_Bool eldbus_message_error_get(const Eldbus_Message *msg, const char **name, const char **text)
Get the error text and name from a Eldbus_Message.
Definition: eldbus_message.c:233
eldbus_message_arguments_get
Eina_Bool eldbus_message_arguments_get(const Eldbus_Message *msg, const char *signature,...)
Get the arguments from an Eldbus_Message.
Definition: eldbus_message.c:274
eldbus_object_get
Eldbus_Object * eldbus_object_get(Eldbus_Connection *conn, const char *bus, const char *path)
Get an object of the given bus and path.
Definition: eldbus_object.c:188
eldbus_proxy_signal_handler_add
Eldbus_Signal_Handler * eldbus_proxy_signal_handler_add(Eldbus_Proxy *proxy, const char *member, Eldbus_Signal_Cb cb, const void *cb_data)
Add a signal handler.
Definition: eldbus_proxy.c:658
eldbus_proxy_call
Eldbus_Pending * eldbus_proxy_call(Eldbus_Proxy *proxy, const char *member, Eldbus_Message_Cb cb, const void *cb_data, double timeout, const char *signature,...)
Call a method in proxy.
Definition: eldbus_proxy.c:621
eldbus_proxy_get
Eldbus_Proxy * eldbus_proxy_get(Eldbus_Object *obj, const char *interface)
Get a proxy of the following interface name in a Eldbus_Object.
Definition: eldbus_proxy.c:208
Eldbus_Connection
struct _Eldbus_Connection Eldbus_Connection
Represents a connection of one the type of connection with the DBus daemon.
Definition: Eldbus.h:227
Eldbus_Proxy
struct _Eldbus_Proxy Eldbus_Proxy
Represents a client object bound to an interface.
Definition: Eldbus.h:221
Eldbus_Object
struct _Eldbus_Object Eldbus_Object
Represents an object path already attached with bus name or unique id.
Definition: Eldbus.h:233