Class: I2P::SAM::Client
- Inherits:
-
Object
- Object
- I2P::SAM::Client
- Defined in:
- lib/i2p/sam/client.rb
Overview
**I2P Simple Anonymous Messaging (SAM) V3 client.**
Instance Attribute Summary collapse
-
#socket ⇒ TCPSocket
readonly
Returns the socket connection to the SAM bridge.
Class Method Summary collapse
-
.open(options = {}) {|client| ... } ⇒ void
Establishes a connection to the SAM bridge.
Instance Method Summary collapse
-
#connect ⇒ void
(also: #reconnect)
Establishes a connection to the SAM bridge.
-
#connected? ⇒ Boolean
Returns ‘true` if a connection to the SAM bridge has been established and is active.
-
#disconnect ⇒ void
(also: #close)
Closes the connection to the SAM bridge.
-
#generate_dest ⇒ KeyPair
(also: #generate_destination)
Generates a new I2P destination and returns the key pair for it.
-
#hello(options = {}) ⇒ Float
Performs the SAM protocol handshake and returns the autonegotiated protocol version.
-
#initialize(options = {}) {|client| ... } ⇒ Client
constructor
Initializes a new client instance.
-
#lookup_name(name) ⇒ Destination
Returns the I2P destination corresponding to ‘name`.
Constructor Details
#initialize(options = {}) {|client| ... } ⇒ Client
Initializes a new client instance.
63 64 65 66 67 68 69 70 |
# File 'lib/i2p/sam/client.rb', line 63 def initialize( = {}, &block) @options = .dup @host = (@options.delete(:host) || DEFAULT_HOST).to_s @port = (@options.delete(:port) || DEFAULT_PORT).to_i @version = (@options.delete(:version) || PROTOCOL_VERSION).to_i block.call(self) if block_given? end |
Instance Attribute Details
#socket ⇒ TCPSocket (readonly)
Returns the socket connection to the SAM bridge.
52 53 54 |
# File 'lib/i2p/sam/client.rb', line 52 def socket @socket end |
Class Method Details
.open(options = {}) {|client| ... } ⇒ void
This method returns an undefined value.
Establishes a connection to the SAM bridge.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/i2p/sam/client.rb', line 32 def self.open( = {}, &block) client = self.new() client.connect.hello # handshake unless block_given? client else begin result = block.call(client) ensure client.disconnect end result end end |
Instance Method Details
#connect ⇒ void Also known as: reconnect
This method returns an undefined value.
Establishes a connection to the SAM bridge.
If called after the connection has already been established, disconnects and then reconnects to the bridge.
96 97 98 99 100 101 |
# File 'lib/i2p/sam/client.rb', line 96 def connect disconnect if connected? @socket = TCPSocket.new(@host, @port) @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) self end |
#connected? ⇒ Boolean
Returns ‘true` if a connection to the SAM bridge has been established and is active.
82 83 84 |
# File 'lib/i2p/sam/client.rb', line 82 def connected? !!@socket end |
#disconnect ⇒ void Also known as: close
This method returns an undefined value.
Closes the connection to the SAM bridge.
If called after the connection has already been closed, does nothing.
113 114 115 116 117 |
# File 'lib/i2p/sam/client.rb', line 113 def disconnect @socket.close if @socket && !@socket.closed? @socket = nil self end |
#generate_dest ⇒ KeyPair Also known as: generate_destination
Generates a new I2P destination and returns the key pair for it.
164 165 166 167 |
# File 'lib/i2p/sam/client.rb', line 164 def generate_dest send_msg(:dest, :generate) KeyPair.parse(read_reply[:priv]) end |
#hello(options = {}) ⇒ Float
Performs the SAM protocol handshake and returns the autonegotiated protocol version.
135 136 137 138 139 140 141 |
# File 'lib/i2p/sam/client.rb', line 135 def hello( = {}) send_msg(:hello, :version, { :min => '%.1f' % ([:min] || @version).to_f, :max => '%.1f' % ([:max] || @version).to_f, }) read_reply[:version].to_f end |
#lookup_name(name) ⇒ Destination
Returns the I2P destination corresponding to ‘name`.
152 153 154 155 |
# File 'lib/i2p/sam/client.rb', line 152 def lookup_name(name) send_msg(:naming, :lookup, :name => name.to_s) Destination.parse(read_reply[:value]) end |