ppf/new: Add newly designed top-level functions in top.py.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 26 Aug 2011 17:18:05 +0000 (20:18 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 26 Aug 2011 17:52:32 +0000 (20:52 +0300)
As specified in top-design-proposal.txt.

ppf/new/top.py

index 82f0ff0..b74aace 100644 (file)
@@ -8,12 +8,11 @@ import storage
 import parsing
 import config
 
-def parse_store_session(writer, parser, session_id):
-    """
-    Parse and store information for a BitTorrent session.
+def parse_store_session(swarm_writer, parser, session_id):
+    """Parse and store information for a BitTorrent session.
 
-    writer is an instance of the storage.SwarmWriter class.
-    parser is an instance of the parsing.LogParser class (may be
+    swarm_writer is an instance of storage.SwarmWriter.
+    parser is an instance of parsing.LogParser (may be
         LibtorrentLogParser, TriblerLogParser or some other).
     session_id is the client session id.
     """
@@ -27,53 +26,66 @@ def parse_store_session(writer, parser, session_id):
         msg.set_client_session_id(session_id)
         writer.add_message(msg)
 
-def parse_store_session_archive(archive_file, session_config_file,
-        access_config_file):
-    """
-    Parse configuration files and call parse_store_session.
+def enhance_swarm_description(swarm_description, swarm_writer):
+    """Enhance swarm description with client session.
+
+    Client sessions are determined by writing in swarm database. This
+    is accomplished through the use of the swarm_writer.
+
+    swarm_description is an instance of config.SwarmDescription.
+    swarm_writer is an instance of storage.SwarmWriter.
     """
+    swarm = swarm_description.get_swarm()
+    swarm_writer.add_swarm(swarm)
 
-    sc = config.SessionConfig.load(swarm_config_file)
-    ac = config.AccessConfig.load(access_config_file)
+    for session_entry in swarm_description.get_session_entries():
+        session = session_entry.get_sssion()
+        session_id = swarm_writer.add_session(session)
+        # Update session_id in swarm_description list.
+        swarm_description.update_session_entry_id(session_entry, session_id)
 
-    # Get SwarmWriter instance from AccessConfig instance (ac).
-    swarm_writer = ac.get_swarm_writer()
+def retrieve_log(session_entry):
+    """Retrieve and unpack logs.
 
-    session_id = sc.get_session_id(archive_file)
+    Retrival may involve copying it from another folder or another
+    system (through SFTP, HTTP, rsync, etc.). If logs are a compressed
+    file that will be unpacked.
 
-    # TODO: This needs rethinking. Client type is designed to be stored in
-    # the database (referenced by the btclient_id).
-    client_type = swarm_writer.first_access_instance().get_client_type_for_session(session_id)
+    session_entry is an instance of config.SessionEntry.
+    Return local file location of uncompressed logs.
+    """
 
-    # TODO: Uncompress log archive.
-    uncompress_path = "/tmp"
-    # uncompress_log_archive(archive_file, uncompress_path)
-    parser = parsing.LogParser.get_parser(client_type, uncompress_path)
+    # TODO: Retrieve logs from remote systems (rsync, HTTP, SFTP, etc.).
 
-    parse_store_session(swarm_writer, parser, session_id)
+    # TODO: Unpack in case of compressed files.
 
-def parse_swarm(swarm_config_file, access_config_file):
-    """
-    Parse configuration files and iterate and parse client sessions.
-    """
+    return log_location
+
+def parse_session(session_entry, swarm_writer):
+    """Top-level function for parsing a BitTorrent client session log.
 
-    sc = config.SwarmConfig.load(swarm_config_file)
-    ac = config.AccessConfig.load(access_config_file)
+    Log files are retrieved from remote locations and unpacked then
+    processed through a parser and stored through a swarm writer.
 
-    # Get SwarmWriter instance from AccessConfig instance (ac).
-    swarm_writer = ac.get_swarm_writer()
+    session_entry is an instance of config.SessionEntry.
+    swarm_writer is an instance of storage.SwarmWriter.
+    """
 
-    # TODO: Add proper SwarmWriter methods for adding swarms and sessions.
-    swarm_writer.add_swarm(sc.get_swarm())
+    location = retrieve_logs(session_entry)
+    parser = LogParser.create_parser_by_type(session_entry.get_client_type(),
+            location)
+    session_id = session_entry.get_session_id()
+    parse_store_session(swarm_writer, parser, session_id)
 
-    for session in sc.get_session_list():
-        id = swarm_writer.add_client_session(session)
-        sc.update_session_id(session, id)
+def parse_swarm(swarm_description, swarm_writer):
+    """Top-level function for parsing logs for an entire BitTorrent swarm.
 
-    session_config = sc.get_session_config()
+    Swarm description file is enanched, such that swarm and client
+    session information are already stored in the database.
 
-    # TODO: Output session configuration file to disk.
-    # session_config.store(session_config_file)
+    session_entry is an instance of config.SessionEntry.
+    swarm_writer is an instance of storage.SwarmWriter.
+    """
 
-    for archive_file in sc.get_file_archives():
-        parse_store_session_config(archive_file, session_config_file, access_config_file)
+    for session_entry in swarm_description.get_session_entries():
+        parse_session_log(session_entry, swarm_writer)