Mercurial > traipse_dev
diff orpg/networking/mplay_client.py @ 71:449a8900f9ac ornery-dev
Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
author | sirebral |
---|---|
date | Thu, 20 Aug 2009 03:00:39 -0500 |
parents | c54768cffbd4 |
children | 763a04270cf6 68c7bd272f27 |
line wrap: on
line diff
--- a/orpg/networking/mplay_client.py Tue Aug 18 20:48:36 2009 -0500 +++ b/orpg/networking/mplay_client.py Thu Aug 20 03:00:39 2009 -0500 @@ -42,7 +42,8 @@ import errno import os import time -from orpg.orpgCore import * +from orpg.orpgCore import component +from orpg.orpg_xml import xml try: import bz2 @@ -82,7 +83,7 @@ def parseXml(data): "parse and return doc" #print data - doc = component.get('xml').parseXml(data) + doc = xml.parseXml(data) doc.normalize() return doc @@ -116,9 +117,11 @@ self.group_id = "0" self.name = "" self.role = "GM" + ## Soon to be removed self.ROLE_GM = "GM" self.ROLE_PLAYER = "Player" self.ROLE_LURKER = "Lurker" + ## --TaS self.ip = socket.gethostbyname(socket.gethostname()) self.remote_ip = None self.version = VERSION @@ -143,8 +146,7 @@ # Loop as long as we have a connection while( self.get_status() == MPLAY_CONNECTED ): - try: - readMsg = self.outbox.get( block=1 ) + try: readMsg = self.outbox.get( block=1 ) except Exception, text: self.log_msg( ("outbox.get() got an exception: ", text) ) @@ -217,12 +219,9 @@ # Now, send the message the the length was describing sentm = sock.send( msg ) - if self.isServer(): - self.log_msg(("data_sent", sentl+sentm)) - except socket.error, e: - self.log_msg( e ) - except Exception, e: - self.log_msg( e ) + if self.isServer(): self.log_msg(("data_sent", sentl+sentm)) + except socket.error, e: self.log_msg( e ) + except Exception, e: self.log_msg( e ) return sentm def recvData( self, sock, readSize ): @@ -270,10 +269,8 @@ # Make the peer IP address available for reference later if self.remote_ip is None: self.remote_ip = self.sock.getpeername() - except IOError, e: - self.log_msg( e ) - except Exception, e: - self.log_msg( e ) + except IOError, e: self.log_msg( e ) + except Exception, e: self.log_msg( e ) return msgData def initialize_threads(self): @@ -289,8 +286,7 @@ self.set_status(MPLAY_DISCONNECTING) self.log_msg("client stub " + self.ip +" disconnecting...") self.log_msg("closing sockets...") - try: - self.sock.shutdown( 2 ) + try: self.sock.shutdown( 2 ) except Exception, e: print "Caught exception: " + str(e) print @@ -312,8 +308,9 @@ else: return 0 def update_self_from_player(self, player): - try: - (self.name, self.ip, self.id, self.text_status, self.version, self.protocol_version, self.client_string,role) = player + try: (self.name, self.ip, self.id, + self.text_status, self.version, + self.protocol_version, self.client_string, role) = player except Exception, e: print e """ @@ -340,8 +337,6 @@ def log_msg(self,msg): if self.log_console: self.log_console(msg) -# else: -# print "message", msg def get_status(self): self.statLock.acquire() @@ -500,15 +495,17 @@ players = self.get_players() if self.players.has_key(player): for m in players: - if player == m[2]: - return m + if player == m[2]: return m return -1 def get_id(self): return self.id def get_my_info(self): - return (self.name, self.ip, self.id, self.text_status, self.version, self.protocol_version, self.client_string, self.role) + return (self.name, self.ip, self.id, + self.text_status, self.version, + self.protocol_version, self.client_string, + self.role) def is_valid_id(self,id): self.statLock.acquire() @@ -519,25 +516,21 @@ def clear_players(self,save_self=0): self.statLock.acquire() keys = self.players.keys() - for k in keys: - del self.players[k] + for k in keys: del self.players[k] self.statLock.release() def clear_groups(self): self.statLock.acquire() keys = self.groups.keys() - for k in keys: - del self.groups[k] + for k in keys: del self.groups[k] self.statLock.release() def find_role(self,id): return self.players[id].role def get_ignore_list(self): - try: - return (self.ignore_id, self.ignore_name) - except: - return (None, None) + try: return (self.ignore_id, self.ignore_name) + except: return (None, None) def toggle_ignore(self, id): for m in self.ignore_id: @@ -560,7 +553,10 @@ #--------------------------------------------------------- def set_room_pass(self,npwd,pwd=""): - self.outbox.put("<alter key=\"pwd\" val=\"" +npwd+ "\" bpw=\"" + pwd + "\" plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />") + recycle_bin = "<alter key=\"pwd\" " + recycle_bin += "val=\"" +npwd+ "\" bpw=\"" + pwd + "\" " + recycle_bin += "plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />" + self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS self.update() def set_room_name(self,name,pwd=""): @@ -591,7 +587,10 @@ e = name[loc+1:] name = b + "'" + e oldloc = loc+1 - self.outbox.put("<alter key=\"name\" val=\"" + name + "\" bpw=\"" + pwd + "\" plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />") + recycle_bin = "<alter key=\"name\" " + recycle_bin += "val=\"" + name + "\" bpw=\"" + pwd + "\" " + recycle_bin += "plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />" + self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS self.update() #--------------------------------------------------------- @@ -605,7 +604,9 @@ self.outbox.put("<role action=\"get\" player=\"" + self.id +"\" group_id=\""+self.group_id + "\" />") def set_role(self,player,role,pwd=""): - self.outbox.put("<role action=\"set\" player=\"" + player + "\" role=\"" +role+ "\" boot_pwd=\"" + pwd + "\" group_id=\"" + self.group_id + "\" />") + recycle_bin = "<role action=\"set\" player=\"" + player + "\" " + recycle_bin += "role=\"" +role+ "\" boot_pwd=\"" + pwd + "\" group_id=\"" + self.group_id + "\" />" + self.outbox.put(recycle_bin); del recycle_bin #makes line easer to read. --TaS self.update() def send(self,msg,player="all"): @@ -619,12 +620,13 @@ self.check_my_status() def send_create_group(self,name,pwd,boot_pwd,minversion): - self.outbox.put("<create_group from=\""+self.id+"\" pwd=\""+pwd+"\" name=\""+ - name+"\" boot_pwd=\""+boot_pwd+"\" min_version=\"" + minversion +"\" />") + recycle_bin = "<create_group from=\""+self.id+"\" " + recycle_bin += "pwd=\""+pwd+"\" name=\""+ name+"\" boot_pwd=\""+boot_pwd+"\" " + recycle_bin += "min_version=\"" + minversion +"\" />" + self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS def send_join_group(self,group_id,pwd): - if (group_id != 0): - self.update_role("Lurker") + if (group_id != 0): self.update_role("Lurker") self.outbox.put("<join_group from=\""+self.id+"\" pwd=\""+pwd+"\" group_id=\""+str(group_id)+"\" />") def poll(self, evt=None): @@ -634,8 +636,7 @@ if self.get_status() != MPLAY_CONNECTED: self.check_my_status() else: - try: - self.pretranslate(msg) + try: self.pretranslate(msg) except Exception, e: print "The following message: " + str(msg) print "created the following exception: " @@ -644,16 +645,13 @@ def add_msg_handler(self, tag, function, core=False): if not self.msg_handlers.has_key(tag): self.msg_handlers[tag] = function - if core: - self.core_msg_handlers.append(tag) - else: - print 'XML Messages ' + tag + ' already has a handler' + if core: self.core_msg_handlers.append(tag) + else: print 'XML Messages ' + tag + ' already has a handler' def remove_msg_handler(self, tag): if self.msg_handlers.has_key(tag) and not tag in self.core_msg_handlers: del self.msg_handlers[tag] - else: - print 'XML Messages ' + tag + ' already deleted' + else: print 'XML Messages ' + tag + ' already deleted' def load_core_msg_handlers(self): self.add_msg_handler('msg', self.on_msg, True) @@ -667,8 +665,7 @@ def pretranslate(self,data): # Pre-qualify our data. If we don't have atleast 5-bytes, then there is # no way we even have a valid message! - if len(data) < 5: - return + if len(data) < 5: return end = data.find(">") head = data[:end+1] msg = data[end+1:] @@ -676,10 +673,8 @@ xml_dom = xml_dom._get_documentElement() tag_name = xml_dom._get_tagName() id = xml_dom.getAttribute("from") - if id == '': - id = xml_dom.getAttribute("id") - if self.msg_handlers.has_key(tag_name): - self.msg_handlers[tag_name](id, data, xml_dom) + if id == '': id = xml_dom.getAttribute("id") + if self.msg_handlers.has_key(tag_name): self.msg_handlers[tag_name](id, data, xml_dom) else: #Unknown messages recived ignoring #using pass insted or printing an error message @@ -687,8 +682,7 @@ #if someone is using a plugin to send messages and this user does not #have the plugin they would be getting errors pass - if xml_dom: - xml_dom.unlink() + if xml_dom: xml_dom.unlink() def on_sound(self, id, data, xml_dom): (ignore_id,ignore_name) = self.get_ignore_list() @@ -710,10 +704,8 @@ self.on_receive(msg,None) # None get's interpreted in on_receive as the sys admin. # Doing it this way makes it harder to impersonate the admin else: - if self.is_valid_id(id): - self.on_receive(msg,self.players[id]) - if xml_dom: - xml_dom.unlink() + if self.is_valid_id(id): self.on_receive(msg,self.players[id]) + if xml_dom: xml_dom.unlink() def on_ping(self, id, msg, xml_dom): #a REAL ping time implementation by Snowdog 8/03 @@ -728,8 +720,7 @@ latency = float( latency) / 10.0 ping_msg = "Ping Results: " + str(latency) + " ms (parsed message, round trip)" self.on_receive(ping_msg,None) - if xml_dom: - xml_dom.unlink() + if xml_dom: xml_dom.unlink() def on_group(self, id, msg, xml_dom): name = xml_dom.getAttribute("name") @@ -747,8 +738,7 @@ elif act == 'update': self.groups[id] = group_data self.on_group_event(mplay_event(GROUP_UPDATE, group_data)) - if xml_dom: - xml_dom.unlink() + if xml_dom: xml_dom.unlink() def on_role(self, id, msg, xml_dom): act = xml_dom.getAttribute("action") @@ -759,13 +749,10 @@ if id == self.id: self.players[id] = (a,b,c,d,e,f,g,role) self.update_role(role) - else: - self.players[id] = (a,b,c,d,e,f,g,role) + else: self.players[id] = (a,b,c,d,e,f,g,role) self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id])) - except: - pass - if xml_dom: - xml_dom.unlink() + except: pass + if xml_dom: xml_dom.unlink() def on_player(self, id, msg, xml_dom): act = xml_dom.getAttribute("action") @@ -775,42 +762,39 @@ version = xml_dom.getAttribute("version") protocol_version = xml_dom.getAttribute("protocol_version") client_string = xml_dom.getAttribute("client_string") - try: - player = (name,ip,id,status,version,protocol_version,client_string,self.players[id][7]) + try: player = (name, ip, id, status, + version, protocol_version, + client_string, self.players[id][7]) except Exception, e: - player = (name,ip,id,status,version,protocol_version,client_string,"Player") + player = (name, ip, id, status, + version, protocol_version, + client_string, "Player") if act == "new": self.players[id] = player - self.on_player_event(mplay_event(PLAYER_NEW,self.players[id])) + self.on_player_event(mplay_event(PLAYER_NEW, self.players[id])) elif act == "group": self.group_id = xml_dom.getAttribute("group_id") self.clear_players() - self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE,self.groups[self.group_id])) + self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE, self.groups[self.group_id])) self.players[self.id] = self.get_my_info() #(self.name,self.ip,self.id,self.text_status) - self.on_player_event(mplay_event(PLAYER_NEW,self.players[self.id])) + self.on_player_event(mplay_event(PLAYER_NEW, self.players[self.id])) elif act == "failed": self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE_F)) elif act == "del": self.on_player_event(mplay_event(PLAYER_DEL,self.players[id])) - if self.players.has_key(id): - del self.players[id] - if id == self.id: - self.do_disconnect() + if self.players.has_key(id): del self.players[id] + if id == self.id: self.do_disconnect() # the next two cases handle the events that are used to let you know when others are typing elif act == "update": if id == self.id: self.players[id] = player self.update_self_from_player(player) - else: - self.players[id] = player + else: self.players[id] = player dont_send = 0 for m in self.ignore_id: - if m == id: - dont_send=1 - if dont_send != 1: - self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id])) - if xml_dom: - xml_dom.unlink() + if m == id: dont_send=1 + if dont_send != 1: self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id])) + if xml_dom: xml_dom.unlink() def on_password(self, id, msg, xml_dom): signal = type = id = data = None @@ -824,8 +808,7 @@ def check_my_status(self): status = self.get_status() - if status == MPLAY_DISCONNECTING: - self.do_disconnect() + if status == MPLAY_DISCONNECTING: self.do_disconnect() def connect(self, addressport): """Establish a connection to a server while still using sendThread & recvThread for its @@ -862,31 +845,29 @@ self.compressionType = bz2 elif cmpZLIB and xml_dom.getAttribute('cmpType') == 'zlib': self.compressionType = zlib - else: - self.compressionType = None - else: - self.compressionType = bz2 + else: self.compressionType = None + else: self.compressionType = bz2 #send confirmation self.sendMsg( self.sock, self.toxml("new") ) except Exception, e: self.log_msg(e) - if xml_dom: - xml_dom.unlink() + if xml_dom: xml_dom.unlink() return 0 # Start things rollings along self.initialize_threads() self.on_mplay_event(mplay_event(MPLAY_CONNECTED)) - self.players[self.id] = (self.name,self.ip,self.id,self.text_status,self.version,self.protocol_version,self.client_string,self.role) + self.players[self.id] = (self.name, self.ip, self.id, + self.text_status, self.version, + self.protocol_version, self.client_string, self.role) self.on_player_event(mplay_event(PLAYER_NEW,self.players[self.id])) - if xml_dom: - xml_dom.unlink() + if xml_dom: xml_dom.unlink() return 1 def start_disconnect(self): self.on_mplay_event(mplay_event(MPLAY_DISCONNECTING)) self.outbox.put( self.toxml("del") ) - ## Client Side Disconect Forced -- Snowdog 10-09-2003 + ## Client Side Disconect Forced -- Snowdog 10-09-2003 #pause to allow GUI events time to sync. time.sleep(1) self.do_disconnect() @@ -906,4 +887,3 @@ self.unique_cookie += 1 return_str = self.id + "-" + str(self.unique_cookie) return return_str -