[Nym3-commit] r80 - trunk

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Wed, 04 Aug 2004 19:27:58 +0200


Author: jr
Date: 2004-08-04 19:27:57 +0200 (Wed, 04 Aug 2004)
New Revision: 80

Modified:
   trunk/Main.py
   trunk/User.py
Log:
move the computation of sendList in a User method(Main.py->User.py)
fix some bug (idxList was wrongly computed)
clean the code


Modified: trunk/Main.py
===================================================================
--- trunk/Main.py	2004-08-04 16:55:20 UTC (rev 79)
+++ trunk/Main.py	2004-08-04 17:27:57 UTC (rev 80)
@@ -112,89 +112,9 @@
 		elif (com.ct() == 5):
 		    pass
                 elif (com.ct() == 6):
-                    def hasMail(midIdx,l):
-                        if(nymUser.mbox.has_key(l[midIdx])):
-                            True
-                        else:
-                            False
-                            
-                    synCnt=0
-                    inter=[]
-		    midList=nymUser.midAfter(com.after)
-                    nymUser.lock()
-                    nymUser.load_index()
-                    nymUser.load_synbox()
-                    nymUser.load_mbox()
-                    #remove already sent synopsis mid
-                    #grumbl unwanted feature
-                    #for m in midList:
-                    #    if(index[m]['sent'] in ['synopsis','full']):
-                    #        midList.remove(m)
-                    #we just need the com.num first
-                    midList=midList[:com.num]
-                    
-                    #create a list of blobs containing the mid in midlist
-                    clearList=[]
-                    sendList=[]
-                    #as long as midList isn't empty
-                    while(midList):
-                        #look for the synblob containing midList[0]
-                        try:
-                            i, (ml, status, synblob) = nymUser.getSyn(midList[0])
-                            #found
-                            if(status == 'clear'):
-                                if(clearList == []):
-                                    bInf=i
-                                clearList.append((ml[0],synblob))
-                                if(len(clearList) == 16):
-                                    nymUser.syn[bInf:i+1]=[nymUser.blobify(clearList)]
-                                    clearList=[]
-                                    (m,s,sy) = nymUser.syn[bInf]
-                                    def test(midx):
-                                        hasMail(midx,m)
-                                    sendList.append(Mail.bf(filter(test,range(len(m)))),sy)
-                                else:
-                                    bSup=i+1
-                                del(midList[0])
-                            elif(status == 'encrypted'):
-                                if(clearList != []):
-                                    nymUser.syn[bInf:bSup]=[nymUser.blobify(clearList)] 
-                                    clearList=[]
-                                    (m,s,sy) = nymUser.syn[bInf]
-                                    def test(midx):
-                                        hasMail(midx,m)
-                                    sendList.append(Mail.bf(filter(test,range(len(m)))),sy)
-                                #idxList=[ml.index(midList[0])]
-                                def test(midx):
-                                    return (nymUser.timecmp(com.num,midx)<0)
-                                idxList=filter(test,ml)
-                                #remove from the midList the mid from idxList
-                                for j in idxList:
-                                    try:
-                                        midList.remove(j)
-                                    except ValueError:
-                                        pass
-                                def test(midx):
-                                    hasMail(midx,ml)
-                                sendList.append(Mail.bf(filter(test,idxList)),synblob)
-                            else:
-                                #this should not happen
-                                raise 'Bug'
-                        except:
-                            #this should not happen
-                            raise 'Bug'
-                    #if clearList isn't empty
-                    if(clearList != []):
-                        #this should be ok if we have proper locks
-                        nymUser.syn[bInf:bSup]=[nymUser.blobify(clearList)] 
-                        (m,s,sy) = nymUser.syn[bInf]
-                        sendList.append(Mail.bf(range(len(m))),sy)
+                    sendList=nymUser.sendList(com.num,com.after)
                     #TODO send what is in sendList (list of BF,enc syn)
                     #TODO modify the state (['sent']='synopsis'
