server.c

Server to reply to client.c requests.

Server to reply to client.c requests.

//Compile with:
// gcc -o server server.c `pkg-config --cflags --libs eldbus ecore`
#include "Eldbus.h"
#include <Ecore.h>
#define BUS "org.Enlightenment"
#define PATH "/org/enlightenment"
#define PATH_TEST_SON "/org/enlightenment/son"
#define INTERFACE "org.enlightenment.Test"
static Eldbus_Connection *conn;
static Eldbus_Message *
_hello(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *message)
{
Eldbus_Message *reply = eldbus_message_method_return_new(message);
eldbus_message_arguments_append(reply, "s", "Hello World");
printf("Hello\n");
return reply;
}
static Eldbus_Message *
_quit(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *message)
{
printf("Quit\n");
ecore_main_loop_quit();
return eldbus_message_method_return_new(message);
}
enum
{
TEST_SIGNAL_ALIVE = 0,
TEST_SIGNAL_HELLO
};
static Eina_Bool
send_signal_alive(void *data)
{
Eldbus_Service_Interface *iface = data;
eldbus_service_signal_emit(iface, TEST_SIGNAL_ALIVE);
return ECORE_CALLBACK_RENEW;
}
static Eina_Bool
send_signal_hello(void *data)
{
Eldbus_Service_Interface *iface = data;
eldbus_service_signal_emit(iface, TEST_SIGNAL_HELLO, "Hello World");
return ECORE_CALLBACK_RENEW;
}
static Eldbus_Message *
_send_bool(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
Eina_Bool bool;
if (!eldbus_message_arguments_get(msg, "b", &bool))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "b", bool);
return reply;
}
static Eldbus_Message *
_send_byte(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
unsigned char byte;
if (!eldbus_message_arguments_get(msg, "y", &byte))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "y", byte);
return reply;
}
static Eldbus_Message *
_send_uint32(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
unsigned int uint32;
if (!eldbus_message_arguments_get(msg, "u", &uint32))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "u", uint32);
return reply;
}
static Eldbus_Message *
_send_int32(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
int int32;
if (!eldbus_message_arguments_get(msg, "i", &int32))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "i", int32);
return reply;
}
static Eldbus_Message *
_send_int16(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
short int int16;
if (!eldbus_message_arguments_get(msg, "n", &int16))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "n", int16);
return reply;
}
static Eldbus_Message *
_send_double(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
double d;
if (!eldbus_message_arguments_get(msg, "d", &d))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "d", d);
return reply;
}
static Eldbus_Message *
_send_string(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
const char *txt;
if (!eldbus_message_arguments_get(msg, "s", &txt))
printf("eldbus_message_arguments_get() error\n");
eldbus_message_arguments_append(reply, "s", txt);
return reply;
}
static Eina_Bool
_resp_async(void *data)
{
Eldbus_Message *msg = data;
eldbus_message_arguments_append(msg, "s", "Async test ok");
eldbus_connection_send(conn, msg, NULL, NULL, -1);
return ECORE_CALLBACK_CANCEL;
}
static Eldbus_Message *
_async_test(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
Eldbus_Message *reply = eldbus_message_method_return_new(msg);
printf("Received a call to AsyncTest.\n");
printf("Response will be send in 5 seconds.\n");
ecore_timer_add(5, _resp_async, reply);
return NULL;
}
static const Eldbus_Signal signals[] = {
[TEST_SIGNAL_ALIVE] = {"Alive", NULL, 0},
[TEST_SIGNAL_HELLO] = {"Hello", ELDBUS_ARGS({ "s", "message" }), 0},
{ }
};
static const Eldbus_Method methods[] = {
{
"Hello", NULL, ELDBUS_ARGS({"s", "message"}),
_hello
},
{
"Quit", NULL, NULL,
_quit, ELDBUS_METHOD_FLAG_DEPRECATED
},
{ "SendBool", ELDBUS_ARGS({"b", "bool"}), ELDBUS_ARGS({"b", "bool"}),
_send_bool
},
{ "SendByte", ELDBUS_ARGS({"y", "byte"}), ELDBUS_ARGS({"y", "byte"}),
_send_byte
},
{ "SendUint32", ELDBUS_ARGS({"u", "uint32"}), ELDBUS_ARGS({"u", "uint32"}),
_send_uint32
},
{ "SendInt32", ELDBUS_ARGS({"i", "int32"}), ELDBUS_ARGS({"i", "int32"}),
_send_int32
},
{ "SendInt16", ELDBUS_ARGS({"n", "int16"}), ELDBUS_ARGS({"n", "int16"}),
_send_int16
},
{ "SendDouble", ELDBUS_ARGS({"d", "double"}), ELDBUS_ARGS({"d", "double"}),
_send_double
},
{ "SendString", ELDBUS_ARGS({"s", "string"}), ELDBUS_ARGS({"s", "string"}),
_send_string
},
{ "AsyncTest", NULL, ELDBUS_ARGS({"s", "text"}),
_async_test
},
{ }
};
static const Eldbus_Service_Interface_Desc iface_desc = {
INTERFACE, methods, signals
};
static void
on_name_request(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eldbus_Service_Interface *iface;
unsigned int reply;
iface = data;
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("error on on_name_request\n");
return;
}
if (!eldbus_message_arguments_get(msg, "u", &reply))
{
printf("error geting arguments on on_name_request\n");
return;
}
if (reply != ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
{
printf("error name already in use\n");
return;
}
ecore_timer_add(5, send_signal_alive, iface);
ecore_timer_add(6, send_signal_hello, iface);
}
int
main(void)
{
Eldbus_Service_Interface *iface;
ecore_init();
eldbus_init();
conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
iface = eldbus_service_interface_register(conn, PATH, &iface_desc);
eldbus_name_request(conn, BUS, ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
on_name_request, iface);
eldbus_service_interface_register(conn, PATH_TEST_SON, &iface_desc);
ecore_main_loop_begin();
eldbus_connection_unref(conn);
eldbus_shutdown();
ecore_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_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition Ecore_Common.h:153
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_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
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_REQUEST_FLAG_DO_NOT_QUEUE
#define ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE
If we can not become the primary owner do not place us in the queue.
Definition eldbus_freedesktop.h:12
eldbus_name_request
Eldbus_Pending * eldbus_name_request(Eldbus_Connection *conn, const char *name, unsigned int flags, Eldbus_Message_Cb cb, const void *cb_data)
Send a "RequestName" method call in proxy.
Definition eldbus_freedesktop.c:6
ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER
#define ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER
Service has become the primary owner of the requested name.
Definition eldbus_freedesktop.h:15
eldbus_connection_unref
void eldbus_connection_unref(Eldbus_Connection *conn)
Decrement connection reference count.
Definition eldbus_core.c:1306
eldbus_connection_send
Eldbus_Pending * eldbus_connection_send(Eldbus_Connection *conn, Eldbus_Message *msg, Eldbus_Message_Cb cb, const void *cb_data, double timeout)
Send a message.
Definition eldbus_pending.c:94
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_shutdown
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition eldbus_core.c:246
Eldbus_Message
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition Eldbus.h:173
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_method_return_new
Eldbus_Message * eldbus_message_method_return_new(const Eldbus_Message *msg)
Create a message that is a reply to a method call.
Definition eldbus_message.c:895
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_message_arguments_append
Eina_Bool eldbus_message_arguments_append(Eldbus_Message *msg, const char *signature,...)
Append arguments into an Eldbus_Message.
Definition eldbus_message.c:495
eldbus_service_interface_register
Eldbus_Service_Interface * eldbus_service_interface_register(Eldbus_Connection *conn, const char *path, const Eldbus_Service_Interface_Desc *desc)
Register an interface in the given path and connection.
Definition eldbus_service.c:998
eldbus_service_signal_emit
Eina_Bool eldbus_service_signal_emit(const Eldbus_Service_Interface *iface, unsigned int signal_id,...)
Emit a signal handler of the interface with non-complex types.
Definition eldbus_service.c:1414
ELDBUS_ARGS
#define ELDBUS_ARGS(args...)
Used to insert complete types to signature of methods or signals.
Definition eldbus_service.h:33
Eldbus_Connection
struct _Eldbus_Connection Eldbus_Connection
Represents a connection of one the type of connection with the DBus daemon.
Definition Eldbus.h:227