# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: cluster.proto
# plugin: python-betterproto
from dataclasses import dataclass
from typing import Dict, List

import betterproto
import grpclib
from betterproto.grpc.grpclib_server import ServiceBase


class MemberInfoVNodeState(betterproto.Enum):
    Initializing = 0
    DiscoverLeader = 1
    Unknown = 2
    PreReplica = 3
    CatchingUp = 4
    Clone = 5
    Follower = 6
    PreLeader = 7
    Leader = 8
    Manager = 9
    ShuttingDown = 10
    Shutdown = 11
    ReadOnlyLeaderless = 12
    PreReadOnlyReplica = 13
    ReadOnlyReplica = 14
    ResigningLeader = 15


@dataclass(eq=False, repr=False)
class GossipRequest(betterproto.Message):
    info: "ClusterInfo" = betterproto.message_field(1)
    server: "EndPoint" = betterproto.message_field(2)


@dataclass(eq=False, repr=False)
class ViewChangeRequest(betterproto.Message):
    server_id: "_client__.Uuid" = betterproto.message_field(1)
    server_http: "EndPoint" = betterproto.message_field(2)
    attempted_view: int = betterproto.int32_field(3)


@dataclass(eq=False, repr=False)
class ViewChangeProofRequest(betterproto.Message):
    server_id: "_client__.Uuid" = betterproto.message_field(1)
    server_http: "EndPoint" = betterproto.message_field(2)
    installed_view: int = betterproto.int32_field(3)


@dataclass(eq=False, repr=False)
class PrepareRequest(betterproto.Message):
    server_id: "_client__.Uuid" = betterproto.message_field(1)
    server_http: "EndPoint" = betterproto.message_field(2)
    view: int = betterproto.int32_field(3)


@dataclass(eq=False, repr=False)
class PrepareOkRequest(betterproto.Message):
    view: int = betterproto.int32_field(1)
    server_id: "_client__.Uuid" = betterproto.message_field(2)
    server_http: "EndPoint" = betterproto.message_field(3)
    epoch_number: int = betterproto.int32_field(4)
    epoch_position: int = betterproto.int64_field(5)
    epoch_id: "_client__.Uuid" = betterproto.message_field(6)
    epoch_leader_instance_id: "_client__.Uuid" = betterproto.message_field(7)
    last_commit_position: int = betterproto.int64_field(8)
    writer_checkpoint: int = betterproto.int64_field(9)
    chaser_checkpoint: int = betterproto.int64_field(10)
    node_priority: int = betterproto.int32_field(11)
    cluster_info: "ClusterInfo" = betterproto.message_field(12)


@dataclass(eq=False, repr=False)
class ProposalRequest(betterproto.Message):
    server_id: "_client__.Uuid" = betterproto.message_field(1)
    server_http: "EndPoint" = betterproto.message_field(2)
    leader_id: "_client__.Uuid" = betterproto.message_field(3)
    leader_http: "EndPoint" = betterproto.message_field(4)
    view: int = betterproto.int32_field(5)
    epoch_number: int = betterproto.int32_field(6)
    epoch_position: int = betterproto.int64_field(7)
    epoch_id: "_client__.Uuid" = betterproto.message_field(8)
    epoch_leader_instance_id: "_client__.Uuid" = betterproto.message_field(9)
    last_commit_position: int = betterproto.int64_field(10)
    writer_checkpoint: int = betterproto.int64_field(11)
    chaser_checkpoint: int = betterproto.int64_field(12)
    node_priority: int = betterproto.int32_field(13)


@dataclass(eq=False, repr=False)
class AcceptRequest(betterproto.Message):
    server_id: "_client__.Uuid" = betterproto.message_field(1)
    server_http: "EndPoint" = betterproto.message_field(2)
    leader_id: "_client__.Uuid" = betterproto.message_field(3)
    leader_http: "EndPoint" = betterproto.message_field(4)
    view: int = betterproto.int32_field(5)


@dataclass(eq=False, repr=False)
class LeaderIsResigningRequest(betterproto.Message):
    leader_id: "_client__.Uuid" = betterproto.message_field(1)
    leader_http: "EndPoint" = betterproto.message_field(2)


@dataclass(eq=False, repr=False)
class LeaderIsResigningOkRequest(betterproto.Message):
    leader_id: "_client__.Uuid" = betterproto.message_field(1)
    leader_http: "EndPoint" = betterproto.message_field(2)
    server_id: "_client__.Uuid" = betterproto.message_field(3)
    server_http: "EndPoint" = betterproto.message_field(4)


