Adding and watching a socket in glib main loop

Luis Cardozo luiscardozocarreras at gmail.com
Tue Aug 5 15:00:31 EDT 2008


Hello.

I want to modify the nullclient.c example to do an "automated IM
sending program", with a main program (nullclient.c) that will be
verifying the status of the connected account, and that need to listen
to a TCP socket to get the message to send to predefined user(s). I am
working with Yahoo prpl.

I can get the account connected, but my problem is: How I get a TCP
socket to listen a TCP port, and watching it in the Glib Main Loop?
(Or how can I
rewrite the example to do the things with my own "main loop"?).

I have read that, with GLib, one can get a socket connected with 3
lines of code... but I can find these lines of codes! (Maybe I need
the GNet lib?)



Here is my code:


#### CODE: nullclient.c #####

/*
* nullclient.c ,   connect YAHOO_USER_SEND account, and listen the
IM_PORT for messages to send to YAHOO_USER_RECV
*/

/*
 * pidgin
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02111-1301  USA
 *
 */

#include "purple.h"

#include <glib.h>

#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>


#include "defines.h"

#define IM_PORT 50062
#define YAHOO_USER_SEND "yahoo_account_that_send_the_messages"
#define YAHOO_USER_SEND_PASS "secret"
#define YAHOO_USER_RECV "yahoo_account_that_receive_the_messages"


/**
 * The following eventloop functions are used in both pidgin and
purple-text. If your
 * application uses glib mainloop, you can safely use this verbatim.
 */
#define PURPLE_GLIB_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
#define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)

typedef struct _PurpleGLibIOClosure {
	PurpleInputFunction function;
	guint result;
	gpointer data;
} PurpleGLibIOClosure;


static void purple_glib_io_destroy(gpointer data)
{
	g_free(data);
}


/* this code I don't understand :(  - I have readed the Glib doc, but,
how it works with libpurple? */
static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition
condition, gpointer data)
{
	PurpleGLibIOClosure *closure = data;
	PurpleInputCondition purple_cond = 0;

	if (condition & PURPLE_GLIB_READ_COND)
		purple_cond |= PURPLE_INPUT_READ;
	if (condition & PURPLE_GLIB_WRITE_COND)
		purple_cond |= PURPLE_INPUT_WRITE;

	closure->function(closure->data, g_io_channel_unix_get_fd(source),
			  purple_cond);

	return TRUE;
}


static guint glib_input_add(gint fd, PurpleInputCondition condition,
PurpleInputFunction function,
							   gpointer data)
{
	PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
	GIOChannel *channel;
	GIOCondition cond = 0;

	closure->function = function;
	closure->data = data;

	if (condition & PURPLE_INPUT_READ)
		cond |= PURPLE_GLIB_READ_COND;
	if (condition & PURPLE_INPUT_WRITE)
		cond |= PURPLE_GLIB_WRITE_COND;

	channel = g_io_channel_unix_new(fd);
	closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
					      purple_glib_io_invoke, closure, purple_glib_io_destroy);

	g_io_channel_unref(channel);
	return closure->result;
}

static PurpleEventLoopUiOps glib_eventloops =
{
	g_timeout_add,
	g_source_remove,
	glib_input_add,
	g_source_remove,
	NULL,
#if GLIB_CHECK_VERSION(2,14,0)
	g_timeout_add_seconds,
#else
	NULL,
#endif

	/* padding */
	NULL,
	NULL,
	NULL
};
/*** End of the eventloop functions. ***/

/*** Conversation uiops ***/
static void
null_write_conv(PurpleConversation *conv, const char *who, const char *alias,
			const char *message, PurpleMessageFlags flags, time_t mtime)
{
	const char *name;
	if (alias && *alias)
		name = alias;
	else if (who && *who)
		name = who;
	else
		name = NULL;

	printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv),
			purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
			name, message);
}

