def on_message_from_bottom(self, eventobj: Event): msg = eventobj.eventcontent hdr = msg.header payload = msg.payload if hdr.messageto == self.componentinstancenumber or hdr.messageto == MessageDestinationIdentifiers.NETWORKLAYERBROADCAST: # Add if broadcast.... self.send_up(Event(self, EventTypes.MFRB, payload)) print( f"I received a message to {hdr.messageto} and I am {self.componentinstancenumber}" ) else: destination = hdr.messageto nexthop = Topology().get_next_hop(self.componentinstancenumber, destination) if nexthop != float('inf'): newhdr = NetworkLayerMessageHeader( NetworkLayerMessageTypes.NETMSG, self.componentinstancenumber, destination, nexthop) newpayload = eventobj.eventcontent.payload msg = GenericMessage(newhdr, newpayload) self.send_down(Event(self, EventTypes.MFRT, msg)) print( f"{self.componentinstancenumber} will FORWARD a message to {destination} over {nexthop}" ) else: print( f"NO PATH {self.componentinstancenumber} will NOT FORWARD a message to {destination} over {nexthop}" )
def on_message_from_bottom(self, eventobj: Event): message = eventobj.eventcontent messageType = message.header.messagetype if messageType is MSTMessageTypes.LOCAL_MST: mstEvent = Event(eventobj.eventsource, RPSEventTypes.MST_CONSTRUCTED, eventobj.eventcontent, eventobj.fromchannel) self.send_self(mstEvent) elif messageType is RPSMessageTypes.START: startEvent = Event(eventobj.eventsource, RPSEventTypes.START, eventobj.eventcontent, eventobj.fromchannel) self.send_self(startEvent) elif messageType is RPSMessageTypes.SHARE: shareEvent = Event(eventobj.eventsource, RPSEventTypes.SHARE, eventobj.eventcontent, eventobj.fromchannel) self.send_self(shareEvent)
def on_message_from_top(self, eventobj: Event): header = eventobj.eventcontent.header if header.nexthop == MessageDestinationIdentifiers.LINKLAYERBROADCAST: myevent = Event(eventobj.eventsource, ChannelEventTypes.INCH, eventobj.eventcontent) self.channelqueue.put_nowait(myevent) elif header.nexthop in self.connectedNodeIds: myevent = Event(eventobj.eventsource, ChannelEventTypes.INCH, eventobj.eventcontent) self.channelqueue.put_nowait(myevent) elif header.messageto != header.messagefrom and header.messageto in self.connectedNodeIds: myevent = Event(eventobj.eventsource, ChannelEventTypes.INCH, eventobj.eventcontent) self.channelqueue.put_nowait(myevent)
def on_process_in_channel(self, eventobj: Event): if isinstance(eventobj.eventcontent, MSTMessage) \ and eventobj.eventcontent.header.messagetype == MSTMessageTypes.NEIGHBOR_DISCOVERY: myeventcontent = copy.deepcopy(eventobj.eventcontent) myeventcontent.payload.messagepayload = self.weight myevent = Event(eventobj.eventsource, ChannelEventTypes.DLVR, myeventcontent) self.outputqueue.put_nowait(myevent) else: if isinstance(eventobj.eventcontent, MSTMessage) \ and eventobj.eventcontent.header.messagetype == MSTMessageTypes.LOCAL_MST: time.sleep(0.25) myevent = Event(eventobj.eventsource, ChannelEventTypes.DLVR, eventobj.eventcontent) self.outputqueue.put_nowait(myevent)
def startRPS(self): print(f"Model Training will be started.") lmstUpdate = LMSTUpdate(self.localMST, isCompressed=False) lmstEventContent = MSTMessage(self.componentinstancenumber, self.componentinstancenumber, MSTMessageTypes.LOCAL_MST, messagepayload=lmstUpdate) lmstEvent = Event(self, EventTypes.MFRB, lmstEventContent) self.send_up(lmstEvent)
def on_process_in_channel(self, eventobj: Event): if random.random() < self.prob: myevent = Event(eventobj.eventsource, ChannelEventTypes.DLVR, eventobj.eventcontent) self.outputqueue.put_nowait(myevent) if random.random() < self.duplicationprobability: self.channelqueue.put_nowait(eventobj)
def on_message_from_bottom(self, eventobj: Event): message = eventobj.eventcontent messageType = message.header.messagetype if messageType is MSTMessageTypes.NEIGHBOR_DISCOVERY: neighborEvent = Event(eventobj.eventsource, MSTEventTypes.MFRNeighbor, eventobj.eventcontent, eventobj.fromchannel) self.send_self(neighborEvent) elif messageType is MSTMessageTypes.LOCAL_MST: lmstEvent = Event(eventobj.eventsource, MSTEventTypes.LMST, eventobj.eventcontent, eventobj.fromchannel) self.send_self(lmstEvent) else: if messageType.value == "start": mst = eventobj.eventcontent.payload.messagepayload self.localMST = deepcopy(mst) self.send_up(eventobj)
def on_minimum_spanning_tree_constructed(self, eventobj: Event): lmstUpdate: LMSTUpdate = eventobj.eventcontent.payload.messagepayload mst = lmstUpdate.localMST selfStartEventContent = RPSMessage(self.componentinstancenumber, self.componentinstancenumber, RPSMessageTypes.START, messagepayload=mst) selfStartEvent = Event(self, RPSEventTypes.START, selfStartEventContent) self.send_self(selfStartEvent)
def on_propose(self, eventobj: Event): destination = 1 hdr = ApplicationLayerMessageHeader( ApplicationLayerMessageTypes.ACCEPT, self.componentinstancenumber, destination) payload = ApplicationLayerMessagePayload("23") proposalmessage = GenericMessage(hdr, payload) self.send_down(Event(self, EventTypes.MFRT, proposalmessage))
def sendLocalMSTUpdateManually(self, nodeId: int): S = self.localMST currentNode = self.componentinstancenumber nextActivationNode = nodeId if nextActivationNode in S.neighbors(currentNode): localMSTEventContent = MSTMessage(currentNode, nextActivationNode, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestActivationLMSTUpdate, manualMode=True) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) else: for node in S.neighbors(currentNode): if S.nodes[node]['activated']: localMSTEventContent = MSTMessage(currentNode, node, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestLocalLMSTUpdate, manualMode=True, nextActivationNode=nextActivationNode) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent)
def on_deliver_to_component(self, eventobj: Event): sourceNodeId = eventobj.eventsource.componentinstancenumber for connector in self.connectors: for node in self.connectors[connector]: if node.componentinstancenumber != sourceNodeId: myevent = Event(eventobj.eventsource, EventTypes.MFRB, eventobj.eventcontent, self.componentinstancenumber) node.trigger_event(myevent)
def on_start(self, eventobj: Event): if not self.rpsStarted: self.rpsStarted = True # print(f"RPS started from Node {self.componentinstancenumber}!") header = eventobj.eventcontent.header messagefrom = header.messagefrom mst = eventobj.eventcontent.payload.messagepayload self.mst = deepcopy(mst) for node in self.mst.neighbors(self.componentinstancenumber): if node != messagefrom: startEventContent = RPSMessage(self.componentinstancenumber, node, RPSMessageTypes.START, messagepayload=self.mst) startEvent = Event(self, EventTypes.MFRT, startEventContent) self.send_down(startEvent) trainEventContent = RPSMessage(self.componentinstancenumber, self.componentinstancenumber, RPSMessageTypes.TRAIN) trainEvent = Event(self, RPSEventTypes.TRAIN, trainEventContent) self.send_self(trainEvent)
def on_message_from_bottom(self, eventobj: Event): msg = eventobj.eventcontent hdr = msg.header payload = msg.payload if hdr.messageto == self.componentinstancenumber or hdr.messageto == MessageDestinationIdentifiers.LINKLAYERBROADCAST: self.send_up( Event(self, EventTypes.MFRB, payload, eventobj.fromchannel) ) # doing decapsulation by just sending the payload else: # print(f"I am {self.componentinstancenumber} and dropping the {hdr.messagetype} message to {hdr.messageto}") # Physical layer is a broadcast medium, and hence will accept all messages. The link layer will drop those messages that are not for me pass
def on_message_from_top(self, eventobj: Event): # if channelid != hdr.interfaceif then drop (should not be on this channel) hdr = eventobj.eventcontent.header if hdr.nexthop != MessageDestinationIdentifiers.LINKLAYERBROADCAST: if hdr.interfaceid == self.componentinstancenumber: #print(f"Will forward message since {hdr.interfaceid} and {self.componentinstancenumber}") myevent = Event(eventobj.eventsource, ChannelEventTypes.INCH, eventobj.eventcontent) self.channelqueue.put_nowait(myevent) else: #print(f"Will drop message since {hdr.interfaceid} and {self.componentinstancenumber}") pass
def on_deliver_to_component(self, eventobj: Event): callername = eventobj.eventsource.componentinstancenumber for item in self.connectors: callees = self.connectors[item] for callee in callees: calleename = callee.componentinstancenumber # print(f"I am connected to {calleename}. Will check if I have to distribute it to {item}") if calleename == callername: pass else: myevent = Event(eventobj.eventsource, EventTypes.MFRB, eventobj.eventcontent, self.componentinstancenumber) callee.trigger_event(myevent)
def senddownbroadcast(self, eventobj: Event, whosends, sequencenumber): applmsg = eventobj.eventcontent destination = MessageDestinationIdentifiers.NETWORKLAYERBROADCAST nexthop = MessageDestinationIdentifiers.LINKLAYERBROADCAST print( f"{self.componentinstancenumber} will SEND a message to {destination} over {nexthop}" ) hdr = BroadcastingMessageHeader(BroadcastingMessageTypes.SIMPLEFLOOD, whosends, destination, nexthop, sequencenumber) payload = applmsg broadcastmessage = GenericMessage(hdr, payload) self.send_down(Event(self, EventTypes.MFRT, broadcastmessage)) self.broadcastdb.append(broadcastmessage.uniqueid)
def on_message_from_bottom(self, eventobj: Event): msg = eventobj.eventcontent hdr = msg.header payload = msg.payload if hdr.messagetype == BroadcastingMessageTypes.SIMPLEFLOOD: if hdr.messageto == self.componentinstancenumber or hdr.messageto == MessageDestinationIdentifiers.NETWORKLAYERBROADCAST: # Add if broadcast.... if msg.uniqueid in self.broadcastdb: pass # we have already handled this flooded message else: # Send to higher layers self.update_topology() self.send_up(Event(self, EventTypes.MFRB, payload)) # Also continue flooding once time.sleep(random.randint(1, 3)) self.senddownbroadcast( eventobj, eventobj.eventcontent.header.messagefrom, eventobj.eventcontent.header.sequencenumber)
def on_init(self, eventobj: Event): print( f"Initializing {self.componentname}.{self.componentinstancenumber}" ) if self.componentinstancenumber == 0: # destination = random.randint(len(Topology.G.nodes)) destination = 1 hdr = ApplicationLayerMessageHeader( ApplicationLayerMessageTypes.PROPOSE, self.componentinstancenumber, destination) payload = ApplicationLayerMessagePayload("23") proposalmessage = GenericMessage(hdr, payload) randdelay = random.randint(0, 5) time.sleep(randdelay) self.send_self(Event(self, "propose", proposalmessage)) else: pass
def on_message_from_top(self, eventobj: Event): # Encapsulate the SDU in network layer PDU applmsg = eventobj.eventcontent destination = applmsg.header.messageto nexthop = Topology().get_next_hop(self.componentinstancenumber, destination) if nexthop != float('inf'): print( f"{self.componentinstancenumber} will SEND a message to {destination} over {nexthop}" ) hdr = NetworkLayerMessageHeader(NetworkLayerMessageTypes.NETMSG, self.componentinstancenumber, destination, nexthop) payload = eventobj.eventcontent msg = GenericMessage(hdr, payload) self.send_down(Event(self, EventTypes.MFRT, msg)) else: print( f"NO PATH: {self.componentinstancenumber} will NOTSEND a message to {destination} over {nexthop}" )
def on_message_from_top(self, eventobj: Event): abovehdr = eventobj.eventcontent.header if abovehdr.messageto == MessageDestinationIdentifiers.NETWORKLAYERBROADCAST: hdr = LinkLayerMessageHeader( LinkLayerMessageTypes.LINKMSG, self.componentinstancenumber, MessageDestinationIdentifiers.LINKLAYERBROADCAST, nexthop=MessageDestinationIdentifiers.LINKLAYERBROADCAST) else: #if we do not broadcast, use nexthop to determine interfaceid and set hdr.interfaceid myinterfaceid = str(self.componentinstancenumber) + "-" + str( abovehdr.nexthop) hdr = LinkLayerMessageHeader(LinkLayerMessageTypes.LINKMSG, self.componentinstancenumber, abovehdr.nexthop, nexthop=abovehdr.nexthop, interfaceid=myinterfaceid) payload = eventobj.eventcontent msg = GenericMessage(hdr, payload) self.send_down(Event(self, EventTypes.MFRT, msg))
def on_init(self, eventobj: Event): evt = Event(self, EventTypes.MFRT, "A to lower layer") self.send_down(evt)
def on_init(self, eventobj: Event): mstMessage = MSTMessage(self.componentinstancenumber, -1, MSTMessageTypes.NEIGHBOR_DISCOVERY) mstEvent = Event(self, EventTypes.MFRT, mstMessage) self.send_down(mstEvent)
def startMST(self, manualMode=False, compressedMode=True): lmstUpdate = LMSTUpdate(isCompressed=compressedMode) localMSTEventContent = MSTMessage(self.componentinstancenumber, -1, MSTMessageTypes.LOCAL_MST, messagepayload=lmstUpdate, manualMode=manualMode) localMSTEvent = Event(self, MSTEventTypes.LMST, localMSTEventContent) self.send_self(localMSTEvent)
def on_local_minimum_spanning_tree(self, eventobj: Event): content = eventobj.eventcontent payload = content.payload messagefrom = content.header.messagefrom messagepayload: LMSTUpdate = payload.messagepayload isCompressed = messagepayload.isCompressed manualMode = payload.manualMode nextActivationNode = payload.nextActivationNode receivedInsertions = messagepayload.insertions receivedDeletions = messagepayload.deletions receivedActivatedNodes = messagepayload.activatedNodes if not isCompressed: receivedMSTUpdate = messagepayload.localMST else: if self.localMST is None: if len(receivedInsertions) > 0 or len(receivedDeletions) > 0 or len(receivedActivatedNodes) > 0: receivedMSTUpdate = nx.Graph() else: receivedMSTUpdate = None else: receivedMSTUpdate = self.localMST if receivedMSTUpdate is not None: for u, v in receivedInsertions: if not receivedMSTUpdate.has_edge(u, v): receivedMSTUpdate.add_edge(u, v) receivedMSTUpdate.edges[u, v]['weight'] = receivedInsertions[(u, v)] self.insertions[u, v] = receivedInsertions[(u, v)] for u, v in receivedDeletions: if receivedMSTUpdate.has_edge(u, v): receivedMSTUpdate.remove_edge(u, v) self.deletions.add((u, v)) for node in receivedMSTUpdate.nodes: if node in receivedActivatedNodes: receivedMSTUpdate.nodes[node]['activated'] = True else: if 'activated' not in receivedMSTUpdate.nodes[node]: receivedMSTUpdate.nodes[node]['activated'] = False self.activatedNodes.update(receivedActivatedNodes) currentNode = self.componentinstancenumber decideNextActivation = False localInsertions = {} localDeletions = set() localActivatedNodes = set() if receivedMSTUpdate is None: S: nx.Graph = nx.Graph() S.add_node(self.componentinstancenumber) S.nodes[currentNode]['activated'] = True decideNextActivation = True localActivatedNodes.add(currentNode) for (node, weight) in self.neighbors.items(): S.add_node(node) S.nodes[node]['activated'] = False S.add_edge(currentNode, node) S.edges[currentNode, node]['weight'] = weight localInsertions[currentNode, node] = weight else: S: nx.Graph = deepcopy(receivedMSTUpdate) if currentNode in S.nodes: if S.nodes[currentNode]['activated']: pass else: for (node, weight) in self.neighbors.items(): if not S.has_node(node): S.add_node(node) S.nodes[node]['activated'] = False S.add_edge(currentNode, node) S.edges[currentNode, node]['weight'] = weight localInsertions[currentNode, node] = weight else: if not S.has_edge(currentNode, node): path = getPathInMST(S, currentNode, node) maximumWeight, node1, node2 = getMaximumWeightedEdge(S, path) if weight < maximumWeight: S.remove_edge(node1, node2) localDeletions.add((node1, node2)) S.add_edge(currentNode, node) S.get_edge_data(currentNode, node)['weight'] = weight localInsertions[currentNode, node] = weight S.nodes[currentNode]['activated'] = True decideNextActivation = True localActivatedNodes.add(currentNode) else: raise NotImplementedError self.localMST = S localInsertions = optimizeInsertions(localInsertions) localDeletions = optimizeDeletions(localDeletions) self.insertions.update(localInsertions) self.deletions.update(localDeletions) self.activatedNodes.update(localActivatedNodes) self.optimizeLocalParameters() self.latestLocalLMSTUpdate = LMSTUpdate(S, localInsertions, localDeletions, localActivatedNodes, isCompressed) self.latestActivationLMSTUpdate = LMSTUpdate(S, self.insertions, self.deletions, self.activatedNodes, isCompressed) self.latestForwardingLMSTUpdate = LMSTUpdate(S, receivedInsertions, receivedDeletions, receivedActivatedNodes, isCompressed) if not manualMode: if decideNextActivation: drawGraph(S, currentNode, self.neighbors, showTopology=True) nextActivation = selectDeactivatedNode(S, currentNode, self.neighbors) if nextActivation == -1: print(f"Minimum Spanning Tree is constructed.") self.startRPS() # if nextActivation in self.neighbors: # localMSTEventContent = MSTMessage(currentNode, nextActivation, MSTMessageTypes.LOCAL_MST, # messagepayload=self.latestActivationLMSTUpdate) # localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) # self.send_down(localMSTEvent) # nextActivation = -1 # if nextActivation in S.neighbors(currentNode): # localMSTEventContent = MSTMessage(currentNode, nextActivation, MSTMessageTypes.LOCAL_MST, # messagepayload=self.latestActivationLMSTUpdate) # localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) # self.send_down(localMSTEvent) # nextActivation = -1 for node in S.neighbors(currentNode): if S.nodes[node]['activated']: localMSTEventContent = MSTMessage(currentNode, node, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestLocalLMSTUpdate) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) if nextActivation != -1: path = getPathInMST(self.localMST, currentNode, nextActivation) localMSTEventContent = MSTMessage(currentNode, path[1], MSTMessageTypes.LOCAL_MST, messagepayload=self.latestActivationLMSTUpdate, nextActivationNode=nextActivation) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) else: # if nextActivationNode in self.neighbors: # localMSTEventContent = MSTMessage(currentNode, nextActivationNode, MSTMessageTypes.LOCAL_MST, # messagepayload=self.latestActivationLMSTUpdate) # localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) # self.send_down(localMSTEvent) # nextActivationNode = -1 # if nextActivationNode in S.neighbors(currentNode): # localMSTEventContent = MSTMessage(currentNode, nextActivationNode, MSTMessageTypes.LOCAL_MST, # messagepayload=self.latestActivationLMSTUpdate) # localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) # self.send_down(localMSTEvent) # nextActivationNode = -1 for node in S.neighbors(currentNode): if S.nodes[node]['activated'] and node is not messagefrom: localMSTEventContent = MSTMessage(currentNode, node, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestForwardingLMSTUpdate) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) if nextActivationNode != -1: path = getPathInMST(self.localMST, currentNode, nextActivationNode, messagefrom) localMSTEventContent = MSTMessage(currentNode, path[1], MSTMessageTypes.LOCAL_MST, messagepayload=self.latestActivationLMSTUpdate, nextActivationNode=nextActivationNode) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) else: if decideNextActivation: drawGraph(S, currentNode, self.neighbors, showTopology=True) for node in S.neighbors(currentNode): if S.nodes[node]['activated']: localMSTEventContent = MSTMessage(currentNode, node, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestLocalLMSTUpdate, manualMode=True) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) else: if nextActivationNode in S.neighbors(currentNode) or nextActivationNode in self.neighbors: localMSTEventContent = MSTMessage(currentNode, nextActivationNode, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestActivationLMSTUpdate, manualMode=True) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent) nextActivationNode = -1 for node in S.neighbors(currentNode): if S.nodes[node]['activated'] and node is not messagefrom: localMSTEventContent = MSTMessage(currentNode, node, MSTMessageTypes.LOCAL_MST, messagepayload=self.latestForwardingLMSTUpdate, manualMode=True, nextActivationNode=nextActivationNode) localMSTEvent = Event(self, EventTypes.MFRT, localMSTEventContent) self.send_down(localMSTEvent)
def on_init(self, eventobj: Event): evt = Event(self, EventTypes.MFRP, "B to peers") self.send_peer(evt)
def sendWeights(self, weights, source, messagefrom=-1): for n in self.mst.neighbors(self.componentinstancenumber): if n != messagefrom: shareEventContent = RPSMessage(self.componentinstancenumber, n, RPSMessageTypes.SHARE, weights, source) shareEvent = Event(self, EventTypes.MFRT, shareEventContent) self.send_down(shareEvent)
def on_message_from_top(self, eventobj: Event): print( f"I am {self.componentname}, eventcontent={eventobj.eventcontent}\n" ) evt = Event(self, EventTypes.MFRT, "N to lower layer") self.send_down(evt)
def on_message_from_top(self, eventobj: Event): print( f"I am {self.componentname}, eventcontent={eventobj.eventcontent}") evt = Event(self, EventTypes.MFRB, "L to higher layer") self.send_up(evt)
def on_message_from_bottom(self, eventobj: Event): self.send_up(Event(self, EventTypes.MFRB, eventobj.eventcontent))
def on_message_from_top(self, eventobj: Event): self.send_down(Event(self, EventTypes.MFRT, eventobj.eventcontent))