exp fixesw
[csql.git] / src / network / NetworkTable.cxx
blobcba8aed7f2bd578d4d90b75281f095ef0604f57a
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 <os.h>
21 #include <CSql.h>
22 #include <Network.h>
24 DbRetVal NetworkTable::initialize()
26 DbRetVal rv = OK;
27 //if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK;
28 rv = readNetworkConfig();
29 return rv;
32 NetworkTable::~NetworkTable()
34 //TODO::
36 DbRetVal NetworkTable::readNetworkConfig()
38 FILE *fp;
39 int nwid;
40 char hostname[IDENTIFIER_LENGTH];
41 int port;
42 //fp = fopen(Conf::config.getReplConfigFile(),"r");
43 if( fp == NULL ) {
44 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
45 return ErrSysInit;
47 int count = 0;
48 while(!feof(fp)) {
49 if (count >=1)
51 fclose(fp);
52 printError(ErrNotYet, "Only 2 hosts are allowed in this version");
53 return ErrNotYet;
55 printDebug(DM_Network, "Count is %d\n", count);
56 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
57 printDebug(DM_Network, "%d:%d:%s\n", nwid, port, hostname);
58 NetworkClient* nClient;
59 //if (nwid == Conf::config.getNetworkID()) continue;
61 nClient = NetworkFactory::createClient(TCP);
63 //printDebug(DM_Network, "nwid %d getCacheNetworkID %d\n", nwid, Conf::config.getCacheNetworkID());
64 //if (nwid == Conf::config.getCacheNetworkID()) nClient->setCacheClient();
65 nClient->setHost(hostname, port, nwid);
66 nwClient = nClient;
67 count++;
69 fclose(fp);
70 return OK;
74 //connect to peer hosts
75 DbRetVal NetworkTable::connect()
77 DbRetVal rv = nwClient->connect();
78 if (rv != OK) {
79 printError(ErrOS, "Unable to connect to peer %d\n", nwClient->getNetworkID());
80 nwClient->setConnectFlag(false);
82 return rv;
84 DbRetVal NetworkTable::connectIfNotConnected()
86 DbRetVal rv = OK;
87 if (!nwClient->isConnected()) rv = nwClient->connect(); else rv =ErrAlready;
88 return rv;
90 //disconnect from all hosts in the table
91 DbRetVal NetworkTable::disconnect()
93 if (nwClient->isConnected()) {
94 nwClient->disconnect();
96 return OK;