8
6
from dbus.service import method, signal, Object
9
from dbus import Interface
7
from dbus import PROPERTIES_IFACE
11
from telepathy.client import (
9
from telepathy.client import Channel
13
10
from telepathy.interfaces import (
14
CONN_INTERFACE, CHANNEL_INTERFACE_GROUP, CHANNEL_TYPE_TUBES,
11
CONN_INTERFACE, CHANNEL_INTERFACE_GROUP,
12
CHANNEL_TYPE_TEXT, CHANNEL_INTERFACE, CONNECTION_INTERFACE_REQUESTS,
13
CHANNEL_INTERFACE_TUBE, CHANNEL_TYPE_DBUS_TUBE)
16
14
from telepathy.constants import (
17
15
CONNECTION_HANDLE_TYPE_CONTACT,
18
16
CONNECTION_HANDLE_TYPE_ROOM, CONNECTION_STATUS_CONNECTED,
19
17
CONNECTION_STATUS_DISCONNECTED, CONNECTION_STATUS_CONNECTING,
20
TUBE_TYPE_DBUS, TUBE_TYPE_STREAM, TUBE_STATE_LOCAL_PENDING,
21
TUBE_STATE_REMOTE_PENDING, TUBE_STATE_OPEN)
18
SOCKET_ACCESS_CONTROL_CREDENTIALS,
19
TUBE_CHANNEL_STATE_LOCAL_PENDING, TUBE_CHANNEL_STATE_REMOTE_PENDING,
20
TUBE_CHANNEL_STATE_OPEN, TUBE_CHANNEL_STATE_NOT_OFFERED)
23
22
from account import connection_from_file
24
23
from tubeconn import TubeConnection
28
27
PATH = "/org/freedesktop/Telepathy/Tube/Test"
30
tube_type = {TUBE_TYPE_DBUS: "D-Bus",\
31
TUBE_TYPE_STREAM: "Stream"}
33
tube_state = {TUBE_STATE_LOCAL_PENDING : 'local pending',\
34
TUBE_STATE_REMOTE_PENDING : 'remote pending',\
35
TUBE_STATE_OPEN : 'open'}
30
tube_state = {TUBE_CHANNEL_STATE_LOCAL_PENDING : 'local pending',\
31
TUBE_CHANNEL_STATE_REMOTE_PENDING : 'remote pending',\
32
TUBE_CHANNEL_STATE_OPEN : 'open',
33
TUBE_CHANNEL_STATE_NOT_OFFERED: 'not offered'}
40
38
def __init__(self, account_file, muc_id):
41
self.conn = connection_from_file(account_file)
39
self.conn = connection_from_file(account_file, ready_handler=self.ready_cb)
42
40
self.muc_id = muc_id
44
42
self.conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
45
43
self.status_changed_cb)
46
self.conn[CONN_INTERFACE].connect_to_signal ("NewChannel",
50
46
self.joined = False
84
83
print "join muc", self.muc_id
85
handle = self.conn[CONN_INTERFACE].RequestHandles(
86
CONNECTION_HANDLE_TYPE_ROOM, [self.muc_id])[0]
88
chan_path = self.conn[CONN_INTERFACE].RequestChannel(
89
CHANNEL_TYPE_TEXT, CONNECTION_HANDLE_TYPE_ROOM,
84
chan_path, props = self.conn[CONNECTION_INTERFACE_REQUESTS].CreateChannel({
85
CHANNEL_INTERFACE + ".ChannelType": CHANNEL_TYPE_TEXT,
86
CHANNEL_INTERFACE + ".TargetHandleType": CONNECTION_HANDLE_TYPE_ROOM,
87
CHANNEL_INTERFACE + ".TargetID": self.muc_id})
92
89
self.channel_text = Channel(self.conn.dbus_proxy.bus_name, chan_path)
95
92
self.channel_text[CHANNEL_INTERFACE_GROUP].connect_to_signal(
96
93
"MembersChanged", self.text_channel_members_changed_cb)
98
chan_path = self.conn[CONN_INTERFACE].RequestChannel(
99
CHANNEL_TYPE_TUBES, CONNECTION_HANDLE_TYPE_ROOM,
101
self.channel_tubes = Channel(self.conn.dbus_proxy.bus_name, chan_path)
103
95
if self.self_handle in self.channel_text[CHANNEL_INTERFACE_GROUP].GetMembers():
104
96
self.joined = True
107
def new_channel_cb(self, object_path, channel_type, handle_type, handle,
109
if channel_type == CHANNEL_TYPE_TUBES:
110
self.channel_tubes = Channel(self.conn.dbus_proxy.bus_name,
113
self.channel_tubes[CHANNEL_TYPE_TUBES].connect_to_signal (
114
"TubeStateChanged", self.tube_state_changed_cb)
115
self.channel_tubes[CHANNEL_TYPE_TUBES].connect_to_signal (
116
"NewTube", self.new_tube_cb)
117
self.channel_tubes[CHANNEL_TYPE_TUBES].connect_to_signal (
118
"TubeClosed", self.tube_closed_cb)
120
for tube in self.channel_tubes[CHANNEL_TYPE_TUBES].ListTubes():
121
id, initiator, type, service, params, state = (tube[0],
122
tube[1], tube[2], tube[3], tube[4], tube[5])
123
self.new_tube_cb(id, initiator, type, service, params, state)
125
def new_tube_cb(self, id, initiator, type, service, params, state):
126
initiator_id = self.conn[CONN_INTERFACE].InspectHandles(
127
CONNECTION_HANDLE_TYPE_CONTACT, [initiator])[0]
129
print "new %s tube (%d) offered by %s. Service: %s. State: %s" % (
130
tube_type[type], id, initiator_id, service, tube_state[state])
132
if state == TUBE_STATE_OPEN:
133
self.tube_opened (id)
135
def tube_opened (self, id):
99
def new_channels_cb(self, channels):
100
if self.tube is not None:
103
for path, props in channels:
104
if props[CHANNEL_INTERFACE + ".ChannelType"] == CHANNEL_TYPE_DBUS_TUBE:
105
self.tube = Channel(self.conn.dbus_proxy.bus_name, path)
107
self.tube[CHANNEL_INTERFACE_TUBE].connect_to_signal(
108
"TubeChannelStateChanged", self.tube_channel_state_changed_cb)
109
self.tube[CHANNEL_INTERFACE].connect_to_signal(
110
"Closed", self.tube_closed_cb)
114
def got_tube(self, props):
115
initiator_id = props[CHANNEL_INTERFACE + ".InitiatorID"]
116
service = props[CHANNEL_TYPE_DBUS_TUBE + ".ServiceName"]
118
state = self.tube[PROPERTIES_IFACE].Get(CHANNEL_INTERFACE_TUBE, 'State')
120
print "new D-Bus tube offered by %s. Service: %s. State: %s" % (
121
initiator_id, service, tube_state[state])
123
def tube_opened (self):
136
124
group_iface = self.channel_text[CHANNEL_INTERFACE_GROUP]
138
tube_conn = TubeConnection(self.conn,
139
self.channel_tubes[CHANNEL_TYPE_TUBES],
140
id, group_iface=group_iface)
142
self.test = Test (tube_conn, self.conn)
144
def tube_state_changed_cb(self, id, state):
145
if state == TUBE_STATE_OPEN:
148
def tube_closed_cb (self, id):
126
tube_conn = TubeConnection(self.conn, self.tube, self.tube_addr,
127
group_iface=group_iface)
129
self.test = Test(tube_conn, self.conn)
131
def tube_channel_state_changed_cb(self, state):
132
print "tube state changed:", tube_state[state]
133
if state == TUBE_CHANNEL_STATE_OPEN:
136
def tube_closed_cb(self):
149
137
print "tube closed", id
151
139
def text_channel_members_changed_cb(self, message, added, removed,
161
149
def __init__(self, account_file, muc_id):
162
150
Client.__init__(self, account_file, muc_id)
164
def connected_cb(self):
165
Client.connected_cb(self)
152
def ready_cb(self, conn):
153
Client.ready_cb(self, conn)
169
157
def muc_joined(self):
170
158
Client.muc_joined(self)
172
print "muc joined. Offer the tube"
175
def tube_opened (self, id):
176
Client.tube_opened(self, id)
160
print "muc joined. Create the tube"
162
self.conn[CONNECTION_INTERFACE_REQUESTS].CreateChannel({
163
CHANNEL_INTERFACE + ".ChannelType": CHANNEL_TYPE_DBUS_TUBE,
164
CHANNEL_INTERFACE + ".TargetHandleType": CONNECTION_HANDLE_TYPE_ROOM,
165
CHANNEL_INTERFACE + ".TargetID": self.muc_id,
166
CHANNEL_TYPE_DBUS_TUBE + ".ServiceName": SERVICE})
168
def got_tube(self, props):
169
Client.got_tube(self, props)
171
params = dbus.Dictionary({"login": "badger", "a_int" : 69},
175
self.tube_addr = self.tube[CHANNEL_TYPE_DBUS_TUBE].Offer(params,
176
SOCKET_ACCESS_CONTROL_CREDENTIALS)
178
def tube_opened (self):
179
Client.tube_opened(self)
178
181
self._emit_test_signal();
179
182
gobject.timeout_add (20000, self._emit_test_signal)
181
def offer_tube(self):
182
params = {"login": "badger", "a_int" : 69}
184
id = self.channel_tubes[CHANNEL_TYPE_TUBES].OfferDBusTube(SERVICE,
187
184
def _emit_test_signal (self):
188
185
print "emit Hello"
189
186
self.test.Hello()
193
190
def __init__(self, account_file, muc_id):
194
191
Client.__init__(self, account_file, muc_id)
196
def connected_cb(self):
197
Client.connected_cb(self)
193
def ready_cb(self, conn):
194
Client.ready_cb(self, conn)
201
def new_tube_cb(self, id, initiator, type, service, params, state):
202
Client.new_tube_cb(self, id, initiator, type, service, params, state)
204
if state == TUBE_STATE_LOCAL_PENDING and service == SERVICE and\
206
print "accept tube", id
207
self.channel_tubes[CHANNEL_TYPE_TUBES].AcceptDBusTube(id)
210
def tube_opened (self, id):
211
Client.tube_opened(self, id)
198
def got_tube(self, props):
199
Client.got_tube(self, props)
202
self.tube_addr = self.tube[CHANNEL_TYPE_DBUS_TUBE].Accept(SOCKET_ACCESS_CONTROL_CREDENTIALS)
205
def tube_opened (self):
206
Client.tube_opened(self)
213
208
self.test.tube.add_signal_receiver(self.hello_cb, 'Hello', IFACE,
214
209
path=PATH, sender_keyword='sender')