Exemplo n.º 1
0
 def handle(self, sock, addr):
     #read or write
     cmd = sock.recv(1)
     if not cmd:
         return
     #logging.info('new cmd arrived')
     
     cmd, = struct.unpack('!b', cmd)
     if cmd == 0x01:#read block
         msg = helper.receive_len(sock, 24)
         block_id, start, length = struct.unpack('!3q', msg)
         data = self.read_block(block_id, start, length)
         sock.sendall(data)
     elif cmd == 0x02:#write block
         msg = helper.receive_len(sock, 24)
         block_id, start, data_len = struct.unpack('!3q', msg)
         data = helper.receive_len(sock, data_len)
         self.write_block(block_id, start, data)
         
     elif cmd ==0x03:#write raw file
         msg = helper.receive_len(sock, 17)
         name_len, start, data_len = struct.unpack('!b2q', msg)
         file_name = sock.recv(name_len)
         data = helper.receive_len(sock, data_len)
         self.write_raw(file_name, start, data)
             
     sock.close()
Exemplo n.º 2
0
 def send_command(self, command, data=' '):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((params.TASK_SERVER_IP, params.TASK_SERVER_PORT))
     msg = command + ':' + data
     msg_len = struct.pack('!i', len(msg))
     sock.sendall(msg_len + msg)
     recv_len, = struct.unpack('!i',helper.receive_len(sock, 4))
     recv_data = helper.receive_len(sock, recv_len)
     pos = recv_data.find(':')
     cmd = str(recv_data[: pos])
     msg = recv_data[pos+1:]
     sock.close()
     return cmd, msg
Exemplo n.º 3
0
 def handle(self, sock, addr):
     data_len, = struct.unpack('!i', helper.receive_len(sock, 4))
     all = helper.receive_len(sock, data_len)
     pos = all.find(':')
     cmd = str(all[: pos])
     msg = all[pos+1:]
     if cmd.startswith('job'):
         self.handle_job(sock, addr, cmd, msg)
     elif cmd.startswith('map'):
         self.handle_map(sock, addr, cmd, msg)
     elif cmd.startswith('reduce'):
         self.handle_reduce(sock, addr, cmd, msg)
     sock.close()
Exemplo n.º 4
0
 def handle_request(self, sock):
     name_len, = struct.unpack("!i", helper.receive_len(sock, 4))
     file_name = helper.receive_len(sock, name_len)
     # logging.info('request:'+ file_name)
     path = self._base_dir + file_name
     fid = open(path, "rb")
     while True:
         data = fid.read(params.BUFFER_SIZE)
         if not data:
             break
         sock.sendall(data)
     fid.close()
     sock.close()
     logging.info("response:" + file_name)