Download
the code of EzAgents.
The zip file contains the following directories: src/
the source
code classes/
the compiled classes
Basics
In the
example of the VerboseAgent done during the lecture, you saw how to
create an agent:
1- find the place: place =
EzPlaces.locate("//localhost/" + <place_name>);
2- ask the place to create the agent: place.createAgent(<agent_class>,
<args>, <agentID>);
You also saw how to move an agent (move(<place>)), how to kill an
agent (kill()), etc.
Messaging
To send a message to
an agent:
1- create the Message object: Message m = new
Message(<msg_name>, <arg>);
2- send the message:
2a- from an agent: send(m,
<agentID>);
2b- from a normal program (eg. the main program):
. If you know the place in which the receiver is
located: place.deliver(m, <agentID>);
. If you do not know the place: EzPlaces.deliver(m, <agentID>);
[note: this is not
efficient, so if you know the place, use the place method]
Processing
messages
The
typical pattern for an agent that processes messages is:
void live(){ while(true){ Message m =
waitMessage(); // blocks until a message is available
if(m.getName().equals("m1")) m1(...); else if
(m.getName().equals("m2")) m2(...); //etc... } }
The live() method should be
called when the agent should start waiting for messages (for instance, onCreation, onArrival).
Note that a
message sent by an agent holds the id of the sender agent (accessible
with m.getSender()). So, *you don't
need to send the id of the sender agent explicitly*.
Information for the Mobile Chat
Room Exercise
Graphical User
Interface
In order to make
things nicer and easier for you, a GUI for the chat client is
available: class mchat.ChatClientGUI [I therefore
assume that your project will be defined in a package called mchat --
e.g. mchat.ChatClient, mchat.ChatRoom, etc.]
Your ChatClient
objects should hold a reference to an instance of ChatClientGUI. To
create the GUI, you need to pass the reference to the agent, and the
name of the chat room.
The GUI will
then send two types of messages to the agent, depending on the
interaction of the user: - POST messages:
message sent to the chat client when the user enters a message and
clicks the OK button - DIE messages:
message sent to the chat client when the user closes the window.
The protocol
between the GUI and the chat client agent (that is, the name of the
POST and DIE messages) is defined as constants in the ChatClientProtocol interface. Also, when your
chat client agent receives a new message, it should call newMessage(m)
on the GUI: this will cause the message to be displayed in the chat
window.
Functionalities
This is
the typical chat application:
When
created, a chat client should join the chat room.
Upon
posting, a chat client should forward the message to the chat room.
Upon
dying, a chat client should leave the chat room.
The chat
room makes it possible for clients to join and leave the room, and post
messages. When a message
is posted to the room, the room notifies all the clients present in the
room.
Remember that you can get the source code of the non-agent-based,
non-mobile, RMI-based chat application we
saw during the lecture.
Chat
Room Mobility
The chat room agent
should move the place hosting the most 'verbose' agent. In order to
avoid the case where the room spends its time hoping from one place to
another, you can introduce a threshold. For instance, if one sends more
than 5 messages more than the other clients, the room moves.
Running
the application
You need to:
- launch the RMI
registry on the localhost > rmiregistry
& [on *nix platforms]
- start a number
of places, one for the chat room initial position, and one per client
(use ezagent.StartEzPlace as a program to start a room) Remember to set
the codebase for the places! You need to run
StartEzPlace with the following parameter:
One path
argument must be the path to the classes of EzAgent. The other path
argument must be the path to the classes of your Chat application.
- have a program
(= a class with a main method) to run a chat client, taking as
parameter the name of the client agent, the name of the place where it
is to be created, and the name of the chat room (this argument must be
passed to the agent at creation time). Run this program for each client
- have a program
to run a chat room, taking as parameter the name of the chat room and
the place where it is to be created
So, that's a total of (at least) four classes you have to make:
ChatClient
ChatRoom
RunChatClient
RunChatRoom
When you'll test the mobility of your chat room, you can see in the
console of each place the trace that is generated: it informs you of
the creation, deletion, moving and arrival of agents.