Class: Linda::SocketIO::Client::TupleSpace

Inherits:
Object
  • Object
show all
Defined in:
lib/linda-socket.io-client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(linda, name) ⇒ TupleSpace

Returns a new instance of TupleSpace.



38
39
40
41
42
43
44
45
46
47
# File 'lib/linda-socket.io-client/client.rb', line 38

def initialize(linda, name)
  @name = name
  @linda = linda
  @watch_callback_ids = {}
  @io_callback_ids = []
  this = self
  @linda.io.on 'disconnect' do
    this.remove_io_callbacks
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/linda-socket.io-client/client.rb', line 36

def name
  @name
end

Instance Method Details

#cancel(id) ⇒ Object



106
107
108
# File 'lib/linda-socket.io-client/client.rb', line 106

def cancel(id)
  @linda.io.emit '__linda_cancel', {:tuplespace => @name, :id => id}
end

#create_callback_idObject



49
50
51
# File 'lib/linda-socket.io-client/client.rb', line 49

def create_callback_id
  "#{Time.now.to_i}_#{rand 10000}"
end

#create_watch_callback_id(tuple) ⇒ Object



53
54
55
56
# File 'lib/linda-socket.io-client/client.rb', line 53

def create_watch_callback_id(tuple)
  key = tuple.to_json
  return @watch_callback_ids[key] ||= create_callback_id
end

#read(tuple, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/linda-socket.io-client/client.rb', line 82

def read(tuple, &block)
  return unless block_given?
  id = create_callback_id
  name = "__linda_read_#{id}"
  io_cid = @linda.io.once name do |err, tuple|
    block.call err, Tuple.new(tuple)
  end
  @io_callback_ids.push io_cid
  @linda.io.emit '__linda_read', {:tuplespace => @name, :tuple => tuple, :id => id}
  return id
end

#remove_io_callbacksObject



58
59
60
61
62
63
# File 'lib/linda-socket.io-client/client.rb', line 58

def remove_io_callbacks
  @io_callback_ids.each do |id|
    @linda.io.remove_listener id
  end
  @io_callback_ids = []
end

#take(tuple, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/linda-socket.io-client/client.rb', line 70

def take(tuple, &block)
  return unless block_given?
  id = create_callback_id
  name = "__linda_take_#{id}"
  io_cid = @linda.io.once name do |err, tuple|
    block.call err, Tuple.new(tuple)
  end
  @io_callback_ids.push io_cid
  @linda.io.emit '__linda_take', {:tuplespace => @name, :tuple => tuple, :id => id}
  return id
end

#watch(tuple, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/linda-socket.io-client/client.rb', line 94

def watch(tuple, &block)
  return unless block_given?
  id = create_watch_callback_id tuple
  name  = "__linda_watch_#{id}"
  io_cid = @linda.io.on name do |err, tuple|
    block.call err, Tuple.new(tuple)
  end
  @io_callback_ids.push io_cid
  @linda.io.emit '__linda_watch', {:tuplespace => @name, :tuple => tuple, :id => id}
  return id
end

#write(tuple, opts = {}) ⇒ Object



65
66
67
68
# File 'lib/linda-socket.io-client/client.rb', line 65

def write(tuple, opts={})
  data = {:tuplespace => @name, :tuple => tuple, :options => opts}
  @linda.io.emit '__linda_write', data
end