def run(self): # try to authticate user if not self.authenticate(): # if authentication fails send close socket and exit print_debug("authentication failed") self.socket.write(qtcore.QByteArray("Authtication failed\n%s\n" % self.authenticationerror)) self.socket.disconnect() return print_debug("authentication succeded") if not self.register(): print_debug("Registration with server failed, probably due to duplicate username") self.socket.write(qtcore.QByteArray("Registration Failed\nRegistration with server failed, the username you chose is already in use already, try a different one\n")) self.socket.disconnect() return self.parser=XmlToQueueEventsConverter(None,self.master.curwindow,0,type=ThreadTypes.server,id=self.id) # pass initial data to client here self.socket.write(qtcore.QByteArray("Success\nConnected To Server\n")) # wait for client to respond so it doesn't get confused and mangle the setup data with the start of the XML file data=self.socket.read(1024) #qtcore.QObject.connect(self.socket, qtcore.SIGNAL("readyRead()"), self.readyRead) #qtcore.QObject.connect(self.socket, qtcore.SIGNAL("disconnected()"), self.disconnected) # start writing thread newwriter=HiveClientWriter(self,self.socket,self.master,self.id) newwriter.start() while 1: if not data: print_debug("remote socket closed") break #print_debug("got animation data from socket: %s" % qtcore.QString(data)) self.parser.xml.addData(data) error=self.parser.read() if error!=QXmlStreamReader.PrematureEndOfDocumentError and error!=QXmlStreamReader.NoError: # queue up command for client to be disconnected break if not self.socket.isConnected(): print_debug("found that socket isn't connected") break data=self.socket.read(1024) # this should be run when the socket is disconnected self.disconnected()
def run(self): # try to authticate user if not self.authenticate(): # if authentication fails send close socket and exit print "authentication failed" self.socket.write(qtcore.QByteArray("Authtication failed\n")) self.socket.disconnectFromHost() self.socket.waitForDisconnected(1000) return print "authentication succeded" self.register() # pass initial data to client here self.socket.write(qtcore.QByteArray("%d\n%d\n%d\n" % (self.master.window.docwidth,self.master.window.docheight,self.id))) # wait for client to respond self.socket.waitForReadyRead(-1) # start writing thread newwriter=HiveClientWriter(self,self.socket,self.master,self.id) newwriter.start() parser=XmlToQueueEventsConverter(None,self.master.window,0,type=ThreadTypes.network,id=self.id) while 1: if self.socket.waitForReadyRead(-1): data=self.socket.read(1024) print "recieved data from client: %s" % qtcore.QString(data) parser.xml.addData(data) parser.read() # if error exit else: print "Recieved error:", self.socket.error(), "when reading from socket" #self.socket.write(qtcore.QByteArray("Authentication Failed")) return
class HiveClientListener(qtcore.QThread): def __init__(self,parent,socket,master,id): qtcore.QThread.__init__(self,parent) self.socket=socket self.master=master self.id=id self.authenticationerror="Unknown Error" def authenticate(self): # attempt to read stream of data, which should include version, username and password # make sure someone dosen't overload the buffer while wating for authentication info authstring=qtcore.QString() while authstring.count('\n')<3 and len(authstring)<512: data=self.socket.read(512) if data: authstring.append(data) # if error exit else: self.authenticationerror="Error: Lost connection during authentication request" return False authlist=authstring.split('\n') # if loop ended without getting enough separators just return false if len(authlist)<3: self.authenticationerror="Error parsing authentication information" return False self.username=authlist[0] password=authlist[1] try: version=int(authlist[2]) except ValueError: self.authenticationerror="Error parsing authentication information" return False if version != PROTOCOL_VERSION: self.authenticationerror="Protocol version mismatch, please change to server version: %d" % PROTOCOL_VERSION return False masterpass=self.master.getPassword() # if password is blank, let authentication pass if masterpass=="": return True # otherwise trim off whitespace and compare to password string if password.trimmed().toAscii()==masterpass: return True self.authenticationerror="Incorrect Password" return False def register(self): # register this new connection self.master.registerReaderThread(self.id,self) return self.master.registerClient(self.username,self.id,self.socket) def disconnected(self): print_debug("disconnecting client with ID: %d" % self.id) self.master.unregisterClient(self.id) def readyRead(self): data=self.socket.read(readybytes) #print_debug("got animation data from socket: %s" % qtcore.QString(data)) self.parser.xml.addData(data) error=self.parser.read() self.socket.waitForBytesWritten() if error!=QXmlStreamReader.PrematureEndOfDocumentError and error!=QXmlStreamReader.NoError: return error return None def run(self): # try to authticate user if not self.authenticate(): # if authentication fails send close socket and exit print_debug("authentication failed") self.socket.write(qtcore.QByteArray("Authtication failed\n%s\n" % self.authenticationerror)) self.socket.disconnect() return print_debug("authentication succeded") if not self.register(): print_debug("Registration with server failed, probably due to duplicate username") self.socket.write(qtcore.QByteArray("Registration Failed\nRegistration with server failed, the username you chose is already in use already, try a different one\n")) self.socket.disconnect() return self.parser=XmlToQueueEventsConverter(None,self.master.curwindow,0,type=ThreadTypes.server,id=self.id) # pass initial data to client here self.socket.write(qtcore.QByteArray("Success\nConnected To Server\n")) # wait for client to respond so it doesn't get confused and mangle the setup data with the start of the XML file data=self.socket.read(1024) #qtcore.QObject.connect(self.socket, qtcore.SIGNAL("readyRead()"), self.readyRead) #qtcore.QObject.connect(self.socket, qtcore.SIGNAL("disconnected()"), self.disconnected) # start writing thread newwriter=HiveClientWriter(self,self.socket,self.master,self.id) newwriter.start() while 1: if not data: print_debug("remote socket closed") break #print_debug("got animation data from socket: %s" % qtcore.QString(data)) self.parser.xml.addData(data) error=self.parser.read() if error!=QXmlStreamReader.PrematureEndOfDocumentError and error!=QXmlStreamReader.NoError: # queue up command for client to be disconnected break if not self.socket.isConnected(): print_debug("found that socket isn't connected") break data=self.socket.read(1024) # this should be run when the socket is disconnected self.disconnected()