comparison orpg/networking/mplay_messaging.py @ 195:b633f4c64aae alpha

Traipse Alpha 'OpenRPG' {100219-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Patch-2) New Features: New Namespace method with two new syntaxes Fixes: Fix to Server GUI startup errors Fix to Server GUI Rooms tab updating Fix to Chat and Settings if non existant die roller is picked Fix to Dieroller and .open() used with .vs(). Successes are correctly calculated Fix to Alias Lib's Export to Tree, Open, Save features Fix to alias node, now works properly Fix to Splitter node, minor GUI cleanup
author sirebral
date Sat, 24 Apr 2010 08:37:20 -0500
parents 06f10429eedc
children
comparison
equal deleted inserted replaced
182:4b2884f29a72 195:b633f4c64aae
19 # 19 #
20 # File: mplay_messaging.py 20 # File: mplay_messaging.py
21 # Author: Dj Gilcrease 21 # Author: Dj Gilcrease
22 # Maintainer: 22 # Maintainer:
23 # Version: 23 # Version:
24 # $Id: mplay_messaging.py,v 1.5 2007/05/06 16:42:59 digitalxero Exp $ 24 # $Id: mplay_messaging.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
25 # 25 #
26 # Description: This file contains the code for the client / server messaging 26 # Description: This file contains the code for the client / server messaging
27 # 27 #
28 28
29 __version__ = "$Id: mplay_messaging.py,v 1.5 2007/05/06 16:42:59 digitalxero Exp $" 29 __version__ = "$Id: mplay_messaging.py,v Traipse 'Ornery-Orc' prof.ebral Exp $"
30 30
31 import socket 31 import socket, Queue, thread, traceback, os, time
32 import Queue 32
33 import thread
34 import traceback
35 from threading import Event, Lock 33 from threading import Event, Lock
36 from xml.sax.saxutils import escape 34 from xml.sax.saxutils import escape
37 from struct import pack, unpack, calcsize 35 from struct import pack, unpack, calcsize
38 from string import * 36 from string import *
39 from orpg.orpg_version import VERSION, PROTOCOL_VERSION, CLIENT_STRING, SERVER_MIN_CLIENT_VERSION 37 from orpg.orpg_version import VERSION, PROTOCOL_VERSION, CLIENT_STRING, SERVER_MIN_CLIENT_VERSION
40 import os
41 import time
42 38
43 from orpg.tools.orpg_log import logger 39 from orpg.tools.orpg_log import logger
44 from orpg.orpgCore import component 40 from orpg.orpgCore import component
45 41
46 def myescape(data): 42 def myescape(data):
89 self.timeout_time = None 85 self.timeout_time = None
90 self.ignorelist = {} 86 self.ignorelist = {}
91 self.players = {} 87 self.players = {}
92 self.groups = {} 88 self.groups = {}
93 89
94 #Setup Stuff from the Server
95 """
96 if kwargs.has_key('inbox'): self.inbox = kwargs['inbox']
97 if kwargs.has_key('sock'): self.sock = kwargs['sock']
98 if kwargs.has_key('ip'): self.ip = kwargs['ip']
99 if kwargs.has_key('role'): self.role = kwargs['role']
100 if kwargs.has_key('id'): self.id = kwargs['id']
101 if kwargs.has_key('group_id'): self.group_id = kwargs['group_id']
102 if kwargs.has_key('name'): self.name = kwargs['name']
103 if kwargs.has_key('version'): self.version = kwargs['version']
104 if kwargs.has_key('protocol_version'): self.protocol_version = kwargs['protocol_version']
105 if kwargs.has_key('client_string'): self.client_string = kwargs['client_string']
106 """
107
108 ### Alpha ### 90 ### Alpha ###
109 self.inbox = kwargs['inbox'] or pass 91 self.inbox = kwargs['inbox'] or pass
110 self.sock = kwargs['sock'] or pass 92 self.sock = kwargs['sock'] or pass
111 self.ip = kwargs['ip'] or pass 93 self.ip = kwargs['ip'] or pass
112 self.role = kwargs['role'] or pass 94 self.role = kwargs['role'] or pass
205 self.startedEvent.set() 187 self.startedEvent.set()
206 188
207 def __str__(self): 189 def __str__(self):
208 return "%s(%s)\nIP:%s\ngroup_id:%s\n%s (%s)" % (self.name, self.id, self.ip, self.group_id, self.idle_time(), self.connected_time()) 190 return "%s(%s)\nIP:%s\ngroup_id:%s\n%s (%s)" % (self.name, self.id, self.ip, self.group_id, self.idle_time(), self.connected_time())
209 191
210 # idle time functions added by snowdog 3/31/04
211 def update_idle_time(self): 192 def update_idle_time(self):
212 self.lastmessagetime = time.time() 193 self.lastmessagetime = time.time()
213 194
214 def idle_time(self): 195 def idle_time(self):
215 curtime = time.time() 196 curtime = time.time()
324 logger.general("Unknown Message Type") 305 logger.general("Unknown Message Type")
325 logger.general(data) 306 logger.general(data)
326 #Message Action thread expires and closes here. 307 #Message Action thread expires and closes here.
327 return 308 return
328 309
329 #Privet functions
330 def sendThread( self, arg ): 310 def sendThread( self, arg ):
331 "Sending thread. This thread reads from the data queue and writes to the socket." 311 "Sending thread. This thread reads from the data queue and writes to the socket."
332 # Wait to be told it's okay to start running 312 # Wait to be told it's okay to start running
333 self.startedEvent.wait() 313 self.startedEvent.wait()
334 314
350 logger.note( "sendThread has terminated...") 330 logger.note( "sendThread has terminated...")
351 331
352 def sendMsg( self, sock, msg ): 332 def sendMsg( self, sock, msg ):
353 """Very simple function that will properly encode and send a message to te 333 """Very simple function that will properly encode and send a message to te
354 remote on the specified socket.""" 334 remote on the specified socket."""
355
356 # Calculate our message length
357 length = len( msg ) 335 length = len( msg )
358
359 # Encode the message length into network byte order
360 lp = pack( 'i', socket.htonl( length ) ) 336 lp = pack( 'i', socket.htonl( length ) )
361 337
362 try: 338 try:
363 # Send the encoded length 339 # Send the encoded length
364 sentl = sock.send( lp ) 340 sentl = sock.send( lp )
432 with the OS until we attempt to read the next complete message.""" 408 with the OS until we attempt to read the next complete message."""
433 409
434 msgData = "" 410 msgData = ""
435 try: 411 try:
436 lenData = self.recvData( sock, self.lensize ) 412 lenData = self.recvData( sock, self.lensize )
437
438 # Now, convert to a usable form 413 # Now, convert to a usable form
439 (length,) = unpack( 'i', lenData ) 414 (length,) = unpack( 'i', lenData )
440 length = socket.ntohl( length ) 415 length = socket.ntohl( length )
441 # Read exactly the remaining amount of data 416 # Read exactly the remaining amount of data
442 msgData = self.recvData( sock, length ) 417 msgData = self.recvData( sock, length )
443
444 if self.isServer: logger.debug("('data_recv', " + str(length+4) + ")") 418 if self.isServer: logger.debug("('data_recv', " + str(length+4) + ")")
445 except: logger.exception("Exception: messenger->recvMsg():\n" + traceback.format_exc()) 419 except: logger.exception("Exception: messenger->recvMsg():\n" + traceback.format_exc())
446 return msgData 420 return msgData
447 421
448 if __name__ == "__main__": 422 if __name__ == "__main__":
449 test = messenger(None) 423 test = messenger(None)
450 print test.build_message('hello', "This is a test message", attrib1="hello world", attrib2="hello world2", attrib3="hello world3") 424 print test.build_message('hello', "This is a test message", attrib1="hello world", attrib2="hello world2", attrib3="hello world3")
425