Ejemplo n.º 1
0
	def fpwd(self,command):
		'''check the current directory in the server'''
		send_msg = message.encode_msg_str(PWD,0,'')
		self.client.sendall(send_msg)
		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)
		print('in the server , the current work directory is ' + dict_msg['server_current_dir_item'])
Ejemplo n.º 2
0
	def pack_get(self,command):
		arg_list = command.split()
		pack_file_name = arg_list[1]
		for file in os.listdir(current_dir):
			if pack_file_name == file:
				print("redundant file name ,eror")
				return
		send_msg = message.encode_msg_str(PAC_GET,0,pack_file_name)
		self.client.sendall(send_msg)

		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)
		print(rcv_msg)
		status_code = dict_msg['status_code']
		if status_code == NO_DIR_FILE:
			print('no such dir or file')
			return

		file_size = dict_msg['get_file_size']
		zipName = pack_file_name + '_tmp.zip'
		f = open(current_dir + '\\' + zipName,'wb')
		f.write(dict_msg['file_byte'])
		rcv_file_size = dict_msg['transfer_byte_length']
		#print('file_size: '+ str(file_size))#test


		while rcv_file_size < file_size:
			rcv_msg = self.__read_as_bytearray()
			dict_msg = message.decode_msg(rcv_msg)
			status_code = dict_msg['status_code']
			if status_code == ON_TRANSFER_GET:
				transfer_byte_length = dict_msg['transfer_byte_length']
				file_byte = rcv_msg[5:5+transfer_byte_length]
				f.write(file_byte)
				#print('rcv_file_size: '+ str(rcv_file_size))#test
				rcv_file_size += transfer_byte_length
			else:
				break

		send_msg = message.encode_msg_str(SUCC_GET,0,'')
		self.client.sendall(send_msg)
		f.close()
		packDir.unzipDirectory(current_dir,zipName)
		#print('current_dir: ' + str(current_dir))#test
		#print('zipName: ' + str(zipName))#test
		os.remove(zipName)
Ejemplo n.º 3
0
	def fcd(self,command):
		'''shift to other directory in the server'''
		arg_list = command.split()
		cd_dir_name = arg_list[1]
		send_msg = message.encode_msg_str(CD,0,cd_dir_name)
		self.client.sendall(send_msg)
		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)
		if dict_msg['status_code'] == NO_DIR_FILE:
			print('no such file or directory')
Ejemplo n.º 4
0
	def fdir(self,command):
		'''check what files the current directory  in the server has'''
		send_msg = message.encode_msg_str(DIR,0,'')
		self.client.sendall(send_msg)
		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)
		output_string = ''
		for file_item in dict_msg['dir_result']:
			output_string += file_item + ' '
		print(output_string)
Ejemplo n.º 5
0
	def fget(self,command):
		'''get file in the server'''
		arg_list = command.split()
		get_file_name = arg_list[1]
		for file in os.listdir(current_dir):
			if file == get_file_name:
				print("redundant file name ,eror")
				return
		send_msg = message.encode_msg_str(GET,0,get_file_name)
		self.client.sendall(send_msg)

		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)
		status_code = dict_msg['status_code']
		if status_code == NO_DIR_FILE:
			print('no such dir or file')
			return

		file_size = dict_msg['get_file_size']
		f = open(current_dir + '\\' + get_file_name,'wb')
		f.write(dict_msg['file_byte'])
		rcv_file_size = dict_msg['transfer_byte_length']
		#print('file_size: ' + str(file_size))#test

		while rcv_file_size < file_size:
			rcv_msg = self.__read_as_bytearray()
			dict_msg = message.decode_msg(rcv_msg)
			status_code = dict_msg['status_code']
			if status_code == ON_TRANSFER_GET:
				transfer_byte_length = dict_msg['transfer_byte_length']
				file_byte = rcv_msg[5:5+transfer_byte_length]
				f.write(file_byte)
				#print('rcv_file_size: ' + str(rcv_file_size))#test
				rcv_file_size += transfer_byte_length
			else:
				break

		send_msg = message.encode_msg_str(SUCC_GET,0,'')
		self.client.sendall(send_msg)
		f.close()
Ejemplo n.º 6
0
    def rpc_call(self, destination, action, *args, **kwargs):
        '''
        Sends (using the protocol specified in message.py) a message to
        the specified destination machine.

        If the send times out, the other machine is considered as having failed,
        and the socket to that machine closed.

        :param destination: MID to which to send the message
        :param action: action of this message (message type, i.e. GETSTATUS)
        :param args: arguments to the message
        :param kwargs: additional (dict-type) arguments to the message
        :return: decoded response to the message
        '''
        msg = self.create_msg(action, *args, **kwargs)
        # self.dprint('Action %s sent to %d', action.name, destination)
        try:
            sock = None
            with self.socket_lock:
                if destination in self.mid_to_sockets:
                    sock = self.mid_to_sockets[destination]

            if sock:
                send_msg(sock, msg)
                if 'expect_response' in kwargs and kwargs['expect_response'] == True:
                    resp = recv_msg(sock)
                else:
                    return None
            else:
                raise RuntimeError()

            return decode_msg(resp)
        except socket.timeout:
            return None
        except (RuntimeError, socket.error) as e:
            with self.socket_lock:
                try:
                    self.mid_to_sockets[destination].shutdown(socket.SHUT_RDWR)
                    self.mid_to_sockets[destination].close()
                except Exception:
                    pass
                if destination in self.mid_to_sockets:
                    del self.mid_to_sockets[destination]
                self.dprint('I think machine %d died' % destination)

        return None