-                    nymUser.save_synbox()
-                    nymUser.save_index()
-                    #we do not modify the mbox
-                    nymUser.release()
                 elif (com.ct() == 7):
 		    nymUser.lock()
                     nymUser.load_mbox()

Modified: trunk/User.py
===================================================================
--- trunk/User.py	2004-08-04 16:55:20 UTC (rev 79)
+++ trunk/User.py	2004-08-04 17:27:57 UTC (rev 80)
@@ -279,6 +279,94 @@
     def setUp(self):
 	self.data['up'] = True
 
+    def encryptSyn(self,i,j):
+        """Replace in the synbox the syn between i and j-1
+        by an encrypted blob. Initially the syn have to be in clear
+        raise an error if it is not the case"""
+        self.load_synbox()
+        self.syn[i:j]=[self.blobify(syn[i:j])]
+
+    def sendList(self,num,after):
+        """returns a list of (bf, synblob)
+        in the process of creating it ca modify the synbox,
+        doing some encryption"""
+        def hasMail(midIdx, l):
+            return self.mbox.has_key(l[midIdx])
+        def addBlob(slist,mlist, ilist, blob):
+            """append to slist (BF,blob)
+            BF is the bitfield obtained from the elements e of ilist
+            which verifies : the mid mlist[e] has a mail"""
+            def test(midx):
+                hasMail(midx,mlist)
+            slist.append(Mail.bf(filter(test,ilist)),sy)
+        #the list of the mid after "after"
+        midList=nymUser.midAfter(after)
+        #load the structures
+        nymUser.load_index()
+        nymUser.load_synbox()
+        nymUser.load_mbox()
+        #we just need the num first
+        midList=midList[:num]
+                    
+        #create a list of blobs containing the mid in midlist
+        clearListEmpty=True #is there any non encrypted synopses between bInf and the current i, or bSup that need to be sent?  
+        sendList=[] #result list
+        
+        try:
+            i, _ = self.getSyn(midList[0])
+        except:
+            #this should not happen
+            raise 'Bug'
+        #as long as midList isn't empty
+        while(midList):
+            #look for the synblob containing midList[0]
+            (ml, status, synblob) = self.syn[i]
+            if(status == 'clear'):
+                if(clearListEmpty):
+                    bInf=i
+                    clearListEmpty = False
+                if( i + 1 - bInf == 16):
+                    self.encryptSyn(bInf,i+1)
+                    clearListEmpty = True
+                    (m,_,sy) = self.[bInf]
+                    addBlob(sendList,m,range(len(m)),sy)
+                else:
+                    bSup=i+1
+                del(midList[0])
+            elif(status == 'encrypted'):
+                if(not clearListEmpty):
+                    self.encryptSyn(bInf,bSup)
+                    clearListEmpty = True
+                    (m,_,sy) = self.syn[bInf]
+                    addBlob(sendList,m,range(len(m)),sy)
+                #idxList=[ml.index(midList[0])]
+                def test(midx):
+                    return (self.timecmp(num,midx)<0)
+                #idxList contains the mid in ml older than num
+                idxList=filter(test,ml)
+                #remove from the midList the mid from idxList
+                for j in idxList:
+                    try:
+                        midList.remove(j)
+                    except ValueError:
+                        pass
+                tlist = []
+                for j in idxList:
+                    tlist.append(ml.index(j))
+                addBlob(sendList,ml,tList,synblob)
+            else:
+                #this should not happen
+                raise 'Bug'
+            i=i+1
+        #if clearList isn't empty
+        if(not clearListEmpty):
+            self.encryptSyn(bInf,bSup) 
+            (m,_,sy) = nymUser.syn[bInf]
+            addBlob(sendList,m,range(len(m)),sy)
+        return sendList
+    
+
+
 if __name__ == '__main__':
     a = User('laurent')
     ec = a.relay("NYM3 TEST : " + repr(time.time()))