예제 #1
0
	def cleanup(self, task):
		for k in [x for x in self.cmds if task in self.cmds[x]]:
			del self.cmds[k][task]
		if task in self.timeouts:
			websocketd.remove_timeout(self.timeouts.pop(task))
		for t in [x for x in self.tasks if task in x.waiters]:
			t.remove(task)
예제 #2
0
 def boot_printer_error():
     log('error during printer detection on port %s.' % port)
     websocketd.remove_timeout(timeout_handle[0])
     printer.close()
     ports[port] = None
     broadcast(None, 'port_state', port, 0)
     return False
예제 #3
0
파일: server.py 프로젝트: mtu-most/franklin
		def boot_machine_error():
			log('error during machine detection on port %s.' % port)
			websocketd.remove_timeout(timeout_handle[0])
			machine.close()
			ports[port] = None
			broadcast(None, 'port_state', port, 0)
			return False
예제 #4
0
파일: server.py 프로젝트: yanggen/franklin
		def boot_printer_input():
			id[2] = True
			ids = [protocol.single[code][0] for code in ('ID', 'STARTUP')]
			# CMD:1 ID:8 Checksum:3
			while len(id[0]) < 12:
				try:
					data = printer.read(12 - len(id[0]))
				except OSError:
					continue
				except IOError:
					continue
				id[0] += data
				#log('incomplete id: ' + id[0])
				if len(id[0]) < 12:
					return True
				if id[0][0] not in ids or not protocol.check(id[0]):
					log('skip non-id: %s (%s)' % (''.join('%02x' % x for x in id[0]), repr(id[0])))
					f = len(id[0])
					for start in ids:
						if start in id[0][1:]:
							p = id[0].index(bytes((start,)))
							if p < f:
								f = p
							log('Keeping some')
					if f == 0:
						f = 1
					id[0] = id[0][f:]
					return True
			# We have something to handle; cancel the timeout, but keep the serial port open to avoid a reset. (I don't think this even works, but it doesn't hurt.)
			websocketd.remove_timeout(timeout_handle[0])
			# This printer was running and tried to send an id.  Check the id.
			id[0] = id[0][1:9]
			if id[0] in orphans:
				log('accepting orphan %s on %s' % (''.join('%02x' % x for x in id[0]), port))
				ports[port] = orphans.pop(id[0])
				ports[port].port = port
				def close_port(success, data):
					log('reconnect complete; closing server port')
					printer.close()
				log('reconnecting %s' % port)
				ports[port].call('reconnect', ['admin', port], {}, lambda success, ret: ports[port].call('send_printer', ['admin', None], {}, close_port) if success else close_port)
				broadcast(None, 'port_state', port, 2)
				return False
			run_id = nextid()
			log('accepting unknown printer on port %s (id %s)' % (port, ''.join('%02x' % x for x in run_id)))
			#log('orphans: %s' % repr(tuple(orphans.keys())))
			process = subprocess.Popen((fhs.read_data('driver.py', opened = False), '--cdriver', fhs.read_data('franklin-cdriver', opened = False), '--port', port, '--run-id', run_id, '--allow-system', config['allow-system']) + (('--system',) if fhs.is_system else ()), stdin = subprocess.PIPE, stdout = subprocess.PIPE, close_fds = True)
			ports[port] = Port(port, process, printer, run_id)
			return False
