Class: CIMD::Loop
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- CIMD::Loop
- 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
-
#conn ⇒ Object
Returns the value of attribute conn.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#keep_alive_counter ⇒ Object
Returns the value of attribute keep_alive_counter.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#no_activity_counter ⇒ Object
Returns the value of attribute no_activity_counter.
-
#options ⇒ Object
Returns the value of attribute options.
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #change_state(new_state) ⇒ Object
- #connection_completed ⇒ Object
-
#initialize ⇒ Loop
constructor
A new instance of Loop.
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
-
#sending_message(message) ⇒ Object
post_init.
- #sending_response_message(message) ⇒ Object
- #to_log(message) ⇒ Object
-
#unbind ⇒ Object
receive_data.
Constructor Details
#initialize ⇒ Loop
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
#conn ⇒ Object
Returns the value of attribute conn.
16 17 18 |
# File 'lib/cimd_loop.rb', line 16 def conn @conn end |
#debug ⇒ Object
Returns the value of attribute debug.
21 22 23 |
# File 'lib/cimd_loop.rb', line 21 def debug @debug end |
#keep_alive_counter ⇒ Object
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 |
#logfile ⇒ Object
Returns the value of attribute logfile.
20 21 22 |
# File 'lib/cimd_loop.rb', line 20 def logfile @logfile end |
#messages ⇒ Object
Returns the value of attribute messages.
15 16 17 |
# File 'lib/cimd_loop.rb', line 15 def @messages end |
#no_activity_counter ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
19 20 21 |
# File 'lib/cimd_loop.rb', line 19 def @options end |
#state ⇒ Object
Returns the value of attribute state.
14 15 16 |
# File 'lib/cimd_loop.rb', line 14 def state @state end |
Class Method Details
.logout_request ⇒ Object
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_completed ⇒ Object
111 112 113 |
# File 'lib/cimd_loop.rb', line 111 def connection_completed change_state(:connected) end |
#post_init ⇒ Object
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{ || ()} when :login_sucessfull change_state(:ready_to_send) when :ready_to_send change_state(:suspend) @messages.pop{ || ()} when :no_activity,:logout_done close_connection when :alive change_state(:suspend) to_log("<<<< #{CIMD::}") (CIMD::) when :logout_request @messages = EM::Queue.new to_log("<<<< #{CIMD::}") .push(CIMD::); 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.parse(line) to_log(">>>> #{.to_s}") # sprawdzic checsum if .is_binary? .parse_binary_data to_log("**** Decoded binary: #{.to_s}") end @no_activity_counter = Time.now.tv_sec case .operation_code when CIMD::OP_LOGIN_RESPONSE if .has_error? to_log("!!!! Error: #{.error}") close_connection else change_state(:login_sucessfull) end when CIMD::OP_SUBMIT_RESPONSE if .has_error? to_log("!!!! Error: #{.error}") end change_state(:ready_to_send) when CIMD::OP_DELIVERY_STATUS_REPORT () when CIMD::OP_DELIVERY_MESSAGE () .push(CIMD::(@conn,.parameter_value(CIMD::P_ORIGINATOR_ADDRESS),.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: #{.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 () .packet_number = @conn.packet_number! to_log("<<<< #{.to_s}") send_data .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 () m = CIMD::() 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() #puts @debug #puts @logfile puts "#{Time.now.strftime("%Y-%m-%d/%H:%M:%S")} #{}" #puts message if @debug #@logfile.puts message if @logfile $stdout.flush end |
#unbind ⇒ Object
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 |