static PurpleConversationUiOps null_conv_uiops =
{
	NULL,                      /* create_conversation  */
	NULL,                      /* destroy_conversation */
	NULL,                      /* write_chat           */
	NULL,                      /* write_im             */
	null_write_conv,           /* write_conv           */
	NULL,                      /* chat_add_users       */
	NULL,                      /* chat_rename_user     */
	NULL,                      /* chat_remove_users    */
	NULL,                      /* chat_update_user     */
	NULL,                      /* present              */
	NULL,                      /* has_focus            */
	NULL,                      /* custom_smiley_add    */
	NULL,                      /* custom_smiley_write  */
	NULL,                      /* custom_smiley_close  */
	NULL,                      /* send_confirm         */
	NULL,
	NULL,
	NULL,
	NULL
};

static void
null_ui_init(void)
{
	/**
	 * This should initialize the UI components for all the modules. Here we
	 * just initialize the UI for conversations.
	 */
	purple_conversations_set_ui_ops(&null_conv_uiops);
}

static PurpleCoreUiOps null_core_uiops =
{
	NULL,
	NULL,
	null_ui_init,
	NULL,

	/* padding */
	NULL,
	NULL,
	NULL,
	NULL
};

static void
init_libpurple(void)
{
	/* Set a custom user directory (optional) */
	purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);

	/* We do not want any debugging for now to keep the noise to a minimum. */
	purple_debug_set_enabled(FALSE);
	//purple_debug_set_enabled(TRUE);

	/* Set the core-uiops, which is used to
	 * 	- initialize the ui specific preferences.
	 * 	- initialize the debug ui.
	 * 	- initialize the ui components for all the modules.
	 * 	- uninitialize the ui components for all the modules when the
core terminates.
	 */
	purple_core_set_ui_ops(&null_core_uiops);

	/* Set the uiops for the eventloop. If your client is glib-based, you
can safely
	 * copy this verbatim. */
	purple_eventloop_set_ui_ops(&glib_eventloops);

	/* Set path to search for plugins. The core (libpurple) takes care of
loading the
	 * core-plugins, which includes the protocol-plugins. So it is not
essential to add
	 * any path here, but it might be desired, especially for ui-specific
plugins. */
	purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);

	/* Now that all the essential stuff has been set, let's try to init
the core. It's
	 * necessary to provide a non-NULL name for the current ui to the
core. This name
	 * is used by stuff that depends on this ui, for example the
ui-specific plugins. */
	if (!purple_core_init(UI_ID)) {
		/* Initializing the core failed. Terminate. */
		fprintf(stderr,
				"libpurple initialization failed. Dumping core.\n"
				"Please report this!\n");
		abort();
	}

	/* Create and load the buddylist. */
	purple_set_blist(purple_blist_new());
	purple_blist_load();

	/* Load the preferences. */
	purple_prefs_load();

	/* Load the desired plugins. The client should save the list of
loaded plugins in
	 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
	purple_plugins_load_saved(PLUGIN_SAVE_PREF);

	/* Load the pounces. */
	purple_pounces_load();
}

/**************************************/
/* added to send a message to im_dest */
void send_menssages(PurpleAccount *account, char *im_dest) {
	PurpleConversation *conversation;
	PurpleConvIm *conv_im;

	/* Empieza la conversacion, del tipo IM */
	conversation = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, im_dest);
	conv_im = PURPLE_CONV_IM(conversation);
	
	purple_conv_im_send(conv_im, "Test message");
}
/**************************************/

static void
signed_on(PurpleConnection *gc, gpointer null)
{
	char *im_dest = YAHOO_USER_RECV;

	PurpleAccount *account = purple_connection_get_account(gc);
	printf("Account connected: %s %s\n", account->username, account->protocol_id);
	
        /* Send a test message */
	send_menssages(account,im_dest);
}

static void
connect_to_signals_for_demonstration_purposes_only(void)
{
	static int handle;
	purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
				PURPLE_CALLBACK(signed_on), NULL);
}


/**************************************/

/* Code added from other project to manage sockets*/

void setnonblocking(int sock)
{
	int opts;

	opts = fcntl(sock,F_GETFL);
	if (opts < 0) {
		perror("fcntl(F_GETFL)");
		exit(EXIT_FAILURE);
	}
	opts = (opts | O_NONBLOCK);
	if (fcntl(sock,F_SETFL,opts) < 0) {
		perror("fcntl(F_SETFL)");
		exit(EXIT_FAILURE);
	}
	return;
}


