hsms_factory.cpp

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include "log.h"
00024 #include "hsms_config.h"
00025 #include "hsms_cnx.h"
00026 #include "hsms_factory.h"
00027 
00028 #define HSMS_FACTORY_LOG_LEVEL 5
00029 
00030 using namespace freesecs;
00031 
00032 void
00033 hsms_factory_t::clear()
00034 {
00035     active.clear    ();
00036     passive.clear   ();
00037     errors.clear    ();
00038 }
00039 
00040 int
00041 hsms_factory_t::parse_config(const char *file_name)
00042 {
00043     FILE                    *fp;
00044     char                    buf[512];
00045     int                     done, len, res = NO_ERR;
00046     hsms_config_parser_t    hp;
00047 
00048     if(0 >= (fp = fopen(file_name, "r")))
00049     {
00050         sprintf(buf, "Filename %s is invalid.\n", file_name);
00051         errors.push_back(buf);
00052         res = INVALID_FILENAME;
00053     }
00054 
00055     if(fp)
00056     {
00057         for(;;)
00058         {
00059             fgets(buf, sizeof(buf), fp);
00060             len = strlen(buf);
00061     
00062             if(ferror(fp))
00063             {
00064                 sprintf(buf, "File %s read error.\n", file_name);
00065                 errors.push_back(buf);
00066                 res = PARSE_ERROR;
00067             }
00068     
00069             done = feof(fp);
00070             if (!hp.parse(buf, len, done))
00071             {
00072                 sprintf(buf, "File %s parse error.\n", file_name);
00073                 errors.push_back(buf);
00074                 res = PARSE_ERROR;
00075             }
00076             if (done)
00077             {
00078                 fclose(fp);
00079                 break;
00080             }
00081         }
00082     }
00083 
00084     if(NO_ERR == res)
00085     {
00086         if(hp.active_configs.empty() 
00087             && hp.passive_configs.empty())
00088         {
00089             TRACE_ERROR("factory: no configs found");
00090 
00091             if(hp.errors.empty())
00092             {
00093                 res = EMPTY_CONFIG;
00094             }
00095             else
00096             {
00097                 res = PARSE_ERROR;
00098             }
00099         }
00100         else
00101         {
00102             std::vector<hsms_active_config_t>::iterator it_a;
00103             std::vector<hsms_passive_config_t>::iterator it_p;
00104 
00105             for(it_a = hp.active_configs.begin(); 
00106                     it_a != hp.active_configs.end();
00107                     ++it_a)
00108             {
00109                 if(it_a->is_valid())
00110                 {
00111                     hsms_active_params_t ap = {   it_a->name, it_a->ip,
00112                                                 it_a->port, it_a->t5, 
00113                                                 it_a->t6, it_a->t8, 
00114                                                 it_a->linktest, it_a->session_id};
00115                     active.push_back(active_cnx_ptr_t(new hsms_active_cnx_t(ap)));
00116                     TRACE_DEBUG(HSMS_FACTORY_LOG_LEVEL, 
00117                                 "factory: added %s to active", it_a->name.c_str());
00118                 }
00119                 else
00120                 {
00121                     TRACE_DEBUG(HSMS_FACTORY_LOG_LEVEL,
00122                                 "factory: %s is not valid", it_a->name.c_str());
00123                 }
00124             }
00125             for(it_p = hp.passive_configs.begin(); 
00126                     it_p != hp.passive_configs.end();
00127                     ++it_p)
00128             {
00129                 if(it_p->is_valid())
00130                 {
00131                     hsms_passive_params_t pp = {  it_p->name, it_p->port, it_p->t6, 
00132                                                 it_p->t7, it_p->t8, 
00133                                                 it_p->linktest, it_p->session_id };
00134                     passive.push_back(passive_cnx_ptr_t(new hsms_passive_cnx_t(pp)));
00135                     TRACE_DEBUG(HSMS_FACTORY_LOG_LEVEL, 
00136                                 "factory: added %s to passive", it_p->name.c_str());
00137                 }
00138                 else
00139                 {
00140                     TRACE_DEBUG(HSMS_FACTORY_LOG_LEVEL,
00141                                 "factory: %s is not valid", it_p->name.c_str());
00142                 }
00143             }
00144         }
00145     }
00146 
00147     errors.insert(errors.end(), hp.errors.begin(), hp.errors.end());
00148 
00149     return res;
00150 }

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1