Class: SimpleIPC::IPC

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

Overview

Socket Class

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (IPC) initialize(args = {})

A new instance of IPC

Parameters:

  • args (Hash) (defaults to: {})

    a hash of config values

Options Hash (args):

  • :port (Integer) — default: 5000

    the port, only used for UDPSockets

  • :host (String) — default: LOCALHOST

    host to talk with, only used for UDPSockets

  • :timeout (Numeric) — default: 0

    timeout for blocking connections

  • :blocking (Bool) — default: false

    use blocking read

Raises:

  • (ArgumentError)


125
126
127
128
129
130
# File 'lib/simple_ipc.rb', line 125

def initialize(args = {})
  raise ArgumentError, "expecting an Hash" unless args.kind_of? Hash
  @cfg = {:port => 5000, :host => LOCALHOST, :timeout => 0}
  @cfg.merge! args
  @socket = Socket.new @cfg
end

Instance Attribute Details

- (Object) cfg

Returns the value of attribute cfg



118
119
120
# File 'lib/simple_ipc.rb', line 118

def cfg
  @cfg
end

Instance Method Details

- (Object) close

Closes the socket.



188
189
190
# File 'lib/simple_ipc.rb', line 188

def close
  @socket.close
end

- (Object) get {|String| ... }

Gets an object (only valid if it is a server). An optional block can be given for parsing the received String. If no block is given, then the YAML#load deserialization is automatically used.

Yields:

  • (String)

    a block that deserializes the received String

Yield Parameters:

  • (String)

Yield Returns:

  • (Object)

Returns:

  • (Object)

    a parsed object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/simple_ipc.rb', line 164

def get
  result = nil
  begin
    if @cfg[:timeout] > 0 and !@cfg[:nonblock] then
      Timeout::timeout(@cfg[:timeout]) do |to|
        result = get_
      end
    else 
      result = get_
    end
  rescue Timeout::Error
    result = nil
  rescue Errno::EAGAIN
    return nil
  end

  if block_given? then
    return yield(result)
  else
    return YAML.load(result)
  end
end

- (Object) listen

Puts the object in listening state (becomes a server).



153
154
155
# File 'lib/simple_ipc.rb', line 153

def listen
  @socket.listen
end

- (Object) send(something) {|Object| ... }

Sends a general object to the server. If an optional block is given, then it is used to perform the object serialization. Otherwise, YAML#dump is used for serialization.

Parameters:

  • something (Object)

    an object

Yields:

  • (Object)

    a block that serializes the received Object

Yield Parameters:

  • (Object)

Yield Returns:

  • (String)


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/simple_ipc.rb', line 139

def send(something)
  if block_given? then
    payload = yield(something)
  else
    payload = YAML.dump(something)
  end
  length = [payload.size].pack(LENGTH_CODE)
  @socket.connect
  @socket.print length
  @socket.print payload
  return payload
end