Class: Wine::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/wine/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Session

Returns a new instance of Session.



20
21
22
# File 'lib/wine/session.rb', line 20

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(host, port) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/wine/session.rb', line 9

def self.connect(host, port)
  begin
    socket     = TCPSocket.new(host, port)
    connection = Connection.new(socket)

    new(connection)
  rescue Errno::ECONNREFUSED
    raise ConnectionRefused
  end
end

Instance Method Details

#closeObject



61
62
63
# File 'lib/wine/session.rb', line 61

def close
  @connection.close
end

#get(key, timeout = Connection::DEFAULT_TIMEOUT) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wine/session.rb', line 41

def get(key, timeout = Connection::DEFAULT_TIMEOUT)
  request = Message::Get.new(:key_data => key)
  @connection.send(request)

  response = @connection.recv(timeout)
  raise ResponseTimeout unless response

  case response.msg_type
  when Message::VALUE
    response.value_data
  else
    raise ProtocolError
  end
end

#login(username, password, timeout = Connection::DEFAULT_TIMEOUT) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wine/session.rb', line 24

def (username, password, timeout = Connection::DEFAULT_TIMEOUT)
  request = Message::Login.new(:username => username, :password => password)
  @connection.send(request)

  response = @connection.recv(timeout)
  raise ResponseTimeout unless response

  case response.msg_type
  when Message::LOGIN_ACCEPTED
    true
  when Message::LOGIN_REJECTED
    false
  else
    raise ProtocolError
  end
end

#set(key, value) ⇒ Object



56
57
58
59
# File 'lib/wine/session.rb', line 56

def set(key, value)
  request = Message::Set.new(:key_data => key, :value_data => value)
  @connection.send(request)
end