[Dfsm-devel] dFSM/src/libdfsm tspread.cpp,NONE,1.1 Makefile,1.15,1.16
Status: Beta
Brought to you by:
amoreno
|
From: Andreu M. <am...@us...> - 2004-04-07 03:57:22
|
Update of /cvsroot/dfsm/dFSM/src/libdfsm In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23396/src/libdfsm Modified Files: Makefile Added Files: tspread.cpp Log Message: Add Spread Group Communication System support Index: Makefile =================================================================== RCS file: /cvsroot/dfsm/dFSM/src/libdfsm/Makefile,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** Makefile 17 Mar 2004 21:59:50 -0000 1.15 --- Makefile 7 Apr 2004 03:44:27 -0000 1.16 *************** *** 3,7 **** LIBNAME= libdfsm LDOPTIONS=-O2 ! CPPFLAGS= -g -Wall -fPIC -DPIC CXX= g++ SRC:= $(wildcard *.cpp) --- 3,7 ---- LIBNAME= libdfsm LDOPTIONS=-O2 ! CPPFLAGS= -g -Wall -fPIC -DPIC $(GCSFLAGS) CXX= g++ SRC:= $(wildcard *.cpp) *************** *** 35,37 **** clean: ! @rm -f *.so *.o *.d --- 35,37 ---- clean: ! @rm -f *.so *.o *.d --- NEW FILE: tspread.cpp --- /*************************************************************************** tspread.cpp - <to do description> ------------------- begin : 05/april/2003 copyright : (C) 2003 by Andreu Moreno email : am...@eu... ***************************************************************************/ /*************************************************************************** * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library 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 * Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * ***************************************************************************/ /* * * $Author: amoreno $ * $Revision: 1.1 $ * $Log: tspread.cpp,v $ * Revision 1.1 2004/04/07 03:44:27 amoreno * Add Spread Group Communication System support * * Revision 1.7 2004/01/02 08:04:19 amoreno * * */ #ifdef SPREAD #include "libdfsm/tspread.h" #include "libdfsm/tfsm.h" #include "libdfsm/tfsm_monitor_sensor.h" #include <stdio.h> mailbox tSpread::Mbox; /**< Spread Id connection */ tEvent *tSpread::ev; /**< Event handler */ /** * Configure Group Communication System * * @param ev Event List * @return True if ok */ bool tSpread::init(tEvent *_ev) { int ret; sp_time timeout; timeout.sec = CONNECT_TIMEOUT; timeout.usec = 0; ev = _ev; sprintf( User, "" ); sprintf( Spread_name, "4803@localhost"); ret = SP_connect_timeout( Spread_name, User, 0, 1, &Mbox, Private_group, timeout ); if( ret != ACCEPT_SESSION ) { SP_error( ret ); SP_disconnect( Mbox ); return false; } E_init(); E_attach_fd( Mbox, READ_FD, &tSpread::receive, 0, NULL, HIGH_PRIORITY ); return true; } /** * Join group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ bool tSpread::join(u_int16_t id_msg) { int ret; char group[80]; sprintf(group, "%i", id_msg); ret = SP_join( Mbox, group ); if( ret < 0 ) { SP_error( ret ); return false; }else { return true; } } /** * Leave group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ bool tSpread::leave(u_int16_t id_msg) { int ret; char group[80]; sprintf(group, "%i", id_msg); ret = SP_leave( Mbox, group ); if( ret < 0 ) { SP_error( ret ); return false; }else { return true; } } /** * Send a message to other machines using Group Communication System * A list of machines is subscribed to a message id (id_msg). When this function is invoked a message * of id_msg kind is sended to every subscribed machine. * * @param id_msg Message id. * @param data Extra data * @param len Length data * @return Bytes sended */ int tSpread::send(u_int16_t id_msg, const char *data, u_int16_t len) { int ret; char group[80]; sprintf(group, "%i", id_msg); // SAFE_MESS: in order to achieve global consistency, it is needed total order and safe message delivery ret= SP_multicast( Mbox, SAFE_MESS, group, 1, len, data ); if( ret < 0 ) { SP_error( ret ); return -1; }else { monitor_push_send_event(id_msg); return ret; } } /** * Receive message callback * */ void tSpread::receive(int fd, int code, void *data) { static char mess[102400]; char sender[MAX_GROUP_NAME]; char target_groups[100][MAX_GROUP_NAME]; int num_groups; int service_type; int16 mess_type; int endian_mismatch; int ret; tEvent::Event event; service_type = 0; ret = SP_receive( tSpread::Mbox, &service_type, sender, 100, &num_groups, target_groups, &mess_type, &endian_mismatch, sizeof(mess), mess ); //printf("\n========Missatge rebut=======: service_type: %x\n", service_type); //printf("\n========Missatge rebut=======: target_groups:: %s\n", target_groups[0]); if( ret < 0 ) { if ( (ret == GROUPS_TOO_SHORT) || (ret == BUFFER_TOO_SHORT) ) { printf("\n========Buffers or Groups too Short=======\n"); } } if (ret < 0 ) { SP_error( ret ); } if( Is_regular_mess( service_type ) ) { mess[ret] = 0; /*printf("message from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n", sender, mess_type, endian_mismatch, num_groups, ret, mess ); */ event.size = ret; event.id_msg = atoi(target_groups[0]); FSMLOGr(2)<<"-----------------------------"<<endl; FSMLOGr(2)<<"Received event: "<<event.id_msg<<endl; FSMLOGr(3)<<"Received "<< ret <<" data bytes from "<< sender <<endl; event.data = NULL; if (event.size > 0) { event.data=new unsigned char[event.size] ; memcpy(event.data, mess, event.size); } tSpread::ev->add_event(event); monitor_push_receive_event(event.id_msg); } } /** * Get connection identification * * @param id identification **/ void tSpread::getid(char *id) { id = Private_group; } /** * Main loop * Handle events * */ void tSpread::mainloop(void) { E_handle_events(); } #endif |