#ifndef GRPC_SRC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
#define GRPC_SRC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
#include <grpc/support/port_platform.h>
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/iomgr/resolved_address.h"
namespace grpc_core {
class ServerAddress {
public:
class AttributeInterface {
public:
virtual ~AttributeInterface() = default;
virtual std::unique_ptr<AttributeInterface> Copy() const = 0;
virtual int Cmp(const AttributeInterface* other) const = 0;
virtual std::string ToString() const = 0;
};
ServerAddress(const grpc_resolved_address& address, const ChannelArgs& args,
std::map<const char*, std::unique_ptr<AttributeInterface>>
attributes = {});
ServerAddress(const ServerAddress& other);
ServerAddress& operator=(const ServerAddress& other);
ServerAddress(ServerAddress&& other) noexcept;
ServerAddress& operator=(ServerAddress&& other) noexcept;
bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; }
int Cmp(const ServerAddress& other) const;
const grpc_resolved_address& address() const { return address_; }
const ChannelArgs& args() const { return args_; }
const AttributeInterface* GetAttribute(const char* key) const;
ServerAddress WithAttribute(const char* key,
std::unique_ptr<AttributeInterface> value) const;
std::string ToString() const;
private:
grpc_resolved_address address_;
ChannelArgs args_;
std::map<const char*, std::unique_ptr<AttributeInterface>> attributes_;
};
using ServerAddressList = std::vector<ServerAddress>;
class ServerAddressWeightAttribute : public ServerAddress::AttributeInterface {
public:
static const char* kServerAddressWeightAttributeKey;
explicit ServerAddressWeightAttribute(uint32_t weight) : weight_(weight) {}
uint32_t weight() const { return weight_; }
std::unique_ptr<AttributeInterface> Copy() const override {
return std::make_unique<ServerAddressWeightAttribute>(weight_);
}
int Cmp(const AttributeInterface* other) const override {
const auto* other_locality_attr =
static_cast<const ServerAddressWeightAttribute*>(other);
return QsortCompare(weight_, other_locality_attr->weight_);
}
std::string ToString() const override;
private:
uint32_t weight_;
};
}
#endif