change node startup to put all local addresses in the PBURL, including 127.0.0.1. This should facilitate testing on both connected and disconnected systems.

This commit is contained in:
Brian Warner 2007-03-07 18:43:17 -07:00
parent 18325251bf
commit 2c261ce996
3 changed files with 42 additions and 10 deletions

View File

@ -2,7 +2,8 @@
from twisted.application import service from twisted.application import service
import os.path import os.path
from foolscap import Tub from foolscap import Tub
from allmydata.util.iputil import get_local_ip_for from foolscap.eventual import fireEventually
from allmydata.util.iputil import get_local_addresses
from allmydata.util import idlib from allmydata.util import idlib
from twisted.python import log from twisted.python import log
@ -58,7 +59,7 @@ class Node(service.MultiService):
def log(self, msg): def log(self, msg):
log.msg(self.short_nodeid + ": " + msg) log.msg(self.short_nodeid + ": " + msg)
def _setup_tub(self, local_ip): def _setup_tub(self, local_addresses):
# we can't get a dynamically-assigned portnum until our Tub is # we can't get a dynamically-assigned portnum until our Tub is
# running, which means after startService. # running, which means after startService.
l = self.tub.getListeners()[0] l = self.tub.getListeners()[0]
@ -68,14 +69,18 @@ class Node(service.MultiService):
f = open(local_ip_filename, "r") f = open(local_ip_filename, "r")
local_ip = f.read() local_ip = f.read()
f.close() f.close()
self.tub.setLocation("%s:%d" % (local_ip, portnum)) if local_ip not in local_addresses:
local_addresses.append(local_ip)
if not os.path.exists(self._portnumfile): if not os.path.exists(self._portnumfile):
# record which port we're listening on, so we can grab the same # record which port we're listening on, so we can grab the same
# one next time # one next time
f = open(self._portnumfile, "w") f = open(self._portnumfile, "w")
f.write("%d\n" % portnum) f.write("%d\n" % portnum)
f.close() f.close()
self.tub.setLocation("%s:%d" % (local_ip, l.getPortnum())) location = ",".join(["%s:%d" % (ip, portnum)
for ip in local_addresses])
self.log("Tub location set to %s" % location)
self.tub.setLocation(location)
return self.tub return self.tub
def tub_ready(self): def tub_ready(self):
@ -89,7 +94,8 @@ class Node(service.MultiService):
def startService(self): def startService(self):
# note: this class can only be started and stopped once. # note: this class can only be started and stopped once.
service.MultiService.startService(self) service.MultiService.startService(self)
local_ip = get_local_ip_for() local_addresses = get_local_addresses()
self._setup_tub(local_ip) self._setup_tub(local_addresses)
self.tub_ready() self.tub_ready()
self.log("%s running" % self.NODETYPE) self.log("%s running" % self.NODETYPE)

View File

@ -1,14 +1,18 @@
from twisted.trial import unittest from twisted.trial import unittest
from allmydata.util.iputil import get_local_addresses from allmydata.util import iputil
class ListAddresses(unittest.TestCase): class ListAddresses(unittest.TestCase):
def test_list(self): def test_list_async(self):
d = get_local_addresses() d = iputil.get_local_addresses_async()
def _check(addresses): def _check(addresses):
self.failUnless(len(addresses) >= 1) # always have localhost self.failUnless(len(addresses) >= 1) # always have localhost
self.failUnless("127.0.0.1" in addresses) self.failUnless("127.0.0.1" in addresses)
print addresses
d.addCallbacks(_check) d.addCallbacks(_check)
return d return d
def test_list(self):
addresses = iputil.get_local_addresses()
self.failUnless(len(addresses) >= 1) # always have localhost
self.failUnless("127.0.0.1" in addresses)

View File

@ -1,6 +1,7 @@
# adapted from nattraverso.ipdiscover # adapted from nattraverso.ipdiscover
import os
from cStringIO import StringIO from cStringIO import StringIO
import re import re
import socket import socket
@ -15,6 +16,27 @@ def get_local_addresses():
# eventually I want to use somebody else's cross-platform library for # eventually I want to use somebody else's cross-platform library for
# this. For right now, I'm running ifconfig and grepping for the 'inet ' # this. For right now, I'm running ifconfig and grepping for the 'inet '
# lines. # lines.
cmd = "ifconfig"
p = os.popen("ifconfig")
addresses = []
for line in p.readlines():
# linux shows: " inet addr:1.2.3.4 Bcast:1.2.3.255..."
# OS-X shows: " inet 1.2.3.4 ..."
m = re.match("^\s+inet\s+[a-z:]*([\d\.]+)\s", line)
if m:
addresses.append(m.group(1))
return addresses
def get_local_addresses_async():
"""Return a Deferred that fires with a list of IPv4 addresses (as
dotted-quad strings) that are currently configured on this host.
"""
# eventually I want to use somebody else's cross-platform library for
# this. For right now, I'm running ifconfig and grepping for the 'inet '
# lines.
# I'd love to do this synchronously.
cmd = "ifconfig" cmd = "ifconfig"
d = getProcessOutput("ifconfig") d = getProcessOutput("ifconfig")
def _parse(output): def _parse(output):