Sha256: c957b781a251df630afe502862571f56d9c6d9cf92b2c6a176ee0a805a85486c
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
require 'global_service.rb' require 'crypt.rb' require 'ostruct' class LocalTupleSpaceApp<GlobalService provides :localTupleSpace MARSHAL=Marshal MAX_LEND_TIME=2 Lent=Struct.new(:time, :transaction) def initialize(cluster,options) super(cluster,options,:localTupleSpace) @dataFile=File.open(File.join(getAppDataPath,"data.bin"),"w+") @indexFile=File.open(File.join(getAppDataPath,"index.bin"),"w+") @index={} @lent={} @transactions={} @mutex=Mutex.new end def read(ref) loadInternal(ref) end def lend(ref,timeout=0.3) old=Time.now while Time.now-old<timeout if @lent[ref] @mutex.synchronize do if @lent[ref] if Time.now-@lent[ref].time>MAX_LEND_TIME @transactions.delete(@lent[ref].transaction) @lent.delete(ref) end end end sleep(timeout/10) else @mutex.synchronize do unless @lent[ref] thxid=getTransactionID @lent[ref]=Lent.new(Time.now,thxid) @transactions[thxid]=true return loadInternal(ref) end end end end return nil end def store(ref,object) unless @lent.member?(ref) unless @index[ref].nil? raise "object not lent!" end end @mutex.synchronize do storeInternal(ref,object) @lent.delete(ref) end puts "STORED #{ref}:=#{object}" true end private def storeInternal(ref,object) pp "STORING #{ref} #{object}" @index[ref]=MARSHAL.dump(object) end def loadInternal(ref) #pp "GETTING #{ref} #{@index[ref]}" if @index[ref] MARSHAL.load(@index[ref]) #@index[ref].dup else nil end end # This function shold be called with synchronized mode only ! def getTransactionID begin tx=Crypt::digest(rand) end while @transactions[tx] tx end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
appswarm-0.0.1 | apps/local_tuple_space/local_tuple_space.rb |