def CHANGEROOT(set_): fb.write_in_file(set_["f_"], " --- Procedure CHANGEROOT ---\n") if set_["canal"][set_["mcan"]] == "branch": eq.send_to(set_["queues"][set_["mcan"]], set_["i"], set_["mcan"], "changeroot", None, None, None, set_["f_"]) else: eq.send_to(set_["queues"][set_["mcan"]], set_["i"], set_["mcan"], "connect", set_["niv"], None, None, set_["f_"]) register.change_var("canal["+set_["mcan"]+"]", set_["canal"][set_["mcan"]], "branch", set_["f_"]) set_["canal"][set_["mcan"]] = "branch" fb.write_in_file(set_["f_"], " --- Fin procedure CHANGEROOT ---\n") return set_
def REPORT(set_): fb.write_in_file(set_["f_"], " - Procedure REPORT -\n") test = 0 for j, val in set_["canal"].items(): if val == "branch" and j != set_["pere"]: test = test + 1 if set_["recu"] == test and set_["testcan"] == None: register.change_var("etat", set_["etat"], "found", set_["f_"]) set_["etat"] = "found" eq.send_to(set_["queues"][set_["pere"]], set_["i"], set_["pere"], "report", set_["mpoids"], None, None, set_["f_"]) fb.write_in_file(set_["f_"], " - Fin procedure REPORT -\n") return set_
def TEST(set_): fb.write_in_file(set_["f_"], " -- Procedure TEST --\n") exists = {} for j, val in set_["canal"].items(): if val == "basic": exists[j] = set_["edges"][j] ##ici mis a jour !! if not bool(exists) == False: # bool(exists)=False when empty j = find_min_val_return_key_in_dict(exists) register.change_var("testcan", set_["testcan"], j, set_["f_"]) set_["testcan"] = j eq.send_to(set_["queues"][set_["testcan"]], set_["i"], set_["testcan"], "test", set_["niv"], set_["nom"], None, set_["f_"]) else: register.change_var("testcan", set_["testcan"], "None", set_["f_"]) set_["testcan"] = None set_ = REPORT(set_) fb.write_in_file(set_["f_"], " -- Fin procedure TEST --\n") return set_
def bloc_initiate(L, F, S, j, set_): register.change_var("niv", set_["niv"], L, set_["f_"]) set_["niv"] = L register.change_var("nom", set_["nom"], F, set_["f_"]) set_["nom"] = F register.change_var("etat", set_["etat"], S, set_["f_"]) set_["etat"] = S register.change_var("pere", set_["pere"], j, set_["f_"]) set_["pere"] = j register.change_var("mcan", set_["mcan"], "None", set_["f_"]) set_["mcan"] = None register.change_var("mpoids", set_["mpoids"], "inf", set_["f_"]) set_["mpoids"] = math.inf for k in set_["edges"]: if k != j and set_["canal"][k] == "branch": eq.send_to(set_["queues"][k], set_["i"], k, "initiate", L, F, S, set_["f_"]) if set_["etat"] == "find": register.change_var("recu", set_["recu"], 0, set_["f_"]) set_["recu"] = 0 set_ = TEST(set_) return set_
def bloc_initialization(edgesList, q_, i, f_name): set_ = { "i" : i, "canal" : {}, "niv" : 0, "etat" : "found", "recu" : 0, "nom" : None, "pere" : None, "mcan" : None, "testcan" : None, "mpoids" : 0.1, "edges" : edgesList, #edgesList must contain only neigbors "queues" : q_, "f_" : f_name } for neighbor in edgesList: set_["canal"][neighbor] = "basic" minimalEdge = find_min_val_return_key_in_dict(edgesList) set_["canal"][minimalEdge] = "branch" eq.send_to(set_["queues"][minimalEdge], set_["i"], minimalEdge, "connect", 0, None, None, set_["f_"]) return set_
def bloc_report(poids, j, set_): if j != set_["pere"]: if poids < set_["mpoids"]: register.change_var("mpoids", set_["mpoids"], poids, set_["f_"]) set_["mpoids"] = poids register.change_var("mcan", set_["mcan"], j, set_["f_"]) set_["mcan"] = j register.change_var("recu", set_["recu"], set_["recu"]+1, set_["f_"]) set_["recu"] = set_["recu"] + 1 set_ = REPORT(set_) else: if set_["etat"] == "find": time.sleep(1) eq.send_to(set_["queues"][set_["i"]], j, set_["i"], "report", poids, None, None, set_["f_"]) # Traiter le msg plus tard else: if poids > set_["mpoids"]: set_ = CHANGEROOT(set_) else: if poids == set_["mpoids"] and set_["mpoids"] == math.inf: # register.set_state(set_, set_["f_"]) print(" ### "+set_["i"]+" à terminé l'algo ### ") eq.send_to(set_["queues"]['father'], set_["i"], "father", "termine", None, None, None, None) return set_ # //!\\ TERMINE return set_
def bloc_connect(L, j, set_): if L < set_["niv"]: register.change_var("canal["+j+"]", set_["canal"][j], "branch", set_["f_"]) set_["canal"][j] = "branch" eq.send_to(set_["queues"][j], set_["i"], j, "initiate", set_["niv"], set_["nom"], set_["etat"], set_["f_"]) else: if set_["canal"][j] == "basic": time.sleep(1) eq.send_to(set_["queues"][set_["i"]], j, set_["i"], "connect", L, None, None, set_["f_"]) #traiter le message plus tard else: eq.send_to(set_["queues"][j], set_["i"], j, "initiate", set_["niv"]+1, set_["edges"][j], "find", set_["f_"]) return set_
def bloc_test(L, F, j, set_): if L > set_["niv"]: time.sleep(1) eq.send_to(set_["queues"][set_["i"]], j, set_["i"], "test", L, F, None, set_["f_"]) # Traiter le msg plus tard else: if F == set_["nom"]: if set_["canal"][j] == "basic": register.change_var("canal["+j+"]", set_["canal"][j], "reject", set_["f_"]) set_["canal"][j] = "reject" if j != set_["testcan"]: eq.send_to(set_["queues"][j], set_["i"], j, "reject", None, None, None, set_["f_"]) else: set_ = TEST(set_) else: eq.send_to(set_["queues"][j], set_["i"], j, "accept", None, None, None, set_["f_"]) return set_
set_ = ghs.bloc_initiate(msg["val1"], msg["val2"], msg["val3"], msg["sender"], set_) elif msg["type"] == "test": set_ = ghs.bloc_test(msg["val1"], msg["val2"], msg["sender"], set_) elif msg["type"] == "accept": set_ = ghs.bloc_accept(msg["sender"], set_) elif msg["type"] == "reject": set_ = ghs.bloc_reject(msg["sender"], set_) elif msg["type"] == "report": set_ = ghs.bloc_report(msg["val1"], msg["sender"], set_) elif msg["type"] == "changeroot": set_ = ghs.bloc_changeroot(set_) elif msg["type"] == "termine": end_set_ = set_ set_ = "TERMINE" #msg = eq.recv_from(set_["queues"][set_["i"]], set_["i"], set_["f_"]) register.set_state(end_set_, end_set_["f_"]) print("DONE !") ## ## In the father else: msg = eq.recv_from(queues["father"], "father", None) time.sleep(2) for node, q_ in queues.items(): eq.send_to(q_, "father", None, "termine", None, None, None, None) print("J'ai fini") ## time.sleep(3) exit(0)