int listen_socket()
{
	int sock;
	struct sockaddr_in server_address; /* bind info structure */
	int reuse_addr = 1;

	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}
	
	/* So that we can re-bind to it without TIME_WAIT problems */
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
	
	/* Set socket to non-blocking with our setnonblocking routine */
	setnonblocking(sock);

	memset((char *) &server_address, 0, sizeof(server_address));
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(IM_PORT);

	if (bind(sock, (struct sockaddr *) &server_address,
sizeof(server_address)) < 0 ) {
		//writelogfile(logfd,LOG_EMERG,"error al hacer bind: %s\n",strerror(errno));
		perror("bind");
		close(sock);
		exit(EXIT_FAILURE);
	}

	/* Set up queue for incoming connections. */
	if((listen(sock,5))<0)
	{
		perror("listen");
		close(sock);
		exit(EXIT_FAILURE);
	}
	
	printf("\nSocket TCP/IP ready: %s:%d\n\n",
inet_ntoa(server_address.sin_addr),ntohs(server_address.sin_port));

	return sock;
}
/* /code added from other project */

/* I tried to work with GIOChannel, but I can't get the program to
listen to socket */
static gboolean
gio_read_socket (GIOChannel *gio, GIOCondition condition, gpointer data)
{
        GIOStatus ret;
        GError *err = NULL;
        gchar *msg;
        gsize len;

        if (condition & G_IO_HUP)
                g_error ("Read end of pipe died!\n");

        ret = g_io_channel_read_line (gio, &msg, &len, NULL, &err);
        if (ret == G_IO_STATUS_ERROR)
                g_error ("Error reading: %s\n", err->message);

        printf ("Read %u bytes: %s\n", len, msg);

        g_free (msg);

        return TRUE;
}



void prepare_socket(void)
{
	GIOChannel *channel;
	int sock;
	
	sock = listen_socket();

	channel = g_io_channel_unix_new(sock);
	if(!channel)
		g_error("No se puede crear el GIOChannel\n");

	if(!g_io_add_watch(channel, G_IO_IN | G_IO_HUP,
			gio_read_socket, NULL))
	{
		g_error("Cannot add watch on GIOChannel!\n");
	}
	
	return;
}
/**************************************/



int main(int argc, char *argv[])
{
	const char *prpl = "prpl-yahoo";
	char *name = YAHOO_USER_SEND;
	char *password = YAHOO_USER_SEND_PASS;
	
	GMainLoop *loop = g_main_loop_new(NULL, FALSE);
	PurpleAccount *account;
	PurpleSavedStatus *status;


	/* libpurple's built-in DNS resolution forks processes to perform
	 * blocking lookups without blocking the main process.  It does not
	 * handle SIGCHLD itself, so if the UI does not you quickly get an army
	 * of zombie subprocesses marching around.
	 */
	signal(SIGCHLD, SIG_IGN);

	init_libpurple();

	printf("AutoIM initialized.\n");

	/* Create the account */
	account = purple_account_new(name, prpl);
	//	purple_accounts_add(account);
	
	/* password for the account */
	purple_account_set_password(account, password);

	/* It's necessary to enable the account first. */
	purple_account_set_enabled(account, UI_ID, TRUE);

	/* Now, to connect the account(s), create a status and activate it. */
	status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
	purple_savedstatus_activate(status);

	connect_to_signals_for_demonstration_purposes_only();

	prepare_socket();

	g_main_loop_run(loop);

	return 0;
}

#### / CODE: nullclient.c #####




With this code, when I connect from a client --example: telnet on the
port IM_PORT-- I get "Unable to connect to remote host: Connection
reset by peer" (in the client side), and
** ERROR **: Error reading: Transport endpoint is not connected
aborting...
(from the server (nullclient.c) side.


What I am doing wrong?



Thanks


Luis Cardozo
Paraguay




More information about the Devel mailing list