1111from binascii import hexlify
1212from hashlib import sha1
1313from base64 import b64encode
14- from twisted .internet import reactor
1514from twisted .internet import defer
1615from log import Logger
1716from protos .message import Message , Command
1817from dht import node
1918from constants import PROTOCOL_VERSION
20- from protos .message import NOT_FOUND , GET_IMAGE , GET_CONTRACT
19+ from protos .message import NOT_FOUND
2120
2221
2322class RPCProtocol :
@@ -103,8 +102,7 @@ def _acceptResponse(self, msgID, data, sender):
103102 self .log .debug ("received response for message id %s from %s" % msgargs )
104103 else :
105104 self .log .warning ("received 404 error response from %s" % sender )
106- d , timeout = self ._outstanding [msgID ]
107- timeout .cancel ()
105+ d = self ._outstanding [msgID ][0 ]
108106 d .callback ((True , data ))
109107 del self ._outstanding [msgID ]
110108
@@ -137,31 +135,17 @@ def _sendResponse(self, response, funcname, msgID, sender, connection):
137135 data = m .SerializeToString ()
138136 connection .send_message (data )
139137
140- def _timeout (self , msgID , address ):
138+ def timeout (self , address , node_to_remove ):
141139 """
142- If a message times out we are first going to try hole punching because
143- the node may be behind a restricted NAT. If it is successful, the original
144- should get through. This timeout will only fire if the hole punching
145- fails.
140+ This timeout is called by the txrudp connection handler. We will run through the
141+ outstanding messages and callback false on any waiting on this IP address.
146142 """
147- # pylint: disable=pointless-string-statement
148- """
149- Hole punching disabled for now
150-
151- seed = SEED_NODE_TESTNET if self.multiplexer.testnet else SEED_NODE
152- if not hp and self.multiplexer.ip_address[0] != seed[0]:
153- args = (address[0], address[1], b64encode(msgID))
154- self.log.debug("did not receive reply from %s:%s for msgID %s, trying hole punching..." % args)
155- self.hole_punch(seed, address[0], address[1], "True")
156- timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID, address, True)
157- self._outstanding[msgID][1] = timeout
158- else:
159- """
160- args = (b64encode (msgID ), self ._waitTimeout )
161- self .log .warning ("did not receive reply for msg id %s within %i seconds" % args )
162- self ._outstanding [msgID ][0 ].callback ((False , None ))
163- del self ._outstanding [msgID ]
164- self .multiplexer [address ].shutdown ()
143+ if node_to_remove is not None :
144+ self .router .removeContact (node_to_remove )
145+ for msgID , val in self ._outstanding .items ():
146+ if address == val [1 ]:
147+ val [0 ].callback ((False , None ))
148+ del self ._outstanding [msgID ]
165149
166150 def rpc_hole_punch (self , sender , ip , port , relay = "False" ):
167151 """
@@ -175,12 +159,6 @@ def rpc_hole_punch(self, sender, ip, port, relay="False"):
175159 self .log .debug ("punching through NAT for %s:%s" % (ip , port ))
176160 self .multiplexer .send_datagram (" " , (ip , int (port )))
177161
178- def _get_waitTimeout (self , command ):
179- if command == GET_IMAGE or command == GET_CONTRACT :
180- return 100
181- else :
182- return self ._waitTimeout
183-
184162 def __getattr__ (self , name ):
185163 if name .startswith ("_" ) or name .startswith ("rpc_" ):
186164 return object .__getattr__ (self , name )
@@ -202,8 +180,7 @@ def func(address, *args):
202180 m .testnet = self .multiplexer .testnet
203181 data = m .SerializeToString ()
204182 d = defer .Deferred ()
205- timeout = reactor .callLater (self ._get_waitTimeout (m .command ), self ._timeout , msgID , address )
206- self ._outstanding [msgID ] = [d , timeout ]
183+ self ._outstanding [msgID ] = [d , address ]
207184 self .multiplexer .send_message (data , address )
208185 self .log .debug ("calling remote function %s on %s (msgid %s)" % (name , address , b64encode (msgID )))
209186 return d
0 commit comments