server.py :  » Development » Pyro » Pyro-3.10 » examples » chatbox-ES » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Pyro 
Pyro » Pyro 3.10 » examples » chatbox ES » server.py
#!/usr/bin/env python

from Pyro.EventService.Clients import Publisher
from Pyro.errors import NamingError
import Pyro.core
import sys

CHAT_SERVER_GROUP = ":ChatBox-ES"
CHAT_SERVER_NAME = CHAT_SERVER_GROUP+".Server"


# Chat box administration server.
# Handles logins, logouts, channels and nicknames.
# The actual chat is fully performed by the Event Server!
# (Yes, this also means that if this Chatbox server dies, the people
#  currently chatting can continue to do so without problems!)
class ChatBox(Pyro.core.ObjBase, Publisher):
  def __init__(self):
    Pyro.core.ObjBase.__init__(self)
    Publisher.__init__(self)
    self.channels={}    # registered channels { eventTopic->nick list }
    self.nicks=[]      # all registered nicks on this server
  def getChannels(self):
    return self.channels.keys()
  def getNicks(self):
    return self.nicks
  def join(self, channel, nick):
    if nick in self.nicks:
      raise ValueError,'this nick is already in use'
    if not self.channels.has_key(channel):
      print 'CREATING NEW CHANNEL',channel
      self.channels[channel]=('ChatBox.Channel.'+channel,[])
    self.channels[channel][1].append(nick)
    self.nicks.append(nick)
    print nick,'JOINED',channel
    self.publish(self.channels[channel][0],('SERVER','** '+nick+' joined **'))
    return self.channels[channel]  # return the eventTopic for this channel
  def leave(self, channel, nick):
    if not self.channels.has_key(channel):
      print 'IGNORED UNKNOWN CHANNEL',channel
      return
    self.channels[channel][1].remove(nick)
    self.publish(self.channels[channel][0],('SERVER','** '+nick+' left **'))
    if len(self.channels[channel][1])<1:
      del self.channels[channel]
      print 'REMOVED CHANNEL',channel
    self.nicks.remove(nick)
    print nick,'LEFT',channel
    

def main():
  Pyro.core.initServer()
  daemon = Pyro.core.Daemon()
  ns = Pyro.naming.NameServerLocator().getNS()
  daemon.useNameServer(ns)

  # make sure our namespace group exists, and that our object name doesn't
  try:
    ns.createGroup(CHAT_SERVER_GROUP)
  except NamingError:
    pass
  try:
    ns.unregister(CHAT_SERVER_NAME)
  except NamingError:
    pass

  uri=daemon.connect(ChatBox(),CHAT_SERVER_NAME)

  # enter the service loop.
  print 'Chatbox open.'
  daemon.requestLoop()


if __name__=='__main__':
  main()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.