stratum http

This commit is contained in:
thomasv 2012-03-19 19:20:55 +01:00
parent 837e2077e3
commit 6561bab1ca
1 changed files with 32 additions and 21 deletions

View File

@ -66,10 +66,11 @@ class Interface:
def handle_json_response(self, c): def handle_json_response(self, c):
#print c #print repr(c)
msg_id = c.get('id') msg_id = c.get('id')
result = c.get('result') result = c.get('result')
error = c.get('error') error = c.get('error')
if msg_id is None: if msg_id is None:
print "error: message without ID" print "error: message without ID"
return return
@ -78,15 +79,8 @@ class Interface:
if error: if error:
print "received error:", c, method, params print "received error:", c, method, params
else: else:
if method == 'session.poll': #embedded messages self.handle_response(method, params, result)
if result: self.is_up_to_date = True
self.is_up_to_date = False
for msg in result:
self.handle_json_response(msg)
else:
self.is_up_to_date = True
else:
self.handle_response(method, params, result)
@ -132,9 +126,13 @@ class Interface:
self.tx_event.set() self.tx_event.set()
elif method == 'numblocks.subscribe': elif method == 'numblocks.subscribe':
print "numblocks", result
self.blocks = result self.blocks = result
if self.newblock_callback: apply(self.newblock_callback,(result,)) if self.newblock_callback: apply(self.newblock_callback,(result,))
elif method == 'ping':
pass
else: else:
print "received message:", method, params, result print "received message:", method, params, result
@ -182,9 +180,12 @@ class PollingInterface(Interface):
def get_history(self, address): def get_history(self, address):
self.send([('address.get_history', [address] )]) self.send([('address.get_history', [address] )])
def poll(self):
self.send([('session.poll', [])])
def update_wallet(self): def update_wallet(self):
while True: while True:
self.send([('session.poll', [])]) self.poll()
if self.is_up_to_date: break if self.is_up_to_date: break
#if is_new or wallet.remote_url: #if is_new or wallet.remote_url:
@ -280,6 +281,8 @@ class HttpInterface(PollingInterface):
self.subscribe(addresses) self.subscribe(addresses)
thread.start_new_thread(self.poll_thread, (15,)) thread.start_new_thread(self.poll_thread, (15,))
def poll(self):
self.send( [] )
def send(self, messages): def send(self, messages):
import urllib2, json, time, cookielib import urllib2, json, time, cookielib
@ -288,20 +291,23 @@ class HttpInterface(PollingInterface):
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener) urllib2.install_opener(opener)
t1 = time.time()
data = [] data = []
for m in messages: for m in messages:
method, params = m method, params = m
if type(params) != type([]): params = [params] if type(params) != type([]): params = [params]
t1 = time.time()
data.append( { 'method':method, 'id':self.message_id, 'params':params } ) data.append( { 'method':method, 'id':self.message_id, 'params':params } )
self.messages[self.message_id] = (method, params) self.messages[self.message_id] = (method, params)
self.message_id += 1 self.message_id += 1
data_json = json.dumps(data) if data:
#print data_json data_json = json.dumps(data)
#host = 'http://%s:%d'%( self.host if method!='server.peers' else self.peers_server, self.port ) else:
host = 'http://%s:%d'%( self.host, self.port ) # poll with GET
data_json = None
host = 'http://%s:%d'%( self.host, self.port )
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if self.session_id: if self.session_id:
headers['cookie'] = 'SESSION=%s'%self.session_id headers['cookie'] = 'SESSION=%s'%self.session_id
@ -313,15 +319,20 @@ class HttpInterface(PollingInterface):
if cookie.name=='SESSION': if cookie.name=='SESSION':
self.session_id = cookie.value self.session_id = cookie.value
response = json.loads( response_stream.read() ) response = response_stream.read()
if response:
print "response",response
response = json.loads( response )
if type(response) is not type([]):
self.handle_json_response(response)
else:
for item in response:
self.handle_json_response(item)
self.rtime = time.time() - t1 self.rtime = time.time() - t1
self.is_connected = True self.is_connected = True
for item in response:
self.handle_json_response(item)