Changeset 269 for trunk

Show
Ignore:
Timestamp:
04/21/09 18:16:11 (3 years ago)
Author:
ocjhb
Message:

SumAnnounce?

Location:
trunk/sandbox/jhb/oc2
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/oc2/containerbase.py

    r259 r269  
    9797    fields = [] 
    9898 
    99     def __init__(self,data={}): 
     99    def __init__(self,data={},**kwargs): 
     100        if not data and kwargs: 
     101            data = kwargs 
    100102        self.fromData(data) 
    101103 
     
    116118        if type(data) != type({}): 
    117119            data = dict(data) 
     120 
    118121        for field in self.fields: 
    119122            if data.has_key(field.name): 
  • trunk/sandbox/jhb/oc2/documentation.py

    r266 r269  
    603603        or  SUM_REJECT( transaction_id, "Reason" ) 
    604604 
     605############################################################################### 
     606 
     607>>> bobport = 9091 
     608>>> bobwallet = Wallet({}) 
     609 
     610>>> aclicetid = wallet.makeSerial() 
     611>>> bob = protocols.SumAnnounceListen(bobwallet) 
     612>>> alice = protocols.SumAnnounce(bob.run, wallet, 5, 'foobar')  
     613>>> bobwallet.approval = "I don't like odd sums" 
     614>>> alice.run() 
     615"I don't like odd sums" 
     616 
     617>>> bobwallet.approval = True 
     618>>> alice.run() 
     619True 
     620 
     621############################################################################### 
    605622 
    606623* Wallet Alice sends tokens to Wallet Bob (this time including their clear  
  • trunk/sandbox/jhb/oc2/messages.py

    r266 r269  
    5151        Field('signature',signing=False) 
    5252    ] 
    53 class Error(Message): 
     53class Error(Message, Exception): 
    5454    fields = Message.fields + [ 
    5555        Field('text'), 
     
    7373    ] 
    7474 
     75class TransferResume(Message): 
     76     fields = Message.fields + [ 
     77        Field('transactionId'), 
     78    ] 
    7579 
     80class TransferResume(Message): 
     81     fields = Message.fields + [ 
     82        Field('transactionId'), 
     83    ] 
     84 
     85class SumAnnounce(Message): 
     86     fields = Message.fields + [ 
     87        Field('transactionId'), 
     88        Field('sum'), 
     89        Field('target'), 
     90    ] 
     91 
     92class SumAccept(Message): 
     93     fields = Message.fields + [ 
     94        Field('transactionId'), 
     95    ] 
     96 
     97    
     98class SumReject(Message): 
     99     fields = Message.fields + [ 
     100        Field('transactionId'), 
     101        Field('reason'), 
     102    ] 
     103 
     104 
     105 
  • trunk/sandbox/jhb/oc2/protocols.py

    r266 r269  
    178178        return answer     
    179179 
     180class SumAnnounce(Protocol): 
     181 
     182    def __init__(self,transport,wallet,sum,target): 
     183        self.transport = transport 
     184        self.sum = sum 
     185        self.target = target 
     186        self.wallet = wallet 
     187     
     188    def run(self,message=None): 
     189        tid = self.wallet.makeSerial() 
     190        message = messages.SumAnnounce() 
     191        message.transactionId = tid 
     192        message.sum = self.sum 
     193        message.target = self.target 
     194        self.wallet.addOutgoing(message) 
     195        response = self.transport(message) 
     196        if response.header == 'SumAccept': 
     197            return True 
     198        else: 
     199            return response.reason 
     200 
     201 
     202class SumAnnounceListen(Protocol): 
     203     
     204    def __init__(self,wallet): 
     205        self.wallet = wallet 
     206     
     207    def run(self,message=None): 
     208        approval = self.wallet.getApproval(message) 
     209        if approval == True: 
     210            answer = messages.SumAccept() 
     211        else: 
     212            answer = messages.SumReject() 
     213            answer.reason = approval 
     214        answer.transactionId = message.transactionId             
     215        return answer 
    180216 
    181217class CoinsSpendSender(Protocol): 
  • trunk/sandbox/jhb/oc2/wallet.py

    r259 r269  
    1717    def makeSerial(self): 
    1818        return occrypto.createSerial() 
     19     
     20    def addOutgoing(self,message): 
     21        self.storage.setdefault('outgoing',{})[message.transactionId] = message 
     22 
     23    def addIncoming(self,message): 
     24        self.storage.setdefault('incoming',{})[message.transactionId] = message 
     25 
     26    def getApproval(self,message): 
     27        sum = message.sum 
     28        target = message.sum 
     29        approval = getattr(self,'approval',True) #get that from ui 
     30        if approval == True: 
     31            self.addIncoming(message) 
     32        return approval 
     33         
     34