def test_codecs(self):
		command1 = PJLinkCommandLine(PJLinkProtocol.POWER, PJLinkProtocol.ON)
		self.failUnlessEqual(command1.version, 1)
		self.failUnlessEqual(command1.command, PJLinkProtocol.POWER)
		self.failUnlessEqual(command1.data, PJLinkProtocol.ON)
		
		command2 = PJLinkCommandLine.decode(command1.encode())
		self.failUnlessEqual(command1.encode(), command2.encode())
		self.failUnlessEqual(command1.command, command2.command)
		self.failUnless(command1.data, command2.data)
		self.failUnless(command1.version, command2.version)
		
		response1 = PJLinkResponse(PJLinkProtocol.POWER, "1")
		self.failUnlessEqual(response1.version, 1)
		self.failUnlessEqual(response1.command, PJLinkProtocol.POWER)
		self.failUnlessEqual(response1.data, "1")
		
		response2 = PJLinkResponse.decode(response1.encode())
		self.failUnlessEqual(response1.encode(), response2.encode())
		self.failUnlessEqual(response1.version, response2.version)
		self.failUnlessEqual(response1.command, response2.command)
		self.failUnlessEqual(response1.data, response2.data)
Beispiel #2
0
    def test_codecs(self):
        command1 = PJLinkCommandLine(PJLinkProtocol.POWER, PJLinkProtocol.ON)
        self.failUnlessEqual(command1.version, 1)
        self.failUnlessEqual(command1.command, PJLinkProtocol.POWER)
        self.failUnlessEqual(command1.data, PJLinkProtocol.ON)

        command2 = PJLinkCommandLine.decode(command1.encode())
        self.failUnlessEqual(command1.encode(), command2.encode())
        self.failUnlessEqual(command1.command, command2.command)
        self.failUnless(command1.data, command2.data)
        self.failUnless(command1.version, command2.version)

        response1 = PJLinkResponse(PJLinkProtocol.POWER, "1")
        self.failUnlessEqual(response1.version, 1)
        self.failUnlessEqual(response1.command, PJLinkProtocol.POWER)
        self.failUnlessEqual(response1.data, "1")

        response2 = PJLinkResponse.decode(response1.encode())
        self.failUnlessEqual(response1.encode(), response2.encode())
        self.failUnlessEqual(response1.version, response2.version)
        self.failUnlessEqual(response1.command, response2.command)
        self.failUnlessEqual(response1.data, response2.data)
	def run(self): 
		if self.running: return
		self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
		self.server.bind(('127.0.0.1',self.port)) 
		self.server.listen(self.backlog)
		self.running = True
		while self.running:
			client, address = self.server.accept()
			auth_request = PJLinkAuthenticationRequest(self.password)
			client.send(auth_request.encode())
			data = client.recv(self.buffer_size) 
			if not data:
				client.close()
				continue

			response = None 
			command_line = PJLinkCommandLine.decode(data)

			if not auth_request.authentication_hash_matches(command_line.authentication_hash):
				response = PJLinkResponse(PJLinkProtocol.AUTHENTICATE, PJLinkProtocol.INVALID_PASSWORD_ERROR, version=None)
				client.send(response.encode())
				client.close()
				continue
				
			if command_line.command == PJLinkProtocol.POWER:
				if command_line.data == PJLinkProtocol.ON or command_line.data == PJLinkProtocol.OFF:
					self.power_state = command_line.data
					for lamp in self.lamps: lamp[1] = command_line.data == PJLinkProtocol.ON
					response = PJLinkResponse(PJLinkProtocol.POWER, PJLinkProtocol.OK)
				elif command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(PJLinkProtocol.POWER, self.power_state)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.INPUT:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, '%s%s' % (self.input[0], self.input[1]))
				elif len(command_line.data) == 2:
					self.input = [command_line.data[0], command_line.data[1]]
					response = PJLinkResponse(command_line.command, PJLinkProtocol.OK)
				else:
					response = PJLinkResponse(command_line.commands, PJLinkProtocol.ERROR_2)
					
			elif command_line.command == PJLinkProtocol.AVAILABLE_INPUTS:
				if command_line.data == PJLinkProtocol.QUERY:
					data = ' '.join(['%s%s' % (input[0], input[1]) for input in self.available_inputs])
					response = PJLinkResponse(command_line.command, data)
				else:
					response = PJLinkResponse(command_line.commands, PJLinkProtocol.ERROR_2)
					
			elif command_line.command == PJLinkProtocol.MUTE:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.mute_state)
				elif command_line.data == PJLinkProtocol.AUDIO_VIDEO_MUTE_ON or command_line.data == PJLinkProtocol.AUDIO_VIDEO_MUTE_OFF or command_line.data == PJLinkProtocol.VIDEO_MUTE_ON or command_line.data == PJLinkProtocol.VIDEO_MUTE_OFF or command_line.data == PJLinkProtocol.AUDIO_MUTE_ON or command_line.data == PJLinkProtocol.AUDIO_MUTE_OFF:
					self.mute_state = command_line.data
					response = PJLinkResponse(command_line.command, PJLinkProtocol.OK)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.NAME:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.projector_name)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.MANUFACTURE_NAME:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.manufacture_name)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.PRODUCT_NAME:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.product_name)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.OTHER_INFO:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.other_info)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.CLASS_INFO:
				if command_line.data == PJLinkProtocol.QUERY:
					response = PJLinkResponse(command_line.command, self.class_info)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.ERROR_STATUS:
				if command_line.data == PJLinkProtocol.QUERY:
					data = ''.join([error[1] for error in self.errors])
					response = PJLinkResponse(command_line.command, data)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			elif command_line.command == PJLinkProtocol.LAMP:
				if command_line.data == PJLinkProtocol.QUERY:
					data = ' '.join(['%s %s' % (lamp[0], '1' if lamp[1] == True else '0') for lamp in self.lamps])
					response = PJLinkResponse(command_line.command, data)
				else:
					response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_2)

			else:
				response = PJLinkResponse(command_line.command, PJLinkProtocol.ERROR_1)
				
			client.send(response.encode())
			client.close()
			
		self.server.close()
