sugar-toolkit-gtk3/sugar/p2p/Stream.py

126 lines
3.5 KiB
Python
Raw Normal View History

import xmlrpclib
import socket
2006-05-22 04:21:42 +02:00
import traceback
2006-05-22 08:11:39 +02:00
import pygtk
pygtk.require('2.0')
import gobject
import network
from MostlyReliablePipe import MostlyReliablePipe
class Stream(object):
def __init__(self, service, group):
2006-05-22 04:21:42 +02:00
if not service:
raise ValueError("service must be valid")
self._service = service
self._group = group
self._owner_nick_name = self._group.get_owner().get_nick_name()
self._port = self._service.get_port()
self._address = self._service.get_address()
self._callback = None
def new_from_service(service, group):
if service.is_multicast():
return MulticastStream(service, group)
else:
return UnicastStream(service, group)
new_from_service = staticmethod(new_from_service)
def set_data_listener(self, callback):
self._callback = callback
def recv(self, nick_name, data):
if nick_name != self._owner_nick_name:
if self._callback:
self._callback(self._group.get_buddy(nick_name), data)
2006-05-23 06:15:14 +02:00
class UnicastStreamWriter(object):
def __init__(self, stream, service, owner_nick_name):
# set up the writer
2006-05-22 04:21:42 +02:00
if not service:
raise ValueError("service must be valid")
self._service = service
self._owner_nick_name = owner_nick_name
self._address = self._service.get_address()
self._port = self._service.get_port()
self._xmlrpc_addr = "http://%s:%d" % (self._address, self._port)
2006-05-23 06:15:14 +02:00
self._writer = network.GlibServerProxy(self._xmlrpc_addr)
2006-05-22 08:11:39 +02:00
2006-05-23 06:15:14 +02:00
def write(self, xmlrpc_data):
2006-05-22 04:21:42 +02:00
"""Write some data to the default endpoint of this pipe on the remote server."""
try:
2006-05-23 06:15:14 +02:00
self._writer.message(None, None, self._owner_nick_name, xmlrpc_data)
return True
except (socket.error, xmlrpclib.Fault, xmlrpclib.ProtocolError):
traceback.print_exc()
return False
2006-05-23 06:15:14 +02:00
def custom_request(self, method_name, request_cb, user_data, *args):
2006-05-22 04:21:42 +02:00
"""Call a custom XML-RPC method on the remote server."""
try:
method = getattr(self._writer, method_name)
2006-05-23 06:15:14 +02:00
method(request_cb, user_data, *args)
return True
2006-05-22 08:11:39 +02:00
except (socket.error, xmlrpclib.Fault, xmlrpclib.ProtocolError):
traceback.print_exc()
2006-05-23 06:15:14 +02:00
return False
2006-05-22 08:11:39 +02:00
class UnicastStream(Stream):
def __init__(self, service, group):
Stream.__init__(self, service, group)
self._setup()
def _setup(self):
# Set up the reader
started = False
tries = 10
port = self._service.get_port()
self._reader = None
while not started and tries > 0:
try:
self._reader = network.GlibXMLRPCServer(("", port))
2006-05-22 04:21:42 +02:00
self._reader.register_function(self._message, "message")
started = True
except(socket.error):
port = port + 1
tries = tries - 1
self._service.set_port(port)
2006-05-22 04:21:42 +02:00
def _message(self, nick_name, message):
"""Called by the XMLRPC server when network data arrives."""
self.recv(nick_name, message)
return True
2006-05-22 04:21:42 +02:00
def register_handler(self, handler, name):
if name == "message":
raise ValueError("Handler name 'message' is a reserved handler.")
self._reader.register_function(handler, name)
2006-05-23 06:15:14 +02:00
def new_writer(self, service):
return UnicastStreamWriter(self, service, self._owner_nick_name)
class MulticastStream(Stream):
def __init__(self, service, group):
Stream.__init__(self, service, group)
self._address = self._service.get_group_address()
self._setup()
def _setup(self):
self._pipe = MostlyReliablePipe('', self._address, self._port, self._recv_data_cb)
self._pipe.start()
def write(self, data):
self._pipe.send(self._owner_nick_name + " |**| " + data)
def _recv_data_cb(self, addr, data, user_data=None):
[ nick_name, data ] = data.split(" |**| ", 2)
self.recv(nick_name, data)
2006-05-23 06:15:14 +02:00
def new_writer(self, service=None):
return self