Commits

Chris Perl committed 076f779

Removing unnecessary exceptions from bayeuxmessage. Made bayeuxclientevent able to take a url and use the to populate http request etc.

  • Participants
  • Parent commits c95574a

Comments (0)

Files changed (2)

bayeuxclientevent.py

 import asyncore
 import socket
+import urlparse
+import sys
 
 from bayeuxmessage import *
 
 import pdb
 
 class HttpRequest(object):
-	def __init__(self, data):
+	def __init__(self, netloc, path, data):
 		self.action  = "POST"
-		self.path    = "/cometd"
+		self.path    = path
 		self.version = "HTTP/1.1"
 		self.body = data
 		self.headers = { 
-				"Host": "localhost:8080",
+				"Host": "%s" % netloc,
 				"Content-Type": "application/json",
 				"Content-Length": len(self.body),
 		}
 class BayeuxClient(asyncore.dispatcher):
 	BUFSIZ = 4096
 
-	def __init__(self):
+	def __init__(self, url):
 		# Call our super constructor
 		asyncore.dispatcher.__init__(self)
 
+		# parse the url 
+		p = urlparse.urlparse(url)
+		self._host = p.hostname
+		self._port = p.port
+		self._netloc = p.netloc
+		self._path = p.path
+
 		# initiate the socket connection to the server
 		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
-		self.connect(("localhost", 8080))
+		self.connect((self._host, self._port))
 		self.channel = "/org/fppe/test"
 
 		# setup various state parameters so we know what part
 		h = HttpResponse(self.ibuffer)
 		if h.status != 200: 
 			raise StandardError("Non 200 response from server: %d" % h.status)
+		if h.body == "":
+			print "Empty body in reply from server!"
+			self.ibuffer = ""
+			self.awaiting_response = False
+			return
 		data = json_decode(h.body)
 		msgs = [ ResponseMessageFactory(BayeuxResponseMessage.FROMDICT, x) for x in data ]
 		event_msgs    = [ x for x in msgs if     isinstance(x, EventMessage) ]
 		if not self.handshake_sent:
 			# we want to start by sending a handshake message to the server
 			m = RequestMessageFactory(BayeuxRequestMessage.HANDSHAKE)
-			h = HttpRequest(m.as_json())
+			h = HttpRequest(self._netloc, self._path, m.as_json())
 			self.obuffer = h.dump()
 			self.write_data()
 			self.handshake_sent = True
 		elif not self.connect_sent:
 			m = RequestMessageFactory(BayeuxRequestMessage.CONNECT, clientId=self.clientId)
-			h = HttpRequest(m.as_json())
+			h = HttpRequest(self._netloc, self._path, m.as_json())
 			self.obuffer = h.dump()
 			self.write_data()
 			self.connect_sent = True
 		elif not self.subscription_sent:
 			m = RequestMessageFactory(BayeuxRequestMessage.SUBSCRIBE, clientId=self.clientId, subscription=self.channel)
-			h = HttpRequest(m.as_json())
+			h = HttpRequest(self._netloc, self._path, m.as_json())
 			self.obuffer = h.dump()
 			self.write_data()
 			self.subscription_sent = True
 		else:
 			# Here, its time to long poll
 			m = RequestMessageFactory(BayeuxRequestMessage.CONNECT, clientId=self.clientId)
-			h = HttpRequest(m.as_json())
+			h = HttpRequest(self._netloc, self._path, m.as_json())
 			self.obuffer = h.dump()
 			self.write_data()
 			self.longpoll_sent = True
 def main():
 	# Create a crap load of clients for load testing
 	clients = []
-	for i in range(50):
-		clients.append(BayeuxClient())
+	for i in range(1):
+		clients.append(BayeuxClient(sys.argv[1]))
 	asyncore.loop(use_poll=True)
 
 if __name__ == "__main__":
 class BayeuxProtocolError(Exception):
 	pass
 
-class BayeuxServerError(Exception):
-	pass
-
-class BayeuxError(Exception):
-	pass
-
 # Bayeux Message Classes
 class RequestMessageFactory(object):
 	"""