typo
[csql.git] / src / network / NetworkTable.cxx
blob802749c299bc303b89a92b169f08ab2ae3f921e8
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <CSql.h>
21 #include <Network.h>
23 DbRetVal NetworkTable::initialize()
25 DbRetVal rv = OK;
26 if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK;
27 rv = readNetworkConfig();
28 return rv;
31 NetworkTable::~NetworkTable()
33 //TODO::
35 DbRetVal NetworkTable::readNetworkConfig()
37 FILE *fp;
38 int nwid;
39 char hostname[IDENTIFIER_LENGTH];
40 int port;
41 fp = fopen(Conf::config.getReplConfigFile(),"r");
42 if( fp == NULL ) {
43 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
44 return ErrSysInit;
46 int count = 0;
47 while(!feof(fp)) {
48 if (count >=1)
50 fclose(fp);
51 printError(ErrNotYet, "Only 2 hosts are allowed in this version");
52 return ErrNotYet;
54 printDebug(DM_Network, "Count is %d\n", count);
55 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
56 printDebug(DM_Network, "%d:%d:%s\n", nwid, port, hostname);
57 NetworkClient* nClient;
58 if (nwid == Conf::config.getNetworkID()) continue;
60 nClient = NetworkFactory::createClient(TCP);
62 printDebug(DM_Network, "nwid %d getCacheNetworkID %d\n", nwid, Conf::config.getCacheNetworkID());
63 if (nwid == Conf::config.getCacheNetworkID()) nClient->setCacheClient();
64 nClient->setHost(hostname, port, nwid);
65 nwClient = nClient;
66 count++;
68 fclose(fp);
69 return OK;
73 //connect to peer hosts
74 DbRetVal NetworkTable::connect()
76 DbRetVal rv = nwClient->connect();
77 if (rv != OK) {
78 printError(ErrOS, "Unable to connect to peer %d\n", nwClient->getNetworkID());
79 nwClient->setConnectFlag(false);
81 return rv;
83 DbRetVal NetworkTable::connectIfNotConnected()
85 DbRetVal rv = OK;
86 if (!nwClient->isConnected()) rv = nwClient->connect(); else rv =ErrAlready;
87 return rv;
89 //disconnect from all hosts in the table
90 DbRetVal NetworkTable::disconnect()
92 if (nwClient->isConnected()) {
93 nwClient->disconnect();
95 return OK;