Class: SimpleIPC::IPC
- Inherits:
-
Object
- Object
- SimpleIPC::IPC
- Defined in:
- lib/simple_ipc.rb
Overview
Socket Class
Instance Attribute Summary (collapse)
-
- (Object) cfg
Returns the value of attribute cfg.
Instance Method Summary (collapse)
-
- (Object) close
Closes the socket.
-
- (Object) get {|String| ... }
Gets an object (only valid if it is a server).
-
- (IPC) initialize(args = {})
constructor
A new instance of IPC.
-
- (Object) listen
Puts the object in listening state (becomes a server).
-
- (Object) send(something) {|Object| ... }
Sends a general object to the server.
Constructor Details
- (IPC) initialize(args = {})
A new instance of IPC
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.
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.
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 |