aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hashserv/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/hashserv/client.py')
-rw-r--r--lib/hashserv/client.py43
1 files changed, 35 insertions, 8 deletions
diff --git a/lib/hashserv/client.py b/lib/hashserv/client.py
index 46085d641..a29af836d 100644
--- a/lib/hashserv/client.py
+++ b/lib/hashserv/client.py
@@ -7,6 +7,7 @@ import json
import logging
import socket
import os
+from . import chunkify, DEFAULT_MAX_CHUNK
logger = logging.getLogger('hashserv.client')
@@ -25,6 +26,7 @@ class Client(object):
self.reader = None
self.writer = None
self.mode = self.MODE_NORMAL
+ self.max_chunk = DEFAULT_MAX_CHUNK
def connect_tcp(self, address, port):
def connect_sock():
@@ -58,7 +60,7 @@ class Client(object):
self.reader = self._socket.makefile('r', encoding='utf-8')
self.writer = self._socket.makefile('w', encoding='utf-8')
- self.writer.write('OEHASHEQUIV 1.0\n\n')
+ self.writer.write('OEHASHEQUIV 1.1\n\n')
self.writer.flush()
# Restore mode if the socket is being re-created
@@ -91,18 +93,35 @@ class Client(object):
count += 1
def send_message(self, msg):
+ def get_line():
+ line = self.reader.readline()
+ if not line:
+ raise HashConnectionError('Connection closed')
+
+ if not line.endswith('\n'):
+ raise HashConnectionError('Bad message %r' % message)
+
+ return line
+
def proc():
- self.writer.write('%s\n' % json.dumps(msg))
+ for c in chunkify(json.dumps(msg), self.max_chunk):
+ self.writer.write(c)
self.writer.flush()
- l = self.reader.readline()
- if not l:
- raise HashConnectionError('Connection closed')
+ l = get_line()
- if not l.endswith('\n'):
- raise HashConnectionError('Bad message %r' % message)
+ m = json.loads(l)
+ if 'chunk-stream' in m:
+ lines = []
+ while True:
+ l = get_line().rstrip('\n')
+ if not l:
+ break
+ lines.append(l)
- return json.loads(l)
+ m = json.loads(''.join(lines))
+
+ return m
return self._send_wrapper(proc)
@@ -155,6 +174,14 @@ class Client(object):
m['unihash'] = unihash
return self.send_message({'report-equiv': m})
+ def get_taskhash(self, method, taskhash, all_properties=False):
+ self._set_mode(self.MODE_NORMAL)
+ return self.send_message({'get': {
+ 'taskhash': taskhash,
+ 'method': method,
+ 'all': all_properties
+ }})
+
def get_stats(self):
self._set_mode(self.MODE_NORMAL)
return self.send_message({'get-stats': None})