View Single Post
  #9 (permalink)  
Old 11-20-2007, 01:49 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: MultiThreading in Ruby

Ya..sure..

check it out the following code....

Code:
class FnC

require 'EPC'
require 'Queue'

#defining & initialising constants
BITS64LENGTH=65
BITS64HEADER=2.to_s(2)

BITS96LENGTH=97
BITS96HEADER="00"+48.to_s(2)

BITS96PARTITION0="00"+0.to_s(2)
BITS96PARTITION1="00"+1.to_s(2)
BITS96PARTITION2="0"+2.to_s(2)

a=Thread.new { 
#create a queue to store EPC for filtering purposes
$queue=Queue.new
#create a queue counter which starts at 0
$queueTotal=0

#read input file which is used as virtual reader
File.open("Input.txt") do |file|

#create an EPC object
EPCtest=EPC.new

#read input file until there is no more EPC
while $scanEPC=file.gets

#if EPC is 64 bits in length
if $scanEPC.length==BITS64LENGTH 

#set bits and header of EPC from virtual reader to the EPC object 
EPCtest.setbits(BITS64LENGTH)
EPCtest.setheader($scanEPC,0,1)

#if header bits are 10 in binary
if EPCtest.getheader()==BITS64HEADER 

#set filter value of EPC from virtual reader to the EPC object
EPCtest.setfilter($scanEPC,0,2)
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1
Thread.pass

#else the EPC is invalid
else
print "The EPC "+$scanEPC+" is not valid \n"
end

#else if EPC is 96 bits in length
elsif $scanEPC.length==BITS96LENGTH

#set bits and header of EPC from virtual reader to the EPC object
EPCtest.setbits(BITS96LENGTH)
EPCtest.setheader($scanEPC,0,7)

#if header bits are 00110000 in binary
if EPCtest.getheader()==BITS96HEADER

#set filter value and partition of EPC from virtual reader to the EPC object
EPCtest.setfilter($scanEPC,0,2)
EPCtest.setpartition($scanEPC,0,2)

case EPCtest.getpartition()

#partition value is 000
when BITS96PARTITION0
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1 
Thread.pass

#partition value is 001
when BITS96PARTITION1
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1 
Thread.pass

#partition value is 010
when BITS96PARTITION2 
#enqueue remaining bits to queue
$queue.enqueue($scanEPC)
#increase queue counter by 1
$queueTotal+=1
Thread.pass

#else the EPC is invalid
else 
puts "The EPC "+$scanEPC+" is not valid \n"
end 

#else the EPC is invalid
else
puts "The EPC "+$scanEPC+" is not valid \n" 
end

#else the EPC is invalid
else 
puts "The EPC "+$scanEPC+" is not valid \n"
end
end 
end
}

b=Thread.new {

#filter until there is less than 10 EPC in the queue
while $queueTotal>=10

#store the first 10 EPC of the queue in another comparing queue
$queueCompare=$queue.peek(10)
#create a queueCompare counter which starts at 1
$queueCompareCounter=1

#filter until we have compared with all 10 EPC's in the queue
while $queueCompareCounter<10

#if similar EPC 
if $queueCompare[0]==$queueCompare[$queueCompareCounter]
#delete the repeated EPC from the queue and the comparing queue 
$queueCompare.delete_at($queueCompareCounter)
$queue.delete_at($queueCompareCounter)
#decrease queue counter by 1
$queueTotal-=1

#else we increase the queueCompare counter by 1 to check next EPC
else 
$queueCompareCounter+=1
end
end 

#dequeue first EPC from the queue which is unique
$EPCdequeue=$queue.dequeue

#if a 64bit EPC
if EPCtest.getbits()==BITS64LENGTH 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,13)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,19)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,25)
puts EPCtest.getserial()+"\n"
Thread.pass


elsif EPCtest.getbits()==BITS96LENGTH && EPCtest.getpartition()==BITS96PARTITION0 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,39)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,3)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

elsif EPCtest.getbits()==BITS96LENGTH && EPCtest.getpartition()==BITS96PARTITION1

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,36)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,6)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

else 

#set company of EPC from virtual reader to the EPC object
EPCtest.setcomp($EPCdequeue,0,33)
puts EPCtest.getcomp()+"\n"
#set item of EPC from virtual reader to the EPC object
EPCtest.setitem($EPCdequeue,0,9)
puts EPCtest.getitem()+"\n"
#set serial of EPC from virtual reader to the EPC object
EPCtest.setserial($EPCdequeue,0,38)
puts EPCtest.getserial()+"\n"
Thread.pass

end 

#decrease queue counter by 1
$queueTotal-=1
end

#there is less than 10 EPCs in the queue, stop filtering
puts "There are too few EPCs to do filtering. \n"
}
end
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Reply With Quote