Add ipc id to TrCore model.

Implement info and stat ipc messages.
This commit is contained in:
Josh Elsasser 2007-05-24 07:51:37 +00:00
parent 8fbb4eab67
commit 80c7f8c922
3 changed files with 203 additions and 4 deletions

196
gtk/ipc.c
View File

@ -43,6 +43,7 @@
#include "ipc.h"
#include "tr_core.h"
#include "tr_prefs.h"
#include "tr_torrent.h"
#include "util.h"
enum contype { CON_SERV, CON_CLIENT };
@ -102,9 +103,18 @@ smsg_add( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg );
static void
smsg_quit( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg );
static void
smsg_info( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg );
static void
smsg_infoall( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg );
static int
addinfo( TrTorrent * tor, enum ipc_msg msgid, int torid, int types,
benc_val_t * val );
static void
all_default( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg );
static gboolean
simpleresp( struct constate * con, enum ipc_msg id, int64_t tag );
static TrTorrent *
findtor( TrCore * core, int id );
/* this is only used on the server */
static char *gl_sockpath = NULL;
@ -122,6 +132,10 @@ ipc_socket_setup( GtkWindow * parent, TrCore * core )
con->msgs = ipc_initmsgs();
if( NULL == con->msgs ||
0 > ipc_addmsg( con->msgs, IPC_MSG_ADDMANYFILES, smsg_add ) ||
0 > ipc_addmsg( con->msgs, IPC_MSG_GETINFO, smsg_info ) ||
0 > ipc_addmsg( con->msgs, IPC_MSG_GETINFOALL, smsg_infoall ) ||
0 > ipc_addmsg( con->msgs, IPC_MSG_GETSTAT, smsg_info ) ||
0 > ipc_addmsg( con->msgs, IPC_MSG_GETSTATALL, smsg_infoall ) ||
0 > ipc_addmsg( con->msgs, IPC_MSG_QUIT, smsg_quit ) )
{
errmsg( con->u.serv.wind, _("Failed to set up IPC:\n%s"),
@ -386,7 +400,7 @@ cli_io_received( GSource * source SHUTUP, char * data, unsigned int len,
return 0;
}
if( 0 < res && 0 == cli->msgid )
if( HASVERS( &con->ipc ) && 0 == cli->msgid )
{
client_sendmsg( con );
}
@ -528,6 +542,159 @@ smsg_quit( enum ipc_msg id SHUTUP, benc_val_t * val SHUTUP, int64_t tag SHUTUP,
tr_core_quit( srv->core );
}
static void
smsg_info( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg )
{
struct constate * con = arg;
struct constate_serv * srv = &con->u.serv;
enum ipc_msg respid;
benc_val_t * ids, * types, * idval, packet, * pkval;
int typeflags, ii;
TrTorrent * tor;
uint8_t * buf;
size_t size;
if( NULL == srv->core )
{
return;
}
if( NULL == val || TYPE_DICT != val->type )
{
simpleresp( con, tag, IPC_MSG_NOTSUP );
return;
}
respid = ( IPC_MSG_GETINFO == id ? IPC_MSG_INFO : IPC_MSG_STAT );
ids = tr_bencDictFind( val, "id" );
types = tr_bencDictFind( val, "types" );
if( NULL == ids || TYPE_LIST != ids->type ||
NULL == types || TYPE_LIST != types->type )
{
simpleresp( con, tag, IPC_MSG_NOTSUP );
return;
}
typeflags = ipc_infotypes( respid, types );
pkval = ipc_initval( &con->ipc, respid, tag, &packet, TYPE_LIST );
if( NULL == pkval )
{
simpleresp( con, tag, IPC_MSG_FAIL );
return;
}
for( ii = 0; ids->val.l.count > ii; ii++ )
{
idval = &ids->val.l.vals[ii];
if( TYPE_INT != idval->type || !TORRENT_ID_VALID( idval->val.i ) ||
NULL == ( tor = findtor( srv->core, idval->val.i ) ) )
{
continue;
}
if( 0 > addinfo( tor, respid, idval->val.i, typeflags, pkval ) )
{
tr_bencFree( &packet );
simpleresp( con, tag, IPC_MSG_FAIL );
return;
}
}
buf = ipc_mkval( &packet, &size );
tr_bencFree( &packet );
if( NULL == buf )
{
simpleresp( con, tag, IPC_MSG_FAIL );
}
else
{
io_send_keepdata( con->source, buf, size );
}
}
static void
smsg_infoall( enum ipc_msg id, benc_val_t * val, int64_t tag, void * arg )
{
struct constate * con = arg;
struct constate_serv * srv = &con->u.serv;
enum ipc_msg respid;
benc_val_t packet, * pkval;
int typeflags;
GtkTreeModel * model;
GtkTreeIter iter;
int rowid;
TrTorrent * tor;
uint8_t * buf;
size_t size;
if( NULL == srv->core )
{
return;
}
if( NULL == val || TYPE_LIST != val->type )
{
simpleresp( con, tag, IPC_MSG_NOTSUP );
return;
}
respid = ( IPC_MSG_GETINFOALL == id ? IPC_MSG_INFO : IPC_MSG_STAT );
typeflags = ipc_infotypes( respid, val );
pkval = ipc_initval( &con->ipc, respid, tag, &packet, TYPE_LIST );
if( NULL == pkval )
{
simpleresp( con, tag, IPC_MSG_FAIL );
return;
}
model = tr_core_model( srv->core );
if( gtk_tree_model_get_iter_first( model, &iter ) )
{
do
{
gtk_tree_model_get( model, &iter, MC_ID, &rowid,
MC_TORRENT, &tor, -1 );
g_object_unref( tor );
if( 0 > addinfo( tor, respid, rowid, typeflags, pkval ) )
{
tr_bencFree( &packet );
simpleresp( con, tag, IPC_MSG_FAIL );
return;
}
}
while( gtk_tree_model_iter_next( model, &iter ) );
}
buf = ipc_mkval( &packet, &size );
tr_bencFree( &packet );
if( NULL == buf )
{
simpleresp( con, tag, IPC_MSG_FAIL );
}
else
{
io_send_keepdata( con->source, buf, size );
}
}
static int
addinfo( TrTorrent * tor, enum ipc_msg msgid, int torid, int types,
benc_val_t * val )
{
tr_info_t * inf;
tr_stat_t * st;
inf = tr_torrent_info( tor );
if( IPC_MSG_INFO == msgid )
{
return ipc_addinfo( val, torid, inf, types );
}
else
{
st = tr_torrent_stat( tor );
return ipc_addstat( val, torid, inf, st, types );
}
}
static void
all_default( enum ipc_msg id, benc_val_t * val SHUTUP, int64_t tag, void * arg )
{
@ -562,3 +729,30 @@ simpleresp( struct constate * con, enum ipc_msg id, int64_t tag )
return TRUE;
}
static TrTorrent *
findtor( TrCore * core, int id )
{
GtkTreeModel * model;
GtkTreeIter iter;
int rowid;
TrTorrent * tor;
model = tr_core_model( core );
if( gtk_tree_model_get_iter_first( model, &iter ) )
{
do
{
gtk_tree_model_get( model, &iter, MC_ID, &rowid, -1 );
if( rowid == id )
{
gtk_tree_model_get( model, &iter, MC_TORRENT, &tor, -1 );
g_object_unref( tor );
return tor;
}
}
while( gtk_tree_model_iter_next( model, &iter ) );
}
return NULL;
}

View File

@ -186,8 +186,8 @@ tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP )
G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
/* completedFromTracker, downloaded, uploaded left */
G_TYPE_INT, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT64,
/* tracker, the TrTorrent object */
TR_TRACKER_BOXED_TYPE, TR_TORRENT_TYPE,
/* tracker, the TrTorrent object, the ID for IPC */
TR_TRACKER_BOXED_TYPE, TR_TORRENT_TYPE, G_TYPE_INT,
};
#ifdef REFDBG
@ -201,6 +201,7 @@ tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP )
self->model = GTK_TREE_MODEL( store );
self->handle = tr_init( "gtk" );
self->zombies = NULL;
self->nextid = 1;
self->quitting = FALSE;
self->disposed = FALSE;
}
@ -638,8 +639,10 @@ tr_core_insert( TrCore * self, TrTorrent * tor )
MC_NAME, inf->name,
MC_SIZE, inf->totalSize,
MC_TORRENT, tor,
MC_ID, self->nextid,
-1);
g_object_unref( tor );
self->nextid++;
}
void

View File

@ -61,6 +61,7 @@ struct _TrCore
GtkTreeModel * model;
tr_handle_t * handle;
GList * zombies;
int nextid;
gboolean quitting;
gboolean disposed;
};
@ -159,7 +160,8 @@ enum {
MC_NAME, MC_SIZE, MC_STAT, MC_ERR, MC_TERR,
MC_PROG, MC_DRATE, MC_URATE, MC_ETA, MC_PEERS,
MC_UPEERS, MC_DPEERS, MC_SEED, MC_LEECH, MC_DONE,
MC_DOWN, MC_UP, MC_LEFT, MC_TRACKER, MC_TORRENT, MC_ROW_COUNT,
MC_DOWN, MC_UP, MC_LEFT, MC_TRACKER, MC_TORRENT, MC_ID,
MC_ROW_COUNT
};
#endif