diff --git a/examplejson.txt b/examplejson.txt new file mode 100644 index 0000000..d7ba19f --- /dev/null +++ b/examplejson.txt @@ -0,0 +1,2 @@ +echo '[{"name": "Nome", "starttime": "ORA", "endtime": "EndTme"}]' | nc 127.0.0.1 9999 + diff --git a/server/server.py b/server/server.py index 488dd80..eb7d4ae 100644 --- a/server/server.py +++ b/server/server.py @@ -1,52 +1,70 @@ import SocketServer - import sys,os - import logging logging.basicConfig(level=logging.INFO) +# include data object from techrec import * """ CONNECTION HANDLER """ class ConnHandler(SocketServer.BaseRequestHandler): + def __init__(self, db, *args, **keys): + self.db = db + SocketServer.BaseRequestHandler.__init__(self, *args, **keys) + def handle(self): + self.data = self.request.recv(1024).strip() - logging.info("Connection from " % ( format(self.client_address[0]) )) - print self.data - self.request.sendall(self.data.upper()) + logging.debug("DATA: %s - %s" % (self.client_address[0], self.data) ) + + # asjson = "[{name: 'Nome', starttime: 'StartTime', endtime: 'EndTime'}]" + # asjson = '[{"name": "Nome", "starttime": "StarTTTIME", "endtime": "EndTme"}]' + # print "JSon example", asjson + print "Data", self.data + print "DATA U", unicode( self.data ) + tmp = Rec(asjson=self.data) + print "Creato REC: ", tmp + + if tmp.err(): + respmsg = "Data ERROR" + else: + respmsg = "OK" + + self.db.add( tmp ) + self.db.printall() + self.request.sendall( respmsg ) + +def handler_factory(db): + def createHandler(*args, **keys): + return ConnHandler(db, *args, **keys) + return createHandler """ MAIN SERVER """ class recserver: def __init__(self, host="localhost", port=9999): self.host = host self.port = port - + self.db = RecDB() + def start(self): - self.server = SocketServer.TCPServer((self.host, self.port), ConnHandler) + try: + # self.server = SocketServer.TCPServer((self.host, self.port), ConnHandler) + self.server = SocketServer.TCPServer((self.host, self.port), handler_factory( self.db ) ) + except: + sys.exit("Bind error.") + logging.info("Server ready to serve forever.") self.server.serve_forever() - def loadall(): - + """ # Testing DB db = RecDB() - a = Rec(name="Mimmo1",starttime="ora",endtime="fine") - b = Rec(name="Mimmo2",starttime="ora",endtime="fine") - c = Rec(name="Mimmo3",starttime="ora",endtime="fine") - db.add( a ) - db.add( b ) - db.add( c ) - db.printall() - db.search( Rec(name="Mimmo1") ) - - - sys.exit() - + """ # Loading Server TODO: make thread r = recserver() r.start() @@ -55,4 +73,4 @@ def loadall(): # Loading Server if __name__ == "__main__": loadall() - \ No newline at end of file + diff --git a/server/techrec.db b/server/techrec.db index ebb0a3d..01604d5 100644 Binary files a/server/techrec.db and b/server/techrec.db differ diff --git a/server/techrec.py b/server/techrec.py index 7f2f162..6960ba1 100644 --- a/server/techrec.py +++ b/server/techrec.py @@ -1,9 +1,18 @@ - import logging +import logging logging.basicConfig(level=logging.DEBUG) +import sys -from sqlalchemy import create_engine, Column, Integer, String -from sqlalchemy.orm import sessionmaker -from sqlalchemy.ext.declarative import declarative_base +try: + from sqlalchemy import create_engine, Column, Integer, String + from sqlalchemy.orm import sessionmaker + from sqlalchemy.ext.declarative import declarative_base +except: + sys.exit("No SQLAlchemy.") + + +import json + +import yaml """ This class describe a single Record (Rec() class) and the @@ -15,6 +24,9 @@ Base = declarative_base() """ ************ RECORD ABSTRACTION ************ """ +QUEUE = 0 +RUN = 2 +DONE = 4 class Rec(Base): @@ -23,15 +35,41 @@ class Rec(Base): name = Column(String) endtime = Column(String) starttime = Column(String) - - def __init__(self, name="", starttime="", endtime=""): - self.name = name - self.starttime = starttime - self.endtime = endtime - self.id = None + state = Column(Integer) + + def __init__(self, name="", starttime="", endtime="",asjson=""): + self.error = 0 + + if len(asjson) == 0: + self.name = name + self.starttime = starttime + self.endtime = endtime + else: + try: + # dec = json.loads( unicode(asjson) ) + dec = yaml.load( asjson ) + except: + self.error = 0 + self.name = dec[0]['name'] + self.starttime = dec[0]['starttime'] + self.endtime = dec[0]['endtime'] + # self.id = None + self.state = QUEUE + print "DECC" , dec + print "NAME", self.name + print "Sstart", self.starttime + print "END", self.endtime + + def err(self): return self.error + + def setrun(self): + self.state = RUN + + def setdone(self): + self.state = DONE def __repr__(self): - return "" % (self.id, self.name, self.starttime, self.endtime) + return "" % (self.id, self.name, self.starttime, self.endtime, self.state) """ ************ @@ -69,7 +107,7 @@ class RecDB: logging.info("Looking for %s" % recfilter) - self.session.query( Rec ).filter( Rec.name=name ).first() + # self.session.query( Rec ).filter( Rec.name=name ).first() for row in records: print "Found:", row diff --git a/server/techrec.pyc b/server/techrec.pyc index 02f90ab..1bf9226 100644 Binary files a/server/techrec.pyc and b/server/techrec.pyc differ