Ruby4Skype 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/skypeapi/os.rb DELETED
@@ -1,439 +0,0 @@
1
- module SkypeAPI
2
- module OS
3
- module Share
4
- WAIT_CMD_LIMIT = 30.0 #sec
5
- PING_CYCLE = 30.0 #sec
6
- PING_LIMIT = 10.0 # < PING_CYCLE
7
- SLEEP_INTERVAL = 0.001
8
-
9
- def sendCMD cmd,method=nil,&block
10
- if method
11
- sendCallBack cmd do |res|
12
- check res,cmd
13
- method.call res
14
- end
15
- return true
16
- elsif block_given?
17
- sendCallBack cmd do |res|
18
- check res,cmd
19
- block.call res
20
- end
21
- return true
22
- else
23
- return check(sendBlock(cmd),cmd)
24
- end
25
- end
26
-
27
- def check res,cmd
28
- if res =~ /^ERROR /m
29
- raise SkypeAPIError::API,res,cmd
30
- else
31
- return res
32
- end
33
- end
34
-
35
- def sendCallBack cmd,cb=Proc.new
36
- @callBack[@sendCount] = cb
37
- begin
38
- sendMSG cmd
39
- rescue => e
40
- @callBack.delete(@sendCount)
41
- raise e
42
- end
43
- return true
44
- end
45
-
46
- def sendBlock cmd, waitLimit = WAIT_CMD_LIMIT
47
- resVal = nil
48
- sendCallBack cmd do |res|
49
- resVal = res
50
- end
51
- startTime = Time.now
52
- loop do
53
- polling
54
- if resVal
55
- return resVal
56
- end
57
- if Time.now - startTime > waitLimit
58
- unless existEvent? :reconnect
59
- raise SkypeAPIError::TimeOut,"TimeOut #{cmd} {(Time.now - startTime).to_i}"
60
- else
61
- doEvent :reconnect
62
- return sendBlock(cmd, waitLimit)
63
- end
64
- end
65
- #Thread.pass
66
- sleep SLEEP_INTERVAL
67
- #sleep 0.00123
68
- #sleep 0.000001
69
- #sleep 0.01
70
- end
71
- end
72
-
73
- def attachWait
74
- flag = true
75
- attach do |state|
76
- flag = false if state == :success
77
- end
78
- while flag
79
- polling
80
- sleep SLEEP_INTERVAL
81
- #sleep 0.0123
82
- end
83
- end
84
-
85
- #����reg==nil�ŁA���̑��ƌ���㩁B
86
- def addNotify reg, block=Proc.new
87
- @notify[reg] = block
88
- end
89
-
90
- def delNotify reg
91
- @notify.delete reg
92
- end
93
-
94
- def addEvent sym, block=Proc.new
95
- @event[sym].push block
96
- block
97
- end
98
-
99
- def setEvent sym, block=Proc.new
100
- @event[sym] = Array.new
101
- addEvent sym, block
102
- end
103
-
104
- def delEvent sym, block=nil
105
- unless block
106
- @event[sym] = Array.new
107
- else
108
- @event[sym].delete block
109
- end
110
- end
111
-
112
- def replaceEvent sym,block=Proc.new
113
- tmp = @event[sym].dup
114
- if defined?(block.each)
115
- block.each do |e|
116
- addEvent sym, e
117
- end
118
- else
119
- setEvent sym,block
120
- end
121
- return tmp
122
- end
123
-
124
- def existEvent? sym
125
- if @event[sym].length > 0
126
- return true
127
- else
128
- return false
129
- end
130
- end
131
-
132
- def getEvent sym
133
- @event[sym]
134
- end
135
-
136
- def doEvent sym,*args
137
- @event[sym].each do |e|
138
- if e.arity == 1
139
- e.call args[0]
140
- else
141
- e.call args
142
- end
143
- end
144
- end
145
-
146
- def pushQueue res
147
- @queue.push(proc{doEvent(:received, res.chop)}) if existEvent? :received
148
-
149
- if res =~ /^(#(\d+?) )?(.+?)\000$/m
150
- if $2
151
- if @callBack[$2.to_i]
152
- cb = @callBack[$2.to_i]
153
- val = $3
154
- if @singleThread
155
- @sent = proc{cb.call val}
156
- else
157
- @queue.push(proc{cb.call val})
158
- end
159
- @callBack.delete($2.to_i)
160
- end
161
- else
162
- cmd = $3
163
- flag = false
164
- @notify.each do |reg,action|
165
- if cmd =~ reg
166
- tmp = $1
167
- @queue.push(proc{action.call(tmp)})
168
- flag = true
169
- end
170
- end
171
-
172
- unless flag
173
- action = @notify[nil]
174
- @queue.push(proc{action.call(cmd)})
175
- end
176
- end
177
- end
178
- end
179
-
180
- def queueProcess
181
- @pingTime = Time.now unless @pingTime
182
- if Time.now - @pingTime > PING_CYCLE
183
- @queue.push method(:checkConnection)
184
- @pingTime = Time.now
185
- end
186
-
187
- if @singleThread
188
- if @sent == nil
189
- while e = @queue.shift
190
- e.call
191
- end
192
- elsif(@sent.class == Proc)
193
- @sent.call
194
- @sent = nil
195
- end
196
- else
197
- while e = @queue.shift
198
- e.call
199
- end
200
- end
201
- end
202
-
203
- def checkConnection
204
- begin
205
- resVal = nil
206
- sendCallBack 'PING' do |res|
207
- resVal = res
208
- end
209
- startTime = Time.now
210
- i = 0
211
- loop do
212
- polling
213
- if resVal
214
- unless resVal == "PONG"
215
- raise SkypeAPIError::Reconnect
216
- else
217
- break
218
- end
219
- end
220
- if Time.now - startTime > PING_LIMIT and i > 3
221
- raise SkypeAPIError::TimeOut
222
- end
223
- i+=1
224
- sleep SLEEP_INTERVAL
225
- end
226
- rescue SkypeAPIError::Reconnect,SkypeAPIError::TimeOut
227
- unless existEvent? :reconnect
228
- raise SkypeAPIError::Reconnect
229
- else
230
- doEvent :reconnect
231
- end
232
- end
233
- return true
234
- end
235
-
236
- end
237
-
238
- class Windows
239
- begin
240
- require 'rubygems'
241
- gem 'swin'
242
- rescue LoadError
243
- end
244
- require "swin"
245
- require 'Win32API'
246
- include Share
247
-
248
- HWND_BROADCAST = 0xFFFF
249
- WM_COPYDATA = 0x004A
250
- WM_USER = 0x0400
251
- WM_USER_MSG = WM_USER + 1
252
- SKYPECONTROLAPI_ATTACH_SUCCESS=0
253
- SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION=1
254
- SKYPECONTROLAPI_ATTACH_REFUSED=2
255
- SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE=3
256
- SKYPECONTROLAPI_ATTACH_API_AVAILABLE=0x8001
257
-
258
- RegisterWindowMessage = Win32API.new('user32','RegisterWindowMessageA', 'P', 'L')
259
- SendMessage = Win32API.new("user32", "SendMessageA", ['L']*4, 'L')
260
- PostMessage = Win32API.new("user32", "PostMessageA", 'LLLP', 'L')
261
-
262
- def initialize
263
- @sendCount = 0
264
- @queue = Array.new
265
- @callBack = Hash.new
266
- @notify = Hash.new
267
- @event = Hash.new do |h,k|
268
- h[k] = Array.new
269
- end
270
- @singleThread = true
271
-
272
- addEvent :reconnect do
273
- flag = true
274
- attachEvent = replaceEvent :attached do
275
- flag = false
276
- end
277
- attach
278
- while flag
279
- polling
280
- sleep 0.123
281
- end
282
- replaceEvent :attach, attachEvent
283
- end
284
-
285
- @wmBuffer = Hash.new
286
- @wmHandler = SWin::LWFactory.new(SWin::Application.hInstance).newwindow nil
287
- @wmHandler.create
288
- @wmHandler.addEvent(WM_COPYDATA)
289
- @wmHandler.addEvent(WM_USER_MSG)
290
- @wmHandler.instance_variable_set :@skypeAPI,self
291
- @wmHandler.instance_variable_set :@wmBuffer,@wmBuffer
292
- @wmHandler.instance_variable_set :@queue,@queue
293
-
294
- class << @wmHandler
295
- attr_reader :hSkypeAPIWindowHandle
296
-
297
- def msghandler(sMsg)
298
- case sMsg.msg
299
- when @dwAttachMsg
300
- case sMsg.lParam
301
- when SKYPECONTROLAPI_ATTACH_SUCCESS
302
- @hSkypeAPIWindowHandle = sMsg.wParam
303
- @queue.push Proc.new{@skypeAPI.sendCMD "PROTOCOL 9999"}
304
- @queue.push Proc.new{@skypeAPI.doEvent(:attach,:success)}
305
- @queue.push Proc.new{@skypeAPI.doEvent(:attached)}
306
- when SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION:
307
- @queue.push Proc.new{@skypeAPI.doEvent(:attach,:authorize)}
308
- @queue.push Proc.new{@skypeAPI.doEvent(:pendingAuthorization)}
309
- when SKYPECONTROLAPI_ATTACH_REFUSED:
310
- unless @skypeAPI.existEvent? :attach
311
- raise SkypeAPIError::Attach,"Refused"
312
- else
313
- @queue.push Proc.new{@skypeAPI.doEvent(:attach,:refused)}
314
- end
315
- when SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE:
316
- unless @skypeAPI.existEvent? :attach
317
- raise SkypeAPIError::Attach,"Not available."
318
- else
319
- @queue.push Proc.new{@skypeAPI.doEvent(:attach,:notAvailable)}
320
- end
321
- when SKYPECONTROLAPI_ATTACH_API_AVAILABLE:
322
- @queue.push Proc.new{@skypeAPI.doEvent(:apiAvailable)}
323
- else
324
- unless @skypeAPI.existEvent? :attach
325
- raise SkypeAPIError::Attach,"Skype API attach unknown message"
326
- else
327
- @queue.push Proc.new{@skypeAPI.doEvent(:attach,:unkown)}
328
- end
329
- end
330
- sMsg.retval = 1
331
- #return true
332
- when WM_COPYDATA
333
- if sMsg.wParam == @hSkypeAPIWindowHandle
334
- retval = application.cstruct2array(sMsg.lParam,"LLL")
335
- cmd = application.pointer2string(retval[2],retval[1])
336
- @wmBuffer.delete($1.to_i) if cmd =~ /^#(\d+) /
337
- @skypeAPI.pushQueue cmd
338
- sMsg.retval = 1
339
- return true
340
- end
341
- when WM_USER_MSG
342
- unless SendMessage.call(sMsg.wParam, WM_COPYDATA, sMsg.hWnd, sMsg.lParam)
343
- raise SkypeAPIError::Connect,"Skype not ready"
344
- end
345
- sMsg.retval = true
346
- return true
347
- end
348
- end
349
- end
350
- end
351
-
352
- #attr_accessor :attached,:received,:sent
353
-
354
- def attach &block
355
- if block
356
- if block.arity == 1
357
- addEvent :attach, block
358
- else
359
- addEvent :attached, block
360
- end
361
- end
362
-
363
- @wmHandler.create unless @wmHandler.alive?
364
- dwDiscoverMsg = RegisterWindowMessage.call("SkypeControlAPIDiscover");
365
- raise SkypeAPIError::Attach,"SkypeControlAPIDiscover nothing" unless dwDiscoverMsg
366
- dwAttachMsg = RegisterWindowMessage.call("SkypeControlAPIAttach")
367
- raise SkypeAPIError::Attach,"SkypeControlAPIAttach nothing" unless dwAttachMsg
368
- @wmHandler.instance_variable_set :@dwAttachMsg, dwAttachMsg
369
- @wmHandler.addEvent dwAttachMsg
370
- #post?
371
- unless PostMessage.call(HWND_BROADCAST, dwDiscoverMsg, @wmHandler.hWnd, 0)
372
- raise SkypeAPIError::Attach,"SkypeControlAPIDiscover broadcast fail"
373
- end
374
- return true
375
- end
376
-
377
- def sendMSG cmd
378
- unless @wmHandler.hSkypeAPIWindowHandle
379
- raise SkypeAPIErorr::Connect,"NullPointerException SendSkype!"
380
- return false
381
- end
382
-
383
- cmd = '#' + @sendCount.to_s + ' ' + cmd
384
- @wmBuffer[@sendCount] = cmd
385
- @sent = true if @singleThread
386
- pCopyData = @wmHandler.application.arg2cstructStr("LLS",0,@wmBuffer[@sendCount].length+1,@wmBuffer[@sendCount])
387
- unless PostMessage.call(@wmHandler.hWnd, WM_USER_MSG, @wmHandler.hSkypeAPIWindowHandle, pCopyData)
388
- @wmHandler.instance_variable_set :@hSkypeAPIWindowHandle,nil
389
- raise SkypeAPIError::Connect,"Skype not ready"
390
- return false;
391
- end
392
- @queue.push(proc{doEvent(:sent, cmd)}) if existEvent? :sent
393
- @sendCount+=1
394
- return true
395
- end
396
-
397
-
398
- def wait someAction=nil
399
- if someAction.class == Proc
400
- @wmHandler.application.messageloop do
401
- queueProcess
402
- someAction.call
403
- end
404
- elsif block_given?
405
- @wmHandler.application.messageloop do
406
- queueProcess
407
- yield
408
- end
409
- else
410
- @wmHandler.application.messageloop{queueProcess}
411
- end
412
- end
413
-
414
- def polling
415
- @wmHandler.application.doevents
416
- queueProcess
417
- end
418
-
419
- def close
420
- @wmHandler.close
421
- end
422
- end
423
-
424
- class Mac
425
- #wiki like
426
- def initialize
427
- raise SkypeAPIError::NotImplement
428
- end
429
- end
430
-
431
- class Linux
432
- #plz write it
433
- def initialize
434
- raise SkypeAPIError::NotImplement
435
- end
436
- end
437
- end
438
-
439
- end