@dataclass(eq=False, repr=False)
class ClusterInfo(betterproto.Message):
    members: List["MemberInfo"] = betterproto.message_field(1)


@dataclass(eq=False, repr=False)
class EndPoint(betterproto.Message):
    address: str = betterproto.string_field(1)
    port: int = betterproto.uint32_field(2)


@dataclass(eq=False, repr=False)
class MemberInfo(betterproto.Message):
    instance_id: "_client__.Uuid" = betterproto.message_field(1)
    time_stamp: int = betterproto.int64_field(2)
    state: "MemberInfoVNodeState" = betterproto.enum_field(3)
    is_alive: bool = betterproto.bool_field(4)
    http_end_point: "EndPoint" = betterproto.message_field(5)
    internal_tcp: "EndPoint" = betterproto.message_field(6)
    external_tcp: "EndPoint" = betterproto.message_field(7)
    internal_tcp_uses_tls: bool = betterproto.bool_field(8)
    external_tcp_uses_tls: bool = betterproto.bool_field(9)
    last_commit_position: int = betterproto.int64_field(10)
    writer_checkpoint: int = betterproto.int64_field(11)
    chaser_checkpoint: int = betterproto.int64_field(12)
    epoch_position: int = betterproto.int64_field(13)
    epoch_number: int = betterproto.int32_field(14)
    epoch_id: "_client__.Uuid" = betterproto.message_field(15)
    node_priority: int = betterproto.int32_field(16)
    is_read_only_replica: bool = betterproto.bool_field(17)
    advertise_host_to_client_as: str = betterproto.string_field(18)
    advertise_http_port_to_client_as: int = betterproto.uint32_field(19)
    advertise_tcp_port_to_client_as: int = betterproto.uint32_field(20)


class GossipStub(betterproto.ServiceStub):
    async def update(
        self, *, info: "ClusterInfo" = None, server: "EndPoint" = None
    ) -> "ClusterInfo":

        request = GossipRequest()
        if info is not None:
            request.info = info
        if server is not None:
            request.server = server

        return await self._unary_unary(
            "/event_store.cluster.Gossip/Update", request, ClusterInfo
        )

    async def read(self) -> "ClusterInfo":

        request = _client__.Empty()

        return await self._unary_unary(
            "/event_store.cluster.Gossip/Read", request, ClusterInfo
        )


