Class: NServer::Client
- Inherits:
-
Object
- Object
- NServer::Client
- Defined in:
- lib/nserver.rb
Instance Attribute Summary collapse
-
#host_ip ⇒ Object
readonly
Returns the value of attribute host_ip.
-
#host_port ⇒ Object
readonly
Returns the value of attribute host_port.
Class Method Summary collapse
-
.notify(msg, priority = :normal) ⇒ Object
Connect to the currently running NServer instance and add the message to the queue.
-
.try_notify(msg, priority = :normal) ⇒ Object
Connect to the currently running NServer, if any, and add the message.
-
.with_notification(msg) ⇒ Object
Run the block, then notify (with errors).
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Client
constructor
Create a new client.
-
#notify(msg, priority = :normal) ⇒ Object
Send a notification.
- #try_notify(msg, priority = :normal) ⇒ Object
- #with_notification(msg) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Create a new client
Options:
- host
-
The host to connect to. Default is 127.0.0.1
- post
-
The port to connect to. Default is 10001.
190 191 192 193 |
# File 'lib/nserver.rb', line 190 def initialize( opts = {} ) @host_ip = ( opts[:host] or '127.0.0.1' ) @host_port = ( opts[:port] or '10001' ).to_i end |
Instance Attribute Details
#host_ip ⇒ Object (readonly)
Returns the value of attribute host_ip.
179 180 181 |
# File 'lib/nserver.rb', line 179 def host_ip @host_ip end |
#host_port ⇒ Object (readonly)
Returns the value of attribute host_port.
180 181 182 |
# File 'lib/nserver.rb', line 180 def host_port @host_port end |
Class Method Details
.notify(msg, priority = :normal) ⇒ Object
Connect to the currently running NServer instance and add the message to the queue. If no server, raise a NoServerAvailable error.
230 231 232 |
# File 'lib/nserver.rb', line 230 def Client.notify( msg, priority = :normal ) Client.new.notify(msg, priority) end |
.try_notify(msg, priority = :normal) ⇒ Object
Connect to the currently running NServer, if any, and add the message. If no server is available, just return false.
236 237 238 |
# File 'lib/nserver.rb', line 236 def Client.try_notify( msg, priority = :normal ) Client.new.try_notify(msg, priority) end |
.with_notification(msg) ⇒ Object
Run the block, then notify (with errors).
241 242 243 244 |
# File 'lib/nserver.rb', line 241 def Client.with_notification( msg ) yield notify( msg ) end |
Instance Method Details
#notify(msg, priority = :normal) ⇒ Object
Send a notification. If ‘msg` is a Message object, send it, and ignore supplied priority
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/nserver.rb', line 197 def notify( msg, priority = :normal ) begin sock = TCPSocket.new(@host_ip, @host_port) sleep(0.1) ## Needs delay, or wait until prompt. if msg.is_a? NServer::Message sock.write( msg.to_yaml + "\n.\n" ) else sock.write("#{priority.to_s.upcase}:#{msg}\n.\n") end sock.close rescue Errno::ECONNREFUSED raise NoServerAvailable.new end end |
#try_notify(msg, priority = :normal) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/nserver.rb', line 213 def try_notify(msg, priority = :normal ) begin notify(msg, priority) rescue NoServerAvailable return nil end return true end |
#with_notification(msg) ⇒ Object
222 223 224 225 |
# File 'lib/nserver.rb', line 222 def with_notification( msg ) yield notify(msg) end |