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
27 if App == self:
28 s = [x for x in Streams if x.PartnerHandle == self.__Connect_username]
29 if s:
30 self.__Connect_stream[0] = s[0]
31 self.__Connect_event.set()
32
33 - def Connect(self, Username, WaitConnected=False):
34 '''Connects application to user.
35
36 @param Username: Name of the user to connect to.
37 @type Username: unicode
38 @param WaitConnected: If True, causes the method to wait untill the connection is established.
39 @type WaitConnected: bool
40 @return: If C{WaitConnected} is True, returns the stream which can be used to send the data.
41 Otherwise returns None.
42 @rtype: L{IApplicationStream} or None
43 '''
44 if WaitConnected:
45 self.__Connect_event = threading.Event()
46 self.__Connect_stream = [None]
47 self.__Connect_username = Username
48 self.__Connect_app_streams(self, self.Streams)
49 self._Skype.RegisterEventHandler('ApplicationStreams', self.__Connect_app_streams)
50 self._Alter('CONNECT', Username)
51 self.__Connect_event.wait()
52 self._Skype.UnregisterEventHandler('ApplicationStreams', self.__Connect_app_streams)
53 try:
54 return self.__Connect_stream[0]
55 finally:
56 del self.__Connect_stream, self.__Connect_event, self.__Connect_username
57 else:
58 self._Alter('CONNECT', Username)
59
61 '''Creates the APP2APP application in Skype client.
62 '''
63 self._Skype._DoCommand('CREATE APPLICATION %s' % self._Name)
64
66 '''Deletes the APP2APP application in Skype client.
67 '''
68 self._Skype._DoCommand('DELETE APPLICATION %s' % self._Name)
69
71 '''Sends datagram to application streams.
72
73 @param Text: Text to send.
74 @type Text: unicode
75 @param Streams: Streams to send the datagram to or None if all currently connected streams should be used.
76 @type Streams: sequence of L{IApplicationStream}
77 '''
78 if Streams == None:
79 Streams = self.Streams
80 for s in Streams:
81 s.SendDatagram(Text)
82
84 return tuple([IUser(x, self._Skype) for x in esplit(self._Property('CONNECTABLE'))])
85
86 ConnectableUsers = property(_GetConnectableUsers,
87 doc='''All connectable users.
88
89 @type: tuple of L{IUser}
90 ''')
91
93 return tuple([IUser(x, self._Skype) for x in esplit(self._Property('CONNECTING'))])
94
95 ConnectingUsers = property(_GetConnectingUsers,
96 doc='''All users connecting at the moment.
97
98 @type: tuple of L{IUser}
99 ''')
100
103
104 Name = property(_GetName,
105 doc='''Name of the application.
106
107 @type: unicode
108 ''')
109
112
113 ReceivedStreams = property(_GetReceivedStreams,
114 doc='''All streams that received data and can be read.
115
116 @type: tuple of L{IApplicationStream}
117 ''')
118
121
122 SendingStreams = property(_GetSendingStreams,
123 doc='''All streams that send data and at the moment.
124
125 @type: tuple of L{IApplicationStream}
126 ''')
127
130
131 Streams = property(_GetStreams,
132 doc='''All currently connected application streams.
133
134 @type: tuple of L{IApplicationStream}
135 ''')
136
137
139 '''Represents an application stream in APP2APP protocol.
140 '''
141
144
147
148 - def _Init(self, Handle, Application):
151
153 '''Disconnects the stream.
154 '''
155 self._Application._Alter('DISCONNECT', self._Handle)
156
157 close = Disconnect
158
160 '''Reads data from stream.
161
162 @return: Read data or an empty string if none were available.
163 @rtype: unicode
164 '''
165 return self._Application._Alter('READ', self._Handle)
166
167 read = Read
168
170 '''Sends datagram to stream.
171
172 @param Text: Datagram to send.
173 @type Text: unicode
174 '''
175 self._Application._Alter('DATAGRAM', '%s %s' % (self._Handle, Text))
176
178 '''Writes data to stream.
179
180 @param Text: Data to send.
181 @type Text: unicode
182 '''
183 self._Application._Alter('WRITE', '%s %s' % (self._Handle, Text))
184
185 write = Write
186
188 return self._Application
189
190 Application = property(_GetApplication,
191 doc='''Application this stream belongs to.
192
193 @type: L{IApplication}
194 ''')
195
197 return self._Application.Name
198
199 ApplicationName = property(_GetApplicationName,
200 doc='''Name of the application this stream belongs to. Same as C{IApplicationStream.Application.Name}.
201
202 @type: unicode
203 ''')
204
206 for s in esplit(self._Application._Property(Type)):
207 h, i = s.split('=')
208 if h == self._Handle:
209 return int(i)
210
212 i = self.__GetDataLength_GetStreamLength('SENDING')
213 if i != None:
214 return i
215 i = self.__GetDataLength_GetStreamLength('RECEIVED')
216 if i != None:
217 return i
218 return 0
219
220 DataLength = property(_GetDataLength,
221 doc='''Number of bytes awaiting in the read buffer.
222
223 @type: int
224 ''')
225
228
229 Handle = property(_GetHandle,
230 doc='''Stream handle in u'<Skypename>:<n>' format.
231
232 @type: unicode
233 ''')
234
236 return self._Handle.split(':')[0]
237
238 PartnerHandle = property(_GetPartnerHandle,
239 doc='''Skypename of the user this stream is connected to.
240
241 @type: unicode
242 ''')
243