1 """APP2APP protocol.
2 """
3 __docformat__ = 'restructuredtext en'
4
5
6 import threading
7
8 from utils import *
9 from user import *
10
11
13 """Represents an application in APP2APP protocol. Use `skype.Skype.Application` to instantiate.
14 """
15 _ValidateHandle = staticmethod(tounicode)
16
19
20 - def _Alter(self, AlterName, Args=None):
21 return self._Owner._Alter('APPLICATION', self.Name, AlterName, Args)
22
25
27 return self._Owner._Property('APPLICATION', self.Name, PropName, Set)
28
30 if App == self:
31 s = [x for x in Streams if x.PartnerHandle == self._Connect_Username]
32 if s:
33 self._Connect_Stream[0] = s[0]
34 self._Connect_Event.set()
35
36 - def Connect(self, Username, WaitConnected=False):
37 """Connects application to user.
38
39 :Parameters:
40 Username : str
41 Name of the user to connect to.
42 WaitConnected : bool
43 If True, causes the method to wait until the connection is established.
44
45 :return: If ``WaitConnected`` is True, returns the stream which can be used to send the
46 data. Otherwise returns None.
47 :rtype: `ApplicationStream` or None
48 """
49 if WaitConnected:
50 self._Connect_Event = threading.Event()
51 self._Connect_Stream = [None]
52 self._Connect_Username = Username
53 self._Connect_ApplicationStreams(self, self.Streams)
54 self._Owner.RegisterEventHandler('ApplicationStreams', self._Connect_ApplicationStreams)
55 self._Alter('CONNECT', Username)
56 self._Connect_Event.wait()
57 self._Owner.UnregisterEventHandler('ApplicationStreams', self._Connect_ApplicationStreams)
58 try:
59 return self._Connect_Stream[0]
60 finally:
61 del self._Connect_Stream, self._Connect_Event, self._Connect_Username
62 else:
63 self._Alter('CONNECT', Username)
64
66 """Creates the APP2APP application in Skype client.
67 """
68 self._Owner._DoCommand('CREATE APPLICATION %s' % self.Name)
69
71 """Deletes the APP2APP application in Skype client.
72 """
73 self._Owner._DoCommand('DELETE APPLICATION %s' % self.Name)
74
76 """Sends datagram to application streams.
77
78 :Parameters:
79 Text : unicode
80 Text to send.
81 Streams : sequence of `ApplicationStream`
82 Streams to send the datagram to or None if all currently connected streams should be
83 used.
84 """
85 if Streams is None:
86 Streams = self.Streams
87 for s in Streams:
88 s.SendDatagram(Text)
89
92
93 ConnectableUsers = property(_GetConnectableUsers,
94 doc="""All connectible users.
95
96 :type: `UserCollection`
97 """)
98
101
102 ConnectingUsers = property(_GetConnectingUsers,
103 doc="""All users connecting at the moment.
104
105 :type: `UserCollection`
106 """)
107
110
111 Name = property(_GetName,
112 doc="""Name of the application.
113
114 :type: unicode
115 """)
116
119
120 ReceivedStreams = property(_GetReceivedStreams,
121 doc="""All streams that received data and can be read.
122
123 :type: `ApplicationStreamCollection`
124 """)
125
128
129 SendingStreams = property(_GetSendingStreams,
130 doc="""All streams that send data and at the moment.
131
132 :type: `ApplicationStreamCollection`
133 """)
134
137
138 Streams = property(_GetStreams,
139 doc="""All currently connected application streams.
140
141 :type: `ApplicationStreamCollection`
142 """)
143
144
146 """Represents an application stream in APP2APP protocol.
147 """
148 _ValidateHandle = str
149
152
155
157 """Disconnects the stream.
158 """
159 self.Application._Alter('DISCONNECT', self.Handle)
160
161 close = Disconnect
162
164 """Reads data from stream.
165
166 :return: Read data or an empty string if none were available.
167 :rtype: unicode
168 """
169 return self.Application._Alter('READ', self.Handle)
170
171 read = Read
172
174 """Sends datagram to stream.
175
176 :Parameters:
177 Text : unicode
178 Datagram to send.
179 """
180 self.Application._Alter('DATAGRAM', '%s %s' % (self.Handle, tounicode(Text)))
181
183 """Writes data to stream.
184
185 :Parameters:
186 Text : unicode
187 Data to send.
188 """
189 self.Application._Alter('WRITE', '%s %s' % (self.Handle, tounicode(Text)))
190
191 write = Write
192
195
196 Application = property(_GetApplication,
197 doc="""Application this stream belongs to.
198
199 :type: `Application`
200 """)
201
204
205 ApplicationName = property(_GetApplicationName,
206 doc="""Name of the application this stream belongs to. Same as ``ApplicationStream.Application.Name``.
207
208 :type: unicode
209 """)
210
216
218 i = self._GetDataLength_GetStreamLength('SENDING')
219 if i is not None:
220 return i
221 i = self._GetDataLength_GetStreamLength('RECEIVED')
222 if i is not None:
223 return i
224 return 0
225
226 DataLength = property(_GetDataLength,
227 doc="""Number of bytes awaiting in the read buffer.
228
229 :type: int
230 """)
231
234
235 Handle = property(_GetHandle,
236 doc="""Stream handle in u'<Skypename>:<n>' format.
237
238 :type: str
239 """)
240
243
244 PartnerHandle = property(_GetPartnerHandle,
245 doc="""Skypename of the user this stream is connected to.
246
247 :type: str
248 """)
249
250
253