vadviktor
11/11/2015 - 3:10 PM

Ruby ICP through UNIX socket with the help of MessagePack

Ruby ICP through UNIX socket with the help of MessagePack

require 'socket'
require 'msgpack'

child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
maxlen                      = 1000

fork do
  10.times do |i|
    instruction = child_socket.recv(maxlen)
    puts "#{MessagePack.unpack(instruction)} accomplished!"
    child_socket.send("#{i}", 0)
  end
end

fork do
  10.times do
    puts parent_socket.recv(maxlen)
  end
end

sleep 2

5.times do
  msg = "Heavy lifting".to_msgpack
  sleep 2
  parent_socket.send(msg, 0)
end

5.times do
  msg = "Feather lifting".to_msgpack
  sleep 1
  parent_socket.send(msg, 0)
end

parent_socket.close
child_socket.close
Heavy lifting accomplished!
0
Heavy lifting accomplished!
1
Heavy lifting accomplished!
2
Heavy lifting accomplished!
3
Heavy lifting accomplished!
4
Feather lifting accomplished!
5
Feather lifting accomplished!
6
Feather lifting accomplished!
7
Feather lifting accomplished!
8
Feather lifting accomplished!
9