src/network/NetworkTable.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
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                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <CSql.h>
00021 #include <Network.h>
00022 
00023 DbRetVal NetworkTable::initialize()
00024 {
00025     DbRetVal rv = OK;
00026     if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK;
00027     rv = readNetworkConfig();
00028     return rv;
00029     
00030 }
00031 NetworkTable::~NetworkTable()
00032 {
00033     //TODO::
00034 }
00035 DbRetVal NetworkTable::readNetworkConfig()
00036 {
00037     FILE *fp;
00038     int nwid;
00039     char hostname[IDENTIFIER_LENGTH];
00040     int port;
00041     fp = fopen(Conf::config.getReplConfigFile(),"r");
00042     if( fp == NULL ) {
00043         printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
00044         return ErrSysInit;
00045     }
00046     int count = 0;
00047     while(!feof(fp)) {
00048        if (count >=1) 
00049        {
00050            fclose(fp);
00051            printError(ErrNotYet, "Only 2 hosts are allowed in this version"); 
00052            return ErrNotYet;
00053        }
00054        printDebug(DM_Network, "Count is %d\n", count);
00055        fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
00056        printDebug(DM_Network, "%d:%d:%s\n", nwid, port, hostname);
00057        NetworkClient* nClient;
00058        if (nwid == Conf::config.getNetworkID()) continue;
00059 
00060        nClient = NetworkFactory::createClient(TCP);
00061 
00062        printDebug(DM_Network, "nwid %d getCacheNetworkID %d\n", nwid,  Conf::config.getCacheNetworkID());
00063        if (nwid == Conf::config.getCacheNetworkID()) nClient->setCacheClient();
00064        nClient->setHost(hostname, port, nwid);
00065        nwClient = nClient;
00066        count++;
00067     }
00068     fclose(fp);
00069     return OK;
00070 }
00071 
00072 
00073 //connect to peer hosts 
00074 DbRetVal  NetworkTable::connect()
00075 {
00076     DbRetVal rv = nwClient->connect();
00077     if (rv != OK) {
00078         printError(ErrOS, "Unable to connect to peer %d\n", nwClient->getNetworkID());
00079         nwClient->setConnectFlag(false);
00080     }
00081     return rv;
00082 }
00083 DbRetVal NetworkTable::connectIfNotConnected()
00084 {
00085    DbRetVal rv = OK;
00086    if (!nwClient->isConnected()) rv = nwClient->connect(); else rv =ErrAlready;
00087    return rv;
00088 }
00089 //disconnect from all hosts in the table
00090 DbRetVal NetworkTable::disconnect()
00091 {
00092     if (nwClient->isConnected()) {
00093         nwClient->disconnect();
00094     }
00095     return OK;
00096 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7