Ejemplo n.º 1
0
    def __init__(self, context):

        self.regtype = '_machinekit._tcp'
        self.timeout = 3
        self.querytimeout = 2
        self.queried = []
        self.resolved = []
        self.results = []
        browse_sdRef = pybonjour.DNSServiceBrowse(
            regtype=self.regtype, callBack=self.browse_callback)

        try:
            try:
                while True:
                    ready = select.select([browse_sdRef], [], [], self.timeout)
                    if browse_sdRef in ready[0]:
                        pybonjour.DNSServiceProcessResult(browse_sdRef)
                    else:
                        break
            except KeyboardInterrupt:
                pass
        finally:
            browse_sdRef.close()

        print "results: ", self.results

        # connect to first result
        if len(self.results) == 0:
            print "no results"
            sys.exit(1)
        (fullname, hosttarget, port, txt, ip) = self.results[0]

        print "connecting to '%s'" % fullname
        uri = "tcp://%s:%d" % (ip, port)

        self.socket = context.socket(zmq.DEALER)
        self.socket.identity = "fooclient"
        self.socket.connect(uri)
        self.socket.RCVTIMEO = 2000
        self.rx = Container()
        self.tx = Container()
        self.run()
Ejemplo n.º 2
0
    def __init__(self, context, statusUri):
        self.context = context
        self.statusUri = statusUri

        self.rx = Container()
        self.tx = Container()

        print(("connecting to '%s'" % self.statusUri))
        self.socket = context.socket(zmq.SUB)
        self.socket.connect(self.statusUri)
        self.socket.setsockopt(zmq.SUBSCRIBE, "task")

        self.commandSocket = context.socket(zmq.DEALER)
        self.commandSocket.connect(self.statusUri)

        self.send_command_msg(MT_PING)

        print("Connected")

        self.run()
Ejemplo n.º 3
0
    def __init__(self, context, statusUri):
        self.context = context
        self.statusUri = statusUri

        self.rx = Container()

        print(("connecting to '%s'" % self.statusUri))
        self.socket = context.socket(zmq.SUB)
        self.socket.connect(self.statusUri)
        self.socket.setsockopt(zmq.SUBSCRIBE, "task")

        self.run()
Ejemplo n.º 4
0
    def __init__(self,
                 context,
                 appDirs=[],
                 topdir=".",
                 ip="",
                 svcUuid=None,
                 debug=False,
                 name=None,
                 ipInName=True):
        self.appDirs = appDirs
        self.ip = ip
        self.name = name
        self.debug = debug
        self.shutdown = threading.Event()
        self.running = False
        self.cfg = ConfigParser.ConfigParser()

        for rootdir in self.appDirs:
            for root, subFolders, files in os.walk(rootdir):
                if 'description.ini' in files:
                    inifile = os.path.join(root, 'description.ini')
                    cfg = ConfigParser.ConfigParser()
                    cfg.read(inifile)
                    appName = cfg.get('Default', 'name')
                    description = cfg.get('Default', 'description')
                    appType = cfg.get('Default', 'type')
                    self.cfg.add_section(appName)
                    self.cfg.set(appName, 'description', description)
                    self.cfg.set(appName, 'type', appType)
                    self.cfg.set(appName, 'files', root)
                    if self.debug:
                        print(("name: " + cfg.get('Default', 'name')))
                        print(("description: " +
                               cfg.get('Default', 'description')))
                        print(("type: " + cfg.get('Default', 'type')))
                        print(("files: " + root))

        self.rx = Container()
        self.tx = Container()
        self.topdir = topdir
        self.context = context
        self.baseUri = "tcp://" + self.ip
        self.socket = context.socket(zmq.ROUTER)
        self.port = self.socket.bind_to_random_port(self.baseUri)
        self.dsname = self.socket.get_string(zmq.LAST_ENDPOINT,
                                             encoding='utf-8')

        if self.name is None:
            self.name = "Machinekit"
        if ipInName:
            self.name = self.name + " on " + self.ip
        self.service = service.Service(type='config',
                                       svcUuid=svcUuid,
                                       dsn=self.dsname,
                                       port=self.port,
                                       ip=self.ip,
                                       name=self.name,
                                       debug=self.debug)

        self.publish()

        threading.Thread(target=self.process_sockets).start()
        self.running = True
