Commit bf1c74d5 authored by Jim Fulton's avatar Jim Fulton

Checked in latest version from Sam, which incorporates changes from me that

add the ability to have multiple socket maps.
parent 5379491e
# -*- Mode: Python; tab-width: 4 -*- # -*- Mode: Python; tab-width: 4 -*-
# $Id: asyncore.py,v 1.8 2000/04/06 22:49:28 petrilli Exp $ # Id: asyncore.py,v 2.49 2000/05/04 06:06:04 jim Exp
# $Id: asyncore.py,v 1.9 2000/05/05 17:28:57 jim Exp $
# Author: Sam Rushing <rushing@nightmare.com> # Author: Sam Rushing <rushing@nightmare.com>
# ====================================================================== # ======================================================================
...@@ -52,11 +53,13 @@ class ExitNow (exceptions.Exception): ...@@ -52,11 +53,13 @@ class ExitNow (exceptions.Exception):
DEBUG = 0 DEBUG = 0
def poll (timeout=0.0): def poll (timeout=0.0, map=None):
global DEBUG global DEBUG
if socket_map: if map is None:
map = socket_map
if map:
r = []; w = []; e = [] r = []; w = []; e = []
for fd, obj in socket_map.items(): for fd, obj in map.items():
if obj.readable(): if obj.readable():
r.append (fd) r.append (fd)
if obj.writable(): if obj.writable():
...@@ -68,7 +71,7 @@ def poll (timeout=0.0): ...@@ -68,7 +71,7 @@ def poll (timeout=0.0):
for fd in r: for fd in r:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
obj.handle_read_event() obj.handle_read_event()
except ExitNow: except ExitNow:
...@@ -80,7 +83,7 @@ def poll (timeout=0.0): ...@@ -80,7 +83,7 @@ def poll (timeout=0.0):
for fd in w: for fd in w:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
obj.handle_write_event() obj.handle_write_event()
except ExitNow: except ExitNow:
...@@ -90,24 +93,26 @@ def poll (timeout=0.0): ...@@ -90,24 +93,26 @@ def poll (timeout=0.0):
except KeyError: except KeyError:
pass pass
def poll2 (timeout=0.0): def poll2 (timeout=0.0, map=None):
import poll import poll
if map is None:
map=socket_map
# timeout is in milliseconds # timeout is in milliseconds
timeout = int(timeout*1000) timeout = int(timeout*1000)
if socket_map: if map:
l = [] l = []
for fd, obj in socket_map.items(): for fd, obj in map.items():
flags = 0 flags = 0
if obj.readable(): if obj.readable():
flags = poll.POLLIN flags = poll.POLLIN
if obj.writable(): if obj.writable():
flags = flags | poll.POLLOUT flags = flags | poll.POLLOUT
if flags: if flags:
l.append (fd, flags) l.append ((fd, flags))
r = poll.poll (l, timeout) r = poll.poll (l, timeout)
for fd, flags in r: for fd, flags in r:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
if (flags & poll.POLLIN): if (flags & poll.POLLIN):
obj.handle_read_event() obj.handle_read_event()
...@@ -120,15 +125,18 @@ def poll2 (timeout=0.0): ...@@ -120,15 +125,18 @@ def poll2 (timeout=0.0):
except KeyError: except KeyError:
pass pass
def loop (timeout=30.0, use_poll=0): def loop (timeout=30.0, use_poll=0, map=None):
if use_poll: if use_poll:
poll_fun = poll2 poll_fun = poll2
else: else:
poll_fun = poll poll_fun = poll
while socket_map: if map is None:
poll_fun (timeout) map=socket_map
while map:
poll_fun (timeout, map)
class dispatcher: class dispatcher:
debug = 0 debug = 0
...@@ -137,9 +145,9 @@ class dispatcher: ...@@ -137,9 +145,9 @@ class dispatcher:
closing = 0 closing = 0
addr = None addr = None
def __init__ (self, sock=None): def __init__ (self, sock=None, map=None):
if sock: if sock:
self.set_socket (sock) self.set_socket (sock, map)
# I think it should inherit this anyway # I think it should inherit this anyway
self.socket.setblocking (0) self.socket.setblocking (0)
self.connected = 1 self.connected = 1
...@@ -166,15 +174,19 @@ class dispatcher: ...@@ -166,15 +174,19 @@ class dispatcher:
return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self),ar) return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self),ar)
def add_channel (self): def add_channel (self, map=None):
#self.log_info ('adding channel %s' % self) #self.log_info ('adding channel %s' % self)
socket_map [self._fileno] = self if map is None:
map=socket_map
map [self._fileno] = self
def del_channel (self): def del_channel (self, map=None):
fd = self._fileno fd = self._fileno
if socket_map.has_key (fd): if map is None:
map=socket_map
if map.has_key (fd):
#self.log_info ('closing channel %d:%s' % (fd, self)) #self.log_info ('closing channel %d:%s' % (fd, self))
del socket_map [fd] del map [fd]
def create_socket (self, family, type): def create_socket (self, family, type):
self.family_and_type = family, type self.family_and_type = family, type
...@@ -183,10 +195,10 @@ class dispatcher: ...@@ -183,10 +195,10 @@ class dispatcher:
self._fileno = self.socket.fileno() self._fileno = self.socket.fileno()
self.add_channel() self.add_channel()
def set_socket (self, sock): def set_socket (self, sock, map=None):
self.__dict__['socket'] = sock self.__dict__['socket'] = sock
self._fileno = sock.fileno() self._fileno = sock.fileno()
self.add_channel() self.add_channel (map)
def set_reuse_addr (self): def set_reuse_addr (self):
# try to re-use a server port if possible # try to re-use a server port if possible
...@@ -399,11 +411,11 @@ def compact_traceback (): ...@@ -399,11 +411,11 @@ def compact_traceback ():
t,v,tb = sys.exc_info() t,v,tb = sys.exc_info()
tbinfo = [] tbinfo = []
while 1: while 1:
tbinfo.append ( tbinfo.append ((
( tb.tb_frame.f_code.co_filename, tb.tb_frame.f_code.co_filename,
tb.tb_frame.f_code.co_name, tb.tb_frame.f_code.co_name,
str(tb.tb_lineno) ) str(tb.tb_lineno)
) ))
tb = tb.tb_next tb = tb.tb_next
if not tb: if not tb:
break break
...@@ -421,11 +433,12 @@ def compact_traceback (): ...@@ -421,11 +433,12 @@ def compact_traceback ():
) + ']' ) + ']'
return (file, function, line), t, v, info return (file, function, line), t, v, info
def close_all (): def close_all (map=None):
global socket_map if map is None:
for x in socket_map.values(): map=socket_map
for x in map.values():
x.socket.close() x.socket.close()
socket_map.clear() map.clear()
# Asynchronous File I/O: # Asynchronous File I/O:
# #
......
# -*- Mode: Python; tab-width: 4 -*- # -*- Mode: Python; tab-width: 4 -*-
# $Id: asyncore.py,v 1.8 2000/04/06 22:49:28 petrilli Exp $ # Id: asyncore.py,v 2.49 2000/05/04 06:06:04 jim Exp
# $Id: asyncore.py,v 1.9 2000/05/05 17:28:57 jim Exp $
# Author: Sam Rushing <rushing@nightmare.com> # Author: Sam Rushing <rushing@nightmare.com>
# ====================================================================== # ======================================================================
...@@ -52,11 +53,13 @@ class ExitNow (exceptions.Exception): ...@@ -52,11 +53,13 @@ class ExitNow (exceptions.Exception):
DEBUG = 0 DEBUG = 0
def poll (timeout=0.0): def poll (timeout=0.0, map=None):
global DEBUG global DEBUG
if socket_map: if map is None:
map = socket_map
if map:
r = []; w = []; e = [] r = []; w = []; e = []
for fd, obj in socket_map.items(): for fd, obj in map.items():
if obj.readable(): if obj.readable():
r.append (fd) r.append (fd)
if obj.writable(): if obj.writable():
...@@ -68,7 +71,7 @@ def poll (timeout=0.0): ...@@ -68,7 +71,7 @@ def poll (timeout=0.0):
for fd in r: for fd in r:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
obj.handle_read_event() obj.handle_read_event()
except ExitNow: except ExitNow:
...@@ -80,7 +83,7 @@ def poll (timeout=0.0): ...@@ -80,7 +83,7 @@ def poll (timeout=0.0):
for fd in w: for fd in w:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
obj.handle_write_event() obj.handle_write_event()
except ExitNow: except ExitNow:
...@@ -90,24 +93,26 @@ def poll (timeout=0.0): ...@@ -90,24 +93,26 @@ def poll (timeout=0.0):
except KeyError: except KeyError:
pass pass
def poll2 (timeout=0.0): def poll2 (timeout=0.0, map=None):
import poll import poll
if map is None:
map=socket_map
# timeout is in milliseconds # timeout is in milliseconds
timeout = int(timeout*1000) timeout = int(timeout*1000)
if socket_map: if map:
l = [] l = []
for fd, obj in socket_map.items(): for fd, obj in map.items():
flags = 0 flags = 0
if obj.readable(): if obj.readable():
flags = poll.POLLIN flags = poll.POLLIN
if obj.writable(): if obj.writable():
flags = flags | poll.POLLOUT flags = flags | poll.POLLOUT
if flags: if flags:
l.append (fd, flags) l.append ((fd, flags))
r = poll.poll (l, timeout) r = poll.poll (l, timeout)
for fd, flags in r: for fd, flags in r:
try: try:
obj = socket_map[fd] obj = map[fd]
try: try:
if (flags & poll.POLLIN): if (flags & poll.POLLIN):
obj.handle_read_event() obj.handle_read_event()
...@@ -120,15 +125,18 @@ def poll2 (timeout=0.0): ...@@ -120,15 +125,18 @@ def poll2 (timeout=0.0):
except KeyError: except KeyError:
pass pass
def loop (timeout=30.0, use_poll=0): def loop (timeout=30.0, use_poll=0, map=None):
if use_poll: if use_poll:
poll_fun = poll2 poll_fun = poll2
else: else:
poll_fun = poll poll_fun = poll
while socket_map: if map is None:
poll_fun (timeout) map=socket_map
while map:
poll_fun (timeout, map)
class dispatcher: class dispatcher:
debug = 0 debug = 0
...@@ -137,9 +145,9 @@ class dispatcher: ...@@ -137,9 +145,9 @@ class dispatcher:
closing = 0 closing = 0
addr = None addr = None
def __init__ (self, sock=None): def __init__ (self, sock=None, map=None):
if sock: if sock:
self.set_socket (sock) self.set_socket (sock, map)
# I think it should inherit this anyway # I think it should inherit this anyway
self.socket.setblocking (0) self.socket.setblocking (0)
self.connected = 1 self.connected = 1
...@@ -166,15 +174,19 @@ class dispatcher: ...@@ -166,15 +174,19 @@ class dispatcher:
return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self),ar) return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self),ar)
def add_channel (self): def add_channel (self, map=None):
#self.log_info ('adding channel %s' % self) #self.log_info ('adding channel %s' % self)
socket_map [self._fileno] = self if map is None:
map=socket_map
map [self._fileno] = self
def del_channel (self): def del_channel (self, map=None):
fd = self._fileno fd = self._fileno
if socket_map.has_key (fd): if map is None:
map=socket_map
if map.has_key (fd):
#self.log_info ('closing channel %d:%s' % (fd, self)) #self.log_info ('closing channel %d:%s' % (fd, self))
del socket_map [fd] del map [fd]
def create_socket (self, family, type): def create_socket (self, family, type):
self.family_and_type = family, type self.family_and_type = family, type
...@@ -183,10 +195,10 @@ class dispatcher: ...@@ -183,10 +195,10 @@ class dispatcher:
self._fileno = self.socket.fileno() self._fileno = self.socket.fileno()
self.add_channel() self.add_channel()
def set_socket (self, sock): def set_socket (self, sock, map=None):
self.__dict__['socket'] = sock self.__dict__['socket'] = sock
self._fileno = sock.fileno() self._fileno = sock.fileno()
self.add_channel() self.add_channel (map)
def set_reuse_addr (self): def set_reuse_addr (self):
# try to re-use a server port if possible # try to re-use a server port if possible
...@@ -399,11 +411,11 @@ def compact_traceback (): ...@@ -399,11 +411,11 @@ def compact_traceback ():
t,v,tb = sys.exc_info() t,v,tb = sys.exc_info()
tbinfo = [] tbinfo = []
while 1: while 1:
tbinfo.append ( tbinfo.append ((
( tb.tb_frame.f_code.co_filename, tb.tb_frame.f_code.co_filename,
tb.tb_frame.f_code.co_name, tb.tb_frame.f_code.co_name,
str(tb.tb_lineno) ) str(tb.tb_lineno)
) ))
tb = tb.tb_next tb = tb.tb_next
if not tb: if not tb:
break break
...@@ -421,11 +433,12 @@ def compact_traceback (): ...@@ -421,11 +433,12 @@ def compact_traceback ():
) + ']' ) + ']'
return (file, function, line), t, v, info return (file, function, line), t, v, info
def close_all (): def close_all (map=None):
global socket_map if map is None:
for x in socket_map.values(): map=socket_map
for x in map.values():
x.socket.close() x.socket.close()
socket_map.clear() map.clear()
# Asynchronous File I/O: # Asynchronous File I/O:
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment