matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame^] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 21 | import SocketServer |
| 22 | import sqlite3 |
| 23 | import logging |
| 24 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 25 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 26 | class DBHandler(object): |
| 27 | db_file = "pdu.db" |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 28 | |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 29 | def __init__(self, db_file="pdu.db"): |
| 30 | self.db_file = db_file |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 31 | logging.debug("Creating new DBHandler: %s" % self.db_file) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 32 | logging.getLogger().name = "DBHandler" |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 33 | self.conn = sqlite3.connect(self.db_file, check_same_thread = False) |
| 34 | self.cursor = self.conn.cursor() |
| 35 | |
| 36 | def do_sql(self, sql): |
| 37 | logging.debug("executing sql: %s" % sql) |
| 38 | self.cursor.execute(sql) |
| 39 | self.conn.commit() |
| 40 | |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 41 | def get_res(self, sql): |
| 42 | return self.cursor.execute(sql) |
| 43 | |
| 44 | def get_one(self, sql): |
| 45 | res = self.get_res(sql) |
| 46 | return res.fetchone() |
| 47 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 48 | def close(self): |
| 49 | self.cursor.close() |
| 50 | self.conn.close() |
| 51 | |
| 52 | |
| 53 | class ListenerServer(object): |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame^] | 54 | # conn = sqlite3.connect("/var/lib/lavapdu/pdu.db", check_same_thread = False) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 55 | # cursor = conn.cursor() |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 56 | |
| 57 | def __init__(self, config): |
| 58 | self.server = TCPServer((config["hostname"], config["port"]), TCPRequestHandler) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 59 | logging.getLogger().name = "ListenerServer" |
Matt Hart | 7d67061 | 2013-08-20 16:47:52 +0100 | [diff] [blame] | 60 | logging.getLogger().setLevel(config["logging_level"]) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 61 | logging.info("listening on %s:%s" % (config["hostname"], config["port"])) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 62 | self.db = DBHandler(config["dbfile"]) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 63 | self.create_db() |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 64 | self.server.db = self.db |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 65 | |
| 66 | def create_db(self): |
| 67 | sql = "create table if not exists pdu_queue (id integer primary key, hostname text, port int, request text)" |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 68 | self.db.do_sql(sql) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 69 | |
| 70 | def start(self): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 71 | logging.info("Starting the ListenerServer") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 72 | self.server.serve_forever() |
| 73 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 74 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 75 | class TCPRequestHandler(SocketServer.BaseRequestHandler): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 76 | #"One instance per connection. Override handle(self) to customize action." |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 77 | def insert_request(self, data): |
| 78 | array = data.split(" ") |
| 79 | hostname = array[0] |
| 80 | port = int(array[1]) |
| 81 | request = array[2] |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 82 | db = self.server.db |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 83 | sql = "insert into pdu_queue values (NULL,'%s',%i,'%s')" % (hostname,port,request) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 84 | db.do_sql(sql) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 85 | #db.close() |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 86 | |
| 87 | def handle(self): |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 88 | logging.getLogger().name = "TCPRequestHandler" |
| 89 | try: |
| 90 | data = self.request.recv(4096).strip() |
| 91 | logging.debug("got request: %s" % data) |
| 92 | self.insert_request(data) |
| 93 | self.request.sendall("ack\n") |
| 94 | except: |
| 95 | self.request.sendall("nack\n") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 96 | self.request.close() |
| 97 | |
| 98 | class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
| 99 | allow_reuse_address = True |
| 100 | daemon_threads = True |
| 101 | pass |
| 102 | |
| 103 | if __name__ == "__main__": |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 104 | logging.basicConfig(level=logging.DEBUG) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 105 | logging.getLogger().setLevel(logging.DEBUG) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 106 | logging.debug("Executing from __main__") |
| 107 | starter = {"hostname": "0.0.0.0", |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 108 | "port":16421, |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame^] | 109 | "dbfile": "/var/lib/lavapdu/pdu.db", |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 110 | "logging_level": logging.DEBUG} |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 111 | ss = ListenerServer(starter) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 112 | ss.start() |