Ejemplo n.º 5
0
    def __init__(self,
                 context,
                 launcherDirs=None,
                 topDir='.',
                 host='',
                 svcUuid=None,
                 debug=False,
                 name=None,
                 hostInName=True,
                 pollInterval=0.5,
                 pingInterval=2.0,
                 loopback=False):
        if launcherDirs is None:
            launcherDirs = []

        self.launcherDirs = launcherDirs
        self.host = host
        self.loopback = loopback
        self.name = name
        self.debug = debug
        self.shutdown = threading.Event()
        self.running = False
        self.pollInterval = pollInterval
        self.pingInterval = pingInterval

        self.container = Container()
        self.txContainer = Container()
        self.launcherSubscribed = False
        self.launcherFullUpdate = False

        self.processes = {}  # for processes mapped to launcher
        self.terminating = set()  # set of terminating processes

        # Create launcher configuration structure
        iniName = 'launcher.ini'
        configDefaults = {
            'name': 'Launcher',
            'command': '',
            'description': '',
            'image': '',
            'shell': 'false',
            'workdir': '.',
            'type': '',
            'manufacturer': '',
            'model': '',
            'variant': ''
        }

        index = 0
        for rootDir in self.launcherDirs:
            for root, _, files in os.walk(rootDir):
                if iniName in files:
                    iniFile = os.path.join(root, iniName)
                    cfg = configparser.ConfigParser(configDefaults)
                    cfg.read(iniFile)
                    launcher = Launcher()
                    for section in cfg.sections():
                        launcher.Clear()
                        launcher.index = index
                        index += 1
                        # descriptive data
                        launcher.name = cfg.get(section, 'name')
                        launcher.description = cfg.get(section, 'description')
                        info = MachineInfo()
                        info.type = cfg.get(section, 'type')
                        info.manufacturer = cfg.get(section, 'manufacturer')
                        info.model = cfg.get(section, 'model')
                        info.variant = cfg.get(section, 'variant')
                        launcher.info.MergeFrom(info)
                        # command data
                        launcher.command = cfg.get(section, 'command')
                        launcher.shell = cfg.getboolean(section, 'shell')
                        workdir = cfg.get(section, 'workdir')
                        if not os.path.isabs(workdir):
                            workdir = os.path.join(root, workdir)
                        launcher.workdir = os.path.normpath(workdir)
                        launcher.returncode = 0
                        launcher.running = False
                        launcher.terminating = False
                        # storing the image file
                        imageFile = cfg.get(section, 'image')
                        if imageFile is not '':
                            if not os.path.isabs(imageFile):
                                imageFile = os.path.join(root, imageFile)
                            fileBuffer = open(imageFile, 'rb').read()
                            image = File()
                            image.name = os.path.basename(imageFile)
                            image.encoding = CLEARTEXT
                            image.blob = fileBuffer
                            launcher.image.MergeFrom(image)
                        self.container.launcher.add().CopyFrom(launcher)
                        self.txContainer.launcher.add().CopyFrom(launcher)

        if self.debug:
            print(self.container)

        # prepare pings
        if self.pingInterval > 0:
            self.pingRatio = math.floor(self.pingInterval / self.pollInterval)
        else:
            self.pingRatio = -1
        self.pingCount = 0

        self.rx = Container()
        self.txCommand = Container()
        self.topDir = topDir
        self.context = context
        self.baseUri = "tcp://"
        if self.loopback:
            self.baseUri += '127.0.0.1'
        else:
            self.baseUri += '*'
        self.launcherSocket = context.socket(zmq.XPUB)
        self.launcherSocket.setsockopt(zmq.XPUB_VERBOSE, 1)
        self.launcherPort = self.launcherSocket.bind_to_random_port(
            self.baseUri)
        self.launcherDsname = self.launcherSocket.get_string(zmq.LAST_ENDPOINT,
                                                             encoding='utf-8')
        self.launcherDsname = self.launcherDsname.replace('0.0.0.0', self.host)
        self.commandSocket = context.socket(zmq.DEALER)
        self.commandPort = self.commandSocket.bind_to_random_port(self.baseUri)
        self.commandDsname = self.commandSocket.get_string(zmq.LAST_ENDPOINT,
                                                           encoding='utf-8')
        self.commandDsname = self.commandDsname.replace('0.0.0.0', self.host)

        if self.name is None:
            self.name = 'Machinekit Launcher'
        if hostInName:
            self.name += ' on ' + self.host
        self.launcherService = service.Service(type='launcher',
                                               svcUuid=svcUuid,
                                               dsn=self.launcherDsname,
                                               port=self.launcherPort,
                                               host=self.host,
                                               name=self.name,
                                               loopback=self.loopback,
                                               debug=self.debug)
        self.commandService = service.Service(type='launchercmd',
                                              svcUuid=svcUuid,
                                              dsn=self.commandDsname,
                                              port=self.commandPort,
                                              host=self.host,
                                              loopback=self.loopback,
                                              debug=self.debug)

        self.publish()

        threading.Thread(target=self.process_sockets).start()
        threading.Thread(target=self.poll).start()
        self.running = True
Ejemplo n.º 6
0
me = options.actor

context = zmq.Context()
cmd = context.socket(zmq.XSUB)
cmd.connect(options.cmduri)
# subscribe XSUB-style by sending a message  \001<topic>
cmd.send("\001%s" % (me))

response = context.socket(zmq.XSUB)
response.connect(options.responseuri)
response.send("\001%s" % (me))

i = 0

container = Container()

time.sleep(1)  # let subscriptions stabilize
for j in range(options.iter):

    for n in range(options.batch):
        container.type = MT_MOTCMD
        motcmd = container.motcmd
        motcmd.command = EMCMOT_SET_LINE
        motcmd.commandNum = i
        pos = motcmd.pos
        t = pos.tran
        t.x = 1.0 + i
        t.y = 2.0 + i
        t.z = 3.0 + i
        pos.b = 3.14
