2006-05-12 04:45:45 +02:00
|
|
|
import network
|
2006-05-11 20:46:34 +02:00
|
|
|
|
|
|
|
class StreamReaderRequestHandler(object):
|
|
|
|
def __init__(self, reader):
|
|
|
|
self._reader = reader
|
|
|
|
|
2006-05-12 04:19:52 +02:00
|
|
|
def message(self, nick_name, message):
|
2006-05-11 20:46:34 +02:00
|
|
|
address = network.get_authinfo()
|
2006-05-12 04:19:52 +02:00
|
|
|
self._reader.recv(nick_name, message)
|
2006-05-11 20:46:34 +02:00
|
|
|
return True
|
|
|
|
|
2006-05-11 08:00:37 +02:00
|
|
|
class StreamReader:
|
2006-05-12 03:56:23 +02:00
|
|
|
def __init__(self, group, service):
|
2006-05-11 20:46:34 +02:00
|
|
|
self._group = group
|
2006-05-12 03:56:23 +02:00
|
|
|
self._service = service
|
2006-05-11 20:46:34 +02:00
|
|
|
|
|
|
|
if self._service.is_multicast():
|
|
|
|
self._setup_multicast()
|
|
|
|
else:
|
|
|
|
self._setup_unicast()
|
|
|
|
|
|
|
|
def set_listener(self, callback):
|
2006-05-11 08:00:37 +02:00
|
|
|
self._callback = callback
|
2006-05-11 20:46:34 +02:00
|
|
|
|
|
|
|
def _setup_multicast(self):
|
|
|
|
address = self._service.get_address()
|
|
|
|
port = self._service.get_port()
|
2006-05-12 04:45:45 +02:00
|
|
|
server = network.GroupServer(address, port, self._recv_multicast)
|
2006-05-11 20:46:34 +02:00
|
|
|
server.start()
|
|
|
|
|
|
|
|
def _setup_unicast(self):
|
2006-05-12 04:31:02 +02:00
|
|
|
started = False
|
|
|
|
tries = 10
|
|
|
|
port = self._service.get_port()
|
|
|
|
while not started and tries > 0:
|
|
|
|
try:
|
2006-05-12 04:59:27 +02:00
|
|
|
p2p_server = network.GlibXMLRPCServer(("", port))
|
2006-05-12 04:31:02 +02:00
|
|
|
p2p_server.register_instance(StreamReaderRequestHandler(self))
|
|
|
|
started = True
|
|
|
|
except:
|
|
|
|
port = port + 1
|
|
|
|
tries = tries - 1
|
|
|
|
self._service.set_port(port)
|
2006-05-11 20:46:34 +02:00
|
|
|
|
|
|
|
def _recv_multicast(self, msg):
|
2006-05-12 04:19:52 +02:00
|
|
|
[ nick_name, data ] = msg['data'].split(" |**| ", 2)
|
2006-05-12 05:01:20 +02:00
|
|
|
self.recv(nick_name, data)
|
2006-05-11 20:46:34 +02:00
|
|
|
|
2006-05-12 05:01:20 +02:00
|
|
|
def recv(self, nick_name, data):
|
2006-05-12 04:19:52 +02:00
|
|
|
if nick_name != self._group.get_owner().get_nick_name():
|
|
|
|
self._callback(self._group.get_buddy(nick_name), data)
|