Beispiel #1
0
	def __init__(self, win):
		asyncore.dispatcher.__init__(self)
		self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
		
		# Begin port.
		self.curr_port = 60000

		# HostListCtrl.
		self.win = win

		# Bind socket.
		self.set_reuse_addr()
		self.bind(('', 38000))

		# Broadcast new host message ASAP.
		self.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
		msg = MsgStruct.construct_msg(MsgStruct.CTRL_NEW_HOST)
		self.sendto(msg, ("255.255.255.255", 38000))

		# Buffers.
		self.ibuf = ""
		self.obuf = {}
		
		# Details about "unconnected" hosts.
		self.busy_hosts = {}

		# All chat sessions.
		self.chat_sessions = []
Beispiel #2
0
	def send_port(self, addr, dest_name=None):
		
		# Init out buffer.
		if addr not in self.obuf.keys():
			self.obuf[addr] = ""

		# Send message first.
		port = self.curr_port
		self.curr_port -= 1

		msg = MsgStruct.construct_msg(MsgStruct.CTRL_PORT, port)
		self.obuf[addr] += msg

		# Consider whether to establish a session.
		# Receive new host, just record.
		if addr not in self.busy_hosts.keys():
			self.busy_hosts[addr] = {'name':dest_name.strip('\0')}
			self.busy_hosts[addr]['src_port'] = port

		# I'm new host and is able to set up a session.
		else:
			print "Hello"
			self.busy_hosts[addr]['src_port'] = port
			dest_addr = (addr[0], self.busy_hosts[addr]['dest_port'])
			self.win.AddHost(dest_addr, self.busy_hosts[addr])

			# This host is "connected".
			# Delete it from the "unconnected" dict.
			del self.busy_hosts[addr]
Beispiel #3
0
	def found_terminator(self):
		# New message.
		ctrl_hdr, info = MsgStruct.deconstruct_msg(self.ibuf)
		if ctrl_hdr==MsgStruct.CTRL_INFINITE:
			pass
		elif ctrl_hdr==MsgStruct.CTRL_RECV:
			# Set new timer.
			pass
		elif ctrl_hdr==MsgStruct.CTRL_MSG:
			event = wx.CommandEvent()
			event.SetString(info)
			self.win.OnRecvMsg(event)
Beispiel #4
0
	def handle_read(self):
		self.ibuf, addr = self.recvfrom(int(MsgStruct.CONTENT_LENGTH)*2)
	
		# Check message type.
		ctrl_hdr, info = MsgStruct.deconstruct_msg(self.ibuf)
		if ctrl_hdr==MsgStruct.CTRL_NEW_HOST:
			self.send_port(addr, info)
		elif ctrl_hdr==MsgStruct.CTRL_PORT:
			self.recv_port(addr, info)
		elif ctrl_hdr==MsgStruct.CTRL_RECV:
			pass
		elif ctrl_hdr==MsgStruct.CTRL_OFF:
			pass
		elif ctrl_hdr==MsgStruct.CTRL_HEART_BEAT:
			pass
		elif ctrl_hdr==MsgStruct.CTRL_INFINITE:
			pass
Beispiel #5
0
	def new_msg(self, msg):
		msg = MsgStruct.construct_msg(MsgStruct.CTRL_MSG, msg)
		self.obuf += msg