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_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 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_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_generate_message(self, eventobj: Event): self.sendcnt = self.sendcnt + 1 msg = GenericMessage(GenericMessageHeader("AL", 0, 1), self.sendcnt) self.send_down(Event(self, EventTypes.MFRT, msg)) # time.sleep(1) self.send_self(Event(self, "generatemessage", "..."))