Example #1
0
    def new_server(self, topic):
        """
        Function used to generate a new server by specifying a topic

        Parameters
        ----------
        topic : string 
            Name of the topic

        Returns
        ----------

        server : nep.server
            Server instance

        """

        print("SERVER: " + topic + ", waiting NEP master ...")
        s, port, ip = nep.masterRegister(self.node_name,
                                         topic,
                                         master_ip='127.0.0.1',
                                         master_port=7000,
                                         socket="server",
                                         pid=self.pid,
                                         data_type="json")
        if s:
            print("SERVER: " + topic + ", in " + ip + ":" + str(port))
            server = nep.server(ip, port,
                                debug=False)  #Create a new server instance
            print("SERVER: " + topic + ", socket ready")
            return server
        else:
            print("NEP ERROR: " + topic + ", server socket not connected")
Example #2
0
    def new_client(self, topic):
        """
        Function used to generate a new client by specifying a topic

        Parameters
        ----------
        topic : string 
            Name of the topic

        Returns
        ----------

        client : nep.client
            Client instance

        """

        print("CLIENT: " + topic + ", waiting NEP master ...")
        s, port, ip = nep.masterRegister(self.node_name,
                                         topic,
                                         master_ip='127.0.0.1',
                                         master_port=7000,
                                         socket="client",
                                         pid=self.pid,
                                         data_type="json")

        if s:
            print("CLIENT: " + topic + ", in " + ip + ":" + str(port))
            client = nep.client(ip, port, debug=False)
            print("CLIENT: " + topic + ", socket ready")
            return client
        else:
            print("NEP ERROR: " + topic + ", client socket not connected")
Example #3
0
    def __init__(self, topic, timeout=1000, node_name="default", debug=False):
        """ Nanomsg surveyor class
            
            Parameters
            ----------

            topic : string
                Surveyor topic

            timeout : int
                Maximun miliseconds waiting for response

            debug: bool
                If True some additional information of the subscriber is shown

            """
        self.topic = topic
        print("SURVEY: " + self.topic + " waiting for NEP master ...")
        self.pid = os.getpid()
        success, port, ip = nep.masterRegister(node_name,
                                               self.topic,
                                               master_ip='127.0.0.1',
                                               master_port=7000,
                                               socket="surveyor",
                                               pid=self.pid,
                                               data_type="json")
        print("SURVEY: " + self.topic + " socket ready")
        self.debug = debug

        if success:

            self.NN_installed = False
            try:
                import nanomsg
                self.NN_installed = True
            except ImportError:
                print("Nanomsg not installed")
                self.NN_installed = False

            if self.NN_installed == False:
                msg = "Unable to use surveyor pattern due that Nanomsg is not installed "
                raise ValueError(msg)

            self.sock = nanomsg.Socket(nanomsg.SURVEYOR)
            endpoint = "tcp://" + ip + ":" + str(port)
            self.sock.bind(endpoint)
            self.sock.set_int_option(nanomsg.SURVEYOR,
                                     nanomsg.SURVEYOR_DEADLINE, timeout)
            time.sleep(1)
            if self.debug:
                print("surveyor started in: " + str(endpoint))
Example #4
0
        def __init__(self, topic, node_name = "default", debug = False):
            
            """ Nanomsg surveyor class
            
            Parameters
            ----------

            topic : string
                Topic to exchange info

            debug: bool
                If True some additional information of the subscriber is shown

            """
            self.topic = topic
            print("RESP: " + self.topic + " waiting for NEP master ...")

            self.pid = os.getpid()
            success, port, ip  = nep.masterRegister(node_name, self.topic, master_ip = '127.0.0.1', master_port = 7000, socket = "respondent", pid = self.pid, data_type = "json")
            print("RESP: " + self.topic + " socket ready")
            self.debug = debug
           
            if success:

                self.NN_installed = False
                try:
                    import nanomsg
                    self.NN_installed = True
                except ImportError:
                    print ("NEP ERROR: Nanomsg not installed")
                    self.NN_installed = False

                if self.NN_installed == False:
                    msg = "Unable to use surveyor pattern due that Nanomsg is not installed "
                    raise ValueError(msg)

                
                self.sock = nanomsg.Socket(nanomsg.RESPONDENT)
                endpoint = "tcp://" + ip + ":" + str(port)
                self.sock.connect(endpoint)
                time.sleep(1)
                if self.debug:
                    print ("respondednt started in: " + str(endpoint))
Example #5
0
    def __network_selection(self):
        """ Get IP and port of this socket

        Returns
        ----------

        success : bool
            Only if True socket can be connected

        port : string
            Port used to connect the socket

        ip : string
            IP used to connect the socket
        """
        success = False
        ip = "127.0.0.1"
        port = "8000"
        self.pid = os.getpid()
        if self.network == "direct":
            # Set the port and ip selected by the user
            port = self.conf["port"]
            ip = self.conf['ip']
            success = True
        elif self.network == "broker":
            if self.topic != "/nep_node":
                print("PUB: " + self.topic + " waiting NEP master ...")
            # Register the topic in the NEP Master and get the port and ip
            success, port, ip = nep.masterRegister(self.node,
                                                   self.topic,
                                                   master_ip=self.master_ip,
                                                   master_port=7000,
                                                   socket="publisher",
                                                   mode=self.mode,
                                                   pid=self.pid,
                                                   data_type=self.msg_type)
            if self.topic != "/nep_node":
                print("PUB: " + self.topic + " socket ready")

        return success, port, ip