Beispiel #4
0
    def run(self):
        if self.running: return
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.bind(('127.0.0.1', self.port))
        self.server.listen(self.backlog)
        self.running = True
        while self.running:
            client, address = self.server.accept()
            auth_request = PJLinkAuthenticationRequest(self.password)
            client.send(auth_request.encode())
            data = client.recv(self.buffer_size)
            if not data:
                client.close()
                continue

            response = None
            command_line = PJLinkCommandLine.decode(data)

            if not auth_request.authentication_hash_matches(
                    command_line.authentication_hash):
                response = PJLinkResponse(
                    PJLinkProtocol.AUTHENTICATE,
                    PJLinkProtocol.INVALID_PASSWORD_ERROR,
                    version=None)
                client.send(response.encode())
                client.close()
                continue

            if command_line.command == PJLinkProtocol.POWER:
                if command_line.data == PJLinkProtocol.ON or command_line.data == PJLinkProtocol.OFF:
                    self.power_state = command_line.data
                    for lamp in self.lamps:
                        lamp[1] = command_line.data == PJLinkProtocol.ON
                    response = PJLinkResponse(PJLinkProtocol.POWER,
                                              PJLinkProtocol.OK)
                elif command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(PJLinkProtocol.POWER,
                                              self.power_state)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.INPUT:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(
                        command_line.command,
                        '%s%s' % (self.input[0], self.input[1]))
                elif len(command_line.data) == 2:
                    self.input = [command_line.data[0], command_line.data[1]]
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.OK)
                else:
                    response = PJLinkResponse(command_line.commands,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.AVAILABLE_INPUTS:
                if command_line.data == PJLinkProtocol.QUERY:
                    data = ' '.join([
                        '%s%s' % (input[0], input[1])
                        for input in self.available_inputs
                    ])
                    response = PJLinkResponse(command_line.command, data)
                else:
                    response = PJLinkResponse(command_line.commands,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.MUTE:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.mute_state)
                elif command_line.data == PJLinkProtocol.AUDIO_VIDEO_MUTE_ON or command_line.data == PJLinkProtocol.AUDIO_VIDEO_MUTE_OFF or command_line.data == PJLinkProtocol.VIDEO_MUTE_ON or command_line.data == PJLinkProtocol.VIDEO_MUTE_OFF or command_line.data == PJLinkProtocol.AUDIO_MUTE_ON or command_line.data == PJLinkProtocol.AUDIO_MUTE_OFF:
                    self.mute_state = command_line.data
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.OK)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.NAME:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.projector_name)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.MANUFACTURE_NAME:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.manufacture_name)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.PRODUCT_NAME:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.product_name)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.OTHER_INFO:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.other_info)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.CLASS_INFO:
                if command_line.data == PJLinkProtocol.QUERY:
                    response = PJLinkResponse(command_line.command,
                                              self.class_info)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.ERROR_STATUS:
                if command_line.data == PJLinkProtocol.QUERY:
                    data = ''.join([error[1] for error in self.errors])
                    response = PJLinkResponse(command_line.command, data)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            elif command_line.command == PJLinkProtocol.LAMP:
                if command_line.data == PJLinkProtocol.QUERY:
                    data = ' '.join([
                        '%s %s' % (lamp[0], '1' if lamp[1] == True else '0')
                        for lamp in self.lamps
                    ])
                    response = PJLinkResponse(command_line.command, data)
                else:
                    response = PJLinkResponse(command_line.command,
                                              PJLinkProtocol.ERROR_2)

            else:
                response = PJLinkResponse(command_line.command,
                                          PJLinkProtocol.ERROR_1)

            client.send(response.encode())
            client.close()

        self.server.close()