예제 #1
0
    def OnInit(self):
        """Corresponds to OnExit()"""

        self.main_frame = MainFrame(self.handle_mainframe)
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()

        self.reset_peers()

        self.sock_thread = AsyncSockThread()
        self.sock_thread.start()

        self.server_handler = ServerHandler(self.handle_server_event)

        return True
예제 #2
0
	def OnInit(self):
		"""Corresponds to OnExit()"""

		self.main_frame = MainFrame(self.handle_mainframe)
		self.SetTopWindow(self.main_frame)
		self.main_frame.Show()

		self.reset_peers()

		self.sock_thread = AsyncSockThread()
		self.sock_thread.start()

		self.server_handler = ServerHandler(self.handle_server_event)

		return True
예제 #3
0
class ClientApp(wx.App):

    # both the below vars need to be saved and loaded from a log file...

    # need to test this on windows
    user_name = os.environ['LOGNAME']
    # need to provide a dialog to change this
    server_addr = ('stupidape.dyndns.org', 3000)

    def OnInit(self):
        """Corresponds to OnExit()"""

        self.main_frame = MainFrame(self.handle_mainframe)
        self.SetTopWindow(self.main_frame)
        self.main_frame.Show()

        self.reset_peers()

        self.sock_thread = AsyncSockThread()
        self.sock_thread.start()

        self.server_handler = ServerHandler(self.handle_server_event)

        return True

    def OnExit(self):
        """This seems to get called after all frames have died."""

        print "Closing sockets..."
        self.sock_thread.stop()
        self.server_handler.close()
        self.sock_thread.join()

    def reset_peers(self):

        self.peers = PeerList(self.handle_peerframe_event)
        self.main_frame.peer_closeall()

    def handle_mainframe(self, caller, event, *args):
        # event is not to be confused with a wx event...
        assert caller == self.main_frame
        getattr(self, 'user_' + event)(*args)

    def user_connect(self):

        self.reset_peers()
        self.server_handler.connect(self.server_addr)

    def user_setname(self):

        print "settnig name!"
        ted = wx.TextEntryDialog(self.main_frame,
                                 'As whom do you wish to be known?',
                                 defaultValue=self.user_name)
        if ted.ShowModal() == wx.ID_OK:
            self.user_name = ted.GetValue()
            self.server_handler.send('setname', self.user_name)

    def user_close(self):

        self.main_frame.Destroy()
        self.peers.closeall(True)
        for a in self.peers.keys():
            self.peers.close(a, True)
        #del self.peers

    def user_openchat(self, ident):

        self.peers[ident].frame.Show(True)

    # SERVER CALLBACKS

    def handle_server_event(self, source, event, *data):

        #wx.CallAfter(getattr(self, attrfunc), *args)
        print "handle_server_event(", event, data, ")"
        #log(event, data)
        wx.CallAfter(getattr(self, 'server_cb_' + event), *data)
        #assert server_event.isSet()
        #server_event.clear()
        a = threading.Event()
        assert not a.isSet()
        print "cleared server event"
        wx.CallAfter(lambda a: a.set(), a)
        #wx.GetApp().ProcessPendingEvents()
        print "waiting on server event"
        a.wait()
        print "server event completed"

    def server_cb_connected(self):

        try:
            self.server_handler.send('login')
            self.server_handler.send('setname', self.user_name)
        except socket.error, wtf:
            print "Failed to login! Did you reconnect too quickly?", wtf[1]
예제 #4
0
class ClientApp(wx.App):

	# both the below vars need to be saved and loaded from a log file...

	# need to test this on windows
	user_name = os.environ['LOGNAME']
	# need to provide a dialog to change this
	server_addr = ('stupidape.dyndns.org', 3000)

	def OnInit(self):
		"""Corresponds to OnExit()"""

		self.main_frame = MainFrame(self.handle_mainframe)
		self.SetTopWindow(self.main_frame)
		self.main_frame.Show()

		self.reset_peers()

		self.sock_thread = AsyncSockThread()
		self.sock_thread.start()

		self.server_handler = ServerHandler(self.handle_server_event)

		return True

	def OnExit(self):
		"""This seems to get called after all frames have died."""

		print "Closing sockets..."
		self.sock_thread.stop()
		self.server_handler.close()
		self.sock_thread.join()

	def reset_peers(self):

		self.peers = PeerList(self.handle_peerframe_event)
		self.main_frame.peer_closeall()

	def handle_mainframe(self, caller, event, *args):
		# event is not to be confused with a wx event...
		assert caller == self.main_frame
		getattr(self, 'user_' + event)(*args)

	def user_connect(self):

		self.reset_peers()
		self.server_handler.connect(self.server_addr)

	def user_setname(self):

		print "settnig name!"
		ted = wx.TextEntryDialog(self.main_frame, 'As whom do you wish to be known?', defaultValue=self.user_name)
		if ted.ShowModal() == wx.ID_OK:
			self.user_name = ted.GetValue()
			self.server_handler.send('setname', self.user_name)

	def user_close(self):

		self.main_frame.Destroy()
		self.peers.closeall(True)
		for a in self.peers.keys():
			self.peers.close(a, True)
		#del self.peers

	def user_openchat(self, ident):

		self.peers[ident].frame.Show(True)

	# SERVER CALLBACKS



	def handle_server_event(self, source, event, *data):

		#wx.CallAfter(getattr(self, attrfunc), *args)
		print "handle_server_event(", event, data, ")"
		#log(event, data)
		wx.CallAfter(getattr(self, 'server_cb_' + event), *data)
		#assert server_event.isSet()
		#server_event.clear()
		a = threading.Event()
		assert not a.isSet()
		print "cleared server event"
		wx.CallAfter(lambda a: a.set(), a)
		#wx.GetApp().ProcessPendingEvents()
		print "waiting on server event"
		a.wait()
		print "server event completed"

	def server_cb_connected(self):

		try:
			self.server_handler.send('login')
			self.server_handler.send('setname', self.user_name)
		except socket.error, wtf:
			print "Failed to login! Did you reconnect too quickly?", wtf[1]