| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2021 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
/***************************************************************************** |
| 12 |
Written by |
| 13 |
Haivision Systems Inc. |
| 14 |
*****************************************************************************/ |
| 15 |
|
| 16 |
#include "platform_sys.h" |
| 17 |
#include <algorithm> |
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
#include "group_backup.h" |
| 21 |
|
| 22 |
|
| 23 |
namespace srt |
| 24 |
{ |
| 25 |
namespace groups |
| 26 |
{ |
| 27 |
|
| 28 |
using namespace std; |
| 29 |
using namespace srt_logging; |
| 30 |
|
| 31 |
const char* stateToStr(BackupMemberState state) |
| 32 |
{ |
| 33 |
switch (state) |
| 34 |
{ |
| 35 |
case srt::groups::BKUPST_UNKNOWN: |
| 36 |
return "UNKNOWN"; |
| 37 |
case srt::groups::BKUPST_PENDING: |
| 38 |
return "PENDING"; |
| 39 |
case srt::groups::BKUPST_STANDBY: |
| 40 |
return "STANDBY"; |
| 41 |
case srt::groups::BKUPST_ACTIVE_FRESH: |
| 42 |
return "ACTIVE_FRESH"; |
| 43 |
case srt::groups::BKUPST_ACTIVE_STABLE: |
| 44 |
return "ACTIVE_STABLE"; |
| 45 |
case srt::groups::BKUPST_ACTIVE_UNSTABLE: |
| 46 |
return "ACTIVE_UNSTABLE"; |
| 47 |
case srt::groups::BKUPST_ACTIVE_UNSTABLE_WARY: |
| 48 |
return "ACTIVE_UNSTABLE_WARY"; |
| 49 |
case srt::groups::BKUPST_BROKEN: |
| 50 |
return "BROKEN"; |
| 51 |
default: |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
return "WRONG_STATE"; |
| 56 |
} |
| 57 |
|
| 58 |
/// @brief Compares group members by their weight (higher weight comes first), then state. |
| 59 |
/// Higher weight comes first, same weight: stable, then fresh active. |
| 60 |
struct FCompareByWeight |
| 61 |
{ |
| 62 |
/// @returns true if the first argument is less than (i.e. is ordered before) the second. |
| 63 |
bool operator()(const BackupMemberStateEntry& a, const BackupMemberStateEntry& b) |
| 64 |
{ |
| 65 |
if (a.pSocketData != NULL && b.pSocketData != NULL |
| 66 |
&& (a.pSocketData->weight != b.pSocketData->weight)) |
| 67 |
return a.pSocketData->weight > b.pSocketData->weight; |
| 68 |
|
| 69 |
if (a.state != b.state) |
| 70 |
{ |
| 71 |
SRT_STATIC_ASSERT(BKUPST_ACTIVE_STABLE > BKUPST_ACTIVE_FRESH, "Wrong ordering"); |
| 72 |
return a.state > b.state; |
| 73 |
} |
| 74 |
|
| 75 |
// the order does not matter, but comparator must return a different value for not equal a and b |
| 76 |
return a.socketID < b.socketID; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
void SendBackupCtx::recordMemberState(SocketData* pSockData, BackupMemberState st) |
| 81 |
{ |
| 82 |
m_memberStates.push_back(BackupMemberStateEntry(pSockData, st)); |
| 83 |
++m_stateCounter[st]; |
| 84 |
|
| 85 |
if (st == BKUPST_STANDBY) |
| 86 |
{ |
| 87 |
m_standbyMaxWeight = max(m_standbyMaxWeight, pSockData->weight); |
| 88 |
} |
| 89 |
else if (isStateActive(st)) |
| 90 |
{ |
| 91 |
m_activeMaxWeight = max(m_activeMaxWeight, pSockData->weight); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void SendBackupCtx::updateMemberState(const SocketData* pSockData, BackupMemberState st) |
| 96 |
{ |
| 97 |
typedef vector<BackupMemberStateEntry>::iterator iter_t; |
| 98 |
for (iter_t i = m_memberStates.begin(); i != m_memberStates.end(); ++i) |
| 99 |
{ |
| 100 |
if (i->pSocketData == NULL) |
| 101 |
continue; |
| 102 |
|
| 103 |
if (i->pSocketData != pSockData) |
| 104 |
continue; |
| 105 |
|
| 106 |
if (i->state == st) |
| 107 |
return; |
| 108 |
|
| 109 |
--m_stateCounter[i->state]; |
| 110 |
++m_stateCounter[st]; |
| 111 |
i->state = st; |
| 112 |
|
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
LOGC(gslog.Error, |
| 118 |
log << "IPE: SendBackupCtx::updateMemberState failed to locate member"); |
| 119 |
} |
| 120 |
|
| 121 |
void SendBackupCtx::sortByWeightAndState() |
| 122 |
{ |
| 123 |
sort(m_memberStates.begin(), m_memberStates.end(), FCompareByWeight()); |
| 124 |
} |
| 125 |
|
| 126 |
BackupMemberState SendBackupCtx::getMemberState(const SocketData* pSockData) const |
| 127 |
{ |
| 128 |
typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t; |
| 129 |
for (const_iter_t i = m_memberStates.begin(); i != m_memberStates.end(); ++i) |
| 130 |
{ |
| 131 |
if (i->pSocketData != pSockData) |
| 132 |
continue; |
| 133 |
|
| 134 |
return i->state; |
| 135 |
} |
| 136 |
|
| 137 |
// The entry was not found |
| 138 |
// TODO: Maybe throw an exception here? |
| 139 |
return BKUPST_UNKNOWN; |
| 140 |
} |
| 141 |
|
| 142 |
unsigned SendBackupCtx::countMembersByState(BackupMemberState st) const |
| 143 |
{ |
| 144 |
return m_stateCounter[st]; |
| 145 |
} |
| 146 |
|
| 147 |
std::string SendBackupCtx::printMembers() const |
| 148 |
{ |
| 149 |
stringstream ss; |
| 150 |
typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t; |
| 151 |
for (const_iter_t i = m_memberStates.begin(); i != m_memberStates.end(); ++i) |
| 152 |
{ |
| 153 |
ss << "@" << i->socketID << " w " << i->pSocketData->weight << " state " << stateToStr(i->state) << ", "; |
| 154 |
} |
| 155 |
return ss.str(); |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace groups |
| 159 |
} // namespace srt |