PERFORCE change 121848 for review
Andrew Turner
andrew at FreeBSD.org
Sun Jun 17 11:45:20 UTC 2007
http://perforce.freebsd.org/chv.cgi?CH=121848
Change 121848 by andrew at 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 </facund-server>
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("<call name=\"ping\" id=\"1\"/>")
+
def disconnect(self):
if self.isReady:
self.isReady = False
# Send a connection close
self.socket.send("</facund-client>")
- # 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("<call name=\"ping\" id=\"1\"/>")
- 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
More information about the p4-projects
mailing list