From: <K-...@us...> - 2009-03-26 15:50:19
|
Revision: 292 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=292&view=rev Author: K-4U Date: 2009-03-26 15:50:09 +0000 (Thu, 26 Mar 2009) Log Message: ----------- Now we have unix sockets! They are only fetched in the connection file, but will be implemented into a getData function later on. Everything send trough this function should be send in XML. There is a minor bug, that you should manually chmod the unixSocket file Modified Paths: -------------- trunk/VPS/TODO trunk/VPS/connection.py trunk/VPS/dataConvert.py trunk/VPS/dataConvert_xml.py Added Paths: ----------- trunk/VPS/object_dict.py trunk/VPS/xml2dict.py Modified: trunk/VPS/TODO =================================================================== --- trunk/VPS/TODO 2009-03-25 19:11:24 UTC (rev 291) +++ trunk/VPS/TODO 2009-03-26 15:50:09 UTC (rev 292) @@ -1,2 +1,2 @@ =General= -* Create an communicator (for the website/UNIX socket) (assigned to K-4U) +* Create an communicator (for the website/UNIX socket) (assigned to K-4U) On Hold Modified: trunk/VPS/connection.py =================================================================== --- trunk/VPS/connection.py 2009-03-25 19:11:24 UTC (rev 291) +++ trunk/VPS/connection.py 2009-03-26 15:50:09 UTC (rev 292) @@ -13,6 +13,8 @@ def closeAll(self): log(1, 'SRV', 'Closing server socket') self.socket.close() + #Remove unix socket: + os.remove("../unixSocket") for i, data in enumerate(self.sh['Data']): if data != None: @@ -21,9 +23,13 @@ data.socket.close() def run(self): + #Create our tcp-socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + #Create our unix-socket: + #log(2,"UNIX","Setting up") + self.unixSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #Trying to get hold of the port while 1: @@ -33,9 +39,32 @@ except: time.sleep(1) + #And, bind unix socket: + while 1: + try: + #log(2,"UNIX","Binding") + self.unixSocket.bind("../unixSocket") + break + except socket.error, msg: + if msg[0] == 98: #File already exists + #log(2,"UNIX","File exists") + os.remove("../unixSocket") #Remove our existing socket + else: + log(3, 'SRV', msg) + break + log(2,"UNIX","Binding succeeded") + #Try to chmod our socket: + os.chmod("../unixSocket",777) + + self.unixSocket.listen(20) self.socket.listen(20) log(2, 'SRV', "I'm online!") + + #Create a new thread for the unix: + self.unixSocket = unixHandler(self.sh,self.unixSocket) + self.unixSocket.start() + #Now we just wait for some lone souls... while 1: sock = self.socket.accept() @@ -45,10 +74,40 @@ self.sh['Data'][i] = Data(self.sh, i, sock) self.sh['Data'][i].start() else: - log(3, 'SRV', 'To much connections!') - + log(3, 'SRV', 'Too much connections!') + +class unixHandler(threading.Thread): + def __init__(self, share, unixSock): + self.sh = share + self.unixSock = unixSock + + threading.Thread.__init__(self) + + def run(self): + while 1: + #Get our connection: + conn, addr = self.unixSock.accept() + buffer = "" + while 1: + #Get our data: + data = conn.recv(1024) + buffer = buffer + data + if data[-1] == chr(1): + part = buffer.split(chr(1)) + buffer = "" + for message in part: + if message != '': + #And here, we handle our stuff: + print message + message = dataConvert.UNIXXML2Data(message) + #log(0,"UNIX","<", message) + break + + #And close the socket after receiving + conn.close() + class Data(threading.Thread): def __init__(self, share, id, sock): self.id = id @@ -114,7 +173,6 @@ except: log(3, self.id, 'Connection closed before sending protocol') return - if D == 'bPickle': self.sh['user'][self.id]['con']['style'] = 'bPickle' self.socket.send('OK') @@ -122,7 +180,7 @@ self.sh['user'][self.id]['con']['style'] = 'XML' self.socket.send('OK') else: - log(3, self.id, 'Unknow protocol') + log(3, self.id, 'Unknown protocol') return self.sh['user'][self.id]['con']['state'] = 1 Modified: trunk/VPS/dataConvert.py =================================================================== --- trunk/VPS/dataConvert.py 2009-03-25 19:11:24 UTC (rev 291) +++ trunk/VPS/dataConvert.py 2009-03-26 15:50:09 UTC (rev 292) @@ -9,6 +9,11 @@ elif style == 'XML': return xml.Data2XML({title: msg}) + +def UNIXXML2Data(msg): + msg = xml.XML2Data(msg) + return msg + def MSG2Data(msg, style): if style == 'bPickle': msg = bPickle.String2Data(msg) Modified: trunk/VPS/dataConvert_xml.py =================================================================== --- trunk/VPS/dataConvert_xml.py 2009-03-25 19:11:24 UTC (rev 291) +++ trunk/VPS/dataConvert_xml.py 2009-03-26 15:50:09 UTC (rev 292) @@ -1,16 +1,24 @@ +from xml2dict import XML2Dict + + +dTypes = {type({}):"array",type([]):"array",type(0):"integer",type(""):"string",type(False):"boolean"} + def Data2XML(msg): data = '' - if msg == None: pass #Add nothing elif msg.__class__().__str__() == '{}': for D in msg.keys(): - data = data + '<' + D + '>' + Data2XML(msg[D]) + '</' + D + '>' + if msg[D].__class__().__str__() == '[]': + for listItem in msg[D]: + data = data + '<' + D + ' type="' + dTypes[type(listItem)] + '">' + Data2XML(listItem) + '</' + D + '>' + else: + data = data + '<' + D + ' type="' + dTypes[type(msg[D])] + '">' + Data2XML(msg[D]) + '</' + D + '>' elif msg.__class__().__str__() == '[]': for D in msg: - data = data + '<[list]>' + Data2XML(D) + '</[list]>' + data = data + '<Item type="' + dTypes[type(D)] + '">' + Data2XML(D) + '</Item>' elif msg.__class__().__str__() == '' or msg.__class__().__str__() == '0': data = str(msg) @@ -21,9 +29,38 @@ return data + + def XML2Data(msg): + xml = XML2Dict() + r = xml.fromstring(msg) + return r + from pprint import pprint + #return handle(r) + +def handle(dict): + endDict = {} + if dict.__class__().__str__() == '{}': + for key in dict: + if key != "type": + if key == "value": + endDict = dict[key] + else: + endDict[key] = handle(dict[key]) + elif dict.__class__().__str__() == "[]": + endDict = [] + for item in dict: + endDict.append(handle(item)) + else: + #endDict = dict + pass + return endDict + + + + +def OLD_XML2Data(msg,depth=[]): data = {} - while 1: pos1 = msg.find('<') if pos1 == -1: @@ -35,9 +72,26 @@ pos2 = msg.find('>',pos1) if pos2 == -1: break name = msg[pos1+1:pos2] + #Check for spaces, wich could indicate for attributes: + attributes = "" + if name.find(" ") != -1: + attributes = name[name.find(" ") +1:] + name=name[:name.find(" ")] - posend = msg.find('</' + name + '>') - if posend == -1: raise Exception('XML geen eind-tag') + newMsg = msg + tagCount = 0 + #posend = msg.find("</" + name + ">") + while 1: + if newMsg.find("<" + name) != -1: + tagCount+=1 + newMsg = newMsg[newMsg.find("<" + name) + len("<" + name):] + else: + tagCount-=1 + if tagCount <= 0: + posend = newMsg.find('</' + name + '>') + break + print "Handling XML-tag: '" + name + "', Attributes: '" + attributes + "'" + if posend == -1: raise Exception('XML geen eind-tag. Verwacht: "' + name + '"') databetween = msg[pos2+1:posend] if name == '[list]': Added: trunk/VPS/object_dict.py =================================================================== --- trunk/VPS/object_dict.py (rev 0) +++ trunk/VPS/object_dict.py 2009-03-26 15:50:09 UTC (rev 292) @@ -0,0 +1,45 @@ +""" +object_dict + +nk...@gm... 2007 + +Provided as-is; use at your own risk; no warranty; no promises; enjoy! +""" + +class object_dict(dict): + """object view of dict, you can + >>> a = object_dict() + >>> a.fish = 'fish' + >>> a['fish'] + 'fish' + >>> a['water'] = 'water' + >>> a.water + 'water' + >>> a.test = {'value': 1} + >>> a.test2 = object_dict({'name': 'test2', 'value': 2}) + >>> a.test, a.test2.name, a.test2.value + (1, 'test2', 2) + """ + def __init__(self, initd=None): + if initd is None: + initd = {} + dict.__init__(self, initd) + + def __getattr__(self, item): + d = self.__getitem__(item) + # if value is the only key in object, you can omit it + if isinstance(d, dict) and 'value' in d and len(d) == 1: + return d['value'] + else: + return d + + def __setattr__(self, item, value): + self.__setitem__(item, value) + + +def _test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": + _test() Added: trunk/VPS/xml2dict.py =================================================================== --- trunk/VPS/xml2dict.py (rev 0) +++ trunk/VPS/xml2dict.py 2009-03-26 15:50:09 UTC (rev 292) @@ -0,0 +1,61 @@ +""" +Thunder Chen<nk...@gm...> 2007.9.1 +""" +try: + import xml.etree.ElementTree as ET +except: + import cElementTree as ET # for 2.4 + +from object_dict import object_dict +import re + +class XML2Dict(object): + + def __init__(self): + pass + + def _parse_node(self, node): + node_tree = object_dict() + # Save attrs and text, hope there will not be a child with same name + if node.text: + node_tree.value = node.text + for (k,v) in node.attrib.items(): + k,v = self._namespace_split(k, object_dict({'value':v})) + node_tree[k] = v + #Save childrens + for child in node.getchildren(): + tag, tree = self._namespace_split(child.tag, self._parse_node(child)) + if tag not in node_tree: # the first time, so store it in dict + node_tree[tag] = tree + continue + old = node_tree[tag] + if not isinstance(old, list): + node_tree.pop(tag) + node_tree[tag] = [old] # multi times, so change old dict to a list + node_tree[tag].append(tree) # add the new one + + return node_tree + + + def _namespace_split(self, tag, value): + """ + Split the tag '{http://cs.sfsu.edu/csc867/myscheduler}patients' + ns = http://cs.sfsu.edu/csc867/myscheduler + name = patients + """ + result = re.compile("\{(.*)\}(.*)").search(tag) + if result: + print tag + value.namespace, tag = result.groups() + return (tag, value) + + def parse(self, file): + """parse a xml file to a dict""" + f = open(file, 'r') + return self.fromstring(f.read()) + + def fromstring(self, s): + """parse a string""" + t = ET.fromstring(s) + root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t)) + return object_dict({root_tag: root_tree}) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |