Pagina Principale   Moduli   Lista dei namespaces   Gerarchia delle classi   Lista in ordine alfabetico   Lista dei composti   Lista dei files   Membri dei namespaces   Membri dei composti   Membri dei files   Esempi  

/home/fbassi/devel/projects/reti/NameServer/Rule.cc

Vai alla documentazione di questo file.
00001 /*
00002  *    FBFS Distributed Object Repository
00003  *    Copyright (C) 2001 Francesco V. Bassi
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; either version 2 of the License, or
00008  *    (at your option) any later version.
00009  *
00010  *    This program is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *    GNU General Public License for more details.
00014  *
00015  *    You should have received a copy of the GNU General Public License
00016  *    along with this program; if not, write to the Free Software
00017  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00025 #include "Rule.h"
00026 #include "../utils/URI.h"
00027 #include "../utils/NameServer.h"
00028 #include "../idl/NameServer.hh"
00029 
00030 #include <stdlib.h>
00031 #include <regex.h>
00032 
00033 using fbfs::Rule;
00034 
00035 fbfs::Rule::Rule( int l, const string &com, const string &a) : level(l), arg(a) {
00036     if(com.compare("MATCH") == 0)       command = COM_MATCH;
00037     else if(com.compare("RESOLV") == 0) command = COM_RESOLV;
00038     else if(com.compare("OK") == 0)     command = COM_OK;
00039     else if(com.compare("NOTFOUND") == 0) command = COM_NOTFOUND;
00040     else if(com.compare("FALSE") == 0)  command = COM_FALSE;
00041     else if(com.compare("TRUE") == 0)   command = COM_TRUE;
00042     else if(com.compare("LOG") == 0)    command = COM_LOG;
00043     else if(com.compare("RANDOM") == 0) command = COM_RANDOM;
00044     else if(com.compare("EXTERN") == 0) command = COM_EXTERN;
00045     else                                command = COM_UNKNOWN;
00046 };
00047 
00048 fbfs::Rule::evalresult fbfs::Rule::eval( const string &id, string &result, const CORBA::ORB_ptr &orb ) {
00049     //cout << "Eval:: command:" << command << " arg:" << arg << " level:" << level << "\n";
00050     switch(command) {
00051         case COM_FALSE:
00052             return FALSE;
00053         case COM_NOTFOUND:
00054             return NOTFOUND;
00055         case COM_OK:
00056             result = arg;
00057             return OK;
00058         case COM_LOG:
00059             cerr << "Log: [Name=" << id << "] " << arg << "\n";
00060             return TRUE;
00061         case COM_TRUE:
00062             return TRUE;
00063         case COM_RANDOM:
00064             if( (atof(arg.c_str()) * rand()) / RAND_MAX <= 1 )
00065                 return TRUE;
00066             return FALSE;
00067         case COM_MATCH:
00068             regex_t rex;
00069             if(regcomp(&rex, arg.c_str(), 0)!=0) {
00070                 cerr << "Log: [Name=" << id << "] Regular Expression error\n";
00071                 return FALSE;
00072             }
00073             if( regexec(&rex, id.c_str(), 0, NULL,0) == 0 ) {
00074                 regfree(&rex);
00075                 return TRUE;
00076             }
00077             regfree(&rex);
00078             return FALSE;
00079         case COM_RESOLV:
00080             try {
00081                 utils::URI uri(arg);
00082                 CORBA::Object_var obj = utils::NameServer::get_object(orb, uri);
00083                 NameServer_var nameserver = NameServer::_narrow(obj);
00084                 Obj_id ident;
00085                 ident.name = CORBA::string_dup(id.c_str());
00086                 Security cert;
00087                 CORBA::String_var value = nameserver->lookup(cert, ident);
00088                 result = string(value);
00089                 return OK;
00090             } catch (...) {}
00091             return FALSE;
00092         case COM_EXTERN:
00093             // Migliorare affinché sia possibile passare il parametro id e
00094             // leggere la stringa di risultato in caso di OK.
00095             if(!system(arg.c_str()))
00096                 return TRUE;
00097             return FALSE;
00098         default:
00099             cerr << "Log: [Name=" << id << "] Unknown Command Executed\n";
00100             return FALSE;
00101     };
00102 };      

Generato il Thu Feb 15 13:24:56 2001 per A Simple Distributed Object Repository with CORBA & C++. da doxygen1.2.3 scritto da Dimitri van Heesch, © 1997-2000