Modified:
trunk/imbus/daemon/main.c
trunk/imbus/src/im_debug.h
Log:
daemon++
Modified: trunk/imbus/daemon/main.c
==============================================================================
--- trunk/imbus/daemon/main.c (original)
+++ trunk/imbus/daemon/main.c Thu Sep 11 00:15:48 2008
@@ -1,19 +1,66 @@
#include <imbus.h>
#include <im_debug.h>
-int create_io_loop()
-{
- return 0;
-}
+static const IMChar *server_address = "local:/tmp/imbus-server";
-int create_bus_channel()
+IMBool on_receive_callback( IMMainLoop *loop, IMInt watch_id, IMPointer
data )
{
- return 0;
+ IMPointer obj;
+ IMInt timeout = -1;
+ IMInt ret;
+ IMIOError error = IM_IO_ERROR_NONE;
+ IMConnection *connection = IM_CONNECTION( data );
+ IMIOChannel *out = im_io_channel_unix_new( 1 );
+ IMStream *stream = im_text_stream_new();
+ _im_assert( connection );
+
+ im_print_log( "Receive data from watch id = " );
+ printf( "%d\n", watch_id );
+
+ while ( ( ret = im_connection_pending (connection, &error ) ) > 0) {
+ obj = im_connection_next( connection, &timeout, &error );
+
+ if (obj) {
+ im_stream_put_object( stream, obj );
+ im_stream_write( stream, out, &timeout, &error );
+ if (im_object_get_type (obj) == IM_TYPE_STRING &&
+ im_string_compare_str( IM_STRING( obj ), "quit" ) == 0 ) {
+ im_print_log( "Exit imbus Loop.\n" );
+ im_main_loop_quit( loop );
+ }
+ im_object_unref( obj );
+ } else {
+ ret = -1;
+ break;
+ }
+ }
+
+ im_object_unref( out );
+ im_object_unref( stream );
+
+ if (ret < 0) {
+ im_print_error( "Error occurred while reading from watch id = " );
+ printf ( "%d, error = %d\n", watch_id, error );
+ return FALSE;
+ }
+ return TRUE;
}
-int create_server()
+void on_new_connection_handler( IMServer *server, IMIOChannel *io_channel,
+ IMPointer data )
{
- return 0;
+ IMMainLoop *main_loop = im_server_get_main_loop( server );
+ IMConnection *connection = im_connection_new ( io_channel );
+ IMInt watch_id;
+
+ watch_id = im_main_loop_add_io_read_watch( main_loop,
+ io_channel,
+ on_receive_callback,
+ connection,
+ im_object_unref );
+
+ im_print_log( "New IO read watch was added for fd: " );
+ printf ( "%d, watch id: %d\n", im_io_channel_unix_get_fd( io_channel
), watch_id );
}
int main ( int argc, char *argv [] )
@@ -23,15 +70,31 @@
IMServer *server;
IMIOError error;
+ im_print_log( "Starting IMBUS Daemon...\n" );
+
im_init ( &argc, &argv ); /* initialize imbus */
+ loop = im_main_loop_unix_new();
+
+ im_print_log( "Starting IMBUS Server...\n" );
+ server = im_server_new( IM_TYPE_SERVER_UNIX );
+
+ if ( !im_server_listen( server, server_address, &error ) )
+ {
+ im_print_error( "Failed to listen to " );
+ im_print( server_address );
+ im_print( "\n" );
+ return 1;
+ }
+
+ im_server_set_main_loop( server, loop );
+ im_server_set_new_connection_handler( server,
+ on_new_connection_handler,
+ 0, 0 );
+
+ im_main_loop_run( loop );
- im_print_debug( "something is happenning here!" );
- im_print_error( "something is wrong here!" );
- im_print_warning( "something may cause error here!" );
-
- create_io_loop();
- create_bus_channel();
- create_server();
+ im_object_unref( loop );
+ im_object_unref( server );
return 0;
}
Modified: trunk/imbus/src/im_debug.h
==============================================================================
--- trunk/imbus/src/im_debug.h (original)
+++ trunk/imbus/src/im_debug.h Thu Sep 11 00:15:48 2008
@@ -9,21 +9,31 @@
* @{
*/
+void im_print_log( const IMChar* data )
+{
+ printf( "[IMBUS_LOG]: %s", data );
+}
+
void im_print_debug( const IMChar* data )
{
#if IM_ENABLE_DEBUG
- printf ("[IMBUS_DEBUG]: %s \n", data );
+ printf( "[IMBUS_DEBUG]: %s", data );
#endif
}
void im_print_error( const IMChar* data )
{
- printf ("[IMBUS_ERROR]: %s \n", data );
+ printf( "[IMBUS_ERROR]: %s", data );
}
void im_print_warning( const IMChar* data )
{
- printf ("[IMBUS_WARNING]: %s \n", data );
+ printf( "[IMBUS_WARNING]: %s", data );
+}
+
+void im_print( const IMChar* data )
+{
+ printf( "%s", data );
}
#endif