blob: d72200e6f628f6e77d8cd5c5f17ab5adb28717f6 [file] [log] [blame]
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +01001#! /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 Hart1306d822013-08-09 12:08:22 +010021import SocketServer
22import sqlite3
23import logging
24
Matt Hart1306d822013-08-09 12:08:22 +010025
Matt Hart1499bd42013-08-20 11:35:46 +010026class DBHandler(object):
27 db_file = "pdu.db"
Matt Hart1306d822013-08-09 12:08:22 +010028
Matt Hart63ed9112013-08-20 13:31:50 +010029 def __init__(self, db_file="pdu.db"):
30 self.db_file = db_file
Matt Hart1499bd42013-08-20 11:35:46 +010031 logging.debug("Creating new DBHandler: %s" % self.db_file)
Matt Hart40d6c312013-08-20 14:47:31 +010032 logging.getLogger().name = "DBHandler"
Matt Hart1499bd42013-08-20 11:35:46 +010033 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 Hart63ed9112013-08-20 13:31:50 +010041 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 Hart1499bd42013-08-20 11:35:46 +010048 def close(self):
49 self.cursor.close()
50 self.conn.close()
51
52
53class ListenerServer(object):
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010054# conn = sqlite3.connect("/var/lib/lavapdu/pdu.db", check_same_thread = False)
Matt Hart1499bd42013-08-20 11:35:46 +010055# cursor = conn.cursor()
Matt Hart1499bd42013-08-20 11:35:46 +010056
57 def __init__(self, config):
58 self.server = TCPServer((config["hostname"], config["port"]), TCPRequestHandler)
Matt Hart40d6c312013-08-20 14:47:31 +010059 logging.getLogger().name = "ListenerServer"
Matt Hart7d670612013-08-20 16:47:52 +010060 logging.getLogger().setLevel(config["logging_level"])
Matt Hart1499bd42013-08-20 11:35:46 +010061 logging.info("listening on %s:%s" % (config["hostname"], config["port"]))
Matt Hart63ed9112013-08-20 13:31:50 +010062 self.db = DBHandler(config["dbfile"])
Matt Hart1306d822013-08-09 12:08:22 +010063 self.create_db()
Matt Hart63ed9112013-08-20 13:31:50 +010064 self.server.db = self.db
Matt Hart1306d822013-08-09 12:08:22 +010065
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 Hart1499bd42013-08-20 11:35:46 +010068 self.db.do_sql(sql)
Matt Hart1306d822013-08-09 12:08:22 +010069
70 def start(self):
Matt Hart1499bd42013-08-20 11:35:46 +010071 logging.info("Starting the ListenerServer")
Matt Hart1306d822013-08-09 12:08:22 +010072 self.server.serve_forever()
73
Matt Hart1499bd42013-08-20 11:35:46 +010074
Matt Hart1306d822013-08-09 12:08:22 +010075class TCPRequestHandler(SocketServer.BaseRequestHandler):
Matt Hart1499bd42013-08-20 11:35:46 +010076 #"One instance per connection. Override handle(self) to customize action."
Matt Hart1306d822013-08-09 12:08:22 +010077 def insert_request(self, data):
78 array = data.split(" ")
79 hostname = array[0]
80 port = int(array[1])
81 request = array[2]
Matt Hart63ed9112013-08-20 13:31:50 +010082 db = self.server.db
Matt Hart1306d822013-08-09 12:08:22 +010083 sql = "insert into pdu_queue values (NULL,'%s',%i,'%s')" % (hostname,port,request)
Matt Hart1499bd42013-08-20 11:35:46 +010084 db.do_sql(sql)
Matt Hart63ed9112013-08-20 13:31:50 +010085 #db.close()
Matt Hart1306d822013-08-09 12:08:22 +010086
87 def handle(self):
Matt Hart40d6c312013-08-20 14:47:31 +010088 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 Hart1306d822013-08-09 12:08:22 +010096 self.request.close()
97
98class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
99 allow_reuse_address = True
100 daemon_threads = True
101 pass
102
103if __name__ == "__main__":
Matt Hart1499bd42013-08-20 11:35:46 +0100104 logging.basicConfig(level=logging.DEBUG)
Matt Hart63ed9112013-08-20 13:31:50 +0100105 logging.getLogger().setLevel(logging.DEBUG)
Matt Hart1499bd42013-08-20 11:35:46 +0100106 logging.debug("Executing from __main__")
107 starter = {"hostname": "0.0.0.0",
Matt Hart63ed9112013-08-20 13:31:50 +0100108 "port":16421,
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +0100109 "dbfile": "/var/lib/lavapdu/pdu.db",
Matt Hart96ca8662013-08-21 13:37:35 +0100110 "logging_level": logging.DEBUG}
Matt Hart1499bd42013-08-20 11:35:46 +0100111 ss = ListenerServer(starter)
Matt Hart1306d822013-08-09 12:08:22 +0100112 ss.start()