slush

Reconnection server

Oct 10th, 2012
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import sys
  3. import random
  4.  
  5. import stratum
  6. import stratum.settings as settings
  7. from twisted.internet import reactor
  8. from twisted.application import service, internet    
  9. from stratum.services import ServiceEventHandler, GenericService
  10. from stratum import socket_transport
  11. from stratum.connection_registry import ConnectionRegistry
  12.  
  13. class MiningService(GenericService):
  14.     service_type = 'mining'
  15.     service_vendor = 'custom'
  16.     is_default = True
  17.  
  18.     @classmethod
  19.     def _reconnect(cls, switchto):
  20.         next_reconnect = random.randint(5,10)
  21.         reactor.callLater(next_reconnect, cls._reconnect, switchto)
  22.        
  23.         selected = random.choice(switchto)
  24.         print switchto
  25.         print "Reconnecting to", selected
  26.        
  27.         for conn in ConnectionRegistry.iterate():
  28.             conn = conn()
  29.             if not conn:
  30.                 continue
  31.            
  32.             conn.rpc('client.reconnect', [None, selected, 2], is_notification=True)
  33.            
  34.         print "Next reconnect in %d sec" % next_reconnect
  35.        
  36.          
  37.     def _finish_after_subscribe(self, result):
  38.         self.connection_ref().rpc('client.get_version', [])
  39.         self.connection_ref().rpc('mining.notify',
  40.             ["124b", "9eb4d8f01c4522cb1191e93896fff7204bae4aebec50a7030000016700000000",
  41.             "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2103a21703062f503253482f042c67755008",
  42.             "072f736c7573682f0000000001026f312a010000001976a914e8e6ace10f10ce5ed479c7188c9b4061e53aa90688ac00000000",
  43.             ["11d30680b28e3101141731fa04daf7c42fb90d4fa8b59bb1411ff4ed31bed51f",
  44.              "968b36b8a85d22695970764a8b8e6ba2f8fd864c2a003cd44eed5947a60a467a",
  45.              "2c691ebaaedb4d53d8c03c1c7d7d4f82dba8d832d39337347a03232bfc57f1de",
  46.              "239eec3b0098cd7f82c4071ff41cb2d235558a55a0d9341d7b9f188ebdc4140f",
  47.              "608fe54d1e97ee8b6516018cd82918012cbf876dfe3ad166c3d37f5dbd7c4546",
  48.              "8841944a4b2679cd297158f6175a655a885adc80116e0608842821a248c8d3eb",
  49.              "59411a7a593424ec42b57bc0f91ee56bbf717564001786ba67e4702e0a47ef1e"],
  50.              "00000002", "1a057e08", "5075672c", True], is_notification=True
  51.         )
  52.  
  53.         return result
  54.        
  55.     def authorize(self, worker_name, password):
  56.         return True
  57.    
  58.     def subscribe(self):
  59.         self.connection_ref().on_finish.addCallback(self._finish_after_subscribe)
  60.         return ([], ''.join([ "%x" % random.randint(0,15) for x in range(8) ]), 4)
  61.    
  62.     def submit(self, worker_name, job_id, extranonce2, ntime, nonce):
  63.         return True
  64.    
  65. if __name__ == '__main__':    
  66.     try:
  67.         listening_port = int(sys.argv[1])
  68.         print "Listening on port:", listening_port
  69.     except:
  70.         print "./reconnect_demo.py <listening_port> [<switchto1> <switchto2> <switchtoN>]"
  71.         sys.exit()
  72.    
  73.     switchto = [ int(x) for x in sys.argv[2:] ]
  74.     print "I'll randomly switch connections to:", switchto
  75.    
  76.     reactor.listenTCP(listening_port, socket_transport.SocketTransportFactory(
  77.             debug=True, event_handler=ServiceEventHandler))
  78.    
  79.     MiningService._reconnect(switchto)
  80.    
  81.     reactor.run()
RAW Paste Data