Class: Poliqarp::Connector
- Inherits:
-
Object
- Object
- Poliqarp::Connector
- Includes:
- Ruby19
- Defined in:
- lib/poliqarpr/connector.rb
Overview
- Author
-
Aleksander Pohl ([email protected])
- License
-
MIT License
This class hold the TCP connection to the server and is responsible for dispatching synchronous and asynchronous queries and answers.
Constant Summary collapse
- ERRORS =
Error messages assigned to error codes (taken from poliqarpd implementation)
{ 1 => "Incorrect number of arguments", 3 => "No session opened", 4 => "Cannot create a session for a connection that", 5 => "Not enough memory", 6 => "Invalid session ID", 7 => "Session with this ID is already bound", 8 => "Session user ID does not match the argument", 10 => "Session already has an open corpus", 12 => "System error while opening the corpus", 13 => "No corpus opened", 14 => "Invalid job ID", 15 => "A job is already in progress", 16 => "Incorrect query", 17 => "Invalid result range", 18 => "Incorrect session option", 19 => "Invalid session option value", 20 => "Invalid sorting criteria" }
- UTF8 =
"utf-8"
Instance Method Summary collapse
-
#initialize(client) ⇒ Connector
constructor
Creates new connector.
-
#open(host, port) ⇒ Object
Opens connection with poliqarp server which runs on given
host
andport
. -
#read_message ⇒ Object
Retrives one message from the server.
-
#send_message(message, mode, &handler) ⇒ Object
Sends message to the poliqarp server.
Methods included from Ruby19
Constructor Details
#initialize(client) ⇒ Connector
Creates new connector
40 41 42 43 44 45 |
# File 'lib/poliqarpr/connector.rb', line 40 def initialize(client) @message_queue = Queue.new @socket_mutex = Mutex.new @loop_mutex = Mutex.new @client = client end |
Instance Method Details
#open(host, port) ⇒ Object
Opens connection with poliqarp server which runs on given host
and port
.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/poliqarpr/connector.rb', line 49 def open(host,port) @socket_mutex.synchronize { @socket = TCPSocket.new(host,port) if @socket.nil? } running = nil @loop_mutex.synchronize { running = @loop_running } main_loop unless running @loop_mutex.synchronize { @loop_running = true } end |
#read_message ⇒ Object
Retrives one message from the server. If the message indicates an error, new runtime error containing the error description is returned.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/poliqarpr/connector.rb', line 83 def = @message_queue.shift if =~ /^ERR/ code = .match(/\d+/)[0].to_i case code when 14 raise InvalidJobId.new() when 15 raise JobInProgress.new() else raise PoliqarpException.new("Poliqarp Error: "+ERRORS[code]) end else end end |
#send_message(message, mode, &handler) ⇒ Object
Sends message to the poliqarp server. Returns the first synchronous answer of the server.
-
message
the message to send -
mode
synchronous (+:sync:) or asynchronous (:async
) -
handler
the handler of the asynchronous message
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/poliqarpr/connector.rb', line 68 def (, mode, &handler) @client.debug{"send #{mode} #{}"} if ruby19? massage = .encode(UTF8) end @socket.write(+"\n") if mode == :async @handler = handler end end |