PERFORCE change 125078 for review

Andrew Turner andrew at FreeBSD.org
Sun Aug 12 04:27:01 PDT 2007


http://perforce.freebsd.org/chv.cgi?CH=125078

Change 125078 by andrew at andrew_hermies on 2007/08/12 11:26:49

	Syncronise reation of a connection
	Create a seperate class to commiunicate over a socket
	Create a new class with the same interface to communicate over a pipe to nc over an ssh tunnel. This will be used to communicate remotely with the back end
	Ignore parse errors when closing the connection
	When we get a parse error in the communication loop return False to exit it

Affected files ...

.. //depot/projects/soc2007/andrew-update/frontend/facund/computer.py#11 edit
.. //depot/projects/soc2007/andrew-update/frontend/facund/network/__init__.py#10 edit

Differences ...

==== //depot/projects/soc2007/andrew-update/frontend/facund/computer.py#11 (text+ko) ====

@@ -161,6 +161,9 @@
 			# Start the communication thread
 			self.start()
 
+			self.__connection.startLock.acquire()
+			self.__connection.startLock.release()
+
 			# Get a list of directories the server offers
 			call = facund.Call("get_directories", None)
 			self.__connection.doCall(call)

==== //depot/projects/soc2007/andrew-update/frontend/facund/network/__init__.py#10 (text+ko) ====

@@ -25,40 +25,70 @@
 #
 
 import facund
+import fcntl
+import os
 import socket
+import subprocess
 import threading
 import xml.sax.handler
 
+class PipeComms:
+	def __init__(self, server):
+		self.popen = subprocess.Popen(["/usr/bin/ssh", server, "/usr/bin/nc -oU /tmp/facund"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+		print self.popen.stdout
+		self.stdout = self.popen.stdout.fileno()
+
+	def read(self, len):
+		return os.read(self.stdout, len)
+
+	def write(self, buf):
+		self.popen.stdin.write(buf)
+
+class SocketComms:
+	def __init__(self, server):
+		self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+		self.socket.connect(server)
+
+	def read(self, len):
+		return self.socket.recv(len)
+
+	def write(self, buf):
+		self.socket.send(buf)
+
 class Connection(xml.sax.handler.ContentHandler):
 	'''A class that works as a client with the Facund XML IPC'''
 	def __init__(self, server):
 		self.isReady = False
+		self.connectionType = "pipe"
 
 		self.__data = None
 		self.__calls = {}
 
 		self.bufSize = 1024
-		self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-		self.socket.connect(server)
-		self.socket.send("<facund-client version=\"0\">")
+		if self.connectionType == "unix":
+			self.__connection = SocketComms(server)
+		elif self.connectionType == "pipe":
+			self.__connection = PipeComms(server)
+
+		self.send("<facund-client version=\"0\">")
 
 		self.parser = xml.sax.make_parser()
 		self.parser.setContentHandler(self)
 
 		self.__connected_lock = threading.Lock()
+		self.startLock = threading.Lock()
+		self.startLock.acquire()
 
 		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
 			try:
-				self.socket.send("</facund-client>")
+				self.send("</facund-client>")
 			except socket.error:
 				pass
 
@@ -66,20 +96,32 @@
 			self.__connected_lock.acquire()
 			self.__connected_lock.release()
 
-			self.parser.close()
+			try:
+				self.parser.close()
+			except xml.sax._exceptions.SAXParseException:
+				pass
 
 	def doCall(self, call):
 		print call.getID()
 		call.acquireLock()
 		self.__calls[str(call.getID())] = call
-		self.socket.send(call.getCall())
+		self.send(call.getCall())
+
+	def send(self, buf):
+		self.__connection.write(buf)
+
+	def recv(self, len):
+		return self.__connection.read(len)
 
 	def interact(self):
 		'''Reads data from the connection and passes it to the
 		XML parser'''
 		if not self.canClose:
-			data = self.socket.recv(self.bufSize)
-			self.parser.feed(data)
+			data = self.recv(self.bufSize)
+			try:
+				self.parser.feed(data)
+			except xml.sax._exceptions.SAXParseException:
+				return False
 			return True
 		return False
 	
@@ -126,6 +168,7 @@
 
 		elif name == "facund-server":
 			self.__connected_lock.acquire()
+			self.startLock.release()
 
 	def endElement(self, name):
 		print "< " + name


More information about the p4-projects mailing list