Class: Cyc::Connection::SynchronyDriver
- Inherits:
-
Object
- Object
- Cyc::Connection::SynchronyDriver
- Defined in:
- lib/cyc/connection/synchrony.rb
Overview
EM-Synchrony Cyc::Client driver
- Author
-
Rafal Michalski ([email protected])
- Licence
-
MIT/X11 License
Requires: igrigorik/em-synchrony
To use this driver simply require this file e.g.
require 'cyc/connection/synchrony'
beside ‘cycr’
If required before ‘cycr’ then the SocketDriver will not be loaded (however you can still load it by hand: require ‘cyc/connection/socket’ but be carefull though it will override default Cyc::Connection.driver).
If required after ‘cycr’ then the SocketDriver will be preserved but the default driver will be set to SynchronyDriver.
Async fiber example:
require 'cyc/connection/synchrony'
require 'cycr'
EM.synchrony do
cyc = EM::Synchrony::ConnectionPool.new(size: 5) do
Cyc::Client.new :url => 'cyc://localhost:3601', :debug => true
end
puts cyc.driver, cyc.driver.type.inspect
Fiber.new do
puts "Ani", cyc.fi_complete("Ani").inspect
end.resume
puts "Mi", cyc.talk('(fi-complete "Mi")').inspect
EM.stop
end
‘Mi` will arrive before `Ani`
Warning: always use EM::Synchrony::ConnectionPool to handle Fiber concurrency race conditions.
Class Method Summary collapse
-
.type ⇒ Object
The type of the driver, i.e.
Instance Method Summary collapse
-
#connect(host, port, timeout) ⇒ Object
Connects to the server on
host
andport
with given connection time-out. -
#connected? ⇒ Boolean
Returns true if the driver is connected to the server.
-
#disconnect ⇒ Object
Disconnects the driver from the server.
-
#initialize ⇒ SynchronyDriver
constructor
Initialize a new driver.
-
#read ⇒ Object
Read next message from the server.
-
#write(rawmsg) ⇒ Object
Send a message to the server.
Constructor Details
#initialize ⇒ SynchronyDriver
Initialize a new driver.
106 107 108 |
# File 'lib/cyc/connection/synchrony.rb', line 106 def initialize @connection = nil end |
Class Method Details
.type ⇒ Object
The type of the driver, i.e. :synchrony
.
103 |
# File 'lib/cyc/connection/synchrony.rb', line 103 def self.type; :synchrony; end |
Instance Method Details
#connect(host, port, timeout) ⇒ Object
Connects to the server on host
and port
with given connection time-out.
117 118 119 120 121 122 123 |
# File 'lib/cyc/connection/synchrony.rb', line 117 def connect(host, port, timeout) conn = EventMachine.connect(host, port, ConnectionClient) do |c| c.pending_connect_timeout = [Float(timeout), 0.1].max end setup_connect_callbacks(conn, Fiber.current) end |
#connected? ⇒ Boolean
Returns true if the driver is connected to the server.
111 112 113 |
# File 'lib/cyc/connection/synchrony.rb', line 111 def connected? @connection && @connection.connected? end |
#disconnect ⇒ Object
Disconnects the driver from the server.
126 127 128 129 |
# File 'lib/cyc/connection/synchrony.rb', line 126 def disconnect @connection.close_connection @connection = nil end |
#read ⇒ Object
Read next message from the server.
137 138 139 140 141 142 143 144 |
# File 'lib/cyc/connection/synchrony.rb', line 137 def read status, answer, msg = @connection.read if status == :error raise answer else return [status, answer, msg] end end |
#write(rawmsg) ⇒ Object
Send a message to the server.
132 133 134 |
# File 'lib/cyc/connection/synchrony.rb', line 132 def write(rawmsg) @connection.send(rawmsg) end |