def send_goodbye_to_controller(self):
     """Send 'goodbye' message to the controller when exiting node."""
     self._call_id += 1
     try:
         data = {"node-id" : self._node.node_id}
         lib.do_json_rpc_post(_id=self._call_id, method="goodbye", host=self._node.config["controller_host"], port=int(self._node.config["controller_port"]), _input=data)
     except Exception:
         pass
 def send_goodbye_to_controller(self):
     """Send 'goodbye' message to the controller when exiting node."""
     self._call_id += 1
     try:
         data = {"node-id": self._node.node_id}
         lib.do_json_rpc_post(_id=self._call_id,
                              method="goodbye",
                              host=self._node.config["controller_host"],
                              port=int(
                                  self._node.config["controller_port"]),
                              _input=data)
     except Exception:
         pass
    def _send_keep_alive_to_controller(self):
        """Send periodic 'keep_alive' message to controller at given interval.

        Schedule repeated delivery  of 'keep_alive' messages to controller. If their is an issue
        the node-controller communication, reset the process and request a new ID.

        """
        if self._reset == 1:
            self._node.node_id = None
            self._reset = 0
            self._node.print_warn(TAG, "Connection to OpenCache controller restarted")
            self._send_hello_to_controller()
            return
        else:
            threading.Timer(
                interval=int(self._node.config["keep_alive_interval"]),
                function=self._send_keep_alive_to_controller,
                args=(),
            ).start()
        data = {"node-id": self._node.node_id}
        self._call_id += 1
        try:
            response = lib.do_json_rpc_post(
                _id=self._call_id,
                method="keep_alive",
                host=self._node.config["controller_host"],
                port=int(self._node.config["controller_port"]),
                _input=data,
            )
            if "error" in response:
                self._reset = 1
        except Exception:
            self._reset = 1
    def _send_keep_alive_to_controller(self):
        """Send periodic 'keep_alive' message to controller at given interval.

        Schedule repeated delivery  of 'keep_alive' messages to controller. If their is an issue
        the node-controller communication, reset the process and request a new ID.

        """
        if self._reset == 1:
            self._node.node_id = None
            self._reset = 0
            self._node.print_warn(
                TAG, 'Connection to OpenCache controller restarted')
            self._send_hello_to_controller()
            return
        else:
            threading.Timer(interval=int(
                self._node.config["keep_alive_interval"]),
                            function=self._send_keep_alive_to_controller,
                            args=()).start()
        data = {"node-id": self._node.node_id}
        self._call_id += 1
        try:
            response = lib.do_json_rpc_post(
                _id=self._call_id,
                method="keep_alive",
                host=self._node.config["controller_host"],
                port=int(self._node.config["controller_port"]),
                _input=data)
            if "error" in response:
                self._reset = 1
        except Exception:
            self._reset = 1
 def _send_to_node(self, call_id, method, host, port, _input={}):
     """Send request to given node."""
     result = lib.do_json_rpc_post(_id=call_id,
                                   method=method,
                                   host=host,
                                   port=port,
                                   _input=_input)
     return result
    def _send_hello_to_controller(self):
        """Send initial 'hello' to controller.

        If a connection cannot be made to the controller, retry in 'n' seconds. 'n' is configured by
        the user in the configuration file.

        From the initial 'hello' message, a node ID is returned that is used in all communications.

        """
        data = {"host" : self._node.config["node_host"], "port" : self._node.config["node_port"]}
        while (self._node.node_id == None):
            try:
                self._call_id += 1
                response = lib.do_json_rpc_post(_id=self._call_id, method="hello", host=self._node.config["controller_host"], port=int(self._node.config["controller_port"]), _input=data)
                self._node.node_id = response["result"]["node-id"]
                self._node.print_info(TAG, 'OpenCache node XXXXXconnected to controller, given ID: %s' % self._node.node_id)
            except Exception:
                self._node.print_warn(TAG, ("Could not connect to controller, retrying in %s seconds." % self._node.config["keep_alive_interval"]))
                time.sleep(float(self._node.config["keep_alive_interval"]))
        threading.Timer(interval=int(self._node.config["keep_alive_interval"]), function=self._send_keep_alive_to_controller, args=()).start()
    def _send_hello_to_controller(self):
        """Send initial 'hello' to controller.

        If a connection cannot be made to the controller, retry in 'n' seconds. 'n' is configured by
        the user in the configuration file.

        From the initial 'hello' message, a node ID is returned that is used in all communications.

        """
        data = {
            "host": self._node.config["node_host"],
            "port": self._node.config["node_port"]
        }
        while (self._node.node_id == None):
            try:
                self._call_id += 1
                response = lib.do_json_rpc_post(
                    _id=self._call_id,
                    method="hello",
                    host=self._node.config["controller_host"],
                    port=int(self._node.config["controller_port"]),
                    _input=data)
                self._node.node_id = response["result"]["node-id"]
                self._node.print_info(
                    TAG,
                    'OpenCache node connected to controller, given ID: %s' %
                    self._node.node_id)
            except Exception:
                self._node.print_warn(
                    TAG,
                    ("Could not connect to controller, retrying in %s seconds."
                     % self._node.config["keep_alive_interval"]))
                time.sleep(float(self._node.config["keep_alive_interval"]))
        threading.Timer(interval=int(self._node.config["keep_alive_interval"]),
                        function=self._send_keep_alive_to_controller,
                        args=()).start()
 def _send_to_node(self, call_id, method, host, port, _input={}):
     """Send request to given node."""
     result = lib.do_json_rpc_post(_id=call_id, method=method, host=host, port=port, _input=_input)
     return result