Skip to content
Snippets Groups Projects
Commit 27d2dc38 authored by shinedday's avatar shinedday
Browse files

Add classes mail & mailbox

parent 3b3dd420
Branches
No related tags found
No related merge requests found
from typing import Any, List
class Mail:
def __init__(self, id_sender: int, id_receiver: int, message: Any) -> None:
self.id_sender = id_sender
self.id_receiver = id_receiver
self.message = message
#set getter & setter
class Mailbox:
def __init__(self, owner_id: int, amas: 'Amas') -> None:
self.__mail_list: List['Mail'] = []
self.__owner_id: int = owner_id
self.__amas: 'Amas' = amas
def get_mail(self) -> 'Mail':
#check si pas empty
mail = self.__mail_list.pop()
if mail.id_receiver != self.__owner_id :
# pas normal
return None
return mail
def receive_mail(self, mail: 'Mail'):
self.__mail_list.append(mail)
def send_message(self, message: Any, id_receiver: int) -> None:
mail = Mail(self.__owner_id, id_receiver, message)
for agent in self.__amas.get_agents():
if agent.get_id() == id:
return agent.receive_mail(mail)
# error, id not found
class CommunicatingAgent(Agent):
def __init__(self, amas: 'Amas') -> None:
super().__init__(amas)
self.__mailbox: 'Mailbox' = Mailbox(self.get_id())
def send_message(self, message, id_receiver):
self.__mailbox.send_message(message, id_receiver)
def receive_mail(self, mail):
self.__mailbox.receive_mail(mail)
def __phase1(self):
"""
this is the first phase of a cycle
"""
self.read_mails()
self.on_perceive()
self.compute_criticality()
self.__next_phase()
def read_mails(self):
mail = self.__mailbox.get_mail()
while mail is not None:
self.read_mail(mail)
mail = self.__mailbox.get_mail()
def read_mail(self, mail):
"""
TO OVERRIDE
"""
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment