예제 #1
0
 def getattr(self, path, fh=None):
     print("memory.py getattr called")
     if path not in self.files:
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for getattr', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_Rfor getattr', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_GETATTR)
         packet['path'] = path
         print('Sending OP_GETATTR', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_GETATTR_R', reply)
         if reply['getattr']:
             return reply['getattr']
         else:
             raise FuseOSError(ENOENT)
     return self.files[path]
예제 #2
0
 def getxattr(self, path, name, position=0):
     if path not in self.files:
         print("GETXATTR in other location")
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for GETXATTR', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for GETXATTR', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_GETXATTR)
         packet['path'] = path
         packet['name'] = name
         print('Sending OP_GETXATTR', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_GETXATTR_R', reply)
         if reply['getxattr']:
             return reply['getxattr']
     else:
         attrs = self.files[path].get('attrs', {})
         try:
             return attrs[name]
         except KeyError:
             return ''       # Should return ENOATTR
예제 #3
0
 def truncate(self, path, length, fh=None):
     if path not in self.files:
         print("Truncate in other location")
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for TRUNCATE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for TRUNCATE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_TRUNC)
         packet['path'] = path
         packet['length'] = length
         print('Sending OP_TRUNCATE', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_TRUNCATE_R', reply)
     else:
         # make sure extending the file fills in zero bytes
         self.data[path] = self.data[path][:length].ljust(
             length, b'\x00'.decode())
         self.files[path]['st_size'] = length
예제 #4
0
 def rename(self, old, new):
     print("In Rename")
     brename = packets.new_packet(OP_RENAME)
     brename['old'] = old
     brename['new'] = new
     if old not in self.files:
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = old
         print('Sending OP_FIND for RENAME', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for RENAME', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_RENAME)
         packet['old'] = old
         packet['new'] = new
         print('Sending OP_RENAME', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_RENAME_R', reply)
     else:
         self.data[new] = self.data[old]
         del self.data[old]
         self.files[new] = self.files.pop(old)
         boot_sock = socks.tcp_sock()
         boot_sock.connect(self.bootstrap)
         boot_sock.send(packets.build(brename))
예제 #5
0
 def unlink(self, path):
     print("In Unlink")
     brename = packets.new_packet(OP_DELETE)
     brename['path'] = path
     if path not in self.files:
         print("In other location")
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for DELETE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for DELETE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_DELETE)
         packet['path'] = path
         print('Sending OP_DELETE', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_DELETE_R', reply)
     else:
         print("In self")
         del self.data[path]
         self.files.pop(path)
         boot_sock = socks.tcp_sock()
         boot_sock.connect(self.bootstrap)
         boot_sock.send(packets.build(brename))
예제 #6
0
 def rename(self, old, new):
     print("In Rename")
     brename = packets.new_packet(OP_RENAME)
     brename['old'] = old
     brename['new'] = new
     if old not in self.files:
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = old
         print('Sending OP_FIND for RENAME', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for RENAME', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_RENAME)
         packet['old'] = old
         packet['new'] = new
         print('Sending OP_RENAME', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_RENAME_R', reply)
     else:
         self.data[new] = self.data[old]
         del self.data[old]
         self.files[new] = self.files.pop(old)
         boot_sock = socks.tcp_sock()
         boot_sock.connect(self.bootstrap)
         boot_sock.send(packets.build(brename))
예제 #7
0
 def truncate(self, path, length, fh=None):
     if path not in self.files:
         print("Truncate in other location")
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for TRUNCATE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for TRUNCATE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_TRUNC)
         packet['path'] = path
         packet['length'] = length
         print('Sending OP_TRUNCATE', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_TRUNCATE_R', reply)
     else:
     # make sure extending the file fills in zero bytes
         self.data[path] = self.data[path][:length].ljust(
             length, b'\x00'.decode())
         self.files[path]['st_size'] = length
예제 #8
0
 def unlink(self, path):
     print("In Unlink")
     brename = packets.new_packet(OP_DELETE)
     brename['path'] = path
     if path not in self.files:
         print("In other location")
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for DELETE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for DELETE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_DELETE)
         packet['path'] = path
         print('Sending OP_DELETE', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_DELETE_R', reply)
     else:
         print("In self")
         del self.data[path]
         self.files.pop(path)
         boot_sock = socks.tcp_sock()
         boot_sock.connect(self.bootstrap)
         boot_sock.send(packets.build(brename))
예제 #9
0
 def getxattr(self, path, name, position=0):
     if path not in self.files:
         print("GETXATTR in other location")
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for GETXATTR', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for GETXATTR', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_GETXATTR)
         packet['path'] = path
         packet['name'] = name
         print('Sending OP_GETXATTR', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_GETXATTR_R', reply)
         if reply['getxattr']:
             return reply['getxattr']
     else:
         attrs = self.files[path].get('attrs', {})
         try:
             return attrs[name]
         except KeyError:
             return ''  # Should return ENOATTR
예제 #10
0
 def getattr(self, path, fh=None):
     print("memory.py getattr called")
     if path not in self.files:
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for getattr', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_Rfor getattr', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_GETATTR)
         packet['path'] = path
         print('Sending OP_GETATTR', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_GETATTR_R', reply)
         if reply['getattr']:
             return reply['getattr']
         else: raise FuseOSError(ENOENT)
     return self.files[path]
예제 #11
0
 def write(self, path, data, offset, fh):
     #THIS IS WHERE WRITE HAPPENS
     print("WRITE in Memory")
     if path not in self.files:
         print('remote write')
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for WRITE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for WRITE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_WRITE)
         packet['path'] = path
         packet['data'] = data.decode()
         packet['offset'] = offset
         packet['fh'] = fh
         print('Sending OP_WRITE', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_WRITE_R', reply)
         if 'datalen' in reply:
             return reply['datalen']
         else:
             raise FuseOSError(ENOENT)
     print("local write")
     print("current contents:", self.data[path])
     print("offset", offset)
     print("fh", fh)
     self.data[path] = self.data[path][:offset].ljust(
         offset, b'\x00'.decode())
     self.data[path] += data.decode()
     self.data[path] += self.data[path][offset + len(data):]
     # self.data[path] = (
     #     # make sure the data gets inserted at the right offset
     #    self.data[path][:offset].ljust(offset, '\x00'.encode('ascii'))
     #    + data.decode()
     #     # and only overwrites the bytes that data is replacing
     #    + self.data[path][offset + len(data):])
     self.files[path]['st_size'] = len(self.data[path])
     print("current contents:", self.data[path])
     return len(data)
예제 #12
0
 def write(self, path, data, offset, fh):
     #THIS IS WHERE WRITE HAPPENS
     print("WRITE in Memory")
     if path not in self.files:
         print('remote write')
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for WRITE', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for WRITE', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_WRITE)
         packet['path'] = path
         packet['data'] = data.decode()
         packet['offset'] = offset
         packet['fh'] = fh
         print('Sending OP_WRITE', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_WRITE_R', reply)
         if 'datalen' in reply:
             return reply['datalen']
         else: raise FuseOSError(ENOENT)
     print("local write")
     print("current contents:", self.data[path])
     print("offset", offset)
     print("fh", fh)
     self.data[path] = self.data[path][:offset].ljust(offset, b'\x00'.decode())
     self.data[path] += data.decode()
     self.data[path] += self.data[path][offset + len(data):]
     # self.data[path] = (
     #     # make sure the data gets inserted at the right offset
     #    self.data[path][:offset].ljust(offset, '\x00'.encode('ascii'))
     #    + data.decode()
     #     # and only overwrites the bytes that data is replacing
     #    + self.data[path][offset + len(data):])
     self.files[path]['st_size'] = len(self.data[path])
     print("current contents:", self.data[path])
     return len(data)
예제 #13
0
 def readdir(self, path, fh):
     #This is where LS happens
     #return ['.', '..'] + [x[1:] for x in self.files if x != '/']
     packet = packets.new_packet(OP_LS)
     sock = socks.tcp_sock()
     sock.connect(self.bootstrap)
     sock.send(packets.build(packet))
     ls = packets.unpack(sock.recv(MAX_READ))
     print('ls contents', ls['ls'])
     return ['.', '..'] + [x[1:] for x in ls['ls']]
예제 #14
0
 def readdir(self, path, fh):
     #This is where LS happens
     #return ['.', '..'] + [x[1:] for x in self.files if x != '/']
     packet = packets.new_packet(OP_LS)
     sock = socks.tcp_sock()
     sock.connect(self.bootstrap)
     sock.send(packets.build(packet))
     ls = packets.unpack(sock.recv(MAX_READ))
     print('ls contents', ls['ls'])
     return ['.', '..'] + [x[1:] for x in ls['ls']]
예제 #15
0
 def read(self, path, size, offset, fh):
     print("memory.py read called")
     if path not in self.files:
         packet = packets.new_packet(
             OP_FIND)  # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for READ', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for READ', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock()
         tcp_sock.connect(loc)  # connect to other node
         packet = packets.new_packet(OP_READ)
         packet['path'] = path
         packet['size'] = size
         packet['offset'] = offset
         packet['fh'] = fh
         print('Sending OP_READ', packet)
         tcp_sock.send(packets.build(packet))  # send syscall
         reply = packets.unpack(
             tcp_sock.recv(MAX_READ))  # TODO: listen for reply in select
         print('Received OP_READ_R', reply)
         if 'read' in reply:
             return reply['read'].encode('utf-8')
         else:
             raise FuseOSError(ENOENT)
     print("offset: ", offset)
     print("size: ", size)
     print("read contents:", self.data[path])
     datalen = len(self.data[path])
     print("datalen", datalen)
     end = datalen
     if datalen > size:
         end = size
     return self.data[path][offset:offset + end].encode('utf-8')
예제 #16
0
def sigint_handler(signal, frame):
    for n in nodes:
        print(tuple(n))
        tcp_sock = socks.tcp_sock()
        try:
            tcp_sock.connect(tuple(n))
        except:
            continue
        msg = packets.new_packet(OP_LEAVE)
        tcp_sock.send(packets.build(msg))
        tcp_sock.close()
    print("Goodbye~!")
    sys.exit(0)
예제 #17
0
def sigint_handler(signal,frame):
    for n in nodes:
        print(tuple(n))
        tcp_sock = socks.tcp_sock() 
        try:
            tcp_sock.connect(tuple(n))
        except:
            continue
        msg = packets.new_packet(OP_LEAVE)
        tcp_sock.send(packets.build(msg))
        tcp_sock.close()
    print("Goodbye~!")
    sys.exit(0)
예제 #18
0
 def read(self, path, size, offset, fh):
     print("memory.py read called")
     if path not in self.files:
         packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file
         packet['path'] = path
         print('Sending OP_FIND for READ', packet)
         sock = socks.tcp_sock()
         sock.connect(self.bootstrap)
         sock.send(packets.build(packet))
         reply = packets.unpack(sock.recv(MAX_READ))
         print('Received OP_FIND_R for READ', reply)
         if 'loc' not in reply:
             raise FuseOSError(ENOENT)
         loc = tuple(reply['loc'])
         tcp_sock = socks.tcp_sock() 
         tcp_sock.connect(loc) # connect to other node
         packet = packets.new_packet(OP_READ)
         packet['path'] = path
         packet['size'] = size
         packet['offset'] = offset
         packet['fh'] = fh
         print('Sending OP_READ', packet)
         tcp_sock.send(packets.build(packet)) # send syscall 
         reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select
         print('Received OP_READ_R', reply)
         if 'read' in reply:
             return reply['read'].encode('utf-8')
         else: raise FuseOSError(ENOENT)
     print("offset: ", offset)
     print("size: ", size)
     print("read contents:",self.data[path])
     datalen = len(self.data[path])
     print("datalen", datalen)
     end = datalen
     if datalen > size:
         end = size
     return self.data[path][offset:offset + end].encode('utf-8')
예제 #19
0
 def create(self, path, mode):
     self.files[path] = dict(st_mode=(S_IFREG | mode),
                             st_nlink=1,
                             st_size=0,
                             st_ctime=time(),
                             st_mtime=time(),
                             st_atime=time())
     self.data[path] = ""
     packet = packets.new_packet(OP_CREATE)
     packet['path'] = path
     packet['loc'] = self.node
     sock = socks.tcp_sock()
     sock.connect(self.bootstrap)
     print('Connected to bootstrap', sock)
     print('Sending packet', packet)
     print('Built packet', packets.build(packet))
     sock.send(packets.build(packet))  # send new file to bootstrap
     sock.close()
     # should we wait for ack here?
     self.fd += 1
     return self.fd
예제 #20
0
 def create(self, path, mode):
     self.files[path] = dict(
         st_mode=(S_IFREG | mode),
         st_nlink=1,
         st_size=0,
         st_ctime=time(),
         st_mtime=time(),
         st_atime=time())
     self.data[path] = ""
     packet = packets.new_packet(OP_CREATE)
     packet['path'] = path
     packet['loc'] = self.node
     sock = socks.tcp_sock()
     sock.connect(self.bootstrap)
     print('Connected to bootstrap', sock)
     print('Sending packet', packet)
     print('Built packet', packets.build(packet))
     sock.send(packets.build(packet)) # send new file to bootstrap
     sock.close()
     # should we wait for ack here?
     self.fd += 1
     return self.fd
예제 #21
0
def recv(conn):
    print('Receiving from', conn)
    packet = packets.unpack(conn.recv(MAX_READ))
    print('Received', packet)
    if not packet:
        print('Empty packet, closing connection to', conn)
        close(conn)
        return
    if 'opcode' not in packet:
        print('Invalid packet: opcode missing')

    if packet['opcode'] == OP_JOIN:
        print("FLOOPLEDYDOOOO",packet)
        nodes.append(packet['loc'])

    elif packet['opcode'] == OP_LS:
        print('Received OP_LS')
        reply = packets.new_packet(OP_LS_R)
        reply['ls'] = list(files.keys())
        conn.send(packets.build(reply))

    elif packet['opcode'] == OP_CREATE:
        print('Creating file')
        files[packet['path']] = packet['loc'] # map new file to node

    elif packet['opcode'] == OP_FIND: 
        print('Received OP_FIND', packet)
        reply = packets.new_packet(OP_FIND_R)
        if packet['path'] in files:
            reply['loc'] = files[packet['path']] 
        # TODO: error file not found
        print('Sending OP_FIND_R', reply)
        conn.send(packets.build(reply))

    elif packet['opcode'] == OP_RENAME:
        print('Received OP_RENAME', packet)
        print('files',files)
        files[packet['new']] = files[packet['old']]
        del files[packet['old']]
        print('Renamed file')

    elif packet['opcode'] == OP_DELETE:
        print('Received OP_DELETE', packet)
        print('files', files)
        del files[packet['path']]
        print('Deleted file')

    elif packet['opcode'] == OP_BYE:
        print('Received OP_LEAVE', packet)
        print('nodes are: ', nodes)
        if packet['loc'] in nodes:
            nodes.remove(packet['loc'])
        temp = {x:y for x,y in files.items()}
        for f in temp:
            print(packet['loc'])
            print(temp[f])
            if temp[f] == packet['loc']:
                del files[f]
        reply = packets.new_packet(OP_BYE_R)
        print('Sending OP_LEAVE_R', reply)
        print('Sending to: ',conn)
        conn.send(packets.build(reply))

    else:
        print('Invalid opcode')
예제 #22
0
def recv(conn):
    print('Receiving from', conn)
    packet = packets.unpack(conn.recv(MAX_READ))
    print('Received', packet)
    if not packet:
        print('Empty packet, closing connection to', conn)
        close(conn)
        return
    if 'opcode' not in packet:
        print('Invalid packet: opcode missing')

    if packet['opcode'] == OP_JOIN:
        print("FLOOPLEDYDOOOO", packet)
        nodes.append(packet['loc'])

    elif packet['opcode'] == OP_LS:
        print('Received OP_LS')
        reply = packets.new_packet(OP_LS_R)
        reply['ls'] = list(files.keys())
        conn.send(packets.build(reply))

    elif packet['opcode'] == OP_CREATE:
        print('Creating file')
        files[packet['path']] = packet['loc']  # map new file to node

    elif packet['opcode'] == OP_FIND:
        print('Received OP_FIND', packet)
        reply = packets.new_packet(OP_FIND_R)
        if packet['path'] in files:
            reply['loc'] = files[packet['path']]
        # TODO: error file not found
        print('Sending OP_FIND_R', reply)
        conn.send(packets.build(reply))

    elif packet['opcode'] == OP_RENAME:
        print('Received OP_RENAME', packet)
        print('files', files)
        files[packet['new']] = files[packet['old']]
        del files[packet['old']]
        print('Renamed file')

    elif packet['opcode'] == OP_DELETE:
        print('Received OP_DELETE', packet)
        print('files', files)
        del files[packet['path']]
        print('Deleted file')

    elif packet['opcode'] == OP_BYE:
        print('Received OP_LEAVE', packet)
        print('nodes are: ', nodes)
        if packet['loc'] in nodes:
            nodes.remove(packet['loc'])
        temp = {x: y for x, y in files.items()}
        for f in temp:
            print(packet['loc'])
            print(temp[f])
            if temp[f] == packet['loc']:
                del files[f]
        reply = packets.new_packet(OP_BYE_R)
        print('Sending OP_LEAVE_R', reply)
        print('Sending to: ', conn)
        conn.send(packets.build(reply))

    else:
        print('Invalid opcode')
예제 #23
0
def recv(sock):
    print("receiving from socket")
    packet = packets.unpack(sock.recv(MAX_READ))
    print("packet is: ", packet)
    if not packet:
        print('Read empty packet, closing connection', sock)
        close(sock)
        return
    if packet['opcode'] == OP_GETATTR:
        print('Received OP_GETATTR in node.py')
        reply = packets.new_packet(OP_GETATTR_R)
        reply['getattr'] = filesystem.getattr(packet['path']) # call getattr locally
        print('Sending OP_GETATTR_R', reply)
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_BYE_R:
        close(sock)
        os.kill(os.getpid(),signal.SIGKILL)
        sys.exit(0)
 
    elif packet['opcode'] == OP_READ:
        print("Received OP_READ in node.py")
        reply = packets.new_packet(OP_READ_R)
        reply['read'] = filesystem.read(packet['path'],packet['size'],packet['offset'],packet['fh']).decode()
        print('Sending OP_READ_R with ', reply['read'])
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_WRITE:
        print("Received OP_WRITE in node.py")
        reply = packets.new_packet(OP_WRITE_R)
        reply['datalen'] = filesystem.write(packet['path'],packet['data'].encode(),packet['offset'],packet['fh'])
        print('Sending OP_WRITE_R with ', reply['datalen'])
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_TRUNC:
        print("Received OP_TRUNC in node.py")
        reply = packets.new_packet(OP_TRUNC_R)
        reply['trunc'] = filesystem.truncate(packet['path'],packet['length'])
        print('Sending OP_TRUNC_R with', reply['trunc'])
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_GETXATTR:
        print("Received OP_GETXATTR in nodde.py")
        reply = packets.new_packet(OP_GETXATTR_R)
        reply['getxattr'] = filesystem.getxattr(packet['path'],packet['name'])
        print('Sending OP_GETXATTR_R with', reply['getxattr'])
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_LEAVE:
        print("Bootstrap has exited, so we will exit too.")
        os.kill(os.getpid(),signal.SIGKILL)
        sys.exit(0)

    elif packet['opcode'] == OP_RENAME:
        print("Received OP_RENAME in node.py")
        reply = packets.new_packet(OP_RENAME_R)
        reply['rename'] = filesystem.rename(packet['old'],packet['new'])
        print("Sending OP_RENAME_R in node.py")
        sock.send(packets.build(reply))

    elif packet['opcode'] == OP_DELETE:
        print("Received OP_DELETE in node.py")
        reply = packets.new_packet(OP_DELETE_R)
        reply['delete'] = filesystem.unlink(packet['path'])
        print("Sending OP_DELETE_R in node.py")
        sock.send(packets.build(reply))
    else:
        print('Invalid opcode')