Ejemplo n.º 7
0
#print "ZMQ=%s pyzmq=%s" % (zmq.zmq_version(), zmq.pyzmq_version())

context = zmq.Context()
preview = context.socket(zmq.SUB)
preview.setsockopt(zmq.SUBSCRIBE, "preview")
preview.connect("tcp://127.0.0.1:4711")

status = context.socket(zmq.SUB)
status.setsockopt(zmq.SUBSCRIBE, "status")
status.connect("tcp://127.0.0.1:4712")

poll = zmq.Poller()
poll.register(preview, zmq.POLLIN)
poll.register(status, zmq.POLLIN)

rx = Container()

while True:
    s = dict(poll.poll())
    if status in s:
        try:
            (origin, msg) = status.recv_multipart()
            rx.ParseFromString(msg)
        except Exception, e:
            print "status Exception", e, msg
        else:
            print "---%s:\n %s" % (origin, str(rx))
        continue

    if preview in s:
        try:
Ejemplo n.º 8
0
    def __init__(self,
                 context,
                 uri,
                 appDirs=[],
                 topdir=".",
                 interface="",
                 ipv4="",
                 svc_uuid=None,
                 debug=False):
        self.appDirs = appDirs
        self.interface = interface
        self.ipv4 = ipv4
        self.debug = debug
        self.cfg = ConfigParser.ConfigParser()

        for rootdir in self.appDirs:
            for root, subFolders, files in os.walk(rootdir):
                if 'description.ini' in files:
                    inifile = os.path.join(root, 'description.ini')
                    cfg = ConfigParser.ConfigParser()
                    cfg.read(inifile)
                    name = cfg.get('Default', 'name')
                    description = cfg.get('Default', 'description')
                    type = cfg.get('Default', 'type')
                    self.cfg.add_section(name)
                    self.cfg.set(name, 'description', description)
                    self.cfg.set(name, 'type', type)
                    self.cfg.set(name, 'files', root)
                    if debug:
                        print "name:", cfg.get('Default', 'name')
                        print "description:", cfg.get('Default', 'description')
                        print "type:", cfg.get('Default', 'type')
                        print "files:", root

        self.topdir = topdir
        self.context = context
        self.socket = context.socket(zmq.ROUTER)
        self.port = self.socket.bind_to_random_port(uri)
        self.dsname = self.socket.get_string(zmq.LAST_ENDPOINT,
                                             encoding='utf-8')

        if self.debug: print "dsname = ", self.dsname, "port =", self.port

        me = uuid.uuid1()
        self.txtrec = [
            str('dsn=' + self.dsname),
            str('uuid=' + svc_uuid),
            str('service=' + 'config'),
            str('instance=' + str(me))
        ]

        if self.debug: print "txtrec:", self.txtrec

        self.rx = Container()
        self.tx = Container()
        poll = zmq.Poller()
        poll.register(self.socket, zmq.POLLIN)
        try:
            self.name = 'Machinekit on %s' % self.ipv4
            self.service = ZeroconfService(
                self.name,
                self.port,
                stype='_machinekit._tcp',
                subtype="_config._sub._machinekit._tcp",
                text=self.txtrec)
            self.service.publish()
        except Exception, e:
            print 'cannot register DNS service', e
            sys.exit(1)
Ejemplo n.º 9
0
    def __init__(self,
                 name,
                 builder,
                 halrcmd_uri=None,
                 halrcomp_uri=None,
                 uuid=None,
                 period=3,
                 debug=False,
                 instance=0,
                 disconnect=True,
                 remote=False):

        gobject.GObject.__init__(self)
        self.builder = builder
        self.toplevel = builder.get_object("window1")
        self.debug = debug
        self.sddebug = debug
        self.period = period
        self.hal_instance = 0  # currently only one
        self.server_uuid = -1
        self.ctx = zmq.Context()
        self.ctx.linger = 0
        self.cmd_notify = None
        self.update_notify = None
        self.disconnect = disconnect  # on service removal
        self.remote = remote
        self.connector = {
            'halrcmd': self.connect_halrcmd,
            'halrcomp': self.connect_halrcomp
        }
        self.disconnector = {
            'halrcmd': self.close_halrcmd,
            'halrcomp': self.close_halrcomp
        }
        self.toplevel.set_sensitive(False)

        if uuid:
            self.instance_uuid = uuid
        else:
            self.instance_uuid = os.getenv("MKUUID")

        self.ping_outstanding = False
        self.timer_id = None
        self.state = fsm.STARTUP

        self.name = name
        self.pinsbyname = {}
        self.pinsbyhandle = {}
        self.synced = False
        # more efficient to reuse a protobuf Message
        self.rx = Container()
        self.tx = Container()

        self.required = {'halrcmd': halrcmd_uri, 'halrcomp': halrcomp_uri}
        self.hr = GServiceResolver(self.required,
                                   instance_uuid=self.instance_uuid,
                                   hal_instance=0,
                                   dir="/tmp",
                                   remote=self.remote,
                                   debug=self.debug)
        self.hr.connect('uri-resolved', self.uri_resolved)
        self.hr.connect('announcement-removed', self.uri_removed)
        self.hr.start()