Class: Orchestrator::Device::MakebreakConnection

Inherits:
UV::OutboundConnection
  • Object
show all
Defined in:
lib/orchestrator/device/transport_makebreak.rb

Instance Method Summary collapse

Instance Method Details

#disconnectObject



156
157
158
159
160
# File 'lib/orchestrator/device/transport_makebreak.rb', line 156

def disconnect
    @connected = false
    @disconnecting = true
    close_connection(:after_writing)
end

#on_closeObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/orchestrator/device/transport_makebreak.rb', line 81

def on_close
    @delaying = false
    @connected = false
    @changing_state = false
    @disconnecting = false


    if @connecting
        @connecting.cancel
        @connecting = nil
    end

    # Prevent re-connect if terminated
    unless @terminated
        @retries += 1
        the_time = @processor.thread.now
        boundry = @last_retry + @config[:thrashing_threshold]

        # ensure we are not thrashing (rapid connect then disconnect)
        # This equals a disconnect and requires a warning
        if @retries == 1 && boundry >= the_time
            @retries += 1
            @manager.logger.warn('possible connection thrashing. Disconnecting')
        end

        @activity.cancel if @activity
        @activity = nil

        if @retries == 1
            if @write_queue.length > 0
                # We reconnect here as there are pending writes
                @last_retry = the_time
                reconnect
            end
        else # retries > 1
            @write_queue.clear

            variation = 1 + rand(2000)
            @connecting = @manager.get_scheduler.in(3000 + variation) do
                @connecting = nil
                reconnect
            end

            # we mark the queue as offline if more than 1 reconnect fails
            #  or if the first connect fails
            if @retries == 2 || (@retries == 3 && @last_retry == 0)
                @processor.disconnected
                @processor.queue.offline(@config[:clear_queue_on_disconnect])
            end
        end
    end
end

#on_connect(transport) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/orchestrator/device/transport_makebreak.rb', line 55

def on_connect(transport)
    @connected = true
    @changing_state = false

    if @connecting
        @connecting.cancel
        @connecting = nil
    end

    if @terminated
        close_connection(:after_writing)
    else
        begin
            use_tls(@config) if @tls
        rescue => e
            @manager.logger.print_error(e, 'error starting tls')
        end

        if @config[:wait_ready]
            @delaying = ''
        else
            init_connection
        end
    end
end

#on_read(data, *args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/orchestrator/device/transport_makebreak.rb', line 134

def on_read(data, *args)
    if @delaying
        @delaying << data
        result = @delaying.split(@config[:wait_ready], 2)
        if result.length > 1
            @delaying = false
            init_connection
            rem = result[-1]
            @processor.buffer(rem) unless rem.empty?
        end
    else
        @processor.buffer(data)
    end
end

#post_init(manager, processor, tls) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/orchestrator/device/transport_makebreak.rb', line 5

def post_init(manager, processor, tls)
    @manager = manager
    @processor = processor
    @config = @processor.config
    @tls = tls

    @connected = false
    @changing_state = true
    @disconnecting = false
    @last_retry = 0


    @activity = nil     # Activity timer
    @connecting = nil   # Connection timer
    @retries = 2        # Connection retries
    @write_queue = []

    @timeout = method(:timeout)
    @reset_timeout = method(:reset_timeout)
end

#terminateObject



149
150
151
152
153
154
# File 'lib/orchestrator/device/transport_makebreak.rb', line 149

def terminate
    @terminated = true
    @connecting.cancel if @connecting
    @activity.cancel if @activity
    close_connection(:after_writing) if @transport.connected
end

#transmit(cmd) ⇒ Object



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
# File 'lib/orchestrator/device/transport_makebreak.rb', line 26

def transmit(cmd)
    return if @terminated

    data = cmd[:data]

    if @connected
        promise = write(data)
        reset_timeout
        if cmd[:wait]
            promise.catch do |err|
                if @processor.queue.waiting == cmd
                    # Fail fast
                    @processor.thread.next_tick do
                        @processor.__send__(:resp_failure, err)
                    end
                else
                    cmd[:defer].reject(err)
                end
            end
        end
    elsif @retries < 2
        @write_queue << cmd
        reconnect unless @disconnecting
    else
        cmd[:defer].reject(Error::CommandFailure.new "transmit aborted as disconnected")
    end
    # discards data when officially disconnected
end