[Zmx-cvs-commit] zmx/InstantXML InstantXMLServer.py,1.3,1.3.2.1
Brought to you by:
sspickle
|
From: Steve S. <ssp...@us...> - 2006-02-24 11:00:41
|
Update of /cvsroot/zmx/zmx/InstantXML In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26983 Modified Files: Tag: ActionScript_2-0_conversion InstantXMLServer.py Log Message: sync with main branch. Index: InstantXMLServer.py =================================================================== RCS file: /cvsroot/zmx/zmx/InstantXML/InstantXMLServer.py,v retrieving revision 1.3 retrieving revision 1.3.2.1 diff -C2 -d -r1.3 -r1.3.2.1 *** InstantXMLServer.py 23 Nov 2003 17:35:29 -0000 1.3 --- InstantXMLServer.py 24 Feb 2006 11:00:35 -0000 1.3.2.1 *************** *** 22,34 **** VERSION = string.split(RCS_ID,' ')[2] import socket import asyncore import asynchat ! from xmlrpclib import loads, dumps def clean(src): return src + '\0' class chat_channel (asynchat.async_chat): --- 22,66 ---- VERSION = string.split(RCS_ID,' ')[2] + PING_SLEEPTIME = 60 # maximum ping delay seconds + import socket import asyncore import asynchat ! from xmlrpclib import loads, dumps, Boolean ! ! from threading import RLock, Thread ! from time import sleep def clean(src): return src + '\0' + class PingThread( Thread ): + + running = 0 + + def __init__(self, client): + Thread.__init__(self) + self.client = client + + def pause(self): + """ + stop pinging. + """ + self.running = 0 + + def run(self): + """ + wait on xml... + """ + self.running = 1 + + while 1: + if self.running: + sleep(self.client.ping_delay) # the client may adjust the ping_delay depending on the configuration of the channels that need pings. + self.client.do_ping() + else: + break + class chat_channel (asynchat.async_chat): *************** *** 41,44 **** --- 73,77 ---- self.xml = '' self.sender_id = '' + self.needs_ping = 0 def collect_incoming_data (self, data): *************** *** 68,72 **** --- 101,113 ---- self.server.log_info('Found type(values) of "%s"' % type(values)) if type(values) == type({}): + if self.server.ssecret: + if self.server.ssecret != values.get('ssecret',''): # if the server + return self.sender_id = values.get('sender_id','') + self.needs_ping = values.get('send_ping',0) + + if self.needs_ping: + self.server.setup_ping_thread(self) # only set up pings if requested. + if not self.sender_id: self.sender_id = None *************** *** 86,89 **** --- 127,137 ---- self.server.log_info('Command received from: %s (%s)' % (self.sender_id, command)) self.handle_command(command) + else: + target_id = cmdDict.get('target_id','') + if target_id: + sentValues = 1 + self.server.log_info('targeted data received from: %s to %s' % (self.sender_id, target_id)) + self.server.push_line(self, xmlToSend, target_id) + if not sentValues: self.server.push_line(self, xmlToSend) *************** *** 98,102 **** if hasattr (self, name): # make sure it's a method... ! method = getattr (self, name) if type(method) == type(self.handle_command): method (command_line[1:]) --- 146,150 ---- if hasattr (self, name): # make sure it's a method... ! method = getattr (self, name, None) if type(method) == type(self.handle_command): method (command_line[1:]) *************** *** 141,149 **** channel_class = chat_channel spy = 0 ! def __init__ (self, ip='', port=8518): self.port = port self.create_socket (socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() --- 189,199 ---- channel_class = chat_channel + ping_delay = PING_SLEEPTIME spy = 0 ! def __init__ (self, ip='', port=8518, ssecret=''): self.port = port + asyncore.dispatcher.__init__(self) self.create_socket (socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() *************** *** 152,173 **** self.listen (5) self.channels = {} ! self.count = 0 def handle_accept (self): conn, addr = self.accept() ! self.count = self.count + 1 ! self.log_info('Instant XML client #%d - %s:%d' % (self.count, addr[0], addr[1])) self.channels[self.channel_class (self, conn, addr)] = 1 ! def push_line (self, from_channel, line): sender_id = from_channel.get_sender_id() if self.spy: ! self.log_info('Instant XML transmit %s: %s\r\n' % (sender_id, line)) for c in self.channels.keys(): if c is not from_channel: self.log_info('checking %s against %s' % (c.get_channel_id(), from_channel.get_channel_id())) if c.get_channel_id() == from_channel.get_channel_id(): ! c.push (clean(line)) def writable (self): return 0 --- 202,274 ---- self.listen (5) self.channels = {} ! self.count = 0L ! self.ssecret = ssecret ! self.ping_thread = None # if pings are requested by a client we'll set this up. ! self.push_lock = RLock() def handle_accept (self): conn, addr = self.accept() ! self.count = self.count + 1L ! self.log_info('Instant XML client #%ld - %s:%d' % (self.count, addr[0], addr[1])) self.channels[self.channel_class (self, conn, addr)] = 1 ! def push_line (self, from_channel, line, target_id=''): ! ! # ! # push a packet to all clients in that channel. This could be more efficient for large numbers of clients. ! # That'll be version 2. ! # ! ! self.push_lock.acquire() sender_id = from_channel.get_sender_id() if self.spy: ! if target_id: ! self.log_info('Instant XML transmit %s: %s\r\n for %s only' % (sender_id, line, target_id)) ! else: ! self.log_info('Instant XML transmit %s: %s\r\n' % (sender_id, line)) ! for c in self.channels.keys(): if c is not from_channel: self.log_info('checking %s against %s' % (c.get_channel_id(), from_channel.get_channel_id())) if c.get_channel_id() == from_channel.get_channel_id(): ! if target_id: ! # ! # if there is a target_id, only send to that channel. ! # ! if c.sender_id == target_id: ! c.push (clean(line)) ! else: ! c.push (clean(line)) ! ! self.push_lock.release() ! ! def setup_ping_thread(self, client): ! """ ! establish the ping thread. ! """ ! if type(client.needs_ping) in [type(1.0), type(1)]: ! if self.ping_delay > client.needs_ping: ! self.ping_delay = client.needs_ping # use client with minimum delay to set delay time ! ! if not self.ping_thread: ! self.ping_thread = PingThread(self) ! self.ping_thread.start() ! ! def do_ping(self): ! self.push_lock.acquire() ! pingcount = 0 ! for c in self.channels.keys(): ! if c.needs_ping: ! pingcount += 1 ! c.push(clean(dumps(({'message':'ping'},)))) + self.push_lock.release() + + if pingcount == 0: + self.log_info('Ping count fell to zero... stopping ping thread.') + self.ping_thread.pause() + self.ping_thread = None + self.ping_delay = PING_SLEEPTIME + def writable (self): return 0 *************** *** 175,178 **** --- 276,281 ---- if __name__ == '__main__': import sys + + ssecret = '' # default is no shared secret. if len(sys.argv) > 1: *************** *** 180,184 **** else: port = 8518 ! s = InstantXMLServer('', port) asyncore.loop() --- 283,290 ---- else: port = 8518 + + if len(sys.argv) > 2: + ssecret = sys.argv[2] ! s = InstantXMLServer('', port, ssecret) asyncore.loop() |