From 18c511c8f6aeb2c0a3cc7e01253defbb8bc521dc Mon Sep 17 00:00:00 2001 From: Tom Early Date: Sat, 21 Mar 2020 14:16:38 -0700 Subject: [PATCH] independent cache for each IRC client --- CacheManager.cpp | 34 +++++++--------------------- CacheManager.h | 3 +-- QnetGateway.cpp | 59 ++++++++++++++++++++++++------------------------ QnetGateway.h | 4 ++-- 4 files changed, 40 insertions(+), 60 deletions(-) diff --git a/CacheManager.cpp b/CacheManager.cpp index de94d1b..35717d9 100644 --- a/CacheManager.cpp +++ b/CacheManager.cpp @@ -124,10 +124,7 @@ void CCacheManager::updateUser(const std::string &user, const std::string &rptr, if (rptr.compare(0, 7, gate, 0, 7)) RptrGate[rptr] = gate; // only do this if they differ - if (addr.npos == addr.find(':')) - GateIPV4[gate] = addr; - else - GateIPV6[gate] = addr; + GateAddr[gate] = addr; mux.unlock(); } @@ -142,10 +139,7 @@ void CCacheManager::updateRptr(const std::string &rptr, const std::string &gate, mux.unlock(); return; } - if (addr.npos == addr.find(':')) - GateIPV4[gate] = addr; - else - GateIPV6[gate] = addr; + GateAddr[gate] = addr; mux.unlock(); } @@ -160,10 +154,7 @@ void CCacheManager::updateGate(const std::string &G, const std::string &addr) p = gate.find('_'); } mux.lock(); - if (addr.npos == addr.find(':')) - GateIPV4[gate] = addr; - else - GateIPV6[gate] = addr; + GateAddr[gate] = addr; mux.unlock(); } @@ -179,8 +170,7 @@ void CCacheManager::updateName(const std::string &name, const std::string &nick) void CCacheManager::eraseGate(const std::string &gate) { mux.lock(); - GateIPV4.erase(gate); - GateIPV6.erase(gate); + GateAddr.erase(gate); mux.unlock(); } @@ -194,8 +184,7 @@ void CCacheManager::eraseName(const std::string &name) void CCacheManager::clearGate() { mux.lock(); - GateIPV4.clear(); - GateIPV6.clear(); + GateAddr.clear(); NameNick.clear(); mux.unlock(); } @@ -231,15 +220,8 @@ std::string CCacheManager::findGateAddr(const std::string &gate) std::string addr; if (gate.empty()) return addr; - auto it6 = GateIPV6.find(gate); - if (it6 == GateIPV6.end()) { - auto it4 = GateIPV4.find(gate); - if (it4 == GateIPV4.end()) { - return addr; - } else { - addr.assign(it4->second); - } - } else - addr.assign(it6->second); + auto ita = GateAddr.find(gate); + if (ita != GateAddr.end()) + addr.assign(ita->second); return addr; } diff --git a/CacheManager.h b/CacheManager.h index e62f1b2..048b2a9 100644 --- a/CacheManager.h +++ b/CacheManager.h @@ -55,8 +55,7 @@ private: std::unordered_map UserTime; std::unordered_map UserRptr; std::unordered_map RptrGate; - std::unordered_map GateIPV4; - std::unordered_map GateIPV6; + std::unordered_map GateAddr; std::unordered_map NameNick; std::mutex mux; }; diff --git a/QnetGateway.cpp b/QnetGateway.cpp index 924a281..6b143b6 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -452,7 +452,7 @@ void CQnetGateway::GetIRCDataThread(const int i) ii[i]->receivePing(rptr); if (! rptr.empty()) { ReplaceChar(rptr, '_', ' '); - cache.findRptrData(rptr, gate, addr); + cache[i].findRptrData(rptr, gate, addr); if (addr.empty()) break; CSockAddress to; @@ -477,11 +477,11 @@ void CQnetGateway::GetIRCDataThread(const int i) } /* return codes: 0=OK(found it), 1=TRY AGAIN, 2=FAILED(bad data) */ -int CQnetGateway::get_yrcall_rptr_from_cache(const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU) +int CQnetGateway::get_yrcall_rptr_from_cache(const int i, const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU) { switch (RoU) { case 'U': - cache.findUserData(call, rptr, gate, addr); + cache[i].findUserData(call, rptr, gate, addr); if (rptr.empty()) { printf("Could not find last heard repeater for user '%s'\n", call.c_str()); return 1; @@ -489,7 +489,7 @@ int CQnetGateway::get_yrcall_rptr_from_cache(const std::string &call, std::strin break; case 'R': rptr.assign(call); - cache.findRptrData(call, gate, addr); + cache[i].findRptrData(call, gate, addr); break; default: fprintf(stderr, "ERROR: Invalid Rou of '%c'\n", RoU); @@ -512,35 +512,34 @@ int CQnetGateway::get_yrcall_rptr_from_cache(const std::string &call, std::strin int CQnetGateway::get_yrcall_rptr(const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU) // returns 0 if unsuccessful, otherwise returns ii index plus one { - int i = 0; - int rc = get_yrcall_rptr_from_cache(call, rptr, gate, addr, RoU); - if (0 == rc) { - //printf("get_yrcall_rptr_from_cache: call='%s' rptr='%s' gate='%s', addr='%s' RoU=%c\n", call.c_str(), rptr.c_str(), gate.c_str(), addr.c_str(), RoU); - if ((addr.npos == addr.find(':')) && ii[1]) - i = 1; - return i + 1; - } else if (2 == rc) - return 0; + int rval[2] = { 1, 1 }; + for (int i=0; i<2; i++) { + if (ii[i]) { + rval[i] = get_yrcall_rptr_from_cache(i, call, rptr, gate, addr, RoU); + if (0 == rval[i]) + return i + 1; + } + } /* at this point, the data is not in cache */ - int cstate = ii[i]->getConnectionState(); - if (cstate < 6) { - return 0; - } else { - // we can try a find - if (RoU == 'U') { - printf("User [%s] not in local cache, try again\n", call.c_str()); - /*** YRCALL=KJ4NHFBL ***/ - if (((call.at(6) == 'A') || (call.at(6) == 'B') || (call.at(6) == 'C')) && (call.at(7) == 'L')) - printf("If this was a gateway link request, that is ok\n"); - if (!ii[i]->findUser(call)) - printf("findUser(%s): Network error\n", call.c_str()); - } else if (RoU == 'R') { - printf("Repeater [%s] not found\n", call.c_str()); + for (int i=0; i<2; i++) { + if (ii[i] && (1 == rval[i])) { + if (ii[i]->getConnectionState() > 5) { + // we can try a find + if (RoU == 'U') { + printf("User [%s] not in local cache, try again\n", call.c_str()); + /*** YRCALL=KJ4NHFBL ***/ + if (((call.at(6) == 'A') || (call.at(6) == 'B') || (call.at(6) == 'C')) && (call.at(7) == 'L')) + printf("If this was a gateway link request, that is ok\n"); + if (!ii[i]->findUser(call)) + printf("findUser(%s): Network error\n", call.c_str()); + } else if (RoU == 'R') { + printf("Repeater [%s] not found\n", call.c_str()); + } + } } - return 0; } - return i + 1; + return 0; } bool CQnetGateway::Flag_is_ok(unsigned char flag) @@ -2341,7 +2340,7 @@ bool CQnetGateway::Init(char *cfgfile) for (int j=0; j<2; j++) { if (ircddb[j].ip.empty()) continue; - ii[j] = new CIRCDDB(ircddb[j].ip, ircddb[j].port, owner, IRCDDB_PASSWORD[j], GW_VERSION.c_str(), &cache); + ii[j] = new CIRCDDB(ircddb[j].ip, ircddb[j].port, owner, IRCDDB_PASSWORD[j], GW_VERSION.c_str(), &cache[j]); bool ok = ii[j]->open(); if (!ok) { printf("%s open failed\n", ircddb[j].ip.c_str()); diff --git a/QnetGateway.h b/QnetGateway.h index adc3b33..262acfa 100644 --- a/QnetGateway.h +++ b/QnetGateway.h @@ -159,7 +159,7 @@ private: CDStarDecode decode; // CACHE used to cache users, repeaters, gateways and addresses - CCacheManager cache; + CCacheManager cache[2]; // dtmf stuff int dtmf_buf_count[3]; @@ -172,7 +172,7 @@ private: int open_port(const SPORTIP *pip, int family); void calcPFCS(unsigned char *packet, int len); void GetIRCDataThread(const int i); - int get_yrcall_rptr_from_cache(const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU); + int get_yrcall_rptr_from_cache(const int i, const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU); int get_yrcall_rptr(const std::string &call, std::string &rptr, std::string &gate, std::string &addr, char RoU); void PlayFileThread(SECHO &edata); void compute_aprs_hash();