Commits

Jakub Gustak  committed 169cafa

client get when missing chunk fails safely

  • Participants
  • Parent commits 3844f08

Comments (0)

Files changed (3)

                 self.sendCmd(FailedCmd())
                 return
 
+            if not p:
+                self.sendCmd(FailedCmd())
+                return
+
             #choose your destiny
             host, port, proto = p.rand_node()
             self.sendCmd(RetrCmd(p.id, f.filename, p.chunk, f.chunks,
                                  host, port))
 
         elif cmd.valid(LsCmd):
+            # show me where this file is
             ls = cmd.parse(LsCmd)
             try:
                 f = self.factory.cache[ls.filename]
                 return
 
             for p in f.parts:
+                if not p or not p.nodes:
+                    self.sendCmd(FailedCmd())
+                    return
+
                 host, port, proto = p.rand_node()
                 self.sendCmd(RetrCmd(p.id, f.filename, p.chunk, f.chunks,
                                      host, port))
 
         elif cmd.valid(LsfCmd):
+            # list all files
             for filename, f in self.factory.cache.items():
                 acc = 0.0
-                for c in f.parts:
-                    if c: acc += len(c.nodes)
+                valid = 0
+                for p in f.parts:
+                    if p and p.nodes:
+                        valid += 1
+                        acc += len(p.nodes)
 
-                self.sendCmd(FileCmd(filename, f.chunks, acc/len(f.parts)))
+                self.sendCmd(FileCmd(filename, valid, f.chunks, acc/len(f.parts)))
 
             self.sendCmd(DoneCmd())
 
         cmd = Command(line)
         if cmd.valid(FileCmd):
             file = cmd.parse(FileCmd)
-            print file.filename, file.chunks, file.replication
+            if file.valid != file.chunks:
+                print '!', 
+
+            print file.filename, file.valid, file.chunks, file.replication
 
         else:
             self.transport.loseConnection()
         self.sendCmd(LsCmd(filename=self.filename))
 
     def lineReceived(self, line):
+        # XXX should collect all, then process in batch
         cmd = Command(line)
         if cmd.valid(RetrCmd):
             retr = cmd.parse(RetrCmd)
                     clientGTPFactory(retr.id, self.chunkd[retr.id], self))
 
         else:
-            print line, "No such file"
+            print line, "no such file or missing chunk"
             self.transport.loseConnection()
 
 
         try:
             self.ctrl.f.append_chunk(self.item.chunk, self._buff, self.id)
         except UncorrectableError:
-            print "chunk failed:", self.item.filename, self.id
+            #print "chunk failed:", self.item.filename, self.item.chunk
             self.ctrl.sendCmd(GetCmd(id=self.item.id, filename=self.item.filename,
                 chunk=self.item.chunk, chunks=self.item.chunks))
             return
     def __init__(self, path):
         self.path = path
         self.size = 0
-        self.file = open(path, "w")
         self.chunks = []
 
     def append_chunk(self, chunkid, raw, id):
         self.size += len(decoded)
     
     def close(self):
+        file = open(self.path, "w")
         for x in self.chunks:
-            self.file.write(x)
+            file.write(x)
 
-        self.file.close()
+        file.close()
         print '[done]'
 
 
 class FileCmd(Cmd):
     token = 'FILE'
 
-    def __init__(self, filename, chunks, replication):
+    def __init__(self, filename, valid, chunks, replication):
         self.filename = filename
+        self.valid = int(valid)
         self.chunks = int(chunks)
         self.replication = float(replication)
-        self.STR = self.token + " %s %d %f"
+        self.STR = self.token + " %s %d %d %f"
 
     def __str__(self):
-        return self.STR % (self.filename, self.chunks, self.replication)
+        return self.STR % (self.filename, self.valid, self.chunks, self.replication)
 
 class FCmd(Cmd):
     token = ''