예제 #5
0
        def boot_printer_input():
            id[2] = True
            ids = [protocol.single[code][0] for code in ('ID', 'STARTUP')]
            # CMD:1 ID:8 Checksum:3
            while len(id[0]) < 12:
                try:
                    data = printer.read(12 - len(id[0]))
                except OSError:
                    continue
                except IOError:
                    continue
                id[0] += data
                #log('incomplete id: ' + id[0])
                if len(id[0]) < 12:
                    return True
                if id[0][0] not in ids or not protocol.check(id[0]):
                    log('skip non-id: %s (%s)' %
                        (''.join('%02x' % x for x in id[0]), repr(id[0])))
                    f = len(id[0])
                    for start in ids:
                        if start in id[0][1:]:
                            p = id[0].index(bytes((start, )))
                            if p < f:
                                f = p
                            log('Keeping some')
                    if f == 0:
                        f = 1
                    id[0] = id[0][f:]
                    return True
            # We have something to handle; cancel the timeout, but keep the serial port open to avoid a reset. (I don't think this even works, but it doesn't hurt.)
            websocketd.remove_timeout(timeout_handle[0])
            # This printer was running and tried to send an id.  Check the id.
            id[0] = id[0][1:9]
            if id[0] in orphans:
                log('accepting orphan %s on %s' %
                    (''.join('%02x' % x for x in id[0]), port))
                ports[port] = orphans.pop(id[0])
                ports[port].port = port

                def close_port(success, data):
                    log('reconnect complete; closing server port')
                    printer.close()

                log('reconnecting %s' % port)
                ports[port].call(
                    'reconnect', ['admin', port], {},
                    lambda success, ret: ports[port].call(
                        'send_printer', ['admin', None], {}, close_port)
                    if success else close_port)
                broadcast(None, 'port_state', port, 2)
                return False
            run_id = nextid()
            log('accepting unknown printer on port %s (id %s)' %
                (port, ''.join('%02x' % x for x in run_id)))
            #log('orphans: %s' % repr(tuple(orphans.keys())))
            process = subprocess.Popen(
                (fhs.read_data('driver.py', opened=False), '--cdriver',
                 fhs.read_data('franklin-cdriver',
                               opened=False), '--port', port, '--run-id',
                 run_id, '--allow-system', config['allow-system']) +
                (('--system', ) if fhs.is_system else ()),
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                close_fds=True)
            ports[port] = Port(port, process, printer, run_id)
            return False
예제 #6
0
 def cancel():
     websocketd.remove_timeout(handle)
     ports[port] = None
예제 #7
0
 def cancel():
     websocketd.remove_timeout(timeout_handle[0])
     websocketd.remove_read(watcher)
     printer.close()
     ports[port] = None
