Class: Cmdserver::TCPCommandServer
- Inherits:
-
Object
- Object
- Cmdserver::TCPCommandServer
- Defined in:
- lib/cmdserver.rb
Overview
}}}
Instance Attribute Summary collapse
-
#reload_settings ⇒ Object
writeonly
Sets the attribute reload_settings.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#socket ⇒ Object
{{{.
Instance Method Summary collapse
- #_update_ ⇒ Object
-
#_update_loop_ ⇒ Object
“Private” schelued updater.
-
#initialize(port, hash = {}, settings = nil, debug = false) ⇒ TCPCommandServer
constructor
A new instance of TCPCommandServer.
- #load_cmd_proto ⇒ Object
-
#process_client(client) ⇒ Object
{{{.
- #registerCallback(string, aproc) ⇒ Object
-
#start ⇒ Object
Start the local updater, and the socket => thread loop.
Constructor Details
#initialize(port, hash = {}, settings = nil, debug = false) ⇒ TCPCommandServer
Returns a new instance of TCPCommandServer.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cmdserver.rb', line 66 def initialize(port, hash={}, settings=nil, debug=false) @socket = TCPServer.new(port) @reload_settings = false # Used from Signal.trap to allow module reloading @cmd_hash = hash # hash of commands @settings = settings @debug = debug if @settings.nil? @settings = Settings.new() end @settings.load_modules() load_cmd_proto() end |
Instance Attribute Details
#reload_settings=(value) ⇒ Object (writeonly)
Sets the attribute reload_settings
64 65 66 |
# File 'lib/cmdserver.rb', line 64 def reload_settings=(value) @reload_settings = value end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
63 64 65 |
# File 'lib/cmdserver.rb', line 63 def settings @settings end |
#socket ⇒ Object
{{{
62 63 64 |
# File 'lib/cmdserver.rb', line 62 def socket @socket end |
Instance Method Details
#_update_ ⇒ Object
102 103 104 105 106 107 |
# File 'lib/cmdserver.rb', line 102 def _update_ if @reload_settings @settings.load_modules() @reload_settings = false end end |
#_update_loop_ ⇒ Object
“Private” schelued updater. Allows for loading of modules without having to restart the server
95 96 97 98 99 100 |
# File 'lib/cmdserver.rb', line 95 def _update_loop_ loop do sleep 2 _update_() end end |
#load_cmd_proto ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/cmdserver.rb', line 79 def load_cmd_proto() phash = Cmdserver::Cmdprotocol.get_protocol_hash() phash.each_key do |key| @cmd_hash[key] = phash[key] end puts phash if @debug end |
#process_client(client) ⇒ Object
{{{
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 164 165 166 167 168 |
# File 'lib/cmdserver.rb', line 131 def process_client(client)# {{{ #NOTE: This should remain as an isolated thread process loop do request = client.gets # TODO: # request = Cmdserver::Connection.client_to_server(request) if not request.nil? request.chomp! puts "Got '#{request}'" if @debug real_key = nil @cmd_hash.each_key do |key| if request.include? key real_key = key end end puts "real_key:#{real_key}" if @debug if not real_key.nil? and request.start_with? real_key begin request.sub! real_key, "" request.lstrip! request.chomp! if @cmd_hash.key? real_key puts "Request after processing: #{request}" if @debug @cmd_hash[real_key].call(client, request) end rescue Exception puts "ERROR: #{$!}" raise $! end else Cmdserver::Cmdprotocol.default(client, request) end else client.close() break end end end |
#registerCallback(string, aproc) ⇒ Object
87 88 89 |
# File 'lib/cmdserver.rb', line 87 def registerCallback(string, aproc) @cmd_hash[string] = aproc end |
#start ⇒ Object
Start the local updater, and the socket => thread loop
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cmdserver.rb', line 111 def start() # Start the local updater Thread.new{ _update_loop_() } loop do begin client = @socket.accept # update so that clients get the updated module # prevents race condition between connection # and the updater loop # Thanks to the variable, the modules will not get # loaded twice in a single update _update_() rescue SystemCallError exit -1 end Thread.new{ process_client(client) } end end |