Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

wxPBchatClient.py

By: Dernhelm on Jul 5th, 2012  |  syntax: Python  |  size: 4.50 KB  |  views: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import wx
  2. from twisted.internet import wxreactor
  3. #wxreactor.install()
  4. from twisted.spread import pb
  5. from twisted.internet import reactor, threads
  6. from twisted.cred import credentials
  7. import getpass
  8.  
  9. class ChatC(pb.Referenceable):
  10.  
  11.     def connect(self):
  12. #        who = str(getpass.getuser()).upper()
  13.         name = raw_input("Username: ")
  14.         factory = pb.PBClientFactory()
  15.         #open connection
  16.         reactor.connectTCP("localhost", 1025, factory)
  17.         def1 = factory.login(credentials.UsernamePassword(name, "none"), client=self)
  18.         def1.addCallback(self.joinGroup)
  19.         #factory.getRootObject().addCallback(self.joinGroup)
  20.         reactor.run()
  21.        
  22.     def joinGroup(self, user):
  23.         #recieve and store avatar object remote reference
  24.         print "got perspective/user ref:", user
  25.         self.user = user #the user object for this client instance...keep around!
  26.         groupN = raw_input('Group Name: ')
  27.         g = user.callRemote('joinGroup', groupN)
  28.         print "joined group!"
  29.         g.addCallback(self.chat)
  30.  
  31.     #need to wait for user input and execute remote_print from server at the same time.
  32.     #threads? stackless? stdio?
  33.     def chat(self, group):
  34.         #take input from GUI
  35.         d = threads.deferToThread(self.write)
  36.         d.addCallback(lambda message: group.callRemote('send', message))
  37.         d.addCallback(self.chat)
  38. #        message = self.write()
  39.         #send to server
  40. #        s = group.callRemote('send', message)
  41. #        s.addCallback(self.chat)
  42.        
  43.     def write(self):
  44.         message = raw_input(": ")
  45.         return message
  46.        
  47.     def remote_print(self, message):
  48.         print "(S) %s" % (message)
  49. #        self.chat(groupObj)
  50.  
  51.     def end(self):
  52.         reactor.stop()
  53.    
  54. #    def userList(self):
  55. #        #list of online users
  56. #        #accept selections
  57. #        pass
  58.  
  59. # INTERFACE
  60. # [http://wiki.wxpython.org/wxPythonAndTwisted]
  61. #class Interface(wx.Frame):
  62. #    def __init__(self):
  63. #        wx.Frame.__init__(self, None, -1, "Test Chat", size = (400, 200))
  64. #        panel = wx.Panel(self)
  65. #        notebook = Notebook(panel)
  66. #        sizer = wx.BoxSizer(wx.VERTICAL)
  67. #        sizer.Add(notebook, 1, wx.EXPAND, 5)
  68. #        panel.SetSizer(sizer)
  69. #        self.Layout()
  70. #        self.Show()
  71.  
  72. #class Notebook(wx.Notebook):
  73. #    def __init__(self, parent):
  74. #        wx.Notebook.__init__(self, parent, -1, size = (380, 180), name = 'Chat')        
  75. #        self.AddPage(ChatPanel(self), 'Chat')
  76. #        self.AddPage(ControlPanel(self), 'Controls')
  77. #        #create a generic tab creation function linked to click-on-online user/joining new groups
  78. #            #add tab(s) to chat tab
  79. #        sizer = wx.BoxSizer()
  80. #        sizer.Add(self, 0, wx.EXPAND)
  81. #        self.SetSizer(sizer)
  82. #        self.Layout()
  83.  
  84. #    #add readonly and message entry textboxes to every chat tab
  85. #class ChatPanel(wx.Panel):
  86. #    def __init__(self, parent):
  87. #        wx.Panel.__init__(self, parent = parent, id=wx.ID_ANY)
  88. #        sizer = wx.BoxSizer(wx.VERTICAL)
  89. #        self.text = wx.TextCtrl(self, style = wx.TE_MULTILINE | wx.TE_READONLY)
  90. #        self.ctrl = wx.TextCtrl(self, style = wx.TE_PROCESS_ENTER, size=(200, 25))
  91. #        sizer.Add(self.text, 5, wx.EXPAND)
  92. #        sizer.Add(self.ctrl, 0, wx.EXPAND)
  93. #        self.SetSizer(sizer)
  94. #        self.ctrl.Bind(wx.EVT_TEXT_ENTER, self.send)
  95. #        self.Layout()
  96. ##      connect to client's remote_print and write methods
  97.  
  98. #    def send(self, event):
  99. #        message  = str(self.ctrl.GetValue())
  100. #        self.ctrl.SetValue("")
  101.  
  102. #class ControlPanel(wx.Panel):
  103. #    def __init__(self, parent):
  104. #        wx.Panel.__init__(self, parent = parent, id=wx.ID_ANY)
  105. #        sizer = wx.BoxSizer(wx.VERTICAL)
  106. #        self.userList = [] #ChatC().user.callRemote('userList')
  107. #        self.listCtrl = wx.ListCtrl(self, size=(-1,100))
  108. #        self.listCtrl.InsertColumn(0, '')
  109. #        self.listCtrl.InsertColumn(1, 'User')
  110. #        
  111. #        i = 0
  112. #        for user in self.userList:
  113. #            line = i
  114. #            self.list_ctrl.InsertStringItem(i, line)
  115. #            self.list_ctrl.SetStringItem(i, 1, name)
  116. #            self.list_ctrl.SetStringItem(i, 2, "online")
  117. #            i += 1
  118. #            
  119. #        sizer.Add(self.listCtrl, 0, wx.ALL|wx.EXPAND, 5)
  120. #        self.SetSizer(sizer)
  121. #        self.Layout()
  122.  
  123.  
  124. if __name__ == "__main__":
  125.  
  126. #    app = wx.App(False)
  127. #    frame = Interface()
  128. #    frame.Show()
  129.     ChatC().connect()
  130. #    app.MainLoop()