Skip to content

Commit

Permalink
new DataQueue implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wrzasa committed Nov 18, 2016
1 parent a973f16 commit 4ea0515
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
27 changes: 25 additions & 2 deletions lib/rbsim/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class CPUToken < HLModel::Node::CPU

class Data
IncompleteDataDefinition = Class.new RuntimeError
attr_reader :data_id # id of data required to collect fragments
attr_reader :src, :dst, :src_node, :size, :type, :content, :id
attr_accessor :dst_node, :route
attr_accessor :fragments # no. of fragments this data was between

def initialize(node, process, opts)
def initialize(data_id, node, process, opts)
@data_id = data_id
@src_node = node
@src = process
@dst = opts[:to]
Expand Down Expand Up @@ -68,10 +71,12 @@ def initialize(process_name, process_tags = {})
@process_name = process_name
@process_tags = process_tags
@queue = []
@incomplete_data = Hash.new { { fragments: 0, data: nil } }
end

def put(o)
@queue << o
enqueue_fragment(o)
check_if_complete(o)
end

def get
Expand All @@ -85,6 +90,24 @@ def length
def empty?
length == 0
end

private

def enqueue_fragment(o)
already_received = @incomplete_data[o.data_id]
already_received[:fragments] += 1
already_received[:data] ||= o
@incomplete_data[o.data_id] = already_received
end

def check_if_complete(o)
already_received = @incomplete_data[o.data_id]
if already_received[:fragments] == o.fragments
@incomplete_data.delete o.data_id
@queue << already_received[:data]
end
end

end

class DataQueueToken < DataQueue
Expand Down
13 changes: 3 additions & 10 deletions spec/tokens/data_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
describe "when all fragments arrive" do
before :each do
subject.put data1
subject.put other_data1
subject.put data2
subject.put other_data1
subject.put data3
end

Expand All @@ -84,26 +84,19 @@

describe "when data is dequeued" do
before :each do
subject.put data1
subject.put other_data1
subject.put data2
subject.put data3
subject.get
end

it_behaves_like "empty queue"


describe "when another data arrives" do
before :each do
subject.put data1
subject.put other_data1
subject.put data2
subject.put data3
subject.get
subject.put other_data2
end

it "dequeues data" do
p subject
expect(subject.get.data_id).to eq other_data_id
end

Expand Down

0 comments on commit 4ea0515

Please sign in to comment.