class IpLocator:
    def __init__(self):
        self._geo_ip = GeoIP.open("%s/GeoLiteCity.dat" % os.path.dirname(__file__),
                                  GeoIP.GEOIP_STANDARD)
        self._gps = GPS()

    def locate(self, addr):
        gir = self._geo_ip.record_by_addr(addr)
        if gir:
            x = self._gps.x(gir['longitude'])
            y = self._gps.y(gir['latitude'])
            return x, y
Exemple #2
0
class IpLocator:
    def __init__(self):
        self._geo_ip = GeoIP.open("%s/GeoLiteCity.dat" % os.path.dirname(__file__),
                                  GeoIP.GEOIP_STANDARD)
        self._gps = GPS()

    def locate(self, addr):
        record = self._geo_ip.record_by_addr(addr)
        if record:
            if record['city']:
                return self._location_tuple_from_record(record, coding="unicode_escape")
            else:
                extension_record = self._look_up_in_db_extension(addr)
                if extension_record:
                    return self._location_tuple_from_record(extension_record)
                else:
                    print "WARNING: unknown city for IP %s (GeoIP reports coordinates %r, %r)" % (
                        addr, record['longitude'], record['latitude'])
                    print "http://www.ip-tracker.org/locator/ip-lookup.php?ip=%s" % addr
                    return self._location_tuple_from_record(record)
        else:
            print "WARNING: unknown location IP %s" % addr

    def _look_up_in_db_extension(self, addr):
        addr_without_last_part = self._strip_last_addr_part(addr)
        try:
            return DB_EXTENSION[addr_without_last_part]
        except KeyError:
            return None

    def _strip_last_addr_part(self, addr):
        return ".".join(addr.split(".")[0:3]) + "."

    def _location_tuple_from_record(self, record, coding=None):
        x = self._gps.x(record['longitude'])
        y = self._gps.y(record['latitude'])
        place_name = record['city']
        if place_name and coding:
            place_name = place_name.decode(coding)
        return x, y, place_name
    for (x, y) in points[1:]:
        write_svg(' L%f,%f' % (x, y))
    write_svg('" />')

def write_svg(string):
    f.write(string)
    f.write('\n')

write_svg('<svg xmlns="http://www.w3.org/2000/svg" version="1.1">')
write_svg('<g>')
write_svg('<rect width="%f" height="%f" fill="white" />' % (width, height))

for path in world.World(width, height).paths:
    draw_path(path)

n = 0
while n < 100000:
    addr = ".".join([str(random.randint(0,255)) for i in range(4)])
    gir = gi.record_by_addr(addr)
    if gir:
        x = gps.x(gir['longitude'])
        y = gps.y(gir['latitude'])
        write_svg('<rect x="%f" y="%f" stroke="black" style="stroke-opacity:1%%" width="1" height="1" />\n' % (x, y))
        n += 1

write_svg('</g>')
write_svg('</svg>')

f.close()

