Module: Toolshed

Included in:
Garden, Garden::Rows, Gardener
Defined in:
lib/toolshed.rb

Overview

This module provides a toolkit of helper methods to Abundance

It uses the Socket Ruby standard library to provide a communication mechanism between Abundance concurent processes. It’s used as both a namespace for init variables of the different Abundance Classes, using Toolshed’s Class Method like qualified names, and as mixins methods which access directly the instance variables of the client classes .

Author

lp ([email protected])

Copyright

2008 Louis-Philippe Perron - Released under the terms of the MIT license

:title:Toolshed

Constant Summary collapse

SOCKET_ROOT =
'/tmp/abundance/'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.block_size=(block_size) ⇒ Object

The Toolshed::block_size= method sets the Socket block size for UNIXSocket operations.

Parameters

  • block_size = the block size in bytes



22
23
24
# File 'lib/toolshed.rb', line 22

def Toolshed::block_size=(block_size)
  @@block_size = block_size
end

Instance Method Details

#add_readable(socket) ⇒ Object



78
79
80
81
# File 'lib/toolshed.rb', line 78

def add_readable(socket)
  client = socket.accept
  @reader[:sockets] << client
end

#add_writable(message_block) ⇒ Object



83
84
85
86
87
# File 'lib/toolshed.rb', line 83

def add_writable(message_block)
  client = UNIXSocket.open(message_block[3])
  @writer[:sockets] << client
  @writer[:buffer][client.to_s] = Marshal.dump(message_block)
end

#my_socket_pathObject

The my_socket_path method act as an attribute reader for the @my_socket_path instance variable of the calling object.



47
48
49
# File 'lib/toolshed.rb', line 47

def my_socket_path
  @my_socket_path
end

#read_message_block(client) ⇒ Object



103
104
105
# File 'lib/toolshed.rb', line 103

def read_message_block(client)
  recv_whole_block(client)
end

#read_raw(client) ⇒ Object



95
96
97
# File 'lib/toolshed.rb', line 95

def read_raw(client)
  client.recvfrom(@@block_size)[0]
end

#remove_writable(client) ⇒ Object



89
90
91
92
93
# File 'lib/toolshed.rb', line 89

def remove_writable(client)
  @writer[:sockets].delete(client)
  @writer[:buffer].delete(client.to_s)
  client.close
end

#set_my_socket_as_a(role, garden_pid = Process.pid) ⇒ Object

The set_my_socket_as_a method sets a UNIXServer socket as an instance variable for the calling object in a role based fashion. When the specified role is not a :garden, the Garden’s socket path is also stored as an instance variable for the calling object.

Parameters

  • role = the role for which you wish to establish the socket

  • garden_pid = the garden pid that will become the basename for the garden socket. No need to specify this argument when role is :garden.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/toolshed.rb', line 31

def set_my_socket_as_a(role,garden_pid=Process.pid)
  case role
  when :garden
    set_my_socket(Process.pid.to_s)
    @reader = {:sockets => [@my_socket], :buffer => {}}
    @writer = {:sockets => [], :buffer => {}}
  when :gardener
    set_garden_path(garden_pid)
    set_my_socket(Process.pid.to_s + Time.now.to_i.to_s + rand(10000).to_s)
  when :row
    set_garden_path(garden_pid)
    set_my_socket(Process.pid.to_s)
  end
end

#socket_duplex(message_block) ⇒ Object

The socket_duplex method open a UNIXSocket and send packets to a UNIXServer socket, then wait for loopback communication from destination and return results like socket_recv.

Parameters

  • command = command part of the sent packet

  • data = data part of the sent packet

  • server_socket_path = a UNIXServer socket path for the packets to be sent to



66
67
68
69
70
# File 'lib/toolshed.rb', line 66

def socket_duplex(message_block)
  send_block(message_block)
client = @my_socket.accept
  recv_whole_block(client)
end

#socket_recvObject

The socket_recv method calls accept on a UNIXServer socket, receives all the packets from a UNIXSocket sender, join the packets back as the original block message.



73
74
75
76
# File 'lib/toolshed.rb', line 73

def socket_recv
client = @my_socket.accept
  recv_whole_block(client)
end

#socket_send(message_block) ⇒ Object

The socket_send method open a UNIXSocket and send packets to a UNIXServer socket. When the server_socket_path is not specified, it defaults sending to the Garden UNIXServer.

Parameters

  • command = command part of the sent packet

  • data = data part of the sent packet

  • server_socket_path = a UNIXServer socket path for the packets to be sent to



57
58
59
# File 'lib/toolshed.rb', line 57

def socket_send(message_block)
  send_block(message_block)
end

#write_raw(client) ⇒ Object



99
100
101
# File 'lib/toolshed.rb', line 99

def write_raw(client)
  client.send(@writer[:buffer][client.to_s].slice!(0..@@block_size-1),0)
end