From owner-p4-projects@FreeBSD.ORG Sun Jun 17 11:45:20 2007 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 2AB6B16A477; Sun, 17 Jun 2007 11:45:20 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id D538216A46B for ; Sun, 17 Jun 2007 11:45:19 +0000 (UTC) (envelope-from andrew@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [69.147.83.41]) by mx1.freebsd.org (Postfix) with ESMTP id C5E6E13C45D for ; Sun, 17 Jun 2007 11:45:19 +0000 (UTC) (envelope-from andrew@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.8/8.13.8) with ESMTP id l5HBjJ37045489 for ; Sun, 17 Jun 2007 11:45:19 GMT (envelope-from andrew@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.8/8.13.8/Submit) id l5HBjJa1045478 for perforce@freebsd.org; Sun, 17 Jun 2007 11:45:19 GMT (envelope-from andrew@freebsd.org) Date: Sun, 17 Jun 2007 11:45:19 GMT Message-Id: <200706171145.l5HBjJa1045478@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to andrew@freebsd.org using -f From: Andrew Turner To: Perforce Change Reviews Cc: Subject: PERFORCE change 121848 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Jun 2007 11:45:20 -0000 http://perforce.freebsd.org/chv.cgi?CH=121848 Change 121848 by andrew@andrew_hermies on 2007/06/17 11:45:03 Create a thread to communicate with the back end Add a lock to syncronise the closing of the connection Send a ping to the back end to test interaction Stop reading data when the back end sends a The back and front ends can now communicate with each other. Currently they just send ping/pong messages untill the connection is closed though. Affected files ... .. //depot/projects/soc2007/andrew-update/frontend/facund/computer.py#2 edit .. //depot/projects/soc2007/andrew-update/frontend/facund/network/__init__.py#5 edit Differences ... ==== //depot/projects/soc2007/andrew-update/frontend/facund/computer.py#2 (text+ko) ==== @@ -26,10 +26,12 @@ import socket import facund.network +import threading -class Computer: +class Computer(threading.Thread): '''A class to describe each computer able to be connected to''' def __init__(self, name, host): + threading.Thread.__init__(self) self.__name = name self.__host = host self.__dirs = [] @@ -67,6 +69,9 @@ try: self.__connection = \ facund.network.Connection(self.__host) + + # Start the communication thread + self.start() except socket.error: print "Couldn't connect to " + self.__host self.__connection = None @@ -79,3 +84,7 @@ self.__connection.disconnect() self.__connection = None + def run(self): + '''The main communications thread''' + while self.__connection.interact(): + continue ==== //depot/projects/soc2007/andrew-update/frontend/facund/network/__init__.py#5 (text+ko) ==== @@ -24,8 +24,9 @@ # SUCH DAMAGE. # +import socket +import threading import xml.sax.handler -import socket class Connection(xml.sax.handler.ContentHandler): '''A class that works as a client with the Facund XML IPC''' @@ -39,33 +40,47 @@ self.parser = xml.sax.make_parser() self.parser.setContentHandler(self) + self.__connected_lock = threading.Lock() + self.canClose = False # Mark the class as ready and able to disconnect self.isReady = True + self.socket.send("") + def disconnect(self): if self.isReady: self.isReady = False # Send a connection close self.socket.send("") - # Wait for the server to close the connection - while not self.canClose: - self.interact() + # Wait for the other end to close + self.__connected_lock.acquire() + self.__connected_lock.release() + self.parser.close() def interact(self): '''Reads data from the connection and passes it to the XML parser''' - data = self.socket.recv(self.bufSize) - self.parser.feed(data) + if not self.canClose: + data = self.socket.recv(self.bufSize) + self.parser.feed(data) + return True + return False + + def startElement(self, name, attributes): + print "> " + name + " " + str(attributes.items()) + if self.isReady and name == "pong": + self.socket.send("") - def startElement(self, name, attributes): - print "> " + name + if name == "facund-server": + self.__connected_lock.acquire() def endElement(self, name): print "< " + name # The server send a close message if name == "facund-server": + self.__connected_lock.release() self.canClose = True