Class: Mocket

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

Constant Summary collapse

LISTENING =
:listening
@@ports =
Hash.new(Hash.new(:ECONNREFUSED))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMocket

Returns a new instance of Mocket.



5
6
7
# File 'lib/mocket.rb', line 5

def initialize
  self.state = :open
end

Class Method Details

.hostunreachable(host, port) ⇒ Object



9
10
11
# File 'lib/mocket.rb', line 9

def self.hostunreachable(host, port)
  self.setport(host, port, :EHOSTUNREACH)
end

.listen(host, port) ⇒ Object



13
14
15
# File 'lib/mocket.rb', line 13

def self.listen(host, port)
  self.setport(host, port, LISTENING)
end

.netunreachable(host, port) ⇒ Object



17
18
19
# File 'lib/mocket.rb', line 17

def self.netunreachable(host, port)
  self.setport(host, port, :ENETUNREACH)
end

.open(host, port) ⇒ Object



21
22
23
24
25
# File 'lib/mocket.rb', line 21

def self.open(host, port)
  state = self.portstate(host, port)
  return Mocket.new if state == LISTENING
  raise Errno.const_get(state)
end

.reset!Object



27
28
29
# File 'lib/mocket.rb', line 27

def self.reset!
  @@ports = Hash.new(Hash.new(:ECONNREFUSED))
end

.timeout(host, port) ⇒ Object



31
32
33
# File 'lib/mocket.rb', line 31

def self.timeout(host, port)
  self.setport(host, port, :ETIMEDOUT)
end

Instance Method Details

#closeObject



35
36
37
# File 'lib/mocket.rb', line 35

def close
  self.state = :closed
end

#write(bytes) ⇒ Object

Raises:

  • (IOError)


39
40
41
42
# File 'lib/mocket.rb', line 39

def write(bytes)
  raise IOError, 'closed stream' if state == :closed
  bytes.size
end