Class: Poliqarp::Connector

Inherits:
Object
  • Object
show all
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

Methods included from Ruby19

#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_messageObject

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 read_message
  message = @message_queue.shift
  if message =~ /^ERR/
    code = message.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
    message
  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 send_message(message, mode, &handler)
  @client.debug{"send #{mode} #{message}"}
  if ruby19?
    massage = message.encode(UTF8)
  end
  @socket.write(message+"\n")
  if mode == :async
    @handler = handler
  end
  read_message
end