Ejemplo n.º 7
0
	def fput(self,command):
		'''put file to server'''
		arg_list = command.split()
		put_file_name = arg_list[1]
		put_file_path = current_dir + '\\'+ put_file_name 
		file_size = 0
		try:
			file_size = os.path.getsize(put_file_path)
		except OSError as e:
			print('no such file or directory')

		info_legth = 1+4+4+32
		f = open(put_file_path,'rb')
		read_size = min(std_msg_length-info_legth,file_size)
		send_size = read_size
		chunk = f.read(std_msg_length-info_legth)    
		file_size_array = bytearray(4)
		file_size_array[:4] = file_size.to_bytes(4,byteorder = 'big')
		name_byte = bytearray(32)
		name_byte[:32] = put_file_name.encode('utf-8') + b'\x00' * (32-len(put_file_name))
		name_byte += chunk
		file_size_array += name_byte
		send_msg = message.encode_msg_byte(PUT,send_size,file_size_array)
		self.client.sendall(send_msg) 

		info_length = 1+4
		while send_size < file_size:
			read_size = min(std_msg_length-info_legth,file_size - send_size)
			chunk = f.read(read_size) 
			send_msg = message.encode_msg_byte(ON_TRANSFER_PUT,read_size,chunk)
			self.client.sendall(send_msg)
			send_size += read_size 

		f.close()
		rcv_msg = self.__read_as_bytearray()
		dict_msg = message.decode_msg(rcv_msg)

		if dict_msg['status_code'] == ERR_INTERRUPT_PUT:
			print('File transfer error for file : ' + put_file_name)
			return
		elif dict_msg['status_code'] == REDUNDANT_FILE:
			print('the same name file has been in server')
			return
		elif dict_msg['status_code'] == SUCC_PUT:
			return
Ejemplo n.º 8
0
	def start(self):
		'''start the program'''
		self.connect()
		while True:
			#no space in username and password
			username = input("username: "******"password: ").strip()

			#transfer the username and password and pack them
			login_msg = message.pack_login_msg(username,password,LOGIN)
			self.client.sendall(login_msg)
			rcv_msg = self.__read_as_bytearray()
			dict_msg = message.decode_msg(rcv_msg)
			status_code = dict_msg['status_code']
			if status_code == SUCC_LOGIN:
				print('you have logined successfully!enter your command')
				cur_server_dir = dict_msg['cur_dir']
				print('in server the current directory is ' + cur_server_dir)
			else:
				self.client.close()
				return

			self.interactive()
Ejemplo n.º 9
0
        check_msg, login = check_user_db(username, password)
        login_recv_msg = message.encode_msg_str(SUCC_LOGIN, 0,
                                                current_work_dir)
        if login:
            connection.sendall(login_recv_msg)
            print(username + " login")
        else:
            login_recv_msg = message.encode_msg_str(ERR_LOGIN, 0, check_msg)
            connection.sendall(login_recv_msg)
            print(username + " tried to login but failed due to " + check_msg)
            connection.close()
            break

    while login:
        cmd_rcv = read_as_bytearray(connection)
        dict_msg = message.decode_msg(cmd_rcv)
        status_code = dict_msg['status_code']

        if status_code == DIR:
            array_all = bytearray(27)
            dir_result = bytearray(std_msg_length - 32)
            write_pos = 32
            for file in os.listdir(current_work_dir):
                dir_result[write_pos:write_pos + 32] = file.encode('utf-8')
                write_pos += 32

            array_all += dir_result
            send_msg = message.encode_msg_byte(CARRY_DIR, 0, array_all)
            connection.sendall(send_msg)
            continue
Ejemplo n.º 10
-1
        def process(sock):
            try:
                while True:
                    mm = recv_msg(sock)
                    msg = decode_msg(mm)
                    # self.dprint('Received %s', msg['action'].name)

                    if msg['kwargs']['migration_id'] != self.migration_id:
                        self.dprint('Migration id mismatch (%d != %d), ignoring message %s', msg['kwargs']['migration_id'], self.migration_id, msg)
                        continue

                    response = {}
                    if msg['action'] == Action.GETSTATUS:
                        response = self.get_status_handler(msg)
                    elif self.status == IslandStatus.MIGRATION_READY:
                        if msg['action'] == Action.SEND_PREPARE_NACK:
                            response = self.prepare_nack_handler(msg)
                        elif msg['action'] == Action.SEND_ACCEPT_NACK:
                            response = self.accept_nack_handler(msg)
                        elif msg['action'] == Action.SEND_PREPARE:
                            response = self.prepare_handler(msg)
                        elif msg['action'] == Action.SEND_PROMISE:
                            response = self.promise_handler(msg)
                        elif msg['action'] == Action.SEND_ACCEPT:
                            response = self.accept_handler(msg)
                        elif msg['action'] == Action.SEND_ACCEPTED:
                            response = self.accepted_handler(msg)
                    else:
                        self.dprint('Cannot handle message')
                   
                    if self.test_failures:
                        # Force a timeout with probability 1% for testing purposes
                        if random.random() < 0.01:
                            # Oopsies network is slow
                            time.sleep(1.5)
                    
                    if response:
                        send_msg(sock, response)

            except RuntimeError as e:
                pass

            try:
                sock.shutdown(socket.SHUT_RDWR)
                sock.close()
            except Exception:
                pass