def _load_ticket(self, stream_slug, destination_uuid): stream = Stream.get_by(slug=stream_slug) if not destination_uuid: return Ticket.get_by(stream=stream, destination=Node.me()) node = Node.get_by(uuid=destination_uuid) return Ticket.query.filter_by(stream=stream, destination=node).first()
def test_delete(self): node = Node.me() ticket = TicketFactory(destination=node) self.http_client.fetch(HTTPRequest( self.get_url(ticket.absolute_url()), 'DELETE'), self.stop) response = self.wait() eq_(response.code, 200) eq_(Ticket.get_by(id=ticket.id), None) ok_(Stream.get_by(slug=ticket.stream.slug))
def test_get_tickets(self): response = self.fetch("/tickets") eq_(response.code, 200) result = json.loads(response.body) ok_("tickets" in result) eq_(len(result["tickets"]), 3) for ticket in result["tickets"]: source = Node.get_by(uuid=ticket["source"]) destination = Node.get_by(uuid=ticket["destination"]) ok_(Ticket.get_by(stream=Stream.get_by(slug=ticket["stream"]), source=source, destination=destination))
def test_delete(self): node = Node.me() ticket = TicketFactory(destination=node) self.http_client.fetch( HTTPRequest(self.get_url(ticket.absolute_url()), 'DELETE'), self.stop) response = self.wait() eq_(response.code, 200) eq_(Ticket.get_by(id=ticket.id), None) ok_(Stream.get_by(slug=ticket.stream.slug))
def _handle_ticket(self, ticket): # reload to get a new database session ticket = Ticket.get_by(id=ticket.id) if ticket.source == Node.me(): source_ip = "127.0.0.1" else: source_ip = ticket.source.ip_address try: port = self.create_tunnel(ticket.id, source_ip, ticket.source_port, self.tunnels) except Exception, e: log.warning("Couldn't create a tunnel for %s: %s", ticket, e)
def _offer_ourselves(cls, stream, destination): tickets = Ticket.query.filter_by(source=Node.me()) if (tickets.count() >= settings.OUTGOING_STREAM_LIMIT or Node.me().upstream and tickets.count() * settings.STREAM_BITRATE > Node.me().upstream): log.info("Can't stream %s to %s, already at limit", stream, destination) raise HTTPError(412) ticket = Ticket.get_by(stream=stream, destination=Node.me()) if ticket: new_ticket = Ticket(stream=stream, destination=destination, source_port=ticket.source_port, hops=ticket.hops + 1) log.info("We are receiving %s and have room to forward -- " "created %s to potentially forward to %s", stream, new_ticket, destination) return new_ticket
def _offer_ourselves(cls, stream, destination): tickets = Ticket.query.filter_by(source=Node.me()) if (tickets.count() >= settings.OUTGOING_STREAM_LIMIT or Node.me().upstream and tickets.count() * settings.STREAM_BITRATE > Node.me().upstream): log.info("Can't stream %s to %s, already at limit", stream, destination) raise HTTPError(412) ticket = Ticket.get_by(stream=stream, destination=Node.me()) if ticket: new_ticket = Ticket(stream=stream, destination=destination, source_port=ticket.source_port, hops=ticket.hops + 1) log.info( "We are receiving %s and have room to forward -- " "created %s to potentially forward to %s", stream, new_ticket, destination) return new_ticket
def close_expired_tunnels(self): for ticket_id, tunnel in self.tunnels.items(): if not Ticket.get_by(id=ticket_id): self.destroy_tunnel(ticket_id, self.tunnels)
def _already_streaming(cls, stream, destination): return Ticket.get_by(stream=stream, destination=destination)