__all__ = ['PingProtocolCommand']

import time
import random

from print_colors import PrintColors
from contact import Contact
from protocol_command import ProtocolCommand

class PingProtocolCommand(ProtocolCommand):
    def start(self):
        self.req()

    def stop(self):
        raise NotImplementedError

    def req(self):
        node_id = self.node.id
        local_host = self.node.listen_host
        local_port = self.node.listen_port

        if random.random() < 0.5:
            # ping contact to be added
            c = self.node.rt.add_contacts.get(0)
        else:
            # ping known contact
            c = self.node.rt.contacts.random(without_id=self.node.id)

        if c:
            # print('ping:', c)
            args = ()

            kwargs = {
                'id': node_id,
                'local_host': local_host,
                'local_port': local_port,
            }

            res = (args, kwargs)
            
            # build message
            message_data = self.node.build_message(
                self.protocol_major_version,
                self.protocol_minor_version,
                self.PROTOCOL_REQ,
                self.protocol_command_code,
                res,
            )

            # force del
            del args
            del kwargs
            del res