예제 #8
0
        def boot_machine_input():
            id[2] = True
            ids = [protocol.single[code][0] for code in ('ID', 'STARTUP')]
            # CMD:1 ID:8 + 16 Checksum:9 Total: 34
            while len(id[0]) < 34:
                try:
                    data = machine.read(34 - len(id[0]))
                except OSError:
                    continue
                except IOError:
                    continue
                id[0] += data
                #log('incomplete id: ' + id[0])
                if len(id[0]) < 34:
                    if len(id[0]) > 0 and id[0][0] == protocol.single[
                            'CONTROLLER'][0]:
                        # This is a controller. Spawn the process, then cancel this detection.
                        websocketd.remove_timeout(timeout_handle[0])
                        machine.close()
                        ports[port] = None
                        broadcast(None, 'port_state', port, 0)
                        log('Starting controller driver on ' + port)
                        env = os.environ.copy()
                        env['PORT'] = port
                        subprocess.Popen(config['controller'],
                                         env=env,
                                         shell=True)
                        return False
                    return True
                if id[0][0] not in ids or not protocol.check(id[0]):
                    log('skip non-id: %s (%s)' %
                        (''.join('%02x' % x for x in id[0]), repr(id[0])))
                    f = len(id[0])
                    for start in ids:
                        if start in id[0][1:]:
                            p = id[0].index(bytes((start, )), 1)
                            if p < f:
                                f = p
                            log('Keeping some')
                    if f == 0:
                        f = 1
                    id[0] = id[0][f:]
                    return True
            # We have something to handle; cancel the timeout, but keep the serial port open to avoid a reset. (I don't think this even works, but it doesn't hurt.)
            websocketd.remove_timeout(timeout_handle[0])
            # This machine was running and tried to send an id.  Check the id.
            uuid = id[0][9:9 + 16]
            if (uuid[7] & 0xf0) != 0x40 or (uuid[9] & 0xc0) != 0x80:
                # Broken uuid; create a new one and set it.
                log('broken uuid: ' + repr(uuid))
                uuid = None
            else:
                uuid = ''.join('%02x' % x for x in uuid[:16])
                uuid = uuid[:8] + '-' + uuid[8:12] + '-' + uuid[
                    12:16] + '-' + uuid[16:20] + '-' + uuid[20:32]
                id[0] = id[0][1:9]
                running_machine = [
                    p for p in machines if machines[p].run_id == id[0]
                ]
                assert len(running_machine) < 2
                if len(running_machine) > 0:
                    p = running_machine[0]
                    assert p.uuid == uuid
                    if p.port is not None:
                        disable(
                            p.uuid,
                            'disabled machine which was detected on different port'
                        )
                    log('rediscovered machine %s on %s' %
                        (''.join('%02x' % x for x in id[0]), port))
                    ports[port] = p.uuid
                    p.port = port

                    def close_port(success, data):
                        log('reconnect complete; closing server port')
                        machine.close()

                    p.call(
                        'reconnect', ['admin', port], {},
                        lambda success, ret: (ports[port].call(
                            'send_machine', ['admin', None], {}, close_port)
                                              if success else close_port()))
                    broadcast(None, 'port_state', port, 2)
                    return False
            run_id = nextid()
            # Find uuid or create new Machine object.
            if uuid in machines:
                log('accepting known machine on port %s (uuid %s)' %
                    (port, uuid))
                machines[uuid].port = port
                ports[port] = uuid
                log('connecting %s to port %s' % (uuid, port))
                machines[uuid].call('connect',
                                    ['admin', port, [chr(x) for x in run_id]],
                                    {}, lambda success, ret: None)
            else:
                log('accepting unknown machine on port %s' % port)
                # Close detect port so it doesn't interfere.
                machine.close()
                #log('machines: %s' % repr(tuple(machines.keys())))
                process = subprocess.Popen(
                    (fhs.read_data('driver.py', opened=False), '--uuid',
                     uuid if uuid is not None else '', '--allow-system',
                     config['allow-system']) +
                    (('--system', ) if fhs.is_system else
                     ()) + (('--arc', 'False') if not config['arc'] else ()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    close_fds=True)
                new_machine = Machine(port,
                                      process,
                                      run_id,
                                      send=uuid is not None)

                def finish():
                    log('finish detect %s' % repr(uuid))
                    ports[port] = uuid
                    machines[uuid] = new_machine
                    log('connecting new machine %s to port %s' % (uuid, port))
                    new_machine.call('connect',
                                     ['admin', port, [chr(x) for x in run_id]],
                                     {}, lambda success, ret: None)

                if uuid is None:

                    def prefinish(success, uuid):
                        assert success
                        new_machine.uuid = uuid
                        new_machine.finish(finish)

                    new_machine.call('reset_uuid', ['admin'], {}, prefinish)
                else:
                    finish()
            return False
예제 #9
0
        def boot_printer_input():
            id[2] = True
            ids = [protocol.single[code][0] for code in ('ID', 'STARTUP')]
            # CMD:1 ID:8 + 16 Checksum:9 Total: 34
            while len(id[0]) < 34:
                try:
                    data = printer.read(34 - len(id[0]))
                except OSError:
                    continue
                except IOError:
                    continue
                id[0] += data
                #log('incomplete id: ' + id[0])
                if len(id[0]) < 34:
                    return True
                if id[0][0] not in ids or not protocol.check(id[0]):
                    log('skip non-id: %s (%s)' %
                        (''.join('%02x' % x for x in id[0]), repr(id[0])))
                    f = len(id[0])
                    for start in ids:
                        if start in id[0][1:]:
                            p = id[0].index(bytes((start, )), 1)
                            if p < f:
                                f = p
                            log('Keeping some')
                    if f == 0:
                        f = 1
                    id[0] = id[0][f:]
                    return True
            # We have something to handle; cancel the timeout, but keep the serial port open to avoid a reset. (I don't think this even works, but it doesn't hurt.)
            websocketd.remove_timeout(timeout_handle[0])
            # This printer was running and tried to send an id.  Check the id.
            uuid = id[0][9:9 + 16]
            if (uuid[7] & 0xf0) != 0x40 or (uuid[9] & 0xc0) != 0x80:
                # Broken uuid; create a new one and set it.
                log('broken uuid: ' + repr(uuid))
                uuid = None
            else:
                uuid = ''.join('%02x' % x for x in uuid[:16])
                uuid = uuid[:8] + '-' + uuid[8:12] + '-' + uuid[
                    12:16] + '-' + uuid[16:20] + '-' + uuid[20:32]
                id[0] = id[0][1:9]
                running_printer = [
                    p for p in printers if printers[p].run_id == id[0]
                ]
                assert len(running_printer) < 2
                if len(running_printer) > 0:
                    p = running_printer[0]
                    assert p.uuid == uuid
                    if p.port is not None:
                        disable(
                            p.uuid,
                            'disabled printer which was detected on different port'
                        )
                    log('rediscovered printer %s on %s' %
                        (''.join('%02x' % x for x in id[0]), port))
                    ports[port] = p.uuid
                    p.port = port

                    def close_port(success, data):
                        log('reconnect complete; closing server port')
                        printer.close()

                    p.call(
                        'reconnect', ['admin', port], {},
                        lambda success, ret: (ports[port].call(
                            'send_printer', ['admin', None], {}, close_port)
                                              if success else close_port()))
                    broadcast(None, 'port_state', port, 2)
                    return False
            run_id = nextid()
            # Find uuid or create new Printer object.
            if uuid in printers:
                log('accepting known printer on port %s (uuid %s)' %
                    (port, uuid))
                printers[uuid].port = port
                ports[port] = uuid
                log('connecting %s to port %s' % (uuid, port))
                printers[uuid].call('connect',
                                    ['admin', port, [chr(x) for x in run_id]],
                                    {}, lambda success, ret: None)
            else:
                log('accepting unknown printer on port %s' % port)
                #log('printers: %s' % repr(tuple(printers.keys())))
                process = subprocess.Popen(
                    (fhs.read_data('driver.py', opened=False), '--cdriver',
                     fhs.read_data('franklin-cdriver', opened=False), '--uuid',
                     uuid if uuid is not None else '', '--allow-system',
                     config['allow-system']) +
                    (('--system', ) if fhs.is_system else
                     ()) + (('--arc', 'False') if not config['arc'] else ()),
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    close_fds=True)
                new_printer = Printer(port, process, printer, run_id)

                def finish(success, uuid):
                    assert success
                    ports[port] = uuid
                    printers[uuid] = new_printer
                    log('connecting new printer %s to port %s' % (uuid, port))
                    new_printer.call('connect',
                                     ['admin', port, [chr(x) for x in run_id]],
                                     {}, lambda success, ret: None)

                if uuid is None:
                    new_printer.call('reset_uuid', ['admin'], {}, finish)
                else:
                    finish(True, uuid)
            return False
예제 #10
0
파일: server.py 프로젝트: mtu-most/franklin
	def cancel():
		websocketd.remove_timeout(handle)
		ports[port] = None
예제 #11
0
파일: server.py 프로젝트: mtu-most/franklin
		def cancel():
			websocketd.remove_timeout(timeout_handle[0])
			websocketd.remove_read(watcher)
			machine.close()
			ports[port] = None
예제 #12
0
파일: server.py 프로젝트: mtu-most/franklin
		def boot_machine_input():
			id[2] = True
			ids = [protocol.single[code][0] for code in ('ID', 'STARTUP')]
			# CMD:1 ID:8 + 16 Checksum:9 Total: 34
			while len(id[0]) < 34:
				try:
					data = machine.read(34 - len(id[0]))
				except OSError:
					continue
				except IOError:
					continue
				id[0] += data
				#log('incomplete id: ' + id[0])
				if len(id[0]) < 34:
					if len(id[0]) > 0 and id[0][0] == protocol.single['CONTROLLER'][0]:
						# This is a controller. Spawn the process, then cancel this detection.
						websocketd.remove_timeout(timeout_handle[0])
						machine.close()
						ports[port] = None
						broadcast(None, 'port_state', port, 0)
						log('Starting controller driver on ' + port)
						env = os.environ.copy()
						env['PORT'] = port
						subprocess.Popen(config['controller'], env = env, shell = True)
						return False
					return True
				if id[0][0] not in ids or not protocol.check(id[0]):
					log('skip non-id: %s (%s)' % (''.join('%02x' % x for x in id[0]), repr(id[0])))
					f = len(id[0])
					for start in ids:
						if start in id[0][1:]:
							p = id[0].index(bytes((start,)), 1)
							if p < f:
								f = p
							log('Keeping some')
					if f == 0:
						f = 1
					id[0] = id[0][f:]
					return True
			# We have something to handle; cancel the timeout, but keep the serial port open to avoid a reset. (I don't think this even works, but it doesn't hurt.)
			websocketd.remove_timeout(timeout_handle[0])
			# This machine was running and tried to send an id.  Check the id.
			uuid = id[0][9:9 + 16]
			if (uuid[7] & 0xf0) != 0x40 or (uuid[9] & 0xc0) != 0x80:
				# Broken uuid; create a new one and set it.
				log('broken uuid: ' + repr(uuid))
				uuid = None
			else:
				uuid = ''.join('%02x' % x for x in uuid[:16])
				uuid = uuid[:8] + '-' + uuid[8:12] + '-' + uuid[12:16] + '-' + uuid[16:20] + '-' + uuid[20:32]
				id[0] = id[0][1:9]
				running_machine = [p for p in machines if machines[p].run_id == id[0]]
				assert len(running_machine) < 2
				if len(running_machine) > 0:
					p = running_machine[0]
					assert p.uuid == uuid
					if p.port is not None:
						disable(p.uuid, 'disabled machine which was detected on different port')
					log('rediscovered machine %s on %s' % (''.join('%02x' % x for x in id[0]), port))
					ports[port] = p.uuid
					p.port = port
					def close_port(success, data):
						log('reconnect complete; closing server port')
						machine.close()
					p.call('reconnect', ['admin', port], {}, lambda success, ret: (ports[port].call('send_machine', ['admin', None], {}, close_port) if success else close_port()))
					broadcast(None, 'port_state', port, 2)
					return False
			run_id = nextid()
			# Find uuid or create new Machine object.
			if uuid in machines:
				log('accepting known machine on port %s (uuid %s)' % (port, uuid))
				machines[uuid].port = port
				ports[port] = uuid
				log('connecting %s to port %s' % (uuid, port))
				machines[uuid].call('connect', ['admin', port, [chr(x) for x in run_id]], {}, lambda success, ret: None)
			else:
				log('accepting unknown machine on port %s' % port)
				# Close detect port so it doesn't interfere.
				machine.close()
				#log('machines: %s' % repr(tuple(machines.keys())))
				process = subprocess.Popen((fhs.read_data('driver.py', opened = False), '--uuid', uuid if uuid is not None else '', '--allow-system', config['allow-system']) + (('--system',) if fhs.is_system else ()) + (('--arc', 'False') if not config['arc'] else ()), stdin = subprocess.PIPE, stdout = subprocess.PIPE, close_fds = True)
				new_machine = Machine(port, process, run_id, send = False)
				def finish():
					log('finish detect %s' % repr(uuid))
					ports[port] = uuid
					machines[uuid] = new_machine
					log('connecting new machine %s to port %s' % (uuid, port))
					new_machine.call('connect', ['admin', port, [chr(x) for x in run_id]], {}, lambda success, ret: None)
				if uuid is None:
					def prefinish(success, uuid):
						assert success
						new_machine.uuid = uuid
						new_machine.finish(finish)
					new_machine.call('reset_uuid', ['admin'], {}, prefinish)
				else:
					new_machine.finish(finish)
			return False