Exemple #1
0
 def read_value(self):
     """Read a TWP value from the stream. Automatically get more bytes from 
     the stream if the buffer does not contain a complete TWP value."""
     tag = self.read_tag()
     log.debug("Read tag %d" % tag)
     if tag == 0:
         raise EndOfContent("Unexpected End-Of-Content")
     elif tag == 1:
         return None
     elif tag == 2:
         # struct
         return self.read_complex()
     elif tag == 3:
         # sequence
         return self.read_complex()
     elif tag in range(4,12): 
         return self.read_union(tag)
     elif tag == 12:
         return self.read_extension(tag)
     elif tag in range(13, 15):
         return self.read_int(tag)
     elif tag in range(15, 17):
         return self.read_binary(tag)
     elif tag in range(17, 128):
         return self.read_string(tag)
     elif tag in range(160, 256):
         return self.read_application_type(tag)
     else:
         # User-defined?
         raise TWPError("Invalid tag: %d" % tag)
Exemple #2
0
 def send_result_if_complete(self):
     if len(self.operands) != len(self.request.arguments):
         return
     result = self.perform_operation()
     reply = Reply(self.request.request_id, result)
     log.debug("Reply for %s: %s" % (reply.request_id, reply.result))
     self.consumer.send_twp(reply)
Exemple #3
0
	def __init__(self, sock, addr):
		asyncore.dispatcher_with_send.__init__(self, sock)
		Connection.__init__(self)
		self._addr = addr
		log.debug("Connect from %s %s" % self._addr)
		self.has_read_magic = False
		self.has_read_protocol_id = False
Exemple #4
0
	def read_message(self):
		# Blocking socket, just keep trying
		while True:
			try:
				return super(TWPClient, self).read_message()
			except ValueError:
				log.debug("need more bytes")
				pass
Exemple #5
0
	def send_twp(self, twp_value):
		"""Send pretty much anything that can be marshalled."""
		log.debug("Sending TWP value %s" % twp_value)
		data = marshalling.marshal(twp_value)
		# bug in asyncore? When we .send() while handling another receive, this 
		# client sometimes does not end up in the write queue.
		# Work around: don't send right away, just buffer
		self.out_buffer += data
		log.debug("Sent data %s" % data)
Exemple #6
0
 def _ensure_buffer_length(self, length):
     """Make sure we have at least length unprocessed bytes on the buffer. 
     Read more bytes into the buffer if neccessary."""
     if self.remaining_byte_length < length:
         if not self._read_from_connection():
             raise ReaderError("Connection closed")
     if self.remaining_byte_length < length:
         log.debug("Not enough bytes to unmarshal")
         raise NotEnoughBytes()
Exemple #7
0
 def read_binary(self, tag):
     log.debug("Reading binary")
     formats = {
         15: "!b",
         16: "!I"
     }
     format = formats[tag]
     length = self.read_with_format(format)
     return self.read_bytes(length)
Exemple #8
0
	def on_message(self, message):
		if isinstance(message, Request):
			log.debug("Request: %s" % message)
			text = message.text
			letters = re.sub('[^A-Za-z]','', text)
			number_of_letters = len(letters)
			response = Response(text, number_of_letters)
			self.send_twp(response)
		else:
			raise error.TWPError("Unexpected Message %s" % message)
Exemple #9
0
 def _read_from_connection(self, size=1024):
     try:
         data = self.connection.recv(self._recvsize)
     except socket.error:
         raise
     if not len(data):
         return False
     log.debug("Recvd %d bytes:\n%s" % (len(data), data))
     self.buffer += data
     return True
Exemple #10
0
	def __init__(self, host, port, message_handler_func=None, protocol_class=None):
		asyncore.dispatcher_with_send.__init__(self)
		self.protocol_class = protocol_class
		Connection.__init__(self)
		self.message_handler_func = message_handler_func
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		log.debug("Async client connecting to %s %s" % (host, port))
		self.connect( (host, port) )
		protocol_id = marshalling.marshal_int(self.protocol.protocol_id)
		self.out_buffer += TWP_MAGIC
		self.out_buffer += protocol_id
Exemple #11
0
 def read_string(self, tag):
     log.debug("Reading string")
     short_tag = 17
     long_tag = 127
     if tag < long_tag:
         length = tag - short_tag
     else:
         length = self.read_with_format("@I")
     value = self.read_bytes(length)
     try:
         value = value.decode("utf-8")
     except UnicodeError:
         raise TWPError("Failed to utf-8 decode string value")
     return value
Exemple #12
0
	def echo(self, text="Hello, World!"):
		ping = Request(text)
		self.send_twp(ping)
		message = self.read_message()
		log.debug(message)
Exemple #13
0
	def send_twp(self, twp_value):
		"""Send pretty much anything that can be marshalled."""
		log.debug("Sending TWP value %s" % twp_value)
		data = marshalling.marshal(twp_value)
		self.send(data)
		log.debug("Sent data %s" % data)
Exemple #14
0
	def on_message(self, message):
		log.debug("Recvd message: %s" % message)
Exemple #15
0
 def on_message(self, msg):
     log.debug("Received message %s" % msg)
     if isinstance(msg, Request):
         self.handle_request(msg)
     else:
         self.send_error("Expected a request.")
Exemple #16
0
def strip_comments(input):
	comments = r"\/\*.*\*\/"
	input = re.sub(comments, "", input)
	return input

if __name__ == '__main__':
	if len(sys.argv) == 3:
		output = sys.argv[2]
	elif len(sys.argv) == 2:
		output = sys.stdout
	else:
		print("Usage: twp-parser <input> [<output>]")
		exit(1)
	try:
		input = open(sys.argv[1])
		if isinstance(output, str):
			output = open(output, "w")
		log.debug("Reading input file...")
		idl = input.read()
		log.debug("Parsing input...")
		idl = strip_comments(idl)
		ast = specification.parse(idl)
		log.debug("Generating stub...")
		gen = StubGenerator(ast, output)
		gen.generate()
	finally:
		input.close()
		if output != sys.stdout:
			output.close()
Exemple #17
0
 def handle_request(self, req):
     log.debug("Received request (%s): %s " % (req.request_id, req.arguments))
     self.log_request(req)
     handler = RequestHandler(self)
     handler.handle(req)