00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
00090 DbRetVal NetworkTable::disconnect()
00091 {
00092 if (nwClient->isConnected()) {
00093 nwClient->disconnect();
00094 }
00095 return OK;
00096 }