# Path: tooz/coordination.py
# LOG = logging.getLogger(__name__)
# TOOZ_BACKENDS_NAMESPACE = "tooz.backends"
#     DISTRIBUTED_ACROSS_THREADS = 'DISTRIBUTED_ACROSS_THREADS'
#     DISTRIBUTED_ACROSS_PROCESSES = 'DISTRIBUTED_ACROSS_PROCESSES'
#     DISTRIBUTED_ACROSS_HOSTS = 'DISTRIBUTED_ACROSS_HOSTS'
#     NON_TIMEOUT_BASED = 'NON_TIMEOUT_BASED'
#     LINEARIZABLE = 'LINEARIZABLE'
#     SEQUENTIAL = 'SEQUENTIAL'
#     CAUSAL = 'CAUSAL'
#     SERIALIZABLE = 'SERIALIZABLE'
#     SAME_VIEW_UNDER_PARTITIONS = 'SAME_VIEW_UNDER_PARTITIONS'
#     SAME_VIEW_ACROSS_CLIENTS = 'SAME_VIEW_ACROSS_CLIENTS'
#     CHARACTERISTICS = ()
#     EXCLUDE_OPTIONS = None
# class Characteristics(enum.Enum):
# class Hooks(list):
# class Event(object):
# class MemberJoinedGroup(Event):
# class MemberLeftGroup(Event):
# class LeaderElected(Event):
# class Heart(object):
# class CoordinationDriver(object):
# class CoordAsyncResult(object, metaclass=abc.ABCMeta):
# class CoordinatorResult(CoordAsyncResult):
# class CoordinationDriverWithExecutor(CoordinationDriver):
# class CoordinationDriverCachedRunWatchers(CoordinationDriver):
# class ToozDriverChosenPoorly(tooz.ToozError):
# class ToozConnectionError(tooz.ToozError):
# class OperationTimedOut(tooz.ToozError):
# class LockAcquireFailed(tooz.ToozError):
# class GroupNotCreated(tooz.ToozError):
# class GroupAlreadyExist(tooz.ToozError):
# class MemberAlreadyExist(tooz.ToozError):
# class MemberNotJoined(tooz.ToozError):
# class GroupNotEmpty(tooz.ToozError):
# class WatchCallbackNotFound(tooz.ToozError):
#     def run(self, *args, **kwargs):
#     def __init__(self, group_id, member_id):
#     def __repr__(self):
#     def __init__(self, group_id, member_id):
#     def __repr__(self):
#     def __init__(self, group_id, member_id):
#     def __init__(self, driver, thread_cls=threading.Thread,
#                  event_cls=threading.Event):
#     def beats(self):
#     def is_alive(self):
#     def _beat_forever_until_stopped(self):
#     def start(self, thread_cls=None):
#     def stop(self):
#     def wait(self, timeout=None):
#     def __init__(self, member_id, parsed_url, options):
#     def _has_hooks_for_group(self, group_id):
#     def join_partitioned_group(
#             self, group_id,
#             weight=1,
#             partitions=partitioner.Partitioner.DEFAULT_PARTITION_NUMBER):
#     def leave_partitioned_group(self, partitioner):
#     def run_watchers(timeout=None):
#     def run_elect_coordinator():
#     def watch_join_group(self, group_id, callback):
#     def unwatch_join_group(self, group_id, callback):
#     def watch_leave_group(self, group_id, callback):
#     def unwatch_leave_group(self, group_id, callback):
#     def watch_elected_as_leader(self, group_id, callback):
#     def unwatch_elected_as_leader(self, group_id, callback):
#     def stand_down_group_leader(group_id):
#     def is_started(self):
#     def start(self, start_heart=False):
#     def _start(self):
#     def stop(self):
#     def _stop(self):
#     def create_group(group_id):
#     def get_groups():
#     def join_group(group_id, capabilities=b""):
#     def join_group_create(self, group_id, capabilities=b""):
#     def leave_group(group_id):
#     def delete_group(group_id):
#     def get_members(group_id):
#     def get_member_capabilities(group_id, member_id):
#     def get_member_info(group_id, member_id):
#     def update_capabilities(group_id, capabilities):
#     def get_leader(group_id):
#     def get_lock(name):
#     def heartbeat():
#     def get(self, timeout=None):
#     def done(self):
#     def __init__(self, fut, failure_translator=None):
#     def get(self, timeout=None):
#     def done(self):
#     def __init__(self, member_id, parsed_url, options):
#     def start(self, start_heart=False):
#     def stop(self):
#     def __init__(self, member_id, parsed_url, options):
#     def _init_watch_group(self, group_id):
#     def watch_join_group(self, group_id, callback):
#     def unwatch_join_group(self, group_id, callback):
#     def watch_leave_group(self, group_id, callback):
#     def unwatch_leave_group(self, group_id, callback):
#     def run_watchers(self, timeout=None):
# def get_coordinator(backend_url, member_id,
#                     characteristics=frozenset(), **kwargs):
#     def __init__(self, group_id):
#     def __init__(self, group_id):
#     def __init__(self, group_id, member_id):
#     def __init__(self, group_id, member_id):
#     def __init__(self, group_id):
#     def __init__(self, group_id, callback):


