Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/devel' into devel
Browse files Browse the repository at this point in the history
Conflicts:
	Tribler/Core/Video/VideoServer.py
  • Loading branch information
Devristo committed Apr 11, 2014
2 parents 3172095 + 2f4bd63 commit 4180cd0
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 270 deletions.
39 changes: 20 additions & 19 deletions Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,37 +416,38 @@ def get_byte_progress(self, byteranges, consecutive=False):
return self.get_piece_progress(pieces, consecutive)

@checkHandleAndSynchronize()
def set_piece_priority(self, pieces, priority):
def set_piece_priority(self, pieces_need, priority):
do_prio = False
pieces_have = self.handle.status().pieces
piecepriorities = self.handle.piece_priorities()
for piece in pieces:
for piece in pieces_need:
if piece < len(piecepriorities):
piecepriorities[piece] = priority
if piecepriorities[piece] != priority and not pieces_have[piece]:
piecepriorities[piece] = priority
do_prio = True
else:
self._logger.info("LibtorrentDownloadImpl: could not set priority for non-existing piece %d / %d", piece, len(piecepriorities))
self.handle.prioritize_pieces(piecepriorities)
if do_prio:
self.handle.prioritize_pieces(piecepriorities)
else:
self._logger.info("LibtorrentDownloadImpl: skipping set_piece_priority")

@checkHandleAndSynchronize()
def set_byte_priority(self, byteranges, priority):
pieces = []
for fileindex, bytes_begin, bytes_end in byteranges:
if fileindex >= 0:
if bytes_begin == 0 and bytes_end == -1:
# Set priority for entire file
filepriorities = self.handle.file_priorities()
filepriorities[fileindex] = priority
self.handle.prioritize_files(filepriorities)
else:
# Ensure the we remain within the file's boundaries
file_entry = self.handle.get_torrent_info().file_at(fileindex)
bytes_begin = min(file_entry.size, bytes_begin) if bytes_begin >= 0 else file_entry.size + (bytes_begin + 1)
bytes_end = min(file_entry.size, bytes_end) if bytes_end >= 0 else file_entry.size + (bytes_end + 1)
# Ensure the we remain within the file's boundaries
file_entry = self.handle.get_torrent_info().file_at(fileindex)
bytes_begin = min(file_entry.size, bytes_begin) if bytes_begin >= 0 else file_entry.size + (bytes_begin + 1)
bytes_end = min(file_entry.size, bytes_end) if bytes_end >= 0 else file_entry.size + (bytes_end + 1)

startpiece = self.handle.get_torrent_info().map_file(fileindex, bytes_begin, 0).piece
endpiece = self.handle.get_torrent_info().map_file(fileindex, bytes_end, 0).piece + 1
startpiece = max(startpiece, 0)
endpiece = min(endpiece, self.handle.get_torrent_info().num_pieces())
startpiece = self.handle.get_torrent_info().map_file(fileindex, bytes_begin, 0).piece
endpiece = self.handle.get_torrent_info().map_file(fileindex, bytes_end, 0).piece + 1
startpiece = max(startpiece, 0)
endpiece = min(endpiece, self.handle.get_torrent_info().num_pieces())

pieces += range(startpiece, endpiece)
pieces += range(startpiece, endpiece)
else:
self._logger.info("LibtorrentDownloadImpl: could not set priority for incorrect fileindex")

Expand Down
5 changes: 2 additions & 3 deletions Tribler/Core/Video/VideoServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from binascii import unhexlify

from Tribler.Core.simpledefs import DLMODE_VOD
from Tribler.Core.Video.utils import get_ranges


class VideoServer(ThreadingMixIn, HTTPServer):
Expand All @@ -24,7 +25,6 @@ def __init__(self, port, session):
VideoServer.__single = self

self._logger = logging.getLogger(self.__class__.__name__)
self._logger.setLevel(logging.DEBUG)

self.port = port
self.session = session
Expand Down Expand Up @@ -93,8 +93,7 @@ def do_GET(self):
fileindex = int(fileindex)
filename, length = download.get_def().get_files_with_length()[fileindex]

from cherrypy.lib import http
requested_range = http.get_ranges(self.headers.getheader('range'), length)
requested_range = get_ranges(self.headers.getheader('range'), length)
if requested_range != None and len(requested_range) != 1:
self.send_error(416, "Requested Range Not Satisfiable")
return
Expand Down
52 changes: 52 additions & 0 deletions Tribler/Core/Video/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,55 @@ def return_feasible_playback_modes():
else:
l.append(PLAYBACKMODE_EXTERNAL_DEFAULT)
return l


# From: cherrypy.lib.httputil
def get_ranges(headervalue, content_length):
"""Return a list of (start, stop) indices from a Range header, or None.
Each (start, stop) tuple will be composed of two ints, which are suitable
for use in a slicing operation. That is, the header "Range: bytes=3-6",
if applied against a Python string, is requesting resource[3:7]. This
function will return the list [(3, 7)].
If this function returns an empty list, you should return HTTP 416.
"""

if not headervalue:
return None

result = []
bytesunit, byteranges = headervalue.split("=", 1)
for brange in byteranges.split(","):
start, stop = [x.strip() for x in brange.split("-", 1)]
if start:
if not stop:
stop = content_length - 1
start, stop = int(start), int(stop)
if start >= content_length:
# From rfc 2616 sec 14.16:
# "If the server receives a request (other than one
# including an If-Range request-header field) with an
# unsatisfiable Range request-header field (that is,
# all of whose byte-range-spec values have a first-byte-pos
# value greater than the current length of the selected
# resource), it SHOULD return a response code of 416
# (Requested range not satisfiable)."
continue
if stop < start:
# From rfc 2616 sec 14.16:
# "If the server ignores a byte-range-spec because it
# is syntactically invalid, the server SHOULD treat
# the request as if the invalid Range header field
# did not exist. (Normally, this means return a 200
# response containing the full entity)."
return None
result.append((start, stop + 1))
else:
if not stop:
# See rfc quote above.
return None
# Negative subscript (last N bytes)
result.append((content_length - int(stop), content_length))

return result
3 changes: 2 additions & 1 deletion Tribler/Main/metadata-injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from hashlib import sha1
import logging
import logging.config
import binascii

from Tribler.Core.TorrentDef import TorrentDef
from Tribler.Core.Session import Session
Expand Down Expand Up @@ -62,7 +63,7 @@ def dispersy_started(session, opt, torrentManager, channelManager,
channelManager.createChannel(my_channel_name, u'')
new_channel_created = True
else:
logger.info(u"Use existing channel", my_channel_id)
logger.info(u"Use existing channel %s", binascii.hexlify(my_channel_id))
my_channel = channelManager.getChannel(my_channel_id)
if my_channel.name != my_channel_name:
logger.info(u"Rename channel to %s", my_channel_name)
Expand Down
Loading

0 comments on commit 4180cd0

Please sign in to comment.