Class: Cyc::Connection::SocketDriver
- Inherits:
-
Object
- Object
- Cyc::Connection::SocketDriver
- Defined in:
- lib/cyc/connection/socket.rb
Overview
TCPSocket Cyc::Client driver
- Author
-
Rafal Michalski ([email protected])
- Licence
-
MIT/X11 License
Default driver for Cyc::Client.
Class Method Summary collapse
-
.type ⇒ Object
The type of the driver, i.e.
Instance Method Summary collapse
-
#connect(host, port, timeout = 0.2) ⇒ Object
Connects to the server on
host
andport
with given connectiontimeout
. -
#connected? ⇒ Boolean
Returns true if the driver is connected to the server.
-
#disconnect ⇒ Object
Disconnects the driver from the server.
-
#initialize ⇒ SocketDriver
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 ⇒ SocketDriver
Initialize a new driver.
19 20 21 22 23 |
# File 'lib/cyc/connection/socket.rb', line 19 def initialize @sock = nil @buffer = DataBuffer.new @last_message = nil end |
Class Method Details
.type ⇒ Object
The type of the driver, i.e. :socket
.
16 |
# File 'lib/cyc/connection/socket.rb', line 16 def self.type; :socket; end |
Instance Method Details
#connect(host, port, timeout = 0.2) ⇒ Object
Connects to the server on host
and port
with given connection timeout
.
32 33 34 35 36 37 38 39 |
# File 'lib/cyc/connection/socket.rb', line 32 def connect(host, port, timeout=0.2) with_timeout(timeout.to_f) do @sock = TCPSocket.new(host, port) @sock.sync = true @sock.binmode #@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 end end |
#connected? ⇒ Boolean
Returns true if the driver is connected to the server.
26 27 28 |
# File 'lib/cyc/connection/socket.rb', line 26 def connected? !! @sock end |
#disconnect ⇒ Object
Disconnects the driver from the server.
42 43 44 45 46 47 48 49 |
# File 'lib/cyc/connection/socket.rb', line 42 def disconnect @sock.close if @sock rescue # This should go to log #2. ensure @sock = nil @buffer.discard! end |
#read ⇒ Object
Read next message from the server.
64 65 66 67 68 69 70 71 72 |
# File 'lib/cyc/connection/socket.rb', line 64 def read begin @buffer << @sock.readpartial(4096) end until result = @buffer.next_result(@last_message) result << @last_message rescue IOError, EOFError, Errno::ECONNRESET disconnect raise Errno::ECONNRESET end |
#write(rawmsg) ⇒ Object
Send a message to the server.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cyc/connection/socket.rb', line 52 def write(rawmsg) @last_message = rawmsg @sock.write(rawmsg + EOL) # ensure that the connection is still with a server # and wait for an answer at the same time if @sock.eof? disconnect raise Errno::ECONNRESET end end |