(self)
| 194 | self.initStop() |
| 195 | |
| 196 | def run(self): |
| 197 | from debug import logger |
| 198 | |
| 199 | logger.debug("Starting UPnP thread") |
| 200 | logger.debug("Local IP: %s", self.localIP) |
| 201 | lastSent = 0 |
| 202 | |
| 203 | # wait until asyncore binds so that we know the listening port |
| 204 | bound = False |
| 205 | while state.shutdown == 0 and not self._stopped and not bound: |
| 206 | for s in BMConnectionPool().listeningSockets.values(): |
| 207 | if s.is_bound(): |
| 208 | bound = True |
| 209 | if not bound: |
| 210 | time.sleep(1) |
| 211 | |
| 212 | self.localPort = BMConfigParser().getint('bitmessagesettings', 'port') |
| 213 | while state.shutdown == 0 and BMConfigParser().safeGetBoolean('bitmessagesettings', 'upnp'): |
| 214 | if time.time() - lastSent > self.sendSleep and len(self.routers) == 0: |
| 215 | try: |
| 216 | self.sendSearchRouter() |
| 217 | except: |
| 218 | pass |
| 219 | lastSent = time.time() |
| 220 | try: |
| 221 | while state.shutdown == 0 and BMConfigParser().safeGetBoolean('bitmessagesettings', 'upnp'): |
| 222 | resp,(ip,port) = self.sock.recvfrom(1000) |
| 223 | if resp is None: |
| 224 | continue |
| 225 | newRouter = Router(resp, ip) |
| 226 | for router in self.routers: |
| 227 | if router.location == newRouter.location: |
| 228 | break |
| 229 | else: |
| 230 | logger.debug("Found UPnP router at %s", ip) |
| 231 | self.routers.append(newRouter) |
| 232 | self.createPortMapping(newRouter) |
| 233 | queues.UISignalQueue.put(('updateStatusBar', tr._translate("MainWindow",'UPnP port mapping established on port %1').arg(str(self.extPort)))) |
| 234 | # retry connections so that the submitted port is refreshed |
| 235 | with shared.alreadyAttemptedConnectionsListLock: |
| 236 | shared.alreadyAttemptedConnectionsList.clear() |
| 237 | shared.alreadyAttemptedConnectionsListResetTime = int( |
| 238 | time.time()) |
| 239 | break |
| 240 | except socket.timeout as e: |
| 241 | pass |
| 242 | except: |
| 243 | logger.error("Failure running UPnP router search.", exc_info=True) |
| 244 | for router in self.routers: |
| 245 | if router.extPort is None: |
| 246 | self.createPortMapping(router) |
| 247 | try: |
| 248 | self.sock.shutdown(socket.SHUT_RDWR) |
| 249 | except: |
| 250 | pass |
| 251 | try: |
| 252 | self.sock.close() |
| 253 | except: |
no test coverage detected