1
# Licensed under the MIT license
2
# http://opensource.org/licenses/mit-license.php
4
# Copyright 2009 Philippe Normand <phil@base-art.net>
6
from telepathy.interfaces import CHANNEL_INTERFACE, CONNECTION_INTERFACE_REQUESTS, \
8
from telepathy.constants import CONNECTION_HANDLE_TYPE_ROOM, \
9
SOCKET_ACCESS_CONTROL_CREDENTIALS
11
from coherence.extern.telepathy.client import Client
13
class TubePublisherMixin(object):
15
def __init__(self, tubes_to_offer):
16
self._tubes_to_offer = tubes_to_offer
19
self.info("muc joined. Offering the tubes")
20
conn_iface = self.conn[CONNECTION_INTERFACE_REQUESTS]
21
params = {CHANNEL_INTERFACE + ".ChannelType": CHANNEL_TYPE_DBUS_TUBE,
22
CHANNEL_INTERFACE + ".TargetHandleType": CONNECTION_HANDLE_TYPE_ROOM,
23
CHANNEL_INTERFACE + ".TargetID": self.muc_id}
24
for interface in self._tubes_to_offer.keys():
25
params[CHANNEL_TYPE_DBUS_TUBE + ".ServiceName"] = interface
26
conn_iface.CreateChannel(params)
28
def got_tube(self, tube):
29
super(TubePublisherMixin, self).got_tube(tube)
30
initiator_handle = tube.props[CHANNEL_INTERFACE + ".InitiatorHandle"]
31
if initiator_handle == self.self_handle:
32
self.finish_tube_offer(tube)
34
def finish_tube_offer(self, tube):
35
self.info("offering my tube located at %r", tube.object_path)
36
service_name = tube.props[CHANNEL_TYPE_DBUS_TUBE + ".ServiceName"]
37
params = self._tubes_to_offer[service_name]
38
params["initiator"] = self.account["account"]
39
address = tube[CHANNEL_TYPE_DBUS_TUBE].Offer(params,
40
SOCKET_ACCESS_CONTROL_CREDENTIALS)
41
tube.local_address = address
42
self.info("local tube address: %r", address)
44
class TubePublisher(TubePublisherMixin, Client):
45
logCategory = "tube_publisher"
47
def __init__(self, manager, protocol, account, muc_id, tubes_to_offer):
48
TubePublisherMixin.__init__(self, tubes_to_offer)
49
Client.__init__(self, manager, protocol, account, muc_id)
52
class TubeConsumerMixin(object):
53
logCategory = "tube_consumer"
55
def __init__(self, found_peer_callback=None, disapeared_peer_callback=None):
56
self.found_peer_callback = found_peer_callback
57
self.disapeared_peer_callback = disapeared_peer_callback
59
def got_tube(self, tube):
60
super(TubeConsumerMixin, self).got_tube(tube)
61
self.accept_tube(tube)
63
def accept_tube(self, tube):
64
if self.pre_accept_tube(tube):
65
self.info("accepting tube %r", tube.object_path)
66
tube_iface = tube[CHANNEL_TYPE_DBUS_TUBE]
67
tube.local_address = tube_iface.Accept(SOCKET_ACCESS_CONTROL_CREDENTIALS)
69
self.warning("tube %r not allowed", tube)
71
def pre_accept_tube(self, tube):
74
def tube_closed(self, tube):
75
self.disapeared_peer_callback(tube)
76
super(TubeConsumerMixin, self).tube_closed(tube)
78
class TubeConsumer(TubeConsumerMixin, Client):
79
logCategory = "tube_consumer"
81
def __init__(self, manager, protocol,
82
account, muc_id, found_peer_callback=None,
83
disapeared_peer_callback=None):
84
TubeConsumerMixin.__init__(self, found_peer_callback=found_peer_callback,
85
disapeared_peer_callback=disapeared_peer_callback)
86
Client.__init__(self, manager, protocol, account, muc_id)