[Dfsm-devel] dFSM/include/libdfsm tgcs.h,NONE,1.1 tinput_gcs.h,NONE,1.1 tspread.h,NONE,1.1 tinput.h,
Status: Beta
Brought to you by:
amoreno
|
From: Andreu M. <am...@us...> - 2004-04-07 03:57:22
|
Update of /cvsroot/dfsm/dFSM/include/libdfsm In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23396/include/libdfsm Modified Files: tinput.h Added Files: tgcs.h tinput_gcs.h tspread.h Log Message: Add Spread Group Communication System support --- NEW FILE: tinput_gcs.h --- /*************************************************************************** tinput_gcs.h - <to do description> ------------------- begin : 10/mar/2004 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: tinput_gcs.h,v $ * Revision 1.1 2004/04/07 03:44:27 amoreno * Add Spread Group Communication System support * * * * */ #ifndef CINPUT_GCS_H #define CINPUT_GCS_H #include <iostream> #include <string> #include <vector> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <dirent.h> #include <signal.h> //#include <asm/msr.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/un.h> #include <sstream> #include <fstream> #include "libdfsm/tevent.h" #include "libposixwrap/tcondition_variable.h" #include "libdfsm/tfsm.h" #include "libdfsm/sm_route_table.h" #ifdef SPREAD #include "libdfsm/tspread.h" #endif using namespace std; #define SOCKET_DIR "/tmp/fsm" #define MAX_PACKET_SIZE 512 /** * Input_gcs Class. Class used to send events and contains the thread that waits registered input events * * T can be tSpread or tEnsemble (Not implemented yet) */ template <class T> class tInput_gcs : public T { public: /** * Class constructor */ tInput_gcs () { initialized = false; } /** * Class destructor */ ~tInput_gcs() {} /** * Initilize with automatic Machine identification * * @return True if OK */ bool init() { tInput_gcs::socket_name = tInput_gcs::reserve_socket_server(); initialized = true; return true; } /** * Initilize * * @param machine_id Machine identification. * @return True if OK */ bool init(u_int16_t machine_id) { ostringstream ost; check_socket_dir(); ost << machine_id; socket_name = ost.str(); //sprintf (socket_name.c_str(), "%d", machine_id); initialized = true; return true; } /** * Send a message to other machines * 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 send(u_int16_t id_msg, char *data, u_int16_t len) { if (!initialized) { //FSMLOG(0) << "ERROR!: TInput not initialized." << endl; cout << "ERROR!: TInput not initialized." << endl; return 0; } //FSMLOG(2) <<"Sending event "<<id_msg<<endl; //cout <<"Sending event "<<id_msg<<endl; return T::send(id_msg, data, len); } /** * Send a message to a machines - NOT IMPLEMENTED YET * * @param machine Machine id to send the message. * @param id_msg Message id. * @param data Extra data * @param len Length data * @return Bytes sended */ int send_to(u_int16_t machine, u_int16_t id_msg, const char *data, u_int16_t len){ cout << "ERROR!: tInput_gcs::send_to not supported with Spread." << endl; return -1; } /** * Register message * Subscribe to a id_msg list of machines that want to receive id_msg king of messages. * * @param id_msg Message id. */ void register_msg(u_int16_t id_msg) {T::join(id_msg);} /** * Opens a socket at SOCKET_DIR * Each machine opens a socket to stablish peer to peer comunications with other machines * This function also spawns a new thread that listens in the socket. * * @param ev pointer to event object (to insert received messages) * @param cv pointer to condition variable */ void run() { if (!initialized) { //FSMLOG(0) << "ERROR!: TInput not initialized." << endl; cout << "ERROR!: TInput not initialized." << endl; return; } T::mainloop(); } /** * Get socket name. * Socket name is equal to FSM id. * * @return Socket name */ string get_socket_name() { if (!initialized) { //FSMLOG(0) << "ERROR!: TInput not initialized." << endl; cout << "ERROR!: TInput not initialized." << endl; return 0; } return tInput_gcs::socket_name; } /** * Unregister machine * Deletes socket and unsubscribes machine from messsage lists * */ void unregister_machine(u_int16_t sn) {} void set_event_handler(tEvent *handler) { ev=handler; T::init(ev); } protected: tEvent *ev; private: string socket_name; /**< Socket name */ bool initialized; /**< initilized flag */ void check_socket_dir() { DIR *dirStructP; if((dirStructP = opendir(SOCKET_DIR)) == NULL) { if (errno == ENOENT) { // Doesn't exist mkdir(SOCKET_DIR, 0766); // Create directory } else { // FIXME: Either we don't have access permissions to this dir or a fatal error // has ocurred. We should throw a very big exception here and maybe exit. // exit(1); } } } /** * Finds the first avalable id in SOCKET_DIR * */ string reserve_socket_server() { // ---------------------------------------------------------------- // machine id = socket_name // ---------------------------------------------------------------- DIR *dirStructP; dirent *direntp; char *n; bool digit, used_sockets[MAX_MACHINE]; u_int16_t i; for(i=0; i < MAX_MACHINE; i++) used_sockets[i]=false; if((dirStructP = opendir(SOCKET_DIR)) != NULL) { // Search a free socket while((direntp = readdir(dirStructP)) != NULL) { n = direntp->d_name; for (register char *i=n;*i;++i) { if (isdigit(*i)) digit=1; else { digit=0; break; } } if (!digit)continue; used_sockets[atoi(n)]=true; } closedir(dirStructP); } else { if (errno == ENOENT) { // Doesn't exist mkdir(SOCKET_DIR, 0766); // Create directory } else { // FIXME: Either we don't have access permissions to this dir or a fatal error // has ocurred. We should throw a very big exception here and maybe exit. // exit(1); } } // Find first available for(i=0; i < MAX_MACHINE; i++) if(!used_sockets[i]) break; if (i >= (MAX_MACHINE)) { //FSMLOG(0) << "Max. number of FSM is " << MAX_MACHINE<< endl; // FIXME: We should throw a fatal exception and exit. exit(1); } char *socket_path=new char[104]; // This isn't a memory leak coz tFSM class uses this pointer. So it is responsible of it. char tmp[5]; strncpy(socket_path,SOCKET_DIR,99); // 99 = 104 - 5 104 is the maximum length of a unix socket path UNP sprintf(tmp,"/%d",i); strncat(socket_path,tmp,5); // There won't be more than 999 machines // Create file ofstream fs (socket_path, ofstream::out | ofstream::trunc); fs.close(); //FSMLOG(3) <<"SN:"<< socket_path << " CNT: "<<i<<endl ; sprintf (socket_path,"%d",i); return socket_path ; } }; #endif //CINPUT_GCS_H Index: tinput.h =================================================================== RCS file: /cvsroot/dfsm/dFSM/include/libdfsm/tinput.h,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** tinput.h 3 Apr 2004 06:32:01 -0000 1.17 --- tinput.h 7 Apr 2004 03:44:27 -0000 1.18 *************** *** 32,35 **** --- 32,38 ---- * $Revision$ * $Log$ + * Revision 1.18 2004/04/07 03:44:27 amoreno + * Add Spread Group Communication System support + * * Revision 1.17 2004/04/03 06:32:01 amoreno * Add support for manual and automatic state machine identification *************** *** 109,112 **** --- 112,119 ---- #include "libposixwrap/tthread.h" + #if defined SPREAD + #include "libdfsm/tinput_gcs.h" + #endif + using namespace std; *************** *** 232,240 **** --- 239,256 ---- ~tInput_service(){}; void run(){ thread_input.start(); }; + #if defined SPREAD + tInput_gcs<tSpread> *get_pointer(){ return thread_input.pointer(); }; + #else tInput *get_pointer(){ return thread_input.pointer(); }; + #endif protected: private: + #if defined SPREAD + tThread <tInput_gcs<tSpread> > thread_input ; + tInput_gcs<tSpread> *tInputp ; + #else tThread <tInput> thread_input ; tInput *tInputp ; + #endif }; #define tinput_service tInput_service::getInstance() --- NEW FILE: tspread.h --- /*************************************************************************** tspread.h - <to do description> ------------------- begin : 03/march/2004 copyright : (C) 2004 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.h,v $ * Revision 1.1 2004/04/07 03:44:27 amoreno * Add Spread Group Communication System support * * Revision 1.10 2004/01/02 08:04:18 amoreno * */ #ifdef SPREAD #ifndef TSPREAD_H #define TSPREAD_H #include "libdfsm/tgcs.h" #include <sp.h> #define CONNECT_TIMEOUT 5 /** * Class tgcs. Group Communication System for dFSM */ class tSpread : public tGcs { public: /** * Configure Group Communication System * * @param ev Event List * @return True if ok */ bool init(tEvent *ev); /** * Join group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ bool join(u_int16_t id_msg); /** * Leave group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ bool leave(u_int16_t id_msg); /** * 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 send(u_int16_t id_msg, const char *data=0,u_int16_t len=0); /** * Receive message callback * * @param fd fd. * @param code code * @param data data */ static void receive(int fd, int code, void *data); /** * Get connection identification * * @param id identification */ void getid(char *id); /** * Main loop * Handle events * * */ void mainloop(void); private: static mailbox Mbox; /**< Spread Id connection */ char User[80]; /**< Private name connection */ char Spread_name[80]; /**< Spread name daemon */ char Private_group[MAX_GROUP_NAME]; /**< Private Group Name */ static tEvent *ev; /**< Event handler */ }; #endif //TSPREAD_H #endif //SPREAD --- NEW FILE: tgcs.h --- /*************************************************************************** tgcs.h - <to do description> ------------------- begin : 03/march/2004 copyright : (C) 2004 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: tgcs.h,v $ * Revision 1.1 2004/04/07 03:44:27 amoreno * Add Spread Group Communication System support * * Revision 1.10 2004/01/02 08:04:18 amoreno * */ #ifndef TGCS_H #define TGCS_H #include <sys/types.h> #include "libdfsm/tevent.h" /** * Class tgcs. Group Communication System for dFSM */ class tGcs { public: virtual ~tGcs(){} /** * Configure Group Communication System * * @param ev Event List * @return True if ok */ virtual bool init(tEvent *ev) = 0; /** * Join group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ virtual bool join(u_int16_t id_msg) = 0; /** * Leave group associated to event id_msg * * * @param id_msg Message id. * @return true if OK */ virtual bool leave(u_int16_t id_msg) = 0; /** * 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 */ virtual int send(u_int16_t id_msg, const char *data=0,u_int16_t len=0) = 0; /** * Main loop * Handle events * * */ virtual void mainloop(void) = 0; }; #endif //TGCS_H |