import time
import uuid
from tooz import coordination


ALIVE_TIME = 1
coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()

# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()

# Join a group
request = coordinator.join_group(group)
request.get()


def when_i_am_elected_leader(event):
    # event is a LeaderElected event
    print(event.group_id, event.member_id)


# Propose to be a leader for the group
coordinator.watch_elected_as_leader(group, when_i_am_elected_leader)

start = time.time()
while time.time() - start < ALIVE_TIME:
    coordinator.heartbeat()
    coordinator.run_watchers()
    time.sleep(0.1)


# Path: tooz/coordination.py
# LOG = logging.getLogger(__name__)
# TOOZ_BACKENDS_NAMESPACE = "tooz.backends"
#     DISTRIBUTED_ACROSS_THREADS = 'DISTRIBUTED_ACROSS_THREADS'
#     DISTRIBUTED_ACROSS_PROCESSES = 'DISTRIBUTED_ACROSS_PROCESSES'
#     DISTRIBUTED_ACROSS_HOSTS = 'DISTRIBUTED_ACROSS_HOSTS'
#     NON_TIMEOUT_BASED = 'NON_TIMEOUT_BASED'
#     LINEARIZABLE = 'LINEARIZABLE'
#     SEQUENTIAL = 'SEQUENTIAL'
#     CAUSAL = 'CAUSAL'
#     SERIALIZABLE = 'SERIALIZABLE'
#     SAME_VIEW_UNDER_PARTITIONS = 'SAME_VIEW_UNDER_PARTITIONS'
#     SAME_VIEW_ACROSS_CLIENTS = 'SAME_VIEW_ACROSS_CLIENTS'
#     CHARACTERISTICS = ()
#     EXCLUDE_OPTIONS = None
# class Characteristics(enum.Enum):
# class Hooks(list):
# class Event(object):
# class MemberJoinedGroup(Event):
# class MemberLeftGroup(Event):
# class LeaderElected(Event):
# class Heart(object):
# class CoordinationDriver(object):
# class CoordAsyncResult(object, metaclass=abc.ABCMeta):
# class CoordinatorResult(CoordAsyncResult):
# class CoordinationDriverWithExecutor(CoordinationDriver):
# class CoordinationDriverCachedRunWatchers(CoordinationDriver):
# class ToozDriverChosenPoorly(tooz.ToozError):
# class ToozConnectionError(tooz.ToozError):
# class OperationTimedOut(tooz.ToozError):
# class LockAcquireFailed(tooz.ToozError):
# class GroupNotCreated(tooz.ToozError):
# class GroupAlreadyExist(tooz.ToozError):
# class MemberAlreadyExist(tooz.ToozError):
# class MemberNotJoined(tooz.ToozError):
# class GroupNotEmpty(tooz.ToozError):
# class WatchCallbackNotFound(tooz.ToozError):
#     def run(self, *args, **kwargs):
#     def __init__(self, group_id, member_id):
#     def __repr__(self):
#     def __init__(self, group_id, member_id):
#     def __repr__(self):
#     def __init__(self, group_id, member_id):
#     def __init__(self, driver, thread_cls=threading.Thread,
#                  event_cls=threading.Event):
#     def beats(self):
#     def is_alive(self):
#     def _beat_forever_until_stopped(self):
#     def start(self, thread_cls=None):
#     def stop(self):
#     def wait(self, timeout=None):
#     def __init__(self, member_id, parsed_url, options):
#     def _has_hooks_for_group(self, group_id):
#     def join_partitioned_group(
#             self, group_id,
#             weight=1,
#             partitions=partitioner.Partitioner.DEFAULT_PARTITION_NUMBER):
#     def leave_partitioned_group(self, partitioner):
#     def run_watchers(timeout=None):
#     def run_elect_coordinator():
#     def watch_join_group(self, group_id, callback):
#     def unwatch_join_group(self, group_id, callback):
#     def watch_leave_group(self, group_id, callback):
#     def unwatch_leave_group(self, group_id, callback):
#     def watch_elected_as_leader(self, group_id, callback):
#     def unwatch_elected_as_leader(self, group_id, callback):
#     def stand_down_group_leader(group_id):
#     def is_started(self):
#     def start(self, start_heart=False):
#     def _start(self):
#     def stop(self):
#     def _stop(self):
#     def create_group(group_id):
#     def get_groups():
#     def join_group(group_id, capabilities=b""):
#     def join_group_create(self, group_id, capabilities=b""):
#     def leave_group(group_id):
#     def delete_group(group_id):
#     def get_members(group_id):
#     def get_member_capabilities(group_id, member_id):
#     def get_member_info(group_id, member_id):
#     def update_capabilities(group_id, capabilities):
#     def get_leader(group_id):
#     def get_lock(name):
#     def heartbeat():
#     def get(self, timeout=None):
#     def done(self):
#     def __init__(self, fut, failure_translator=None):
#     def get(self, timeout=None):
#     def done(self):
#     def __init__(self, member_id, parsed_url, options):
#     def start(self, start_heart=False):
#     def stop(self):
#     def __init__(self, member_id, parsed_url, options):
#     def _init_watch_group(self, group_id):
#     def watch_join_group(self, group_id, callback):
#     def unwatch_join_group(self, group_id, callback):
#     def watch_leave_group(self, group_id, callback):
#     def unwatch_leave_group(self, group_id, callback):
#     def run_watchers(self, timeout=None):
# def get_coordinator(backend_url, member_id,
#                     characteristics=frozenset(), **kwargs):
#     def __init__(self, group_id):
#     def __init__(self, group_id):
#     def __init__(self, group_id, member_id):
#     def __init__(self, group_id, member_id):
#     def __init__(self, group_id):
#     def __init__(self, group_id, callback):


# Path: examples/leader_election.py
import time
import uuid
from tooz import coordination



ALIVE_TIME = 1
coordinator = coordination.get_coordinator('zake://', b'host-1')
coordinator.start()

# Create a group
group = bytes(str(uuid.uuid4()).encode('ascii'))
request = coordinator.create_group(group)
request.get()

# Join a group
request = coordinator.join_group(group)
request.get()


def when_i_am_elected_leader(event):
    # event is a LeaderElected event
    print(event.group_id, event.member_id)


# Propose to be a leader for the group
coordinator.watch_elected_as_leader(group, when_i_am_elected_leader)

start = time.time()
while time.time() - start < ALIVE_TIME:
    coordinator.heartbeat()
    coordinator.run_watchers()
    time.sleep(0.1)


coordinator.stop()