######################################## # # BARRIER-RELATED FUNCTIONS # ######################################## # # Constants # BARRIER_TIMEOUT = 200 # in steps BARRIER_VSTIG = 80 timeW = 0 barrier = nil hvs = 0; # # Sets a barrier # function barrier_create() { # reset timeW = 0 # create barrier vstig #log("---> Prev. br. ", barrier, " ", BARRIER_VSTIG) if(barrier!=nil) { barrier=nil if(hvs) { BARRIER_VSTIG = BARRIER_VSTIG -1 hvs = 0 }else{ BARRIER_VSTIG = BARRIER_VSTIG +1 hvs = 1 } } #log("---> New. br. ", barrier, " ", BARRIER_VSTIG) barrier = stigmergy.create(BARRIER_VSTIG) } function barrier_set(threshold, transf, resumef, bc) { statef = function() { barrier_wait(threshold, transf, resumef, bc); } BVMSTATE = "BARRIERWAIT" barrier_create() } # # Make yourself ready # function barrier_ready(bc) { #log("BARRIER READY -------") barrier.put(id, bc) barrier.put("d", 0) } # # Executes the barrier # function barrier_wait(threshold, transf, resumef, bc) { if(barrier==nil) #failsafe barrier_create() barrier.put(id, bc) barrier.get(id) log("--->BS: ", barrier.size(), " / ", threshold, " (", BARRIER_VSTIG, " - ", barrier.get("d"), ") t= ", timeW) if(barrier.size()-1 >= threshold or barrier.get("d") == 1) { if(barrier_allgood(barrier,bc)) { barrier.put("d", 1) timeW = 0 BVMSTATE = transf } else barrier.put("d", 0) } if(timeW >= BARRIER_TIMEOUT) { log("------> Barrier Timeout !!!!") barrier = nil timeW = 0 BVMSTATE = resumef } else if(timeW % 100 == 0 and bc > 0) neighbors.broadcast("cmd", bc) timeW = timeW+1 } # Check all members state function barrier_allgood(barrier, bc) { barriergood = 1 barrier.foreach( function(key, value, robot){ #log("VS entry : ", key, " ", value, " ", robot) if(value != bc and key != "d"){ barriergood = 0 } } ) return barriergood }