2007-02-09 01:42:04 +03:00
/*
ctdb_message protocol code
Copyright ( C ) Andrew Tridgell 2007
2007-05-31 07:50:53 +04:00
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 ,
2007-02-09 01:42:04 +03:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2007-05-31 07:50:53 +04:00
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 . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
2007-02-09 01:42:04 +03:00
*/
/*
see http : //wiki.samba.org/index.php/Samba_%26_Clustering for
protocol design and packet details
*/
# include "includes.h"
# include "lib/events/events.h"
# include "lib/tdb/include/tdb.h"
# include "system/network.h"
# include "system/filesys.h"
# include "../include/ctdb_private.h"
2007-04-16 04:21:44 +04:00
# include "lib/util/dlinklist.h"
2007-02-09 01:42:04 +03:00
/*
this dispatches the messages to the registered ctdb message handler
*/
2007-05-03 11:12:23 +04:00
int ctdb_dispatch_message ( struct ctdb_context * ctdb , uint64_t srvid , TDB_DATA data )
2007-02-09 01:42:04 +03:00
{
2007-04-11 08:05:01 +04:00
struct ctdb_message_list * ml ;
2007-04-11 07:43:15 +04:00
2007-04-11 08:05:01 +04:00
for ( ml = ctdb - > message_list ; ml ; ml = ml - > next ) {
2007-04-27 17:16:17 +04:00
if ( ml - > srvid = = srvid | | ml - > srvid = = CTDB_SRVID_ALL ) {
ml - > message_handler ( ctdb , srvid , data , ml - > message_private ) ;
}
2007-02-09 01:42:04 +03:00
}
2007-04-11 08:05:01 +04:00
2007-04-11 08:26:14 +04:00
return 0 ;
}
/*
called when a CTDB_REQ_MESSAGE packet comes in
*/
void ctdb_request_message ( struct ctdb_context * ctdb , struct ctdb_req_header * hdr )
{
struct ctdb_req_message * c = ( struct ctdb_req_message * ) hdr ;
TDB_DATA data ;
2007-02-09 01:42:04 +03:00
data . dptr = & c - > data [ 0 ] ;
data . dsize = c - > datalen ;
2007-04-11 08:26:14 +04:00
ctdb_dispatch_message ( ctdb , c - > srvid , data ) ;
2007-02-09 01:42:04 +03:00
}
2007-04-11 07:43:15 +04:00
2007-04-11 05:58:28 +04:00
/*
when a client goes away , we need to remove its srvid handler from the list
*/
static int message_handler_destructor ( struct ctdb_message_list * m )
{
DLIST_REMOVE ( m - > ctdb - > message_list , m ) ;
2007-02-09 01:42:04 +03:00
return 0 ;
2007-04-11 05:58:28 +04:00
}
2007-02-09 01:42:04 +03:00
/*
setup handler for receipt of ctdb messages from ctdb_send_message ( )
*/
2007-04-11 05:58:28 +04:00
int ctdb_register_message_handler ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx ,
2007-04-27 18:31:45 +04:00
uint64_t srvid ,
2007-04-11 05:58:28 +04:00
ctdb_message_fn_t handler ,
2007-04-13 14:38:24 +04:00
void * private_data )
2007-02-09 01:42:04 +03:00
{
2007-04-11 05:58:28 +04:00
struct ctdb_message_list * m ;
m = talloc ( mem_ctx , struct ctdb_message_list ) ;
CTDB_NO_MEMORY ( ctdb , m ) ;
m - > ctdb = ctdb ;
m - > srvid = srvid ;
m - > message_handler = handler ;
2007-04-13 14:38:24 +04:00
m - > message_private = private_data ;
2007-04-11 05:58:28 +04:00
DLIST_ADD ( ctdb - > message_list , m ) ;
talloc_set_destructor ( m , message_handler_destructor ) ;
2007-02-09 01:42:04 +03:00
return 0 ;
}
2007-05-04 05:41:29 +04:00
/*
setup handler for receipt of ctdb messages from ctdb_send_message ( )
*/
int ctdb_deregister_message_handler ( struct ctdb_context * ctdb , uint64_t srvid , void * private_data )
{
struct ctdb_message_list * m ;
for ( m = ctdb - > message_list ; m ; m = m - > next ) {
if ( m - > srvid = = srvid & & m - > message_private = = private_data ) {
talloc_free ( m ) ;
return 0 ;
}
}
return - 1 ;
}