Class: Async

Inherits:
Object
  • Object
show all
Defined in:
lib/bellite.rb

Overview

Simple implementation of Python Asyncore

Constant Summary collapse

@@map =

!attribute @@map List of objects, implements asyncore methods

[]

Class Method Summary collapse

Class Method Details

.check(timeout = false, map = false) ⇒ Object

Checks sockets from map objects using IO.select

Parameters:

  • timeout (Float) (defaults to: false)

    timeout in seconds, passed to IO.select

  • map (Hash) (defaults to: false)

    list of objects implements asyncore methods



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bellite.rb', line 17

def Async.check(timeout=false, map=false)
    if not map
        map = @@map
    end
    readable = []
    writable = []
    excepted = []

    changedCount = 0
    map.each do |obj|
        if obj.writable?
            if obj.fileno
                writable << obj.fileno
            end
        end

        if obj.readable?
            if obj.fileno
                readable << obj.fileno
            end
        end

        if obj.exceptable?
            if obj.fileno
                excepted << obj.fileno
            end
        end
    end

    if readable.size == 0 and writable.size == 0 and excepted.size == 0
        return false
    end

    if (timeout)
        r,w,e = IO.select(readable, writable, excepted,timeout)
    else
        r,w,e = IO.select(readable, writable, excepted)
    end
    map.each do |obj|
        if obj.writable? and w.include? obj.fileno
            obj.handle_write_event
        end
        if obj.readable? and r.include? obj.fileno
            obj.handle_read_event
        end
        if obj.exceptable? and e.include? obj.fileno
            obj.handle_expt_event
        end
    end

    return r.size + w.size + e.size
end

.loop(timeout, map = false) ⇒ Object

Running check while at least one socket in objects map is connected

Parameters:

  • timeout (Float)
    • timeout, passed to check
  • map, (Hash)

    passed to check



73
74
75
# File 'lib/bellite.rb', line 73

def Async.loop(timeout, map=false)
    return Async.check(timeout, map) != false
end