Class: CIMD::Loop

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/cimd_loop.rb

Constant Summary collapse

STATE =
[:not_connected,:connected,:banner_received,:ready_to_send,:suspend]
MAX_NO_ACTIVITY_TIME =
180
@@logout_request =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoop

Returns a new instance of Loop.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cimd_loop.rb', line 43

def initialize
  @state = :not_connected
  @debug = false
  @log = nil
  @keep_alive_counter = Time.now.tv_sec
  @no_activity_counter = Time.now.tv_sec
  #@debug = true if @options[:debug]
  #begin
  #  @log = File.new("ss", "w") if options[:logfile]
  #rescue Exception => e
  #  puts e.to_s
  #  exit
  #end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



16
17
18
# File 'lib/cimd_loop.rb', line 16

def conn
  @conn
end

#debugObject

Returns the value of attribute debug.



21
22
23
# File 'lib/cimd_loop.rb', line 21

def debug
  @debug
end

#keep_alive_counterObject

Returns the value of attribute keep_alive_counter.



17
18
19
# File 'lib/cimd_loop.rb', line 17

def keep_alive_counter
  @keep_alive_counter
end

#logfileObject

Returns the value of attribute logfile.



20
21
22
# File 'lib/cimd_loop.rb', line 20

def logfile
  @logfile
end

#messagesObject

Returns the value of attribute messages.



15
16
17
# File 'lib/cimd_loop.rb', line 15

def messages
  @messages
end

#no_activity_counterObject

Returns the value of attribute no_activity_counter.



18
19
20
# File 'lib/cimd_loop.rb', line 18

def no_activity_counter
  @no_activity_counter
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/cimd_loop.rb', line 19

def options
  @options
end

#stateObject

Returns the value of attribute state.



14
15
16
# File 'lib/cimd_loop.rb', line 14

def state
  @state
end

Class Method Details

.logout_requestObject



39
40
41
# File 'lib/cimd_loop.rb', line 39

def self.logout_request
  @@logout_request = true
end

Instance Method Details

#change_state(new_state) ⇒ Object



32
33
34
35
36
37
# File 'lib/cimd_loop.rb', line 32

def change_state(new_state)
  if @state != new_state
    to_log("#### Transition: #{@state.to_s} ==> #{new_state.to_s} (no_act:#{ Time.now.tv_sec - @no_activity_counter}, keep:#{Time.now.tv_sec - @keep_alive_counter}, queue:#{@messages.size})")
    @state = new_state
  end
end

#connection_completedObject



111
112
113
# File 'lib/cimd_loop.rb', line 111

def connection_completed
  change_state(:connected)
end

#post_initObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cimd_loop.rb', line 58

def post_init
  
  @timer = EM.add_periodic_timer(1) do
  
  if @@logout_request == true 
    change_state(:logout_request)
    @@logout_request = false
  end
  
  change_state(:alive) if Time.now.tv_sec - @keep_alive_counter > @conn.keep_alive
  change_state(:no_activity) if Time.now.tv_sec - @no_activity_counter > MAX_NO_ACTIVITY_TIME
  
  #puts "No act:#{ Time.now.tv_sec - @no_activity_counter} Keep:#{Time.now.tv_sec - @keep_alive_counter} Queue: #{@messages.size}"
  case @state
    when :banner_received
      change_state(:login_request)
      @messages.pop{ |message|  sending_message(message)}
    when :login_sucessfull
      change_state(:ready_to_send)
    when :ready_to_send
      change_state(:suspend)
      @messages.pop{ |message|  sending_message(message)}
    when :no_activity,:logout_done
      close_connection
    when :alive
      change_state(:suspend)
      to_log("<<<< #{CIMD::alive_message}")
      sending_message(CIMD::alive_message)
    when :logout_request
      @messages = EM::Queue.new
      to_log("<<<< #{CIMD::logout_message}")
      messages.push(CIMD::logout_message);
      change_state(:ready_to_send)
    when :suspend
      #puts "Suspend"
    end
  end
  
end

#receive_data(data) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cimd_loop.rb', line 115

def receive_data(data)
#puts "Received plain data: #{data}"
  
  case data
    when /CIMD2-A/
      change_state(:banner_received)
    else
  
      (@buffer ||= BufferedTokenizer.new("\003")).extract(data).each do |line|
      message = Message.parse(line)
  
      to_log(">>>> #{message.to_s}")
      # sprawdzic checsum
      if message.is_binary?
        message.parse_binary_data
        to_log("**** Decoded binary: #{message.to_s}")
      end
      @no_activity_counter = Time.now.tv_sec
  
      case message.operation_code
        when CIMD::OP_LOGIN_RESPONSE
          if message.has_error?
            to_log("!!!! Error: #{message.error}")
            close_connection
          else
            change_state(:login_sucessfull)
          end
        when CIMD::OP_SUBMIT_RESPONSE
          if message.has_error?
            to_log("!!!! Error: #{message.error}")
          end
          change_state(:ready_to_send)
        when CIMD::OP_DELIVERY_STATUS_REPORT
          sending_response_message(message)
        when CIMD::OP_DELIVERY_MESSAGE
          sending_response_message(message)
          messages.push(CIMD::submit_text_message(@conn,message.parameter_value(CIMD::P_ORIGINATOR_ADDRESS),message.parameter_value(CIMD::P_USER_DATA)))
        when CIMD::OP_LOGOUT_RESPONSE
          change_state(:logout_done)
        when CIMD::OP_ALIVE_RESPONSE
        when CIMD::OP_GENERAL_ERROR_RESPONSE
          to_log("!!!! Error: #{message.error}")
          close_connection
      else
        to_log("???? Unknown message type: #{data}")
      end # case message.operation_code
    end
  end # case data
end

#sending_message(message) ⇒ Object

post_init



98
99
100
101
102
103
# File 'lib/cimd_loop.rb', line 98

def sending_message(message)
  message.packet_number = @conn.packet_number!
  to_log("<<<< #{message.to_s}")
  send_data message.to_binary
  @keep_alive_counter = Time.now.tv_sec
end

#sending_response_message(message) ⇒ Object



105
106
107
108
109
# File 'lib/cimd_loop.rb', line 105

def sending_response_message(message)
  m = CIMD::only_response_message(message)
  to_log("<<<< #{m.to_s}")
  send_data m.to_binary
end

#to_log(message) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/cimd_loop.rb', line 23

def to_log(message)
  #puts @debug
  #puts @logfile
  puts "#{Time.now.strftime("%Y-%m-%d/%H:%M:%S")} #{message}"
  #puts message if @debug
  #@logfile.puts message if @logfile
  $stdout.flush
end

#unbindObject

receive_data



165
166
167
168
169
# File 'lib/cimd_loop.rb', line 165

def unbind
  @logfile.close unless @logfile.nil?
  change_state(:not_connected)
  EventMachine.stop_event_loop
end