Permalink
Browse files

rendezvous/server.py: implement geo-aware rendezvous

  • Loading branch information...
bassosimone committed May 25, 2011
1 parent ae9bc5a commit 341ad82757c962133e04a6a76f87e910d9248a89
Showing with 42 additions and 8 deletions.
  1. +42 −8 neubot/rendezvous/server.py
@@ -21,23 +21,29 @@
#
import StringIO
import random
import sys
if __name__ == "__main__":
sys.path.insert(0, ".")
from neubot.config import CONFIG
from neubot.database import DATABASE
from neubot.database import table_geoloc
from neubot.http.message import Message
from neubot.http.server import ServerHTTP
from neubot.log import LOG
from neubot.net.poller import POLLER
from neubot.rendezvous.geoip_wrapper import Geolocator
from neubot.rendezvous import compat
from neubot import boot
from neubot import marshal
from neubot import system
from neubot import utils
GEOLOCATOR = Geolocator()
class ServerRendezvous(ServerHTTP):
def configure(self, conf, measurer=None):
@@ -60,15 +66,32 @@ def process_request(self, stream, request):
boot.VERSION
)
#
# Select test server address.
# The default test server is the master server itself.
# If we know the country, lookup the list of servers for
# that country in the database.
# If there are no servers for that country, register
# the master server for the country so that we can notice
# we have new users and can take the proper steps to
# deploy nearby servers.
#
server = self.conf.get("rendezvous.server.default",
"master.neubot.org")
agent_address = stream.peername[0]
country = GEOLOCATOR.lookup_country(agent_address)
if country:
servers = table_geoloc.lookup_servers(DATABASE.connection(),
country)
if not servers:
LOG.info("* learning new country: %s" % country)
table_geoloc.insert_server(DATABASE.connection(),
country, server)
servers = [server]
server = random.choice(servers)
if "speedtest" in m.accept:
#
# TODO! Here we should read a table with the internet
# addresses of registered test servers. That makes more
# sense than keeping the list into the configuration.
#
m1.available["speedtest"] = [
self.conf.get("rendezvous.server.speedtest",
"http://speedtest1.neubot.org/speedtest")]
m1.available["speedtest"] = [ "http://%s/speedtest" % server ]
#
# Neubot <=0.3.7 expects to receive an XML document while
@@ -112,6 +135,13 @@ def main(args):
boot.common("rendezvous.server", "Rendezvous server", args)
conf = CONFIG.copy()
#
# Open the databases needed to perform geolocation after
# boot.common() because the pathnames for the databases
# are configurable.
#
GEOLOCATOR.open_or_die()
server = ServerRendezvous(POLLER)
server.configure(conf)
for port in conf["rendezvous.server.ports"].split(","):
@@ -122,6 +152,10 @@ def main(args):
system.go_background()
LOG.redirect()
# Honour MaxMind license.
LOG.info("This product includes GeoLite data created by MaxMind, "
"available from <http://www.maxmind.com/>.")
system.drop_privileges()
POLLER.loop()

0 comments on commit 341ad82

Please sign in to comment.