Example #1
0
    def add_client(self, ip):
        '''
        Set up the zeroMQ resources and client tracking structs for a new client. Also inform
        existing clients that a new peer has arrived
        '''
        self.log.debug("adding client")
        nonce = self.make_nonce()

        ip_address = ip_int_to_string(ip)

        self.log.debug("trying to connect to client at IP: %s and port %i",
                       ip_address, self.client_port)

        # create a socket for sending updates back to client
        client_socket = self.z_context.socket(zmq.PUSH)
        client_socket.connect("tcp://%s:%i" % (ip_address, self.client_port))

        # add socket to poller
        self.poller.register(client_socket, zmq.POLLOUT)

        # handle race condition in container boot process that causes a collab
        # client to expire prematurely
        if len(self.clients) == 0:
            self.tick = time.time()

        # store off new client
        self.clients[nonce] = {
            "ip_address": ip,
            "keepalive_counter": self.keepalive,
            "socket": client_socket
        }

        self.log.debug("getting current list of clients")
        client_addresses = self.list_clients()
        client_address_strings = [
            ip_int_to_string(s) for s in client_addresses
        ]

        self.log.debug("list of clients: %s", client_address_strings)

        # create an INFORM message for the new client
        message = reg.TellClient()
        message.inform.client_nonce = nonce
        message.inform.keepalive_seconds = self.keepalive
        message.inform.neighbors.extend(client_addresses)

        self.log.debug("sending inform message to client: %s", message)

        # send message INFORM message to new client
        self.send_with_timeout(client_socket, message, self.message_timeout)

        # notify existing clients of the new client
        self.send_notify_messages()
        return
Example #2
0
    def send_notify_messages(self):
        """
        Inform all clients of the current state of the client list
        """

        # construct a notify message
        message = reg.TellClient()
        message.notify.neighbors.extend(self.list_clients())

        self.log.debug("Sending notify message %s", message)

        # send a notify message to each client in the client list
        for nonce, client in self.clients.items():

            self.send_with_timeout(client["socket"], message, self.message_timeout)
            
            self.log.debug("message sent to client at ip %s",
                           ip_int_to_string(client["ip_address"]))