Exemple #4
0
class Geography(visualizer.Visualizer):
    def __init__(self, args):
        self._ip_locator = ip_locator.IpLocator()
        self._gps = GPS(WORLD_WIDTH, WORLD_HEIGHT)
        self._here_x = self._gps.x(HERE_LONGITUDE)
        self._here_y = self._gps.y(HERE_LATITUDE)
        self._load_locations()
        visualizer.Visualizer.__init__(self, args)
        self._set_camera_position(CAMERA_POSITION)
        self._set_camera_orientation(CAMERA_Y_ORIENTATION, CAMERA_X_ORIENTATION)
        self._world = world.World(WORLD_WIDTH, WORLD_HEIGHT)
        self.enable_3d()

    def _load_locations(self):
        self._locations = []
        self._grid = numpy.zeros((LOCATION_PRECISION, LOCATION_PRECISION), int)
        self._add_peers_from_log()
        self._location_max_value = numpy.max(self._grid)

    def _add_peers_from_log(self):
        for location in locations.get_locations():
            self._add_location(location)

    def _add_location(self, location):
        if location and location not in self._locations:
            self._locations.append(location)
            x, y = location
            nx = int(LOCATION_PRECISION * x)
            ny = int(LOCATION_PRECISION * y)
            self._grid[ny, nx] += 1
            return True

    def InitGL(self):
        visualizer.Visualizer.InitGL(self)
        glClearColor(0.0, 0.0, 0.0, 0.0)

    def render(self):
        glEnable(GL_LINE_SMOOTH)
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self._render_world()
        # self._render_bar_grid_lines()
        # self._render_bar_grid_points()
        self._render_parabolae()
        self._render_land_points()
        # self._render_locations()

    def _render_world(self):
        glColor3f(*LAND_COLOR)

        for path in self._world.paths:
            self._render_land(path)

    def _render_land(self, path):
        glBegin(GL_LINE_STRIP)
        for x, y in path:
            glVertex3f(x, 0, y)
        glEnd()

    def _render_locations(self):
        glColor4f(1, 1, 1, 0.01)
        glBegin(GL_LINES)
        for lx, ly in self._locations:
            x = lx * WORLD_WIDTH
            y = ly * WORLD_HEIGHT
            glVertex3f(x, BARS_BOTTOM, y)
            glVertex3f(x, BARS_TOP, y)
        glEnd()

    def _render_bar_grid_lines(self):
        glBegin(GL_LINES)
        ny = 0
        for row in self._grid:
            y = (ny + 0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    strength = pow(float(value) / self._location_max_value, 0.2)
                    x = (nx + 0.5) / LOCATION_PRECISION * WORLD_WIDTH

                    glColor4f(1, 1, 1, 0.5 * strength)
                    glVertex3f(x, BARS_TOP, y)
                    glColor4f(1, 1, 1, 0.05)
                    glVertex3f(x, BARS_BOTTOM, y)

                    # glColor4f(1, 1, 1, 0.2)
                    # glVertex3f(x, BARS_TOP, y)
                    # glVertex3f(x, BARS_TOP - strength*BARS_TOP, y)
                nx += 1
            ny += 1
        glEnd()

    def _render_bar_grid_points(self):
        self._render_grid_points(BARS_TOP)

    def _render_grid_points(self, h):
        glEnable(GL_POINT_SMOOTH)
        glPointSize(3.0)
        glBegin(GL_POINTS)
        glColor4f(1, 1, 1, 0.5)
        ny = 0
        for row in self._grid:
            y = (ny + 0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    x = (nx + 0.5) / LOCATION_PRECISION * WORLD_WIDTH
                    glVertex3f(x, h, y)
                nx += 1
            ny += 1
        glEnd()

    def _render_parabolae(self):
        ny = 0
        for row in self._grid:
            y = (ny + 0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    x = (nx + 0.5) / LOCATION_PRECISION * WORLD_WIDTH
                    strength = pow(float(value) / self._location_max_value, 0.4) * 0.8
                    glColor4f(1, 1, 1, strength)
                    self._render_parabola(x, y, self._here_x, self._here_y)
                nx += 1
            ny += 1

    def _render_parabola(self, x1, y1, x2, y2):
        h1 = 0
        h2 = 2
        glBegin(GL_LINE_STRIP)
        precision = 100
        for n in range(precision):
            r = float(n) / (precision - 1)
            x = x1 + (x2 - x1) * r
            y = y1 + (y2 - y1) * r
            h = h1 + (h2 - h1) * (math.cos((r - 0.5) / 5) - 0.995) / 0.005
            glVertex3f(x, h, y)
        glEnd()

    def _render_land_points(self):
        self._render_grid_points(BARS_BOTTOM)

    def _render_surface_points(self):
        self._render_grid_points(BARS_TOP)
Exemple #5
0
class Geography(visualizer.Visualizer):
    def __init__(self, args):
        self._gps = GPS(WORLD_WIDTH, WORLD_HEIGHT)
        self._here_x = self._gps.x(HERE_LONGITUDE)
        self._here_y = self._gps.y(HERE_LATITUDE)
        self._load_locations()
        visualizer.Visualizer.__init__(self, args, file_class=File)
        self._set_camera_position(CAMERA_POSITION)
        self._set_camera_orientation(CAMERA_Y_ORIENTATION, CAMERA_X_ORIENTATION)
        self._world = world.World(WORLD_WIDTH, WORLD_HEIGHT)
        self.enable_3d()
        self.playing_segments = collections.OrderedDict()
        #self._load_traces()

    def InitGL(self):
        visualizer.Visualizer.InitGL(self)
        glClearColor(0.0, 0.0, 0.0, 0.0)
        self._stable_layer = self.new_layer(self._render_world_and_history)

    def _load_traces(self):
        #f = open("sessions/120827-084403-TDL4/traces.data", "r")
        f = open("sessions/121104-171222-TDL4-slow/traces.data", "r")
        self._traces = cPickle.load(f)
        f.close()

    def _load_locations(self):
        self._locations = []
        self._grid = numpy.zeros((LOCATION_PRECISION, LOCATION_PRECISION), int)
        self._add_peers_from_log()

    def _add_peers_from_log(self):
        for location in locations.get_locations():
            self._add_location(location)

    def _add_location(self, location):
        if location and location not in self._locations:
            self._locations.append(location)
            x, y = location
            nx = int(LOCATION_PRECISION * x)
            ny = int(LOCATION_PRECISION * y)
            self._grid[ny, nx] += 1
            return True

    def _addr_location(self, addr):
        location = self._ip_locator.locate(addr)
        if location:
            x, y = location
            nx = int(LOCATION_PRECISION * x)
            ny = int(LOCATION_PRECISION * y)
            return x, y, nx, ny

    def added_segment(self, segment):
        self._add_location(self.peers_by_addr[segment.peer.addr].location)
        self.playing_segments[segment.id] = segment

    def render(self):
        glEnable(GL_LINE_SMOOTH)
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        self._stable_layer.draw()
        self._render_grid_activity()
        #self._render_active_traces()

    def _render_world_and_history(self):
        self._location_max_value = numpy.max(self._grid)
        #self._render_world()

        self._render_bar_grid_lines()
        self._render_bar_grid_points()

        #self._render_parabolae()
        #self._render_land_points()

        #self._render_locations()

    def update(self):
        self._grid_activity = numpy.zeros((LOCATION_PRECISION, LOCATION_PRECISION), int)
        outdated = []
        for segment in self.playing_segments.values():
            if segment.is_playing():
                if segment.peer.location:
                    x, y = segment.peer.location
                    nx = int(LOCATION_PRECISION * x)
                    ny = int(LOCATION_PRECISION * y)
                    self._grid_activity[ny, nx] = 1
            else:
                outdated.append(segment.id)

        if len(outdated) > 0:
            for segment_id in outdated:
                del self.playing_segments[segment_id]

    def _render_world(self):
        glColor3f(*LAND_COLOR)

        for path in self._world.paths:
            self._render_land(path)

    def _render_land(self, path):
        glBegin(GL_LINE_STRIP)
        for x, y in path:
            glVertex3f(x, 0, y)
        glEnd()

    def _render_locations(self):
        glColor4f(1, 1, 1, .01)
        glBegin(GL_LINES)
        for lx, ly in self._locations:
            x = lx * WORLD_WIDTH
            y = ly * WORLD_HEIGHT
            glVertex3f(x, BARS_BOTTOM, y)
            glVertex3f(x, BARS_TOP, y)
        glEnd()

    def _render_bar_grid_lines(self):
        glColor4f(1, 1, 1, 0.2)
        glBegin(GL_LINES)
        ny = 0
        for row in self._grid:
            y = (ny+0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    strength = pow(float(value) / self._location_max_value, 0.2)
                    x = (nx+0.5) / LOCATION_PRECISION * WORLD_WIDTH

                    # glColor4f(1, 1, 1, 0.5 * strength)
                    # glVertex3f(x, BARS_TOP, y)
                    # glColor4f(1, 1, 1, 0.05)
                    # glVertex3f(x, BARS_BOTTOM, y)

                    glVertex3f(x, BARS_TOP, y)
                    glVertex3f(x, BARS_TOP - strength*BARS_TOP, y)
                nx += 1
            ny += 1
        glEnd()

    def _render_bar_grid_points(self):
        self._render_grid_points(BARS_TOP)

    def _render_grid_points(self, h):
        glEnable(GL_POINT_SMOOTH)
        glPointSize(3.0)
        glBegin(GL_POINTS)
        glColor4f(1, 1, 1, 0.5)
        ny = 0
        for row in self._grid:
            y = (ny+0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    x = (nx+0.5) / LOCATION_PRECISION * WORLD_WIDTH
                    glVertex3f(x, h, y)
                nx += 1
            ny += 1
        glEnd()

    def _render_parabolae(self):
        ny = 0
        for row in self._grid:
            y = (ny+0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    x = (nx+0.5) / LOCATION_PRECISION * WORLD_WIDTH
                    strength = pow(float(value) / self._location_max_value, 0.4) * 0.5
                    glColor4f(1, 1, 1, strength)
                    self._render_parabola(x, y, self._here_x, self._here_y)
                nx += 1
            ny += 1

    def _render_grid_activity(self):
        glBegin(GL_LINES)
        ny = 0
        for row in self._grid_activity:
            y = (ny+0.5) / LOCATION_PRECISION * WORLD_HEIGHT
            nx = 0
            for value in row:
                if value > 0:
                    x = (nx+0.5) / LOCATION_PRECISION * WORLD_WIDTH
                    glColor4f(1, 1, 1, 1)
                    #self._render_parabola(x, y, self._here_x, self._here_y)
                    glVertex3f(x, BARS_TOP, y)
                    glColor4f(1, 1, 1, 0.25)
                    glVertex3f(x, BARS_BOTTOM, y)
                nx += 1
            ny += 1
        glEnd()

    def _render_parabola(self, x1, y1, x2, y2):
        h1 = 0
        h2 = 2
        glBegin(GL_LINE_STRIP)
        precision = 100
        for n in range(precision):
            r = float(n) / (precision - 1)
            x = x1 + (x2 - x1) * r
            y = y1 + (y2 - y1) * r
            h = h1 + (h2 - h1) * (math.cos((r - 0.5) / 5) - 0.995) / 0.005
            glVertex3f(x, h, y)
        glEnd()

    def _render_land_points(self):
        self._render_grid_points(0)

    def _render_active_traces(self):
        for segment in self.playing_segments.values():
            trace = self._traces[segment.peer.addr]
            self._render_trace(trace)

    def _render_trace(self, trace):
        glColor4f(1,1,1,1)
        glBegin(GL_LINE_STRIP)
        n = 1
        for lx, ly in trace:
            #opacity = float(n) / (len(trace)-1)
            #glColor4f(1,1,1, opacity)
            x = lx * WORLD_WIDTH
            y = ly * WORLD_HEIGHT
            glVertex3f(x, 0, y)
            n += 1
        glEnd()