1 '''APP2APP protocol.
2 '''
3
4 from utils import *
5 from user import *
6 import threading
7
8
10 '''Represents an application in APP2APP protocol. Use L{ISkype.Application<skype.ISkype.Application>} to instatinate.
11 '''
12
15
16 - def _Alter(self, AlterName, Args=None):
17 return self._Skype._Alter('APPLICATION', self._Name, AlterName, Args)
18
19 - def _Init(self, Name, Skype):
20 self._Name = unicode(Name)
21 self._Skype = Skype
22
24 return self._Skype._Property('APPLICATION', self._Name, PropName, Set)
25
26 - def Connect(self, Username, WaitConnected=False):
27 '''Connects application to user.
28
29 @param Username: Name of the user to connect to.
30 @type Username: unicode
31 @param WaitConnected: If True, causes the method to wait untill the connection is established.
32 @type WaitConnected: bool
33 @return: If C{WaitConnected} is True, returns the stream which can be used to send the data.
34 Otherwise returns None.
35 @rtype: L{IApplicationStream} or None
36 '''
37 if WaitConnected:
38 event = threading.Event()
39 stream = [None]
40 def app_streams(App, Streams):
41 if App == self:
42 s = [x for x in Streams if x.PartnerHandle == Username]
43 if s:
44 stream[0] = s[0]
45 event.set()
46 app_streams(self, self.Streams)
47 self._Skype.RegisterEventHandler('ApplicationStreams', app_streams)
48 self._Alter('CONNECT', Username)
49 event.wait()
50 self._Skype.UnregisterEventHandler('ApplicationStreams', app_streams)
51 return stream[0]
52 else:
53 self._Alter('CONNECT', Username)
54
56 '''Creates the APP2APP application in Skype client.
57 '''
58 self._Skype._DoCommand('CREATE APPLICATION %s' % self._Name)
59
61 '''Deletes the APP2APP application in Skype client.
62 '''
63 self._Skype._DoCommand('DELETE APPLICATION %s' % self._Name)
64
66 '''Sends datagram to application streams.
67
68 @param Text: Text to send.
69 @type Text: unicode
70 @param Streams: Streams to send the datagram to or None if all currently connected streams should be used.
71 @type Streams: sequence of L{IApplicationStream}
72 '''
73 if Streams == None:
74 Streams = self.Streams
75 for s in Streams:
76 s.SendDatagram(Text)
77
79 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('CONNECTABLE')))
80
81 ConnectableUsers = property(_GetConnectableUsers,
82 doc='''All connectable users.
83
84 @type: tuple of L{IUser}
85 ''')
86
88 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('CONNECTING')))
89
90 ConnectingUsers = property(_GetConnectingUsers,
91 doc='''All users connecting at the moment.
92
93 @type: tuple of L{IUser}
94 ''')
95
98
99 Name = property(_GetName,
100 doc='''Name of the application.
101
102 @type: unicode
103 ''')
104
107
108 ReceivedStreams = property(_GetReceivedStreams,
109 doc='''All streams that received data and can be read.
110
111 @type: tuple of L{IApplicationStream}
112 ''')
113
116
117 SendingStreams = property(_GetSendingStreams,
118 doc='''All streams that send data and at the moment.
119
120 @type: tuple of L{IApplicationStream}
121 ''')
122
125
126 Streams = property(_GetStreams,
127 doc='''All currently connected application streams.
128
129 @type: tuple of L{IApplicationStream}
130 ''')
131
132
134 '''Represents an application stream in APP2APP protocol.
135 '''
136
139
142
143 - def _Init(self, Handle, Application):
146
148 '''Disconnects the stream.
149 '''
150 self._Application._Alter('DISCONNECT', self._Handle)
151
152 close = Disconnect
153
155 '''Reads data from stream.
156
157 @return: Read data or an empty string if none were available.
158 @rtype: unicode
159 '''
160 return self._Application._Alter('READ', self._Handle)
161
162 read = Read
163
165 '''Sends datagram to stream.
166
167 @param Text: Datagram to send.
168 @type Text: unicode
169 '''
170 self._Application._Alter('DATAGRAM', '%s %s' % (self._Handle, Text))
171
173 '''Writes data to stream.
174
175 @param Text: Data to send.
176 @type Text: unicode
177 '''
178 self._Application._Alter('WRITE', '%s %s' % (self._Handle, Text))
179
180 write = Write
181
183 return self._Application
184
185 Application = property(_GetApplication,
186 doc='''Application this stream belongs to.
187
188 @type: L{IApplication}
189 ''')
190
192 return self._Application.Name
193
194 ApplicationName = property(_GetApplicationName,
195 doc='''Name of the application this stream belongs to. Same as C{IApplicationStream.Application.Name}.
196
197 @type: unicode
198 ''')
199
201 def GetStreamLength(Type):
202 for s in esplit(self._Application._Property(Type)):
203 h, i = s.split('=')
204 if h == self._Handle:
205 return int(i)
206 i = GetStreamLength('SENDING')
207 if i != None:
208 return i
209 i = GetStreamLength('RECEIVED')
210 if i != None:
211 return i
212 return 0
213
214 DataLength = property(_GetDataLength,
215 doc='''Number of bytes awaiting in the read buffer.
216
217 @type: int
218 ''')
219
222
223 Handle = property(_GetHandle,
224 doc='''Stream handle in u'<Skypename>:<n>' format.
225
226 @type: unicode
227 ''')
228
230 return self._Handle.split(':')[0]
231
232 PartnerHandle = property(_GetPartnerHandle,
233 doc='''Skypename of the user this stream is connected to.
234
235 @type: unicode
236 ''')
237