ppf/new: Add newly designed top-level functions in top.py.
[cs-p2p-next.git] / ppf / new / top.py
1 """
2 Top level functions for parsing and storing information.
3
4 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
5 """
6
7 import storage
8 import parsing
9 import config
10
11 def parse_store_session(swarm_writer, parser, session_id):
12     """Parse and store information for a BitTorrent session.
13
14     swarm_writer is an instance of storage.SwarmWriter.
15     parser is an instance of parsing.LogParser (may be
16         LibtorrentLogParser, TriblerLogParser or some other).
17     session_id is the client session id.
18     """
19
20     while True:
21         msg = parser.get_next_message()
22         # In case of no more messages, exit loop.
23         if msg == None:
24             break
25
26         msg.set_client_session_id(session_id)
27         writer.add_message(msg)
28
29 def enhance_swarm_description(swarm_description, swarm_writer):
30     """Enhance swarm description with client session.
31
32     Client sessions are determined by writing in swarm database. This
33     is accomplished through the use of the swarm_writer.
34
35     swarm_description is an instance of config.SwarmDescription.
36     swarm_writer is an instance of storage.SwarmWriter.
37     """
38     swarm = swarm_description.get_swarm()
39     swarm_writer.add_swarm(swarm)
40
41     for session_entry in swarm_description.get_session_entries():
42         session = session_entry.get_sssion()
43         session_id = swarm_writer.add_session(session)
44         # Update session_id in swarm_description list.
45         swarm_description.update_session_entry_id(session_entry, session_id)
46
47 def retrieve_log(session_entry):
48     """Retrieve and unpack logs.
49
50     Retrival may involve copying it from another folder or another
51     system (through SFTP, HTTP, rsync, etc.). If logs are a compressed
52     file that will be unpacked.
53
54     session_entry is an instance of config.SessionEntry.
55     Return local file location of uncompressed logs.
56     """
57
58     # TODO: Retrieve logs from remote systems (rsync, HTTP, SFTP, etc.).
59
60     # TODO: Unpack in case of compressed files.
61
62     return log_location
63
64 def parse_session(session_entry, swarm_writer):
65     """Top-level function for parsing a BitTorrent client session log.
66
67     Log files are retrieved from remote locations and unpacked then
68     processed through a parser and stored through a swarm writer.
69
70     session_entry is an instance of config.SessionEntry.
71     swarm_writer is an instance of storage.SwarmWriter.
72     """
73
74     location = retrieve_logs(session_entry)
75     parser = LogParser.create_parser_by_type(session_entry.get_client_type(),
76             location)
77     session_id = session_entry.get_session_id()
78     parse_store_session(swarm_writer, parser, session_id)
79
80 def parse_swarm(swarm_description, swarm_writer):
81     """Top-level function for parsing logs for an entire BitTorrent swarm.
82
83     Swarm description file is enanched, such that swarm and client
84     session information are already stored in the database.
85
86     session_entry is an instance of config.SessionEntry.
87     swarm_writer is an instance of storage.SwarmWriter.
88     """
89
90     for session_entry in swarm_description.get_session_entries():
91         parse_session_log(session_entry, swarm_writer)