NVC/PPC protocol changes support
[p2pool.git] / p2pool / util / jsonrpc.py
index c124d03..bf11813 100644 (file)
@@ -4,10 +4,11 @@ import json
 import weakref
 
 from twisted.internet import defer
+from twisted.protocols import basic
 from twisted.python import failure, log
 from twisted.web import client, error
 
-from p2pool.util import deferred_resource, memoize
+from p2pool.util import deferral, deferred_resource, memoize
 
 class Error(Exception):
     def __init__(self, code, message, data=None):
@@ -128,7 +129,7 @@ def _http_do(url, headers, timeout, method, params):
     
     if resp['id'] != id_:
         raise ValueError('invalid id')
-    if 'error' in resp and resp['error'] is not None:
+    if 'error' in resp and resp['error'] is not None and resp['error']['code'] != -100:
         raise Error_for_code(resp['error']['code'])(resp['error']['message'], resp['error'].get('data', None))
     defer.returnValue(resp['result'])
 HTTPProxy = lambda url, headers={}, timeout=5: Proxy(lambda method, params: _http_do(url, headers, timeout, method, params))
@@ -145,3 +146,19 @@ class HTTPServer(deferred_resource.DeferredResource):
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Content-Length', len(data))
         request.write(data)
+
+class LineBasedPeer(basic.LineOnlyReceiver):
+    delimiter = '\n'
+    
+    def __init__(self):
+        #basic.LineOnlyReceiver.__init__(self)
+        self._matcher = deferral.GenericDeferrer(max_id=2**30, func=lambda id, method, params: self.sendLine(json.dumps({
+            'jsonrpc': '2.0',
+            'method': method,
+            'params': params,
+            'id': id,
+        })))
+        self.other = Proxy(self._matcher)
+    
+    def lineReceived(self, line):
+        _handle(line, self, response_handler=self._matcher.got_response).addCallback(lambda line2: self.sendLine(line2) if line2 is not None else None)