2012-01-19 08:11:36 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2012-10-27 01:24:43 -07:00
|
|
|
import random, socket, ast, re, ssl
|
2012-03-31 02:47:16 -07:00
|
|
|
import threading, traceback, sys, time, json, Queue
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-05-02 05:21:58 -07:00
|
|
|
from version import ELECTRUM_VERSION
|
2012-07-07 11:53:54 -07:00
|
|
|
from util import print_error
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2012-05-02 05:21:58 -07:00
|
|
|
|
2012-03-14 10:00:24 -07:00
|
|
|
DEFAULT_TIMEOUT = 5
|
2012-10-14 05:40:07 -07:00
|
|
|
DEFAULT_SERVERS = [
|
|
|
|
'electrum.novit.ro:50001:t',
|
2012-10-18 11:29:53 -07:00
|
|
|
'electrum.pdmc.net:50001:t',
|
2012-10-20 17:57:31 -07:00
|
|
|
'ecdsa.org:50001:t',
|
2012-10-14 05:40:07 -07:00
|
|
|
'electrum.bitcoins.sk:50001:t',
|
|
|
|
'uncle-enzo.info:50001:t',
|
|
|
|
'electrum.bytesized-hosting.com:50001:t',
|
2012-10-20 17:57:31 -07:00
|
|
|
'electrum.bitcoin.cz:50001:t',
|
2012-10-14 05:40:07 -07:00
|
|
|
'electrum.bitfoo.org:50001:t'
|
|
|
|
]
|
2012-03-14 10:00:24 -07:00
|
|
|
|
2012-10-01 09:14:50 -07:00
|
|
|
proxy_modes = ['socks4', 'socks5', 'http']
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-03-30 01:48:32 -07:00
|
|
|
|
2012-10-14 05:40:07 -07:00
|
|
|
def pick_random_server():
|
2012-10-20 08:21:12 -07:00
|
|
|
return random.choice( DEFAULT_SERVERS )
|
2012-03-30 01:48:32 -07:00
|
|
|
|
2012-10-11 11:10:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
class Interface(threading.Thread):
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2012-10-22 02:34:21 -07:00
|
|
|
def register_callback(self, event, callback):
|
2012-10-20 17:57:31 -07:00
|
|
|
with self.lock:
|
2012-10-22 04:43:58 -07:00
|
|
|
if not self.callbacks.get(event):
|
|
|
|
self.callbacks[event] = []
|
|
|
|
self.callbacks[event].append(callback)
|
2012-10-20 17:57:31 -07:00
|
|
|
|
2012-10-22 02:34:21 -07:00
|
|
|
def trigger_callback(self, event):
|
2012-10-20 17:57:31 -07:00
|
|
|
with self.lock:
|
2012-10-22 04:43:58 -07:00
|
|
|
callbacks = self.callbacks.get(event,[])[:]
|
|
|
|
if callbacks:
|
|
|
|
[callback() for callback in callbacks]
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
def init_server(self, host, port, proxy=None, use_ssl=True):
|
2012-03-14 10:21:27 -07:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2012-09-19 03:37:20 -07:00
|
|
|
self.proxy = proxy
|
2012-10-17 02:35:24 -07:00
|
|
|
self.use_ssl = use_ssl
|
2012-03-30 05:15:05 -07:00
|
|
|
self.poll_interval = 1
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
#json
|
|
|
|
self.message_id = 0
|
2012-05-04 00:28:44 -07:00
|
|
|
self.unanswered_requests = {}
|
2012-03-23 06:37:02 -07:00
|
|
|
|
2012-05-16 10:45:45 -07:00
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-23 06:37:02 -07:00
|
|
|
def queue_json_response(self, c):
|
2012-04-29 09:33:01 -07:00
|
|
|
|
2012-10-01 08:41:47 -07:00
|
|
|
# uncomment to debug
|
2012-11-04 11:53:27 -08:00
|
|
|
print_error( "<--",c )
|
2012-07-06 13:36:13 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
msg_id = c.get('id')
|
|
|
|
error = c.get('error')
|
2012-03-30 01:48:32 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
if error:
|
2012-10-26 09:25:43 -07:00
|
|
|
print_error("received error:", c)
|
2012-04-29 09:33:01 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
if msg_id is not None:
|
2012-10-20 17:57:31 -07:00
|
|
|
with self.lock:
|
|
|
|
method, params, channel = self.unanswered_requests.pop(msg_id)
|
2012-04-29 09:33:01 -07:00
|
|
|
result = c.get('result')
|
2012-10-27 05:15:03 -07:00
|
|
|
|
|
|
|
if method == 'server.version':
|
|
|
|
self.server_version = result
|
|
|
|
|
|
|
|
elif method == 'server.peers.subscribe':
|
|
|
|
servers = []
|
|
|
|
for item in result:
|
|
|
|
s = []
|
|
|
|
host = item[1]
|
|
|
|
ports = []
|
|
|
|
version = None
|
|
|
|
if len(item) > 2:
|
|
|
|
for v in item[2]:
|
|
|
|
if re.match("[stgh]\d+", v):
|
|
|
|
ports.append((v[0], v[1:]))
|
|
|
|
if re.match("v(.?)+", v):
|
|
|
|
version = v[1:]
|
2012-11-04 02:08:05 -08:00
|
|
|
try:
|
|
|
|
is_recent = float(version)>=0.5
|
|
|
|
except:
|
|
|
|
is_recent = False
|
|
|
|
if ports and is_recent:
|
2012-10-27 05:15:03 -07:00
|
|
|
servers.append((host, ports))
|
|
|
|
self.servers = servers
|
|
|
|
self.trigger_callback('peers')
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
else:
|
2012-10-24 12:45:45 -07:00
|
|
|
# notification: find the channel(s)
|
2012-04-29 09:33:01 -07:00
|
|
|
method = c.get('method')
|
|
|
|
params = c.get('params')
|
|
|
|
|
|
|
|
if method == 'blockchain.numblocks.subscribe':
|
|
|
|
result = params[0]
|
|
|
|
params = []
|
|
|
|
|
2012-10-25 06:40:30 -07:00
|
|
|
elif method == 'blockchain.headers.subscribe':
|
|
|
|
result = params[0]
|
|
|
|
params = []
|
|
|
|
|
2012-04-29 09:33:01 -07:00
|
|
|
elif method == 'blockchain.address.subscribe':
|
|
|
|
addr = params[0]
|
|
|
|
result = params[1]
|
|
|
|
params = [addr]
|
|
|
|
|
2012-10-22 22:45:56 -07:00
|
|
|
with self.lock:
|
|
|
|
for k,v in self.subscriptions.items():
|
|
|
|
if (method, params) in v:
|
|
|
|
channel = k
|
2012-10-22 23:15:53 -07:00
|
|
|
break
|
2012-10-22 22:45:56 -07:00
|
|
|
else:
|
2012-10-26 09:25:43 -07:00
|
|
|
print_error( "received unexpected notification", method, params)
|
|
|
|
print_error( self.subscriptions )
|
2012-10-22 22:45:56 -07:00
|
|
|
return
|
2012-10-22 02:34:21 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
response_queue = self.responses[channel]
|
|
|
|
response_queue.put({'method':method, 'params':params, 'result':result, 'id':msg_id})
|
2012-03-23 06:37:02 -07:00
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def get_response(self, channel='default', block=True, timeout=10000000000):
|
|
|
|
return self.responses[channel].get(block, timeout)
|
2012-03-19 07:57:47 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def register_channel(self, channel):
|
|
|
|
with self.lock:
|
|
|
|
self.responses[channel] = Queue.Queue()
|
2012-03-19 07:57:47 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def poke(self, channel):
|
|
|
|
self.responses[channel].put(None)
|
2012-10-11 16:13:54 -07:00
|
|
|
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def init_http(self, host, port, proxy=None, use_ssl=True):
|
|
|
|
self.init_server(host, port, proxy, use_ssl)
|
2012-03-31 02:47:16 -07:00
|
|
|
self.session_id = None
|
2012-10-19 01:30:51 -07:00
|
|
|
self.connection_msg = ('https' if self.use_ssl else 'http') + '://%s:%d'%( self.host, self.port )
|
2012-10-22 08:18:07 -07:00
|
|
|
self.is_connected = True
|
2012-03-17 03:56:31 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def run_http(self):
|
2012-03-31 02:47:16 -07:00
|
|
|
self.is_connected = True
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
2012-02-08 04:37:14 -08:00
|
|
|
try:
|
2012-03-31 02:47:16 -07:00
|
|
|
if self.session_id:
|
|
|
|
self.poll()
|
2012-03-30 05:15:05 -07:00
|
|
|
time.sleep(self.poll_interval)
|
2012-03-14 07:35:39 -07:00
|
|
|
except socket.gaierror:
|
|
|
|
break
|
|
|
|
except socket.error:
|
|
|
|
break
|
2012-02-08 04:37:14 -08:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
self.is_connected = False
|
2012-02-08 04:37:14 -08:00
|
|
|
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
2012-03-31 02:47:16 -07:00
|
|
|
self.send([])
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
def send_http(self, messages, channel='default'):
|
2012-03-19 02:19:42 -07:00
|
|
|
import urllib2, json, time, cookielib
|
2012-09-19 03:37:20 -07:00
|
|
|
|
2012-10-01 09:14:50 -07:00
|
|
|
if self.proxy:
|
2012-09-19 03:37:20 -07:00
|
|
|
import socks
|
2012-10-28 13:59:51 -07:00
|
|
|
socks.setdefaultproxy(proxy_modes.index(self.proxy["mode"]) + 1, self.proxy["host"], int(self.proxy["port"]) )
|
2012-09-19 03:37:20 -07:00
|
|
|
socks.wrapmodule(urllib2)
|
2012-10-09 08:13:26 -07:00
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
cj = cookielib.CookieJar()
|
|
|
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
|
|
|
urllib2.install_opener(opener)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
t1 = time.time()
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
data = []
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
if type(params) != type([]): params = [params]
|
|
|
|
data.append( { 'method':method, 'id':self.message_id, 'params':params } )
|
2012-10-20 17:57:31 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params, channel
|
2012-03-17 15:32:36 -07:00
|
|
|
self.message_id += 1
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
if data:
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
else:
|
|
|
|
# poll with GET
|
|
|
|
data_json = None
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-10-17 02:35:24 -07:00
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
if self.session_id:
|
|
|
|
headers['cookie'] = 'SESSION=%s'%self.session_id
|
|
|
|
|
2012-10-19 01:30:51 -07:00
|
|
|
req = urllib2.Request(self.connection_msg, data_json, headers)
|
2012-03-14 08:31:31 -07:00
|
|
|
response_stream = urllib2.urlopen(req)
|
2012-03-19 02:19:42 -07:00
|
|
|
|
|
|
|
for index, cookie in enumerate(cj):
|
|
|
|
if cookie.name=='SESSION':
|
|
|
|
self.session_id = cookie.value
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
response = response_stream.read()
|
2012-05-17 04:15:55 -07:00
|
|
|
self.bytes_received += len(response)
|
2012-03-19 11:20:55 -07:00
|
|
|
if response:
|
|
|
|
response = json.loads( response )
|
|
|
|
if type(response) is not type([]):
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(response)
|
2012-03-19 11:20:55 -07:00
|
|
|
else:
|
|
|
|
for item in response:
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(item)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-30 05:15:05 -07:00
|
|
|
if response:
|
|
|
|
self.poll_interval = 1
|
|
|
|
else:
|
|
|
|
if self.poll_interval < 15:
|
|
|
|
self.poll_interval += 1
|
|
|
|
#print self.poll_interval, response
|
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def init_tcp(self, host, port, proxy=None, use_ssl=True):
|
|
|
|
self.init_server(host, port, proxy, use_ssl)
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-09-19 03:37:20 -07:00
|
|
|
global proxy_modes
|
2012-10-14 05:40:07 -07:00
|
|
|
self.connection_msg = "%s:%d"%(self.host,self.port)
|
2012-10-01 09:14:50 -07:00
|
|
|
if self.proxy is None:
|
2012-10-17 02:35:24 -07:00
|
|
|
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2012-09-19 03:37:20 -07:00
|
|
|
else:
|
2012-10-14 05:40:07 -07:00
|
|
|
self.connection_msg += " using proxy %s:%s:%s"%(self.proxy.get('mode'), self.proxy.get('host'), self.proxy.get('port'))
|
2012-09-19 03:37:20 -07:00
|
|
|
import socks
|
2012-10-17 02:35:24 -07:00
|
|
|
s = socks.socksocket()
|
2012-10-28 13:59:51 -07:00
|
|
|
s.setproxy(proxy_modes.index(self.proxy["mode"]) + 1, self.proxy["host"], int(self.proxy["port"]) )
|
2012-10-17 02:35:24 -07:00
|
|
|
|
|
|
|
if self.use_ssl:
|
2012-10-17 03:54:32 -07:00
|
|
|
s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv23)
|
2012-10-17 02:35:24 -07:00
|
|
|
|
|
|
|
s.settimeout(2)
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
try:
|
2012-10-17 02:35:24 -07:00
|
|
|
s.connect(( self.host.encode('ascii'), int(self.port)))
|
|
|
|
s.settimeout(60)
|
|
|
|
self.s = s
|
2012-03-13 08:43:56 -07:00
|
|
|
self.is_connected = True
|
2012-03-31 02:47:16 -07:00
|
|
|
except:
|
|
|
|
self.is_connected = False
|
2012-10-17 02:35:24 -07:00
|
|
|
self.s = None
|
2012-03-31 02:47:16 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
def run_tcp(self):
|
2012-03-31 02:47:16 -07:00
|
|
|
try:
|
2012-03-13 08:43:56 -07:00
|
|
|
out = ''
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
2012-10-27 03:31:43 -07:00
|
|
|
|
2012-10-27 01:24:43 -07:00
|
|
|
try:
|
2012-10-27 03:31:43 -07:00
|
|
|
timeout = False
|
2012-10-27 01:24:43 -07:00
|
|
|
msg = self.s.recv(1024)
|
2012-05-02 07:04:00 -07:00
|
|
|
except socket.timeout:
|
2012-10-27 03:31:43 -07:00
|
|
|
timeout = True
|
2012-10-27 01:24:43 -07:00
|
|
|
except ssl.SSLError:
|
2012-10-27 03:31:43 -07:00
|
|
|
timeout = True
|
|
|
|
|
|
|
|
if timeout:
|
|
|
|
# ping the server with server.version, as a real ping does not exist yet
|
2012-10-27 01:24:43 -07:00
|
|
|
self.send([('server.version', [ELECTRUM_VERSION])])
|
|
|
|
continue
|
2012-10-27 03:31:43 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
out += msg
|
2012-05-17 04:15:55 -07:00
|
|
|
self.bytes_received += len(msg)
|
2012-03-13 09:31:29 -07:00
|
|
|
if msg == '':
|
|
|
|
self.is_connected = False
|
2012-03-14 07:35:39 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
while True:
|
|
|
|
s = out.find('\n')
|
|
|
|
if s==-1: break
|
|
|
|
c = out[0:s]
|
|
|
|
out = out[s+1:]
|
|
|
|
c = json.loads(c)
|
2012-03-23 06:37:02 -07:00
|
|
|
self.queue_json_response(c)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
|
|
|
|
self.is_connected = False
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
def send_tcp(self, messages, channel='default'):
|
2012-10-19 05:16:02 -07:00
|
|
|
"""return the ids of the requests that we sent"""
|
2012-03-17 08:02:28 -07:00
|
|
|
out = ''
|
2012-10-19 05:16:02 -07:00
|
|
|
ids = []
|
2012-03-17 08:02:28 -07:00
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
request = json.dumps( { 'id':self.message_id, 'method':method, 'params':params } )
|
2012-10-20 17:57:31 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params, channel
|
2012-10-19 05:16:02 -07:00
|
|
|
ids.append(self.message_id)
|
2012-10-01 08:41:47 -07:00
|
|
|
# uncomment to debug
|
|
|
|
# print "-->",request
|
2012-03-17 08:02:28 -07:00
|
|
|
self.message_id += 1
|
|
|
|
out += request + '\n'
|
2012-05-19 07:52:59 -07:00
|
|
|
while out:
|
2012-10-22 05:56:59 -07:00
|
|
|
try:
|
|
|
|
sent = self.s.send( out )
|
|
|
|
out = out[sent:]
|
|
|
|
except:
|
|
|
|
# this happens when we get disconnected
|
2012-10-26 09:25:43 -07:00
|
|
|
print_error( "Not connected, cannot send" )
|
2012-10-22 05:56:59 -07:00
|
|
|
return None
|
2012-10-19 05:16:02 -07:00
|
|
|
return ids
|
2012-03-17 08:02:28 -07:00
|
|
|
|
2012-03-16 14:18:58 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-10-22 02:34:21 -07:00
|
|
|
def __init__(self, config=None, loop=False):
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-10-12 07:31:05 -07:00
|
|
|
if config is None:
|
|
|
|
from simple_config import SimpleConfig
|
|
|
|
config = SimpleConfig()
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.loop = loop
|
|
|
|
self.config = config
|
2012-10-22 03:58:57 -07:00
|
|
|
self.connect_event = threading.Event()
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
self.subscriptions = {}
|
|
|
|
self.responses = {}
|
|
|
|
self.responses['default'] = Queue.Queue()
|
|
|
|
|
2012-10-22 02:34:21 -07:00
|
|
|
self.callbacks = {}
|
2012-10-20 17:57:31 -07:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
2012-10-22 08:18:07 -07:00
|
|
|
self.servers = [] # actual list from IRC
|
|
|
|
self.rtime = 0
|
|
|
|
self.bytes_received = 0
|
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
def init_interface(self):
|
|
|
|
if self.config.get('server'):
|
|
|
|
self.init_with_server(self.config)
|
|
|
|
else:
|
|
|
|
print "Using random server..."
|
2012-10-22 09:50:17 -07:00
|
|
|
servers = DEFAULT_SERVERS[:]
|
2012-10-20 17:57:31 -07:00
|
|
|
while servers:
|
|
|
|
server = random.choice( servers )
|
|
|
|
servers.remove(server)
|
|
|
|
self.config.set_key('server', server, False)
|
|
|
|
self.init_with_server(self.config)
|
|
|
|
if self.is_connected: break
|
|
|
|
|
|
|
|
if not servers:
|
2012-10-22 08:18:07 -07:00
|
|
|
print 'no server available'
|
|
|
|
self.is_connected = False
|
|
|
|
self.connect_event.set() # to finish start
|
|
|
|
self.server = 'ecdsa.org:50001:t'
|
|
|
|
self.proxy = None
|
|
|
|
return
|
2012-10-20 17:57:31 -07:00
|
|
|
|
2012-10-22 03:58:57 -07:00
|
|
|
self.connect_event.set()
|
2012-10-20 17:57:31 -07:00
|
|
|
if self.is_connected:
|
2012-11-04 13:45:50 -08:00
|
|
|
self.send([('server.version', [ELECTRUM_VERSION, '0.5'])])
|
2012-10-22 02:34:21 -07:00
|
|
|
self.trigger_callback('connected')
|
2012-10-20 17:57:31 -07:00
|
|
|
else:
|
2012-10-22 02:34:21 -07:00
|
|
|
self.trigger_callback('notconnected')
|
|
|
|
#print_error("Failed to connect " + self.connection_msg)
|
2012-10-20 17:57:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
def init_with_server(self, config):
|
2012-10-12 07:31:05 -07:00
|
|
|
|
2012-10-14 05:40:07 -07:00
|
|
|
s = config.get('server')
|
|
|
|
host, port, protocol = s.split(':')
|
|
|
|
port = int(port)
|
2012-05-16 08:29:05 -07:00
|
|
|
|
2012-10-11 16:13:54 -07:00
|
|
|
self.protocol = protocol
|
2012-10-12 02:56:41 -07:00
|
|
|
proxy = self.parse_proxy_options(config.get('proxy'))
|
2012-10-11 11:10:12 -07:00
|
|
|
self.server = host + ':%d:%s'%(port, protocol)
|
|
|
|
|
2012-05-16 08:29:05 -07:00
|
|
|
#print protocol, host, port
|
2012-10-17 02:35:24 -07:00
|
|
|
if protocol in 'st':
|
2012-10-20 17:57:31 -07:00
|
|
|
self.init_tcp(host, port, proxy, use_ssl=(protocol=='s'))
|
2012-10-17 02:35:24 -07:00
|
|
|
elif protocol in 'gh':
|
2012-10-20 17:57:31 -07:00
|
|
|
self.init_http(host, port, proxy, use_ssl=(protocol=='g'))
|
2012-05-16 08:29:05 -07:00
|
|
|
else:
|
2012-10-17 02:35:24 -07:00
|
|
|
raise BaseException('Unknown protocol: %s'%protocol)
|
2012-10-11 11:10:12 -07:00
|
|
|
|
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def send(self, messages, channel='default'):
|
|
|
|
|
|
|
|
sub = []
|
|
|
|
for message in messages:
|
|
|
|
m, v = message
|
|
|
|
if m[-10:] == '.subscribe':
|
|
|
|
sub.append(message)
|
|
|
|
|
|
|
|
if sub:
|
|
|
|
with self.lock:
|
|
|
|
if self.subscriptions.get(channel) is None:
|
|
|
|
self.subscriptions[channel] = []
|
2012-10-28 02:22:12 -07:00
|
|
|
for message in sub:
|
|
|
|
if message not in self.subscriptions[channel]:
|
|
|
|
self.subscriptions[channel].append(message)
|
2012-10-11 16:13:54 -07:00
|
|
|
|
2012-10-17 02:35:24 -07:00
|
|
|
if self.protocol in 'st':
|
2012-10-20 17:57:31 -07:00
|
|
|
with self.lock:
|
|
|
|
out = self.send_tcp(messages, channel)
|
2012-10-11 16:13:54 -07:00
|
|
|
else:
|
2012-10-20 17:57:31 -07:00
|
|
|
# do not use lock, http is synchronous
|
|
|
|
out = self.send_http(messages, channel)
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
def resend_subscriptions(self):
|
|
|
|
for channel, messages in self.subscriptions.items():
|
|
|
|
if messages:
|
|
|
|
self.send(messages, channel)
|
|
|
|
|
2012-10-11 16:13:54 -07:00
|
|
|
|
|
|
|
|
2012-10-11 12:37:02 -07:00
|
|
|
def parse_proxy_options(self, s):
|
2012-10-12 02:56:41 -07:00
|
|
|
if type(s) == type({}): return s # fixme: type should be fixed
|
2012-10-11 12:37:02 -07:00
|
|
|
if type(s) != type(""): return None
|
|
|
|
if s.lower() == 'none': return None
|
|
|
|
proxy = { "mode":"socks5", "host":"localhost" }
|
|
|
|
args = s.split(':')
|
|
|
|
n = 0
|
|
|
|
if proxy_modes.count(args[n]) == 1:
|
|
|
|
proxy["mode"] = args[n]
|
|
|
|
n += 1
|
|
|
|
if len(args) > n:
|
|
|
|
proxy["host"] = args[n]
|
|
|
|
n += 1
|
|
|
|
if len(args) > n:
|
|
|
|
proxy["port"] = args[n]
|
|
|
|
else:
|
|
|
|
proxy["port"] = "8080" if proxy["mode"] == "http" else "1080"
|
|
|
|
return proxy
|
|
|
|
|
|
|
|
|
2012-10-11 11:10:12 -07:00
|
|
|
def set_server(self, server, proxy=None):
|
|
|
|
# raise an error if the format isnt correct
|
|
|
|
a,b,c = server.split(':')
|
|
|
|
b = int(b)
|
2012-10-18 11:29:53 -07:00
|
|
|
assert c in 'stgh'
|
2012-10-11 11:10:12 -07:00
|
|
|
# set the server
|
|
|
|
if server != self.server or proxy != self.proxy:
|
|
|
|
print "changing server:", server, proxy
|
|
|
|
self.server = server
|
|
|
|
self.proxy = proxy
|
2012-10-27 07:46:42 -07:00
|
|
|
if self.protocol in 'st' and self.s:
|
2012-10-20 17:57:31 -07:00
|
|
|
self.s.shutdown(socket.SHUT_RDWR)
|
|
|
|
self.s.close()
|
2012-10-11 11:10:12 -07:00
|
|
|
self.is_connected = False # this exits the polling loop
|
2012-10-22 06:15:52 -07:00
|
|
|
self.trigger_callback('disconnecting') # for actively disconnecting
|
2012-10-19 05:16:02 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def is_empty(self, channel):
|
|
|
|
q = self.responses.get(channel)
|
|
|
|
if q:
|
|
|
|
return q.empty()
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def get_pending_requests(self, channel):
|
|
|
|
result = []
|
|
|
|
with self.lock:
|
|
|
|
for k, v in self.unanswered_requests.items():
|
|
|
|
a, b, c = v
|
|
|
|
if c == channel: result.append(k)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def is_up_to_date(self, channel):
|
|
|
|
return self.is_empty(channel) and not self.get_pending_requests(channel)
|
2012-10-14 22:43:00 -07:00
|
|
|
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2012-10-19 05:16:02 -07:00
|
|
|
def synchronous_get(self, requests, timeout=100000000):
|
|
|
|
# todo: use generators, unanswered_requests should be a list of arrays...
|
|
|
|
ids = self.send(requests)
|
|
|
|
id2 = ids[:]
|
|
|
|
res = {}
|
|
|
|
while ids:
|
2012-10-20 17:57:31 -07:00
|
|
|
r = self.responses['default'].get(True, timeout)
|
2012-10-19 05:16:02 -07:00
|
|
|
_id = r.get('id')
|
|
|
|
if _id in ids:
|
|
|
|
ids.remove(_id)
|
|
|
|
res[_id] = r.get('result')
|
|
|
|
out = []
|
|
|
|
for _id in id2:
|
|
|
|
out.append(res[_id])
|
|
|
|
return out
|
2012-10-11 11:10:12 -07:00
|
|
|
|
|
|
|
|
2012-10-22 03:58:57 -07:00
|
|
|
def start(self):
|
|
|
|
threading.Thread.start(self)
|
|
|
|
# wait until connection is established
|
|
|
|
self.connect_event.wait()
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def run(self):
|
|
|
|
while True:
|
2012-10-22 03:58:57 -07:00
|
|
|
self.init_interface()
|
2012-10-22 08:18:07 -07:00
|
|
|
if self.is_connected:
|
|
|
|
self.resend_subscriptions()
|
|
|
|
self.run_tcp() if self.protocol in 'st' else self.run_http()
|
2012-10-22 03:58:57 -07:00
|
|
|
|
2012-10-22 02:34:21 -07:00
|
|
|
self.trigger_callback('disconnected')
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2012-10-22 08:18:07 -07:00
|
|
|
if not self.loop: break
|
2012-10-20 17:57:31 -07:00
|
|
|
time.sleep(5)
|
2012-03-31 02:47:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|