import re
# the names used by Tribler for the BitTorrent messages
-msg_types = {"BT_REQUEST": "new_request"}
+msg_types = {"BT_REQUEST": "new_request", "BT_CHOKE": "_rechoke", "BT_HAVE": "got_have"}
msg_db_code = {"BT_CHOKE" : 0, "BT_HAVE" : 4, "BT_REQUEST" : 6}
DEBUG = True
print "\t--help"
print "\t-h\t\t\tprint this help screen"
-""" line format: 20-10-2009 12:56:39 Downloader: new_request 52 98304 16384 to 141.85.37.41 14398
+""" Get date and timestamp and transform it into datetime format.
+ Format: dd-mm-yyyy hh:mm:ss
+"""
+def tribler_parse_timestamp(date, time):
+
+ date_array = date.split("-");
+ time_array = time.split(":");
+ if len(date_array) != 3 or len(time_array) != 3:
+ return None
+
+ timestamp = datetime.datetime(int(date_array[2]), int(date_array[1]), int(date_array[0]), #year, month, day
+ int(time_array[0]), int(time_array[1]), int(time_array[2])) #hour, min, sec
+ return timestamp
+
+""" line format example: 20-10-2009 12:56:39 Downloader: new_request 52 98304 16384 to 141.85.37.41 14398
BitTorrent protocol message: request: <len=0013><id=6><index><begin><length>
Returns a list (timestamp, peer_ip, peer_port, message_type, index, begin, length, listen_port)
"""
print "Error: invalid line format for Downloader."
return None
- # get date and timestamp and transform it in datetime format
-
- date_array = line_parts[0].split("-");
- time_array = line_parts[1].split(":");
- if len(date_array) != 3 or len(time_array) != 3:
+ timestamp = tribler_parse_timestamp(line_parts[0], line_parts[1])
+ if timestamp == None:
print "Error: invalid line format for Downloader."
return None
- timestamp = datetime.datetime(int(date_array[2]), int(date_array[1]), int(date_array[0]), #year, month, day
- int(time_array[0]), int(time_array[1]), int(time_array[2])) #hour, min, sec
index = int(line_parts[4])
begin = int(line_parts[5])
length = int(line_parts[6])
return (timestamp, peer_ip, peer_port, msg_db_code["BT_REQUEST"], index, begin, length, 0)
+
+"""
+ line format example: 20-10-2009 12:56:39 Downloader: got_have 12
+ BitTorrent message format: have <id> <index>
+ Returns a list :...
+"""
def tribler_parse_have_msg(line):
- pass
+ if line.find(msg_types["BT_HAVE"]) == -1:
+ return None
+ # the messages can also have the format: Downloader: got_have <piece_number> is invalid piece
+ if line.find("invalid") != -1:
+ return None # didn't decide yet what to do with it
+
+ line_parts = re.split(" *", line)
+ timestamp = tribler_parse_timestamp(line_parts[0], line_parts[1])
+
+ if timestamp == None:
+ print "Error: invalid date & time format for Downloader."
+ return None
+
+ if len(line_parts) < 5 :
+ print "Error: invalid line format for Downloader."
+ return None
+
+ index = int(line_parts[4])
+ return (timestamp, None, None, msg_db_code["BT_HAVE"], index, None, None, 0)
+
+
+
+"""
+ Parses a line that contains choke/unchoke messages.
+ line format: choker: _rechoke
+"""
def tribler_parse_choke_msg(line):
+ if line.find(msg_types["BT_CHOKE"]) == -1:
+ return None
pass
+
def tribler_parse_piece_msg(line):
pass
return result
result = tribler_parse_have_msg(line)
+
if result != None:
return result
if __name__ == "__main__":
sys.exit(main_with_DB())
+ #sys.exit(main_just_parse())