Class: SimpleIPC::Socket
- Inherits:
-
Object
- Object
- SimpleIPC::Socket
- Defined in:
- lib/simple_ipc.rb
Overview
Wrapper class exposing the same API for UNIXSocket
and UDPSocket
classes.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the socket and removes the socket file if it exists.
-
#connect ⇒ Object
Opens the connection.
-
#initialize(args = {}) ⇒ Socket
constructor
Default initialization hash is: { :port => 5000, # port, only used for UDPSockets :host => LOCALHOST, # Host to talk with, only used for UDPSockets :kind => :unix, # kind of socket, either :unix or :udp :force => true # if true, force removing of stale socket files }.
-
#listen ⇒ Object
Listens for incoming messages, i.e.
-
#print(string) ⇒ Object
Sends a
String
through the socket. -
#recv_nonblock(bytes) ⇒ String
Receives a message of length
bytes
in non-blocking way. -
#recvfrom(bytes) ⇒ String
Receives a message of length
bytes
.
Constructor Details
#initialize(args = {}) ⇒ Socket
Default initialization hash is:
{
:port => 5000, # port, only used for UDPSockets
:host => LOCALHOST, # Host to talk with, only used for UDPSockets
:kind => :unix, # kind of socket, either :unix or :udp
:force => true # if true, force removing of stale socket files
}
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/simple_ipc.rb', line 36 def initialize(args = {}) @cfg = { :port => 5000, :host => LOCALHOST, :kind => :unix, :force => true } @cfg.merge! args case @cfg[:kind] when :unix @socket_file = "/tmp/#{$0}.sok" @socket = nil when :udp @socket = UDPSocket.new else raise ArgumentError, "Either :unix or :udp allowed" end @open = false end |
Instance Method Details
#close ⇒ Object
Closes the socket and removes the socket file if it exists.
111 112 113 114 115 |
# File 'lib/simple_ipc.rb', line 111 def close @socket.close @open = false FileUtils::rm(@socket_file) if @socket_file end |
#connect ⇒ Object
Opens the connection. Only has to be called once before sending messages. Only used for client sockets.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simple_ipc.rb', line 58 def connect return false if @open case @cfg[:kind] when :unix @socket = UNIXSocket.open(@socket_file) when :udp @socket.connect(@cfg[:host], @cfg[:port]) end @open = true end |
#listen ⇒ Object
Listens for incoming messages, i.e. becomes a server. If @cfg is true, it also silently removes any existing stale socket file, otherwise stops.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/simple_ipc.rb', line 80 def listen case @cfg[:kind] when :unix @socket = UNIXServer.open(@socket_file).accept when :udp @socket.bind(BROADCAST, @cfg[:port]) end rescue Errno::EADDRINUSE if @cfg[:force] then FileUtils::rm(@socket_file) retry else raise Errno::EADDRINUSE, $! end end |
#print(string) ⇒ Object
Sends a String
through the socket.
71 72 73 |
# File 'lib/simple_ipc.rb', line 71 def print(string) @socket.print(string) end |
#recv_nonblock(bytes) ⇒ String
Receives a message of length bytes
in non-blocking way.
106 107 108 |
# File 'lib/simple_ipc.rb', line 106 def recv_nonblock(bytes) @socket.recv_nonblock(bytes) end |
#recvfrom(bytes) ⇒ String
Receives a message of length bytes
.
99 100 101 |
# File 'lib/simple_ipc.rb', line 99 def recvfrom(bytes) @socket.recvfrom(bytes) end |