bug-gnubg
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnubg] playing a bot on FIBS


From: Øystein Schønning-Johansen
Subject: Re: [Bug-gnubg] playing a bot on FIBS
Date: Mon, 23 Jan 2012 11:37:07 +0100

I did this with glib and the GSocket classes. I guess you can connect to fibs with whatever system you want. Python probably has something similar.

Fractions of my code below. Totally without any warranty of even working. The only thing you should need to write is now the callback function input_message_cb().

gint main( gint argc, gchar *argv[] )
{
GSocketClient *client;
GSocketConnection *conn;
GDataInputStream *input;
GOutputStream *output;
GError *err = NULL;
/* Engine *engine; */

g_type_init();

/* engine =  engine_new_ .... */
engine_set_equity_calculator( engine, DOUBLE_MATCHPOINT_EQUITY );

client = g_socket_client_new();
conn = g_socket_client_connect_to_host( client, "fibs.com", 4321, NULL, &err);
if( err )
g_error( "%s", err->message );

input = g_data_input_stream_new( g_io_stream_get_input_stream(G_IO_STREAM(conn)) );
output = g_io_stream_get_output_stream(G_IO_STREAM(conn));

fibs_login( G_INPUT_STREAM(input), output, &err );
if ( err )
g_error( "%s", err->message );

g_data_input_stream_read_line_async(input, G_PRIORITY_DEFAULT, NULL, input_message_cb, (gpointer) engine );

/* main loop */
while( TRUE ){
gchar *message;
gboolean doexit = FALSE;
gsize length;
GError *err = NULL;

message = g_data_input_stream_read_line( input, &length, NULL, &err);
if( err ) g_error( "%s", err->message );
doexit = message_handler( message, engine, output, players, saved );
g_free( message );
if ( doexit )
break;
}
g_print("Cleaning up....\n" );

g_object_unref ( engine );
g_object_unref ( client );
return 0;
}

/* You can not read line by line until after login, since the is no newline after the
 * login prompt. We therefore need to read the unbuffered stream until we're
 * logged in. The login procedure is therefor separated out. */
static gboolean
fibs_login( GInputStream *input, GOutputStream *output, GError **err )
{
GError *local_err = NULL;
gint i;
gchar prelogin[PRELOGIN_SIZE];
gchar **spl;

g_return_val_if_fail (err == NULL || *err == NULL, FALSE);

/* It's actually 2 reads */
for( i = 0; i < 2; i++ ){
gssize size = g_input_stream_read( G_INPUT_STREAM(input), &prelogin, PRELOGIN_SIZE, NULL, &local_err );
if ( local_err ){
g_propagate_error (err, local_err);
return FALSE;
}
if ( size < 0 ) g_error( "size -1" );
if ( size > PRELOGIN_SIZE ) g_error( "buffer overflow");

prelogin[size-1] = '\0';
if (!i)
g_printf( "%s\n", prelogin );
}

spl = g_strsplit( prelogin, "\n", 0 );
if ( strncmp( spl[1], "login:", 6 ) ){
g_strfreev( spl );
g_warning("Can't find login prompt");
return FALSE;
}

g_strfreev( spl );

g_output_stream_write( output, loginstring, strlen(loginstring), NULL, &local_err ); 
if ( local_err ){
g_propagate_error (err, local_err);
return FALSE;
}
return TRUE;
}



On Mon, Jan 23, 2012 at 10:20 AM, Ian Shaw <address@hidden> wrote:
Øystein has done this.  He might have some useful tips.

-- Ian


reply via email to

[Prev in Thread] Current Thread [Next in Thread]