class ElectionsStub(betterproto.ServiceStub):
    async def view_change(
        self,
        *,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        attempted_view: int = 0
    ) -> "_client__.Empty":

        request = ViewChangeRequest()
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        request.attempted_view = attempted_view

        return await self._unary_unary(
            "/event_store.cluster.Elections/ViewChange", request, _client__.Empty
        )

    async def view_change_proof(
        self,
        *,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        installed_view: int = 0
    ) -> "_client__.Empty":

        request = ViewChangeProofRequest()
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        request.installed_view = installed_view

        return await self._unary_unary(
            "/event_store.cluster.Elections/ViewChangeProof", request, _client__.Empty
        )

    async def prepare(
        self,
        *,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        view: int = 0
    ) -> "_client__.Empty":

        request = PrepareRequest()
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        request.view = view

        return await self._unary_unary(
            "/event_store.cluster.Elections/Prepare", request, _client__.Empty
        )

    async def prepare_ok(
        self,
        *,
        view: int = 0,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        epoch_number: int = 0,
        epoch_position: int = 0,
        epoch_id: "_client__.Uuid" = None,
        epoch_leader_instance_id: "_client__.Uuid" = None,
        last_commit_position: int = 0,
        writer_checkpoint: int = 0,
        chaser_checkpoint: int = 0,
        node_priority: int = 0,
        cluster_info: "ClusterInfo" = None
    ) -> "_client__.Empty":

        request = PrepareOkRequest()
        request.view = view
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        request.epoch_number = epoch_number
        request.epoch_position = epoch_position
        if epoch_id is not None:
            request.epoch_id = epoch_id
        if epoch_leader_instance_id is not None:
            request.epoch_leader_instance_id = epoch_leader_instance_id
        request.last_commit_position = last_commit_position
        request.writer_checkpoint = writer_checkpoint
        request.chaser_checkpoint = chaser_checkpoint
        request.node_priority = node_priority
        if cluster_info is not None:
            request.cluster_info = cluster_info

        return await self._unary_unary(
            "/event_store.cluster.Elections/PrepareOk", request, _client__.Empty
        )

    async def proposal(
        self,
        *,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        leader_id: "_client__.Uuid" = None,
        leader_http: "EndPoint" = None,
        view: int = 0,
        epoch_number: int = 0,
        epoch_position: int = 0,
        epoch_id: "_client__.Uuid" = None,
        epoch_leader_instance_id: "_client__.Uuid" = None,
        last_commit_position: int = 0,
        writer_checkpoint: int = 0,
        chaser_checkpoint: int = 0,
        node_priority: int = 0
    ) -> "_client__.Empty":

        request = ProposalRequest()
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        if leader_id is not None:
            request.leader_id = leader_id
        if leader_http is not None:
            request.leader_http = leader_http
        request.view = view
        request.epoch_number = epoch_number
        request.epoch_position = epoch_position
        if epoch_id is not None:
            request.epoch_id = epoch_id
        if epoch_leader_instance_id is not None:
            request.epoch_leader_instance_id = epoch_leader_instance_id
        request.last_commit_position = last_commit_position
        request.writer_checkpoint = writer_checkpoint
        request.chaser_checkpoint = chaser_checkpoint
        request.node_priority = node_priority

        return await self._unary_unary(
            "/event_store.cluster.Elections/Proposal", request, _client__.Empty
        )

    async def accept(
        self,
        *,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None,
        leader_id: "_client__.Uuid" = None,
        leader_http: "EndPoint" = None,
        view: int = 0
    ) -> "_client__.Empty":

        request = AcceptRequest()
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http
        if leader_id is not None:
            request.leader_id = leader_id
        if leader_http is not None:
            request.leader_http = leader_http
        request.view = view

        return await self._unary_unary(
            "/event_store.cluster.Elections/Accept", request, _client__.Empty
        )

    async def leader_is_resigning(
        self, *, leader_id: "_client__.Uuid" = None, leader_http: "EndPoint" = None
    ) -> "_client__.Empty":

        request = LeaderIsResigningRequest()
        if leader_id is not None:
            request.leader_id = leader_id
        if leader_http is not None:
            request.leader_http = leader_http

        return await self._unary_unary(
            "/event_store.cluster.Elections/LeaderIsResigning", request, _client__.Empty
        )

    async def leader_is_resigning_ok(
        self,
        *,
        leader_id: "_client__.Uuid" = None,
        leader_http: "EndPoint" = None,
        server_id: "_client__.Uuid" = None,
        server_http: "EndPoint" = None
    ) -> "_client__.Empty":

        request = LeaderIsResigningOkRequest()
        if leader_id is not None:
            request.leader_id = leader_id
        if leader_http is not None:
            request.leader_http = leader_http
        if server_id is not None:
            request.server_id = server_id
        if server_http is not None:
            request.server_http = server_http

        return await self._unary_unary(
            "/event_store.cluster.Elections/LeaderIsResigningOk",
            request,
            _client__.Empty,
        )


class GossipBase(ServiceBase):
    async def update(self, info: "ClusterInfo", server: "EndPoint") -> "ClusterInfo":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def read(self) -> "ClusterInfo":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_update(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "info": request.info,
            "server": request.server,
        }

        response = await self.update(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_read(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {}

        response = await self.read(**request_kwargs)
        await stream.send_message(response)

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/event_store.cluster.Gossip/Update": grpclib.const.Handler(
                self.__rpc_update,
                grpclib.const.Cardinality.UNARY_UNARY,
                GossipRequest,
                ClusterInfo,
            ),
            "/event_store.cluster.Gossip/Read": grpclib.const.Handler(
                self.__rpc_read,
                grpclib.const.Cardinality.UNARY_UNARY,
                _client__.Empty,
                ClusterInfo,
            ),
        }


