Module Server
[hide private]
[frames] | no frames]

Source Code for Module Server

 1  ##################################################################### 
 2  # -*- coding: iso-8859-1 -*-                                        # 
 3  #                                                                   # 
 4  # Frets on Fire                                                     # 
 5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
 6  #                                                                   # 
 7  # This program is free software; you can redistribute it and/or     # 
 8  # modify it under the terms of the GNU General Public License       # 
 9  # as published by the Free Software Foundation; either version 2    # 
10  # of the License, or (at your option) any later version.            # 
11  #                                                                   # 
12  # This program is distributed in the hope that it will be useful,   # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
15  # GNU General Public License for more details.                      # 
16  #                                                                   # 
17  # You should have received a copy of the GNU General Public License # 
18  # along with this program; if not, write to the Free Software       # 
19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
20  # MA  02110-1301, USA.                                              # 
21  ##################################################################### 
22   
23  import Network 
24  import Engine 
25  import Log 
26  import cPickle as pickle 
27   
28  from Session import ServerSession, MessageBroker 
29  from World import WorldServer 
30   
31 -class Server(Network.Server, Engine.Task):
32 - def __init__(self, engine):
33 Network.Server.__init__(self) 34 self.engine = engine 35 self.sessions = {} 36 self.broker = MessageBroker() 37 self.world = WorldServer(self.engine, server = self) 38 self.broker.addMessageHandler(self.world)
39
40 - def createConnection(self, sock):
41 return ServerSession(self.engine, sock)
42
43 - def handleConnectionOpen(self, conn):
44 Log.debug("Session #%d connected." % conn.id) 45 self.sessions[conn.id] = conn 46 self.engine.addTask(conn, synchronized = False)
47
48 - def handleConnectionClose(self, conn):
49 Network.Server.handleConnectionClose(self, conn) 50 self.engine.removeTask(conn) 51 try: 52 del self.sessions[conn.id] 53 except KeyError: 54 pass
55
56 - def broadcastMessage(self, message, meToo = True, ignore = []):
57 for id, session in self.sessions.items(): 58 if id in ignore: continue 59 session.sendMessage(message) 60 if meToo: 61 session.handleMessage(0, message)
62
63 - def sendMessage(self, receiverId, message):
64 try: 65 self.sessions[receiverId].sendMessage(message) 66 except IndexError: 67 Log.warning("Tried to send message to nonexistent session #%d." % receiverId)
68
69 - def run(self, ticks):
71
72 - def stopped(self):
73 self.close()
74