comparison 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
comparison
equal deleted inserted replaced
70:52a5fa913008 71:449a8900f9ac
40 from string import * 40 from string import *
41 from orpg.orpg_version import CLIENT_STRING, PROTOCOL_VERSION, VERSION 41 from orpg.orpg_version import CLIENT_STRING, PROTOCOL_VERSION, VERSION
42 import errno 42 import errno
43 import os 43 import os
44 import time 44 import time
45 from orpg.orpgCore import * 45 from orpg.orpgCore import component
46 from orpg.orpg_xml import xml
46 47
47 try: 48 try:
48 import bz2 49 import bz2
49 cmpBZ2 = True 50 cmpBZ2 = True
50 except: cmpBZ2 = False 51 except: cmpBZ2 = False
80 STATUS_SET_URL = 1 81 STATUS_SET_URL = 1
81 82
82 def parseXml(data): 83 def parseXml(data):
83 "parse and return doc" 84 "parse and return doc"
84 #print data 85 #print data
85 doc = component.get('xml').parseXml(data) 86 doc = xml.parseXml(data)
86 doc.normalize() 87 doc.normalize()
87 return doc 88 return doc
88 89
89 def myescape(data): 90 def myescape(data):
90 return escape(data,{"\"":""}) 91 return escape(data,{"\"":""})
114 self.recvThreadExitEvent = Event() 115 self.recvThreadExitEvent = Event()
115 self.id = "0" 116 self.id = "0"
116 self.group_id = "0" 117 self.group_id = "0"
117 self.name = "" 118 self.name = ""
118 self.role = "GM" 119 self.role = "GM"
120 ## Soon to be removed
119 self.ROLE_GM = "GM" 121 self.ROLE_GM = "GM"
120 self.ROLE_PLAYER = "Player" 122 self.ROLE_PLAYER = "Player"
121 self.ROLE_LURKER = "Lurker" 123 self.ROLE_LURKER = "Lurker"
124 ## --TaS
122 self.ip = socket.gethostbyname(socket.gethostname()) 125 self.ip = socket.gethostbyname(socket.gethostname())
123 self.remote_ip = None 126 self.remote_ip = None
124 self.version = VERSION 127 self.version = VERSION
125 self.protocol_version = PROTOCOL_VERSION 128 self.protocol_version = PROTOCOL_VERSION
126 self.client_string = CLIENT_STRING 129 self.client_string = CLIENT_STRING
141 # Wait to be told it's okay to start running 144 # Wait to be told it's okay to start running
142 self.startedEvent.wait() 145 self.startedEvent.wait()
143 146
144 # Loop as long as we have a connection 147 # Loop as long as we have a connection
145 while( self.get_status() == MPLAY_CONNECTED ): 148 while( self.get_status() == MPLAY_CONNECTED ):
146 try: 149 try: readMsg = self.outbox.get( block=1 )
147 readMsg = self.outbox.get( block=1 )
148 except Exception, text: 150 except Exception, text:
149 self.log_msg( ("outbox.get() got an exception: ", text) ) 151 self.log_msg( ("outbox.get() got an exception: ", text) )
150 152
151 # If we are here, it's because we have data to send, no doubt! 153 # If we are here, it's because we have data to send, no doubt!
152 if self.status == MPLAY_CONNECTED: 154 if self.status == MPLAY_CONNECTED:
215 # Send the encoded length 217 # Send the encoded length
216 sentl = sock.send( lp ) 218 sentl = sock.send( lp )
217 219
218 # Now, send the message the the length was describing 220 # Now, send the message the the length was describing
219 sentm = sock.send( msg ) 221 sentm = sock.send( msg )
220 if self.isServer(): 222 if self.isServer(): self.log_msg(("data_sent", sentl+sentm))
221 self.log_msg(("data_sent", sentl+sentm)) 223 except socket.error, e: self.log_msg( e )
222 except socket.error, e: 224 except Exception, e: self.log_msg( e )
223 self.log_msg( e )
224 except Exception, e:
225 self.log_msg( e )
226 return sentm 225 return sentm
227 226
228 def recvData( self, sock, readSize ): 227 def recvData( self, sock, readSize ):
229 """Simple socket receive method. This method will only return when the exact 228 """Simple socket receive method. This method will only return when the exact
230 byte count has been read from the connection, if remote terminates our 229 byte count has been read from the connection, if remote terminates our
268 if self.isServer(): 267 if self.isServer():
269 self.log_msg(("data_recv", length+4)) 268 self.log_msg(("data_recv", length+4))
270 # Make the peer IP address available for reference later 269 # Make the peer IP address available for reference later
271 if self.remote_ip is None: 270 if self.remote_ip is None:
272 self.remote_ip = self.sock.getpeername() 271 self.remote_ip = self.sock.getpeername()
273 except IOError, e: 272 except IOError, e: self.log_msg( e )
274 self.log_msg( e ) 273 except Exception, e: self.log_msg( e )
275 except Exception, e:
276 self.log_msg( e )
277 return msgData 274 return msgData
278 275
279 def initialize_threads(self): 276 def initialize_threads(self):
280 "Starts up our threads (2) and waits for them to make sure they are running!" 277 "Starts up our threads (2) and waits for them to make sure they are running!"
281 self.status = MPLAY_CONNECTED 278 self.status = MPLAY_CONNECTED
287 284
288 def disconnect(self): 285 def disconnect(self):
289 self.set_status(MPLAY_DISCONNECTING) 286 self.set_status(MPLAY_DISCONNECTING)
290 self.log_msg("client stub " + self.ip +" disconnecting...") 287 self.log_msg("client stub " + self.ip +" disconnecting...")
291 self.log_msg("closing sockets...") 288 self.log_msg("closing sockets...")
292 try: 289 try: self.sock.shutdown( 2 )
293 self.sock.shutdown( 2 )
294 except Exception, e: 290 except Exception, e:
295 print "Caught exception: " + str(e) 291 print "Caught exception: " + str(e)
296 print 292 print
297 print "Continuing" 293 print "Continuing"
298 self.set_status(MPLAY_DISCONNECTED) 294 self.set_status(MPLAY_DISCONNECTED)
310 if self.useroles: 306 if self.useroles:
311 return 1 307 return 1
312 else: 308 else:
313 return 0 309 return 0
314 def update_self_from_player(self, player): 310 def update_self_from_player(self, player):
315 try: 311 try: (self.name, self.ip, self.id,
316 (self.name, self.ip, self.id, self.text_status, self.version, self.protocol_version, self.client_string,role) = player 312 self.text_status, self.version,
313 self.protocol_version, self.client_string, role) = player
317 except Exception, e: 314 except Exception, e:
318 print e 315 print e
319 """ 316 """
320 The IP field should really be deprecated as too many systems are NAT'd and/or behind firewalls for a 317 The IP field should really be deprecated as too many systems are NAT'd and/or behind firewalls for a
321 client provided IP address to have much value. As such, we now label it as deprecated. 318 client provided IP address to have much value. As such, we now label it as deprecated.
338 return xml_data 335 return xml_data
339 336
340 def log_msg(self,msg): 337 def log_msg(self,msg):
341 if self.log_console: 338 if self.log_console:
342 self.log_console(msg) 339 self.log_console(msg)
343 # else:
344 # print "message", msg
345 340
346 def get_status(self): 341 def get_status(self):
347 self.statLock.acquire() 342 self.statLock.acquire()
348 status = self.status 343 status = self.status
349 self.statLock.release() 344 self.statLock.release()
498 493
499 def get_player_by_player_id(self,player): 494 def get_player_by_player_id(self,player):
500 players = self.get_players() 495 players = self.get_players()
501 if self.players.has_key(player): 496 if self.players.has_key(player):
502 for m in players: 497 for m in players:
503 if player == m[2]: 498 if player == m[2]: return m
504 return m
505 return -1 499 return -1
506 500
507 def get_id(self): 501 def get_id(self):
508 return self.id 502 return self.id
509 503
510 def get_my_info(self): 504 def get_my_info(self):
511 return (self.name, self.ip, self.id, self.text_status, self.version, self.protocol_version, self.client_string, self.role) 505 return (self.name, self.ip, self.id,
506 self.text_status, self.version,
507 self.protocol_version, self.client_string,
508 self.role)
512 509
513 def is_valid_id(self,id): 510 def is_valid_id(self,id):
514 self.statLock.acquire() 511 self.statLock.acquire()
515 value = self.players.has_key( id ) 512 value = self.players.has_key( id )
516 self.statLock.release() 513 self.statLock.release()
517 return value 514 return value
518 515
519 def clear_players(self,save_self=0): 516 def clear_players(self,save_self=0):
520 self.statLock.acquire() 517 self.statLock.acquire()
521 keys = self.players.keys() 518 keys = self.players.keys()
522 for k in keys: 519 for k in keys: del self.players[k]
523 del self.players[k]
524 self.statLock.release() 520 self.statLock.release()
525 521
526 def clear_groups(self): 522 def clear_groups(self):
527 self.statLock.acquire() 523 self.statLock.acquire()
528 keys = self.groups.keys() 524 keys = self.groups.keys()
529 for k in keys: 525 for k in keys: del self.groups[k]
530 del self.groups[k]
531 self.statLock.release() 526 self.statLock.release()
532 527
533 def find_role(self,id): 528 def find_role(self,id):
534 return self.players[id].role 529 return self.players[id].role
535 530
536 def get_ignore_list(self): 531 def get_ignore_list(self):
537 try: 532 try: return (self.ignore_id, self.ignore_name)
538 return (self.ignore_id, self.ignore_name) 533 except: return (None, None)
539 except:
540 return (None, None)
541 534
542 def toggle_ignore(self, id): 535 def toggle_ignore(self, id):
543 for m in self.ignore_id: 536 for m in self.ignore_id:
544 if `self.ignore_id[self.ignore_id.index(m)]` == `id`: 537 if `self.ignore_id[self.ignore_id.index(m)]` == `id`:
545 name = self.ignore_name[self.ignore_id.index(m)] 538 name = self.ignore_name[self.ignore_id.index(m)]
558 #--------------------------------------------------------- 551 #---------------------------------------------------------
559 # [START] Snowdog Password/Room Name altering code 12/02 552 # [START] Snowdog Password/Room Name altering code 12/02
560 #--------------------------------------------------------- 553 #---------------------------------------------------------
561 554
562 def set_room_pass(self,npwd,pwd=""): 555 def set_room_pass(self,npwd,pwd=""):
563 self.outbox.put("<alter key=\"pwd\" val=\"" +npwd+ "\" bpw=\"" + pwd + "\" plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />") 556 recycle_bin = "<alter key=\"pwd\" "
557 recycle_bin += "val=\"" +npwd+ "\" bpw=\"" + pwd + "\" "
558 recycle_bin += "plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />"
559 self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS
564 self.update() 560 self.update()
565 561
566 def set_room_name(self,name,pwd=""): 562 def set_room_name(self,name,pwd=""):
567 loc = name.find("&") 563 loc = name.find("&")
568 oldloc=0 564 oldloc=0
589 if loc > -1: 585 if loc > -1:
590 b = name[:loc] 586 b = name[:loc]
591 e = name[loc+1:] 587 e = name[loc+1:]
592 name = b + "&#39;" + e 588 name = b + "&#39;" + e
593 oldloc = loc+1 589 oldloc = loc+1
594 self.outbox.put("<alter key=\"name\" val=\"" + name + "\" bpw=\"" + pwd + "\" plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />") 590 recycle_bin = "<alter key=\"name\" "
591 recycle_bin += "val=\"" + name + "\" bpw=\"" + pwd + "\" "
592 recycle_bin += "plr=\"" + self.id +"\" gid=\"" + self.group_id + "\" />"
593 self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS
595 self.update() 594 self.update()
596 595
597 #--------------------------------------------------------- 596 #---------------------------------------------------------
598 # [END] Snowdog Password/Room Name altering code 12/02 597 # [END] Snowdog Password/Room Name altering code 12/02
599 #--------------------------------------------------------- 598 #---------------------------------------------------------
603 602
604 def get_role(self): 603 def get_role(self):
605 self.outbox.put("<role action=\"get\" player=\"" + self.id +"\" group_id=\""+self.group_id + "\" />") 604 self.outbox.put("<role action=\"get\" player=\"" + self.id +"\" group_id=\""+self.group_id + "\" />")
606 605
607 def set_role(self,player,role,pwd=""): 606 def set_role(self,player,role,pwd=""):
608 self.outbox.put("<role action=\"set\" player=\"" + player + "\" role=\"" +role+ "\" boot_pwd=\"" + pwd + "\" group_id=\"" + self.group_id + "\" />") 607 recycle_bin = "<role action=\"set\" player=\"" + player + "\" "
608 recycle_bin += "role=\"" +role+ "\" boot_pwd=\"" + pwd + "\" group_id=\"" + self.group_id + "\" />"
609 self.outbox.put(recycle_bin); del recycle_bin #makes line easer to read. --TaS
609 self.update() 610 self.update()
610 611
611 def send(self,msg,player="all"): 612 def send(self,msg,player="all"):
612 if self.status == MPLAY_CONNECTED and player != self.id: 613 if self.status == MPLAY_CONNECTED and player != self.id:
613 self.outbox.put("<msg to='"+player+"' from='"+self.id+"' group_id='"+self.group_id+"' />"+msg) 614 self.outbox.put("<msg to='"+player+"' from='"+self.id+"' group_id='"+self.group_id+"' />"+msg)
617 if self.status == MPLAY_CONNECTED: 618 if self.status == MPLAY_CONNECTED:
618 self.outbox.put(snd_xml) 619 self.outbox.put(snd_xml)
619 self.check_my_status() 620 self.check_my_status()
620 621
621 def send_create_group(self,name,pwd,boot_pwd,minversion): 622 def send_create_group(self,name,pwd,boot_pwd,minversion):
622 self.outbox.put("<create_group from=\""+self.id+"\" pwd=\""+pwd+"\" name=\""+ 623 recycle_bin = "<create_group from=\""+self.id+"\" "
623 name+"\" boot_pwd=\""+boot_pwd+"\" min_version=\"" + minversion +"\" />") 624 recycle_bin += "pwd=\""+pwd+"\" name=\""+ name+"\" boot_pwd=\""+boot_pwd+"\" "
625 recycle_bin += "min_version=\"" + minversion +"\" />"
626 self.outbox.put(recycle_bin); del recycle_bin #makes line easier to read. --TaS
624 627
625 def send_join_group(self,group_id,pwd): 628 def send_join_group(self,group_id,pwd):
626 if (group_id != 0): 629 if (group_id != 0): self.update_role("Lurker")
627 self.update_role("Lurker")
628 self.outbox.put("<join_group from=\""+self.id+"\" pwd=\""+pwd+"\" group_id=\""+str(group_id)+"\" />") 630 self.outbox.put("<join_group from=\""+self.id+"\" pwd=\""+pwd+"\" group_id=\""+str(group_id)+"\" />")
629 631
630 def poll(self, evt=None): 632 def poll(self, evt=None):
631 try: 633 try:
632 msg = self.inbox.get_nowait() 634 msg = self.inbox.get_nowait()
633 except: 635 except:
634 if self.get_status() != MPLAY_CONNECTED: 636 if self.get_status() != MPLAY_CONNECTED:
635 self.check_my_status() 637 self.check_my_status()
636 else: 638 else:
637 try: 639 try: self.pretranslate(msg)
638 self.pretranslate(msg)
639 except Exception, e: 640 except Exception, e:
640 print "The following message: " + str(msg) 641 print "The following message: " + str(msg)
641 print "created the following exception: " 642 print "created the following exception: "
642 traceback.print_exc() 643 traceback.print_exc()
643 644
644 def add_msg_handler(self, tag, function, core=False): 645 def add_msg_handler(self, tag, function, core=False):
645 if not self.msg_handlers.has_key(tag): 646 if not self.msg_handlers.has_key(tag):
646 self.msg_handlers[tag] = function 647 self.msg_handlers[tag] = function
647 if core: 648 if core: self.core_msg_handlers.append(tag)
648 self.core_msg_handlers.append(tag) 649 else: print 'XML Messages ' + tag + ' already has a handler'
649 else:
650 print 'XML Messages ' + tag + ' already has a handler'
651 650
652 def remove_msg_handler(self, tag): 651 def remove_msg_handler(self, tag):
653 if self.msg_handlers.has_key(tag) and not tag in self.core_msg_handlers: 652 if self.msg_handlers.has_key(tag) and not tag in self.core_msg_handlers:
654 del self.msg_handlers[tag] 653 del self.msg_handlers[tag]
655 else: 654 else: print 'XML Messages ' + tag + ' already deleted'
656 print 'XML Messages ' + tag + ' already deleted'
657 655
658 def load_core_msg_handlers(self): 656 def load_core_msg_handlers(self):
659 self.add_msg_handler('msg', self.on_msg, True) 657 self.add_msg_handler('msg', self.on_msg, True)
660 self.add_msg_handler('ping', self.on_ping, True) 658 self.add_msg_handler('ping', self.on_ping, True)
661 self.add_msg_handler('group', self.on_group, True) 659 self.add_msg_handler('group', self.on_group, True)
665 self.add_msg_handler('sound', self.on_sound, True) 663 self.add_msg_handler('sound', self.on_sound, True)
666 664
667 def pretranslate(self,data): 665 def pretranslate(self,data):
668 # Pre-qualify our data. If we don't have atleast 5-bytes, then there is 666 # Pre-qualify our data. If we don't have atleast 5-bytes, then there is
669 # no way we even have a valid message! 667 # no way we even have a valid message!
670 if len(data) < 5: 668 if len(data) < 5: return
671 return
672 end = data.find(">") 669 end = data.find(">")
673 head = data[:end+1] 670 head = data[:end+1]
674 msg = data[end+1:] 671 msg = data[end+1:]
675 xml_dom = self.xml.parseXml(head) 672 xml_dom = self.xml.parseXml(head)
676 xml_dom = xml_dom._get_documentElement() 673 xml_dom = xml_dom._get_documentElement()
677 tag_name = xml_dom._get_tagName() 674 tag_name = xml_dom._get_tagName()
678 id = xml_dom.getAttribute("from") 675 id = xml_dom.getAttribute("from")
679 if id == '': 676 if id == '': id = xml_dom.getAttribute("id")
680 id = xml_dom.getAttribute("id") 677 if self.msg_handlers.has_key(tag_name): self.msg_handlers[tag_name](id, data, xml_dom)
681 if self.msg_handlers.has_key(tag_name):
682 self.msg_handlers[tag_name](id, data, xml_dom)
683 else: 678 else:
684 #Unknown messages recived ignoring 679 #Unknown messages recived ignoring
685 #using pass insted or printing an error message 680 #using pass insted or printing an error message
686 #because plugins should now be able to send and proccess messages 681 #because plugins should now be able to send and proccess messages
687 #if someone is using a plugin to send messages and this user does not 682 #if someone is using a plugin to send messages and this user does not
688 #have the plugin they would be getting errors 683 #have the plugin they would be getting errors
689 pass 684 pass
690 if xml_dom: 685 if xml_dom: xml_dom.unlink()
691 xml_dom.unlink()
692 686
693 def on_sound(self, id, data, xml_dom): 687 def on_sound(self, id, data, xml_dom):
694 (ignore_id,ignore_name) = self.get_ignore_list() 688 (ignore_id,ignore_name) = self.get_ignore_list()
695 for m in ignore_id: 689 for m in ignore_id:
696 if m == id: 690 if m == id:
708 msg = data[end+1:] 702 msg = data[end+1:]
709 if id == "0": 703 if id == "0":
710 self.on_receive(msg,None) # None get's interpreted in on_receive as the sys admin. 704 self.on_receive(msg,None) # None get's interpreted in on_receive as the sys admin.
711 # Doing it this way makes it harder to impersonate the admin 705 # Doing it this way makes it harder to impersonate the admin
712 else: 706 else:
713 if self.is_valid_id(id): 707 if self.is_valid_id(id): self.on_receive(msg,self.players[id])
714 self.on_receive(msg,self.players[id]) 708 if xml_dom: xml_dom.unlink()
715 if xml_dom:
716 xml_dom.unlink()
717 709
718 def on_ping(self, id, msg, xml_dom): 710 def on_ping(self, id, msg, xml_dom):
719 #a REAL ping time implementation by Snowdog 8/03 711 #a REAL ping time implementation by Snowdog 8/03
720 # recieves special server <ping time="###" /> command 712 # recieves special server <ping time="###" /> command
721 # where ### is a returning time from the clients ping command 713 # where ### is a returning time from the clients ping command
726 latency = float(float(ct) - float(ot)) 718 latency = float(float(ct) - float(ot))
727 latency = int( latency * 10000.0 ) 719 latency = int( latency * 10000.0 )
728 latency = float( latency) / 10.0 720 latency = float( latency) / 10.0
729 ping_msg = "Ping Results: " + str(latency) + " ms (parsed message, round trip)" 721 ping_msg = "Ping Results: " + str(latency) + " ms (parsed message, round trip)"
730 self.on_receive(ping_msg,None) 722 self.on_receive(ping_msg,None)
731 if xml_dom: 723 if xml_dom: xml_dom.unlink()
732 xml_dom.unlink()
733 724
734 def on_group(self, id, msg, xml_dom): 725 def on_group(self, id, msg, xml_dom):
735 name = xml_dom.getAttribute("name") 726 name = xml_dom.getAttribute("name")
736 players = xml_dom.getAttribute("players") 727 players = xml_dom.getAttribute("players")
737 act = xml_dom.getAttribute("action") 728 act = xml_dom.getAttribute("action")
745 del self.groups[id] 736 del self.groups[id]
746 self.on_group_event(mplay_event(GROUP_DEL, group_data)) 737 self.on_group_event(mplay_event(GROUP_DEL, group_data))
747 elif act == 'update': 738 elif act == 'update':
748 self.groups[id] = group_data 739 self.groups[id] = group_data
749 self.on_group_event(mplay_event(GROUP_UPDATE, group_data)) 740 self.on_group_event(mplay_event(GROUP_UPDATE, group_data))
750 if xml_dom: 741 if xml_dom: xml_dom.unlink()
751 xml_dom.unlink()
752 742
753 def on_role(self, id, msg, xml_dom): 743 def on_role(self, id, msg, xml_dom):
754 act = xml_dom.getAttribute("action") 744 act = xml_dom.getAttribute("action")
755 role = xml_dom.getAttribute("role") 745 role = xml_dom.getAttribute("role")
756 if (act == "set") or (act == "update"): 746 if (act == "set") or (act == "update"):
757 try: 747 try:
758 (a,b,c,d,e,f,g,h) = self.players[id] 748 (a,b,c,d,e,f,g,h) = self.players[id]
759 if id == self.id: 749 if id == self.id:
760 self.players[id] = (a,b,c,d,e,f,g,role) 750 self.players[id] = (a,b,c,d,e,f,g,role)
761 self.update_role(role) 751 self.update_role(role)
762 else: 752 else: self.players[id] = (a,b,c,d,e,f,g,role)
763 self.players[id] = (a,b,c,d,e,f,g,role)
764 self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id])) 753 self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id]))
765 except: 754 except: pass
766 pass 755 if xml_dom: xml_dom.unlink()
767 if xml_dom:
768 xml_dom.unlink()
769 756
770 def on_player(self, id, msg, xml_dom): 757 def on_player(self, id, msg, xml_dom):
771 act = xml_dom.getAttribute("action") 758 act = xml_dom.getAttribute("action")
772 ip = xml_dom.getAttribute("ip") 759 ip = xml_dom.getAttribute("ip")
773 name = xml_dom.getAttribute("name") 760 name = xml_dom.getAttribute("name")
774 status = xml_dom.getAttribute("status") 761 status = xml_dom.getAttribute("status")
775 version = xml_dom.getAttribute("version") 762 version = xml_dom.getAttribute("version")
776 protocol_version = xml_dom.getAttribute("protocol_version") 763 protocol_version = xml_dom.getAttribute("protocol_version")
777 client_string = xml_dom.getAttribute("client_string") 764 client_string = xml_dom.getAttribute("client_string")
778 try: 765 try: player = (name, ip, id, status,
779 player = (name,ip,id,status,version,protocol_version,client_string,self.players[id][7]) 766 version, protocol_version,
767 client_string, self.players[id][7])
780 except Exception, e: 768 except Exception, e:
781 player = (name,ip,id,status,version,protocol_version,client_string,"Player") 769 player = (name, ip, id, status,
770 version, protocol_version,
771 client_string, "Player")
782 if act == "new": 772 if act == "new":
783 self.players[id] = player 773 self.players[id] = player
784 self.on_player_event(mplay_event(PLAYER_NEW,self.players[id])) 774 self.on_player_event(mplay_event(PLAYER_NEW, self.players[id]))
785 elif act == "group": 775 elif act == "group":
786 self.group_id = xml_dom.getAttribute("group_id") 776 self.group_id = xml_dom.getAttribute("group_id")
787 self.clear_players() 777 self.clear_players()
788 self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE,self.groups[self.group_id])) 778 self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE, self.groups[self.group_id]))
789 self.players[self.id] = self.get_my_info() #(self.name,self.ip,self.id,self.text_status) 779 self.players[self.id] = self.get_my_info() #(self.name,self.ip,self.id,self.text_status)
790 self.on_player_event(mplay_event(PLAYER_NEW,self.players[self.id])) 780 self.on_player_event(mplay_event(PLAYER_NEW, self.players[self.id]))
791 elif act == "failed": 781 elif act == "failed":
792 self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE_F)) 782 self.on_mplay_event(mplay_event(MPLAY_GROUP_CHANGE_F))
793 elif act == "del": 783 elif act == "del":
794 self.on_player_event(mplay_event(PLAYER_DEL,self.players[id])) 784 self.on_player_event(mplay_event(PLAYER_DEL,self.players[id]))
795 if self.players.has_key(id): 785 if self.players.has_key(id): del self.players[id]
796 del self.players[id] 786 if id == self.id: self.do_disconnect()
797 if id == self.id:
798 self.do_disconnect()
799 # the next two cases handle the events that are used to let you know when others are typing 787 # the next two cases handle the events that are used to let you know when others are typing
800 elif act == "update": 788 elif act == "update":
801 if id == self.id: 789 if id == self.id:
802 self.players[id] = player 790 self.players[id] = player
803 self.update_self_from_player(player) 791 self.update_self_from_player(player)
804 else: 792 else: self.players[id] = player
805 self.players[id] = player
806 dont_send = 0 793 dont_send = 0
807 for m in self.ignore_id: 794 for m in self.ignore_id:
808 if m == id: 795 if m == id: dont_send=1
809 dont_send=1 796 if dont_send != 1: self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id]))
810 if dont_send != 1: 797 if xml_dom: xml_dom.unlink()
811 self.on_player_event(mplay_event(PLAYER_UPDATE,self.players[id]))
812 if xml_dom:
813 xml_dom.unlink()
814 798
815 def on_password(self, id, msg, xml_dom): 799 def on_password(self, id, msg, xml_dom):
816 signal = type = id = data = None 800 signal = type = id = data = None
817 id = xml_dom.getAttribute("id") 801 id = xml_dom.getAttribute("id")
818 type = xml_dom.getAttribute("type") 802 type = xml_dom.getAttribute("type")
822 if xml_dom: 806 if xml_dom:
823 xml_dom.unlink() 807 xml_dom.unlink()
824 808
825 def check_my_status(self): 809 def check_my_status(self):
826 status = self.get_status() 810 status = self.get_status()
827 if status == MPLAY_DISCONNECTING: 811 if status == MPLAY_DISCONNECTING: self.do_disconnect()
828 self.do_disconnect()
829 812
830 def connect(self, addressport): 813 def connect(self, addressport):
831 """Establish a connection to a server while still using sendThread & recvThread for its 814 """Establish a connection to a server while still using sendThread & recvThread for its
832 communication.""" 815 communication."""
833 if self.is_connected(): 816 if self.is_connected():
860 if xml_dom.hasAttribute('cmpType'): 843 if xml_dom.hasAttribute('cmpType'):
861 if cmpBZ2 and xml_dom.getAttribute('cmpType') == 'bz2': 844 if cmpBZ2 and xml_dom.getAttribute('cmpType') == 'bz2':
862 self.compressionType = bz2 845 self.compressionType = bz2
863 elif cmpZLIB and xml_dom.getAttribute('cmpType') == 'zlib': 846 elif cmpZLIB and xml_dom.getAttribute('cmpType') == 'zlib':
864 self.compressionType = zlib 847 self.compressionType = zlib
865 else: 848 else: self.compressionType = None
866 self.compressionType = None 849 else: self.compressionType = bz2
867 else:
868 self.compressionType = bz2
869 #send confirmation 850 #send confirmation
870 self.sendMsg( self.sock, self.toxml("new") ) 851 self.sendMsg( self.sock, self.toxml("new") )
871 except Exception, e: 852 except Exception, e:
872 self.log_msg(e) 853 self.log_msg(e)
873 if xml_dom: 854 if xml_dom: xml_dom.unlink()
874 xml_dom.unlink()
875 return 0 855 return 0
876 856
877 # Start things rollings along 857 # Start things rollings along
878 self.initialize_threads() 858 self.initialize_threads()
879 self.on_mplay_event(mplay_event(MPLAY_CONNECTED)) 859 self.on_mplay_event(mplay_event(MPLAY_CONNECTED))
880 self.players[self.id] = (self.name,self.ip,self.id,self.text_status,self.version,self.protocol_version,self.client_string,self.role) 860 self.players[self.id] = (self.name, self.ip, self.id,
861 self.text_status, self.version,
862 self.protocol_version, self.client_string, self.role)
881 self.on_player_event(mplay_event(PLAYER_NEW,self.players[self.id])) 863 self.on_player_event(mplay_event(PLAYER_NEW,self.players[self.id]))
882 if xml_dom: 864 if xml_dom: xml_dom.unlink()
883 xml_dom.unlink()
884 return 1 865 return 1
885 866
886 def start_disconnect(self): 867 def start_disconnect(self):
887 self.on_mplay_event(mplay_event(MPLAY_DISCONNECTING)) 868 self.on_mplay_event(mplay_event(MPLAY_DISCONNECTING))
888 self.outbox.put( self.toxml("del") ) 869 self.outbox.put( self.toxml("del") )
889 ## Client Side Disconect Forced -- Snowdog 10-09-2003 870 ## Client Side Disconect Forced -- Snowdog 10-09-2003
890 #pause to allow GUI events time to sync. 871 #pause to allow GUI events time to sync.
891 time.sleep(1) 872 time.sleep(1)
892 self.do_disconnect() 873 self.do_disconnect()
893 874
894 def do_disconnect(self): 875 def do_disconnect(self):
904 885
905 def get_next_id(self): 886 def get_next_id(self):
906 self.unique_cookie += 1 887 self.unique_cookie += 1
907 return_str = self.id + "-" + str(self.unique_cookie) 888 return_str = self.id + "-" + str(self.unique_cookie)
908 return return_str 889 return return_str
909