class ElectionsBase(ServiceBase):
    async def view_change(
        self, server_id: "_client__.Uuid", server_http: "EndPoint", attempted_view: int
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def view_change_proof(
        self, server_id: "_client__.Uuid", server_http: "EndPoint", installed_view: int
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def prepare(
        self, server_id: "_client__.Uuid", server_http: "EndPoint", view: int
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def prepare_ok(
        self,
        view: int,
        server_id: "_client__.Uuid",
        server_http: "EndPoint",
        epoch_number: int,
        epoch_position: int,
        epoch_id: "_client__.Uuid",
        epoch_leader_instance_id: "_client__.Uuid",
        last_commit_position: int,
        writer_checkpoint: int,
        chaser_checkpoint: int,
        node_priority: int,
        cluster_info: "ClusterInfo",
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def proposal(
        self,
        server_id: "_client__.Uuid",
        server_http: "EndPoint",
        leader_id: "_client__.Uuid",
        leader_http: "EndPoint",
        view: int,
        epoch_number: int,
        epoch_position: int,
        epoch_id: "_client__.Uuid",
        epoch_leader_instance_id: "_client__.Uuid",
        last_commit_position: int,
        writer_checkpoint: int,
        chaser_checkpoint: int,
        node_priority: int,
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def accept(
        self,
        server_id: "_client__.Uuid",
        server_http: "EndPoint",
        leader_id: "_client__.Uuid",
        leader_http: "EndPoint",
        view: int,
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def leader_is_resigning(
        self, leader_id: "_client__.Uuid", leader_http: "EndPoint"
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def leader_is_resigning_ok(
        self,
        leader_id: "_client__.Uuid",
        leader_http: "EndPoint",
        server_id: "_client__.Uuid",
        server_http: "EndPoint",
    ) -> "_client__.Empty":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_view_change(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "server_id": request.server_id,
            "server_http": request.server_http,
            "attempted_view": request.attempted_view,
        }

        response = await self.view_change(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_view_change_proof(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "server_id": request.server_id,
            "server_http": request.server_http,
            "installed_view": request.installed_view,
        }

        response = await self.view_change_proof(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_prepare(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "server_id": request.server_id,
            "server_http": request.server_http,
            "view": request.view,
        }

        response = await self.prepare(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_prepare_ok(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "view": request.view,
            "server_id": request.server_id,
            "server_http": request.server_http,
            "epoch_number": request.epoch_number,
            "epoch_position": request.epoch_position,
            "epoch_id": request.epoch_id,
            "epoch_leader_instance_id": request.epoch_leader_instance_id,
            "last_commit_position": request.last_commit_position,
            "writer_checkpoint": request.writer_checkpoint,
            "chaser_checkpoint": request.chaser_checkpoint,
            "node_priority": request.node_priority,
            "cluster_info": request.cluster_info,
        }

        response = await self.prepare_ok(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_proposal(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "server_id": request.server_id,
            "server_http": request.server_http,
            "leader_id": request.leader_id,
            "leader_http": request.leader_http,
            "view": request.view,
            "epoch_number": request.epoch_number,
            "epoch_position": request.epoch_position,
            "epoch_id": request.epoch_id,
            "epoch_leader_instance_id": request.epoch_leader_instance_id,
            "last_commit_position": request.last_commit_position,
            "writer_checkpoint": request.writer_checkpoint,
            "chaser_checkpoint": request.chaser_checkpoint,
            "node_priority": request.node_priority,
        }

        response = await self.proposal(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_accept(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "server_id": request.server_id,
            "server_http": request.server_http,
            "leader_id": request.leader_id,
            "leader_http": request.leader_http,
            "view": request.view,
        }

        response = await self.accept(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_leader_is_resigning(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "leader_id": request.leader_id,
            "leader_http": request.leader_http,
        }

        response = await self.leader_is_resigning(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_leader_is_resigning_ok(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {
            "leader_id": request.leader_id,
            "leader_http": request.leader_http,
            "server_id": request.server_id,
            "server_http": request.server_http,
        }

        response = await self.leader_is_resigning_ok(**request_kwargs)
        await stream.send_message(response)

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/event_store.cluster.Elections/ViewChange": grpclib.const.Handler(
                self.__rpc_view_change,
                grpclib.const.Cardinality.UNARY_UNARY,
                ViewChangeRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/ViewChangeProof": grpclib.const.Handler(
                self.__rpc_view_change_proof,
                grpclib.const.Cardinality.UNARY_UNARY,
                ViewChangeProofRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/Prepare": grpclib.const.Handler(
                self.__rpc_prepare,
                grpclib.const.Cardinality.UNARY_UNARY,
                PrepareRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/PrepareOk": grpclib.const.Handler(
                self.__rpc_prepare_ok,
                grpclib.const.Cardinality.UNARY_UNARY,
                PrepareOkRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/Proposal": grpclib.const.Handler(
                self.__rpc_proposal,
                grpclib.const.Cardinality.UNARY_UNARY,
                ProposalRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/Accept": grpclib.const.Handler(
                self.__rpc_accept,
                grpclib.const.Cardinality.UNARY_UNARY,
                AcceptRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/LeaderIsResigning": grpclib.const.Handler(
                self.__rpc_leader_is_resigning,
                grpclib.const.Cardinality.UNARY_UNARY,
                LeaderIsResigningRequest,
                _client__.Empty,
            ),
            "/event_store.cluster.Elections/LeaderIsResigningOk": grpclib.const.Handler(
                self.__rpc_leader_is_resigning_ok,
                grpclib.const.Cardinality.UNARY_UNARY,
                LeaderIsResigningOkRequest,
                _client__.Empty,
            ),
        }


from .. import client as _client__
