Changeset 299

Show
Ignore:
Timestamp:
05/01/09 21:38:45 (3 years ago)
Author:
ocjhb
Message:

even though the mobile asks a ton of times for the right connection - the first opencoin mobile to mobile transfer ever

Location:
trunk/sandbox/jhb
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/mobile/ocwallet.py

    r297 r299  
    1212        self.makeWalletMenu() 
    1313        self.displayWalletMenu()         
    14         self.actions=[(u'Send',u'Send coins to someone',self.getDetails), 
    15                       (u'Receive',u'Receive coins',self.getReceiveDetails), 
     14        self.actions=[(u'Send',u'Send coins to someone',self.spendCoins), 
     15                      (u'Receive',u'Receive coins',self.receiveCoins), 
    1616                      (u'Freshen up',u'Freshen up the coins',self.freshenUp), 
    17                       (u'Buy',u'Buy new coins',self.mintCoins), 
    18                       (u'Sell',u'Sell coins',self.redeemCoins), 
     17                      (u'Mint',u'new coins from issuer',self.mintCoins), 
     18                      (u'Redeem',u'redeem from issuer',self.redeemCoins), 
    1919                      (u'Details',u'See what coins you hold',self.inspectCurrency),] 
    2020 
    21         self.methods=[(u'Internet',u'Use the internet'), 
    22                       (u'Bluetooth',u'Mobile to mobile')] 
    2321         
    2422        self.todo = {} 
     
    9492        self.execute() 
    9593 
    96     def getReceiveDetails(self): 
    97         method = self.getMethod() 
    98         if method ==1: 
    99             appuifw.note(u'we are reachable at:','conf') 
    100         self.execute() 
    101  
    102  
    103  
    104     def getMethod(self): 
    105         methodlist = [u'mobile to mobile',u'internet'] 
    106         method = appuifw.popup_menu(methodlist) 
    107         self.todo['method'] = method 
    108         return method 
    109                                
     94 
    11095    def inspectCurrency(self): 
    11196        #print 'inspect' 
     
    115100        coinlist = [] 
    116101        for coin in coins: 
    117             coinlist.append(u'coin: %s' % coin.denomination) 
     102            coinlist.append(u'%s %s' % (coin.denomination,cdd.currencyId)) 
    118103        self.currency_menu = appuifw.Listbox(coinlist,self.inspectCoin) 
    119104        self.currency_menu.bind(EKeyRightArrow,self.inspectCoin) 
     
    128113        coin = coins[self.currency_menu.current()] 
    129114        details = [] 
    130         details.append((unicode(coin.standardId),u'Standard Id')) 
    131         details.append((unicode(coin.currencyId),u'Currency Id')) 
    132         details.append((unicode(coin.denomination),u'Denomination')) 
     115        details.append((u'Standard Id',unicode(coin.standardId))) 
     116        details.append((u'Currency Id',unicode(coin.currencyId))) 
     117        details.append((u'Denomination',unicode(coin.denomination))) 
    133118        self.coin_menu = appuifw.Listbox(details,self.inspectCurrency) 
    134119        self.coin_menu.bind(EKeyLeftArrow,self.inspectCurrency) 
     
    210195        transport = transports.HTTPTransport(url) 
    211196        return transport 
     197     
     198    def receiveCoins(self): 
     199        methodlist = [u'internet',u'bluetooth'] 
     200        method = appuifw.popup_menu(methodlist) 
     201        if method ==1: 
     202            appuifw.note(u'bt not implemented yet','conf') 
     203        else: 
     204            cdd,alreadythere = self.getCurrentCurrency() 
     205            transport = transports.HTTPTransport(cdd.issuerServiceLocation) 
     206            self.receiveCoinsHTTP(transport) 
     207         
     208        self.makeWalletMenu() 
     209        self.displayWalletMenu() 
     210 
     211 
     212 
     213    def receiveCoinsHTTP(self,transport): 
     214        import BaseHTTPServer, urllib 
     215         
     216        class OCHandler(BaseHTTPServer.BaseHTTPRequestHandler): 
     217 
     218            def do_POST(self): 
     219                #print self.server 
     220                if self.path == '/stop': 
     221                    raise 'foobar' 
     222                length = self.headers.get('Content-Length') 
     223                data = self.rfile.read(int(length)) 
     224                data = urllib.unquote(data) 
     225                message = transports.createMessage(data) 
     226                if message.header == 'SumAnnounce': 
     227                    answer = self.wallet.listenSum(message) 
     228                if message.header == 'SpendRequest': 
     229                    answer = self.wallet.listenSpend(message,transport) 
     230                self.send_response(200) 
     231                self.send_header("Content-type", "text/plain") 
     232                self.wfile.write('\r\n') 
     233                self.wfile.write(answer.toString(True)) 
     234                 
     235        port = int(appuifw.query(u'port','number','9091')) 
     236        OCHandler.wallet = self.wallet 
     237        OCHandler.wallet.getApproval = self.getApproval 
     238        self.startInternet() 
     239         
     240        #hack to open internet 
     241        #r = urllib.urlopen('http://google.com') 
     242         
     243        httpd = BaseHTTPServer.HTTPServer(("",port),OCHandler) 
     244        appuifw.note(u'waiting at %s:%s' % ('localhost',port),'conf') 
     245        httpd.handle_request() 
     246        httpd.handle_request() 
     247        self.stopInternet() 
     248 
     249    def spendCoins(self): 
     250 
     251        amount = self.getAmount() 
     252        if not amount: 
     253            return 
     254         
     255        target = self.getTarget() 
     256        if not target: 
     257            return 
     258        cdd,alreadythere = self.getCurrentCurrency() 
     259        url = appuifw.query(u'url','text',u'http://192.168.2.105:9091') 
     260        transport = transports.HTTPTransport(url) 
     261 
     262        self.wallet.spendCoins(transport,cdd.currencyId,amount,target) 
     263        self.makeWalletMenu() 
     264        self.displayWalletMenu() 
     265 
     266 
     267 
     268 
     269    def getApproval(self,message): 
     270        amount = message.amount 
     271        target = message.target 
     272        return appuifw.query(u'Accept %s (ref "%s")?' % (amount,target),'query')  
     273 
     274 
     275     
     276 
     277 
     278 
    212279 
    213280    def startInternet(self): 
     
    217284                #sys.modules['socket'] = __import__('btsocket') 
    218285                import btsocket as socket 
    219                 #apid = socket.select_access_point() 
    220                 #apo = socket.access_point(apid) 
    221                 #socket.set_default_access_point(apo) 
    222                 #apo.start() 
    223                 #self.apo = apo 
    224                 #self.ip = apo.ip() 
     286                apid = socket.select_access_point() 
     287                apo = socket.access_point(apid) 
     288                socket.set_default_access_point(apo) 
     289                apo.start() 
     290                self.apo = apo 
     291                self.ip = apo.ip() 
    225292 
    226293            except ImportError: 
    227294                import socket 
     295                self.ip = '127.0.0.2' 
    228296             
    229297 
  • trunk/sandbox/jhb/oc2/documentation.py

    r279 r299  
    3939>>> from wallet import Wallet 
    4040>>> wallet = Wallet({}) 
    41 >>> import protocols 
    4241>>> cdd == wallet.askLatestCDD(issuer.giveLatestCDD) 
    4342True 
  • trunk/sandbox/jhb/oc2/wallet.py

    r298 r299  
    3838        target = message.target 
    3939        approval = getattr(self,'approval',True) #get that from ui 
    40         if approval == True: 
    41             self.addIncoming(message) 
    4240        return approval 
    4341         
     
    107105        if approval == True: 
    108106            answer = messages.SumAccept() 
     107            self.addIncoming(message) 
    109108        else: 
    110109            answer = messages.SumReject() 
     
    123122            return True 
    124123 
    125     def listenSpend(self,message): 
     124 
     125    def listenSpend(self,message,transport=None): 
    126126        tid = message.transactionId 
    127127        amount = sum([int(m.denomination) for m in message.coins]) 
     
    138138            return answer 
    139139        #do exchange 
    140  
     140        if transport: 
     141            cdd = self.askLatestCDD(transport) 
     142            currency = self.getCurrency(cdd.currencyId) 
     143            newcoins = message.coins  
     144            self.freshenUp(transport,cdd,newcoins) 
    141145 
    142146        answer = messages.SpendAccept() 
     
    159163        out = [] 
    160164        for key,currency in self.storage.items(): 
    161             cdd = currency['cdds'][-1] 
    162             amount = sum([int(coin.denomination) for coin in currency['coins']]) 
    163             out.append((cdd,amount)) 
     165            try: 
     166                cdd = currency['cdds'][-1] 
     167                amount = sum([int(coin.denomination) for coin in currency['coins']]) 
     168                out.append((cdd,amount)) 
     169            except: 
     170                del(self.storage[key]) 
    164171        return out             
    165172 
     
    260267 
    261268 
    262     def freshenUp(self,transport,cdd):         
     269    def freshenUp(self,transport,cdd,newcoins=[]):         
    263270        currency = self.getCurrency(cdd.currencyId) 
    264         paycoins,secrets,data = self.prepare4exchange(transport,cdd,currency['coins'],[]) 
     271        paycoins,secrets,data = self.prepare4exchange(transport,cdd,currency['coins'],newcoins) 
    265272        if secrets: 
    266273            tid = self.makeSerial() 
    267             response = self.requestTransfer(transport,tid,None,data,paycoins) 
     274            response = self.requestTransfer(transport,tid,None,data,paycoins+newcoins) 
    268275            coins = currency['coins'] 
    269276            for coin in paycoins: 
     
    293300        else: 
    294301            return [],[],[] 
     302 
     303 
     304    def spendCoins(self,transport,currencyId,amount,target): 
     305        currency = self.getCurrency(currencyId) 
     306        coins = currency['coins'] 
     307        picked = self.pickForSpending(amount,coins) 
     308        tid = self.makeSerial() 
     309         
     310        self.announceSum(transport,tid,amount,target) 
     311 
     312        response = self.requestSpend(transport,tid,picked) 
     313        if response == True:  
     314            newcoins = [c for c in coins if c not in picked] 
     315            currency['coins'] = newcoins         
     316            self.storage.save() 
     317 
     318