示例#1
0
	def request(self,msg,timeout=None):

		start_time = time.time()

		req_id = self.send_seq
		req_msg = BotMessage.BotMessage(msg.type,msg.data,BotMessage.BotMessage.REQ,req_id)

		with self.init_cond:
			if(self.initialized == False):
				self.init_cond.wait()

		with self.send_lock:
			txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,req_msg)
			self.send_seq += 1
			self.msg_input_q.put(txt)

		while(True):
			with self.msg_arrival_cond:
				if(self.msg_output_q.empty()):
					self.msg_arrival_cond.wait(timeout)
				
				with self.recv_lock:
					if(self.msg_output_q.empty() == False):

						msg = self.msg_output_q.get(False)
						obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
						self.recv_seq += 1

						if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id):
							return obj
						else:
							self.request_buffer.put(obj,True,timeout)
					
					#Check the buffer for the desired message
					result = None
					tmp = []
					while(self.request_buffer.empty() == False):
						obj = self.request_buffer.get()
						if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id):
							result = obj
							break
						else:
							tmp.append(obj)

					for obj in tmp:
						self.request_buffer.put(obj,True)

					if(result == None and (time.time() - start_time) < timeout):
						continue

					return result
						
		"""
示例#2
0
	def request(self,msg,timeout=None):

		start_time = time.time()

		req_id = self.send_seq
		req_msg = BotMessage.BotMessage(msg.type,msg.data,BotMessage.BotMessage.REQ,req_id)

		with self.init_cond:
			if(self.initialized == False):
				self.init_cond.wait()

		with self.send_lock:
			txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,req_msg)
			self.send_seq += 1
			self.msg_input_q.put(txt)

		while(True):
			with self.msg_arrival_cond:
				if(self.msg_output_q.empty()):
					self.msg_arrival_cond.wait(timeout)
				
				with self.recv_lock:
					if(self.msg_output_q.empty() == False):

						msg = self.msg_output_q.get(False)
						obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
						self.recv_seq += 1

						if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id):
							return obj
						else:
							self.request_buffer.put(obj,True,timeout)
					
					#Check the buffer for the desired message
					result = None
					tmp = []
					while(self.request_buffer.empty() == False):
						obj = self.request_buffer.get()
						if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id):
							result = obj
							break
						else:
							tmp.append(obj)

					for obj in tmp:
						self.request_buffer.put(obj,True)

					if(result == None and (time.time() - start_time) < timeout):
						continue

					return result
						
		"""
示例#3
0
	def receive(self,block=True,timeout=None):
		with self.init_cond:
			if(self.initialized == False):
				self.init_cond.wait()

		if(self.request_buffer.empty() == False):
			return self.request_buffer.get(block,timeout)

		try:	
			msg = self.msg_output_q.get(block,timeout)
		except Queue.Empty:
			return None

		with self.recv_lock:
			obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
			self.recv_seq += 1
		
		return obj
示例#4
0
	def receive(self,block=True,timeout=None):
		with self.init_cond:
			if(self.initialized == False):
				self.init_cond.wait()

		if(self.request_buffer.empty() == False):
			return self.request_buffer.get(block,timeout)

		try:	
			msg = self.msg_output_q.get(block,timeout)
		except Queue.Empty:
			return None

		with self.recv_lock:
			obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
			self.recv_seq += 1
		
		return obj
示例#5
0
	def protocolInit(self):
		with self.init_cond:
			asyncore.loop(timeout=1,count=2,map=self.map)
			if(self.msg_output_q.empty() == False):
				msg = self.msg_output_q.get(self)
				obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
				self.recv_seq += 1
				if(obj.type == "SET_IV"):
					self.iv = obj.data
					hello = ClientHello(self.client_type)
					txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,hello)
					self.msg_input_q.put(txt)
					self.send_seq += 1
					self.initialized = True
					self.init_cond.notifyAll()
				else:
					assert("Failed to set IV")
			else:
				assert("Server did not send initial message!")
示例#6
0
	def protocolInit(self):
		with self.init_cond:
			asyncore.loop(timeout=1,count=2,map=self.map)
			if(self.msg_output_q.empty() == False):
				msg = self.msg_output_q.get(self)
				obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg)
				self.recv_seq += 1
				if(obj.type == "SET_IV"):
					self.iv = obj.data
					hello = ClientHello(self.client_type)
					txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,hello)
					self.msg_input_q.put(txt)
					self.send_seq += 1
					self.initialized = True
					self.init_cond.notifyAll()
				else:
					assert("Failed to set IV")
			else:
				assert("Server did not send initial message!")
示例#7
0
 def found_terminator(self):
     msg = BotMessage.fromTxt(self.key, self.iv, self.recv_seq, self.data)
     self.recv_seq += 1
     self.data = ""
     self.gamelogic.handleBotMessage(msg, self)
示例#8
0
	def found_terminator(self):
		msg = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,self.data)
		self.recv_seq += 1
		self.data = ""
		self.gamelogic.handleBotMessage(msg,self)