From: Razvan Deaconescu Date: Wed, 28 Oct 2009 07:32:24 +0000 (+0200) Subject: added DatabaseWriter.py (draft); updated DatabaseAccess methods X-Git-Tag: getopt_long~292^2~2 X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=2e324b545b7d1763d30e369b219d1d4927d51086;p=cs-p2p-next.git added DatabaseWriter.py (draft); updated DatabaseAccess methods --- diff --git a/auto/db/DatabaseAccess.py b/auto/db/DatabaseAccess.py index d2d03ed..fc45588 100644 --- a/auto/db/DatabaseAccess.py +++ b/auto/db/DatabaseAccess.py @@ -166,8 +166,30 @@ class DatabaseAccess: except sqlite3.Error, e: print ("[status_messages]An error ocurred: ", e.args[0]) - def insert_status_message(self, cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta): - self.insert_status_message_row([cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta]) + def insert_status_messages(self, cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta): + self.insert_status_messages_row([cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta]) + + def select_status_messages(self, cs_id = -1): + try: + if cs_id == -1: + self.cursor.execute("select * from status_messages") + else: + self.cursor.execute("select * from status_messages where cs_id=?", (cs_id, )) + for row in self.cursor: + print row + except sqlite3.Error, e: + print("[status_messages]An error ocurred: ", e.args[0]) + + def delete_status_messages(self, cs_id = -1): + try: + if cs_id == -1: + self.cursor.execute("delete from status_messages") + else: + self.cursor.execute("delete from status_messages where cs_id=?", (cs_id, )) + for row in self.cursor: + print row + except sqlite3.Error, e: + print("[status_messages]An error ocurred: ", e.args[0]) def insert_verbose_messages_row(self, row): try: @@ -179,6 +201,29 @@ class DatabaseAccess: def insert_verbose_messages(self, cs_id, timestamp, peer_ip, peer_port, message_type, _index, begin, length, listen_port): self.insert_verbose_messages_row([cs_id, timestamp, peer_ip, peer_port, message_type, _index, begin, length, listen_port]) + def select_verbose_messages(self, cs_id = -1): + try: + if cs_id == -1: + self.cursor.execute("select * from verbose_messages") + else: + self.cursor.execute("select * from verbose_messages where cs_id=?", (cs_id, )) + for row in self.cursor: + print row + except sqlite3.Error, e: + print("[status_messages]An error ocurred: ", e.args[0]) + + def delete_verbose_messages(self, cs_id = -1): + try: + if cs_id == -1: + self.cursor.execute("delete from verbose_messages") + else: + self.cursor.execute("delete from verbose_messages where cs_id=?", (cs_id, )) + for row in self.cursor: + print row + except sqlite3.Error, e: + print("[status_messages]An error ocurred: ", e.args[0]) + + """ Test case """ diff --git a/auto/db/DatabaseWriter.py b/auto/db/DatabaseWriter.py new file mode 100644 index 0000000..14f3af9 --- /dev/null +++ b/auto/db/DatabaseWriter.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import sys +import getopt +import julian +import datetime +from DatabaseAccess import DatabaseAccess + +class DatabaseWriter: + def __init__ (self, dbname): + self.dbname = dbname + self.dba = DatabaseAccess(dbname) + self.dba.connect() + + def add_status_message(self, cs_id, date, time, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_time): + timestamp = float(julian.datetimeToJulian(date, time)); + eta = datetime.timedelta(eta_time[0], eta_time[3], 0, 0, eta_time[2], eta_time[1], 0) + dba.insert_status_message(cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta): + + def add_verbose_message(self, cs_id, date, time, peer_ip, peer_port, message_type, index, begin, length, listen_port): + timestamp = float(julian.datetimeToJulian(date, time)); + dba.insert_verbose_message(cs_id, timestamp, peer_ip, peer_port, message_type, index, begin, length, listen_port): + + def show_status_messages(self, cs_id = -1): + self.dba.select_status_messages(cs_id) + + def show_verbose_messages(self, cs_id = -1): + self.dba.select_verbose_messages(cs_id) + + def delete_status_messages(self, cs_id = -1): + self.dba.delete_status_messages(cs_id) + + def delete_verbose_messages(self, cs_id = -1): + self.dba.delete_verbose_messages(cs_id)