8.5.txt

[java]
import jade.util.Logger

public class Foo {
private Logger myLogger = Logger.getMyLogger(getClass().getName());

public Foo() {
myLogger.log(Logger.INFO, "Creating a Foo instance");

try {
myLogger.log (Logger.FINE, "Entering try bloc. . .");
. . .
}
catch (Exception e) {
myLogger.log(Logger.WARNING, "Unexpected error", e);
}
}
}

=========================================================

private class CallForOfferServer extends CyclicBehaviour {
public void action() {
ACLMessage msg = myAgent.receive();
if (msg != null) {
// Message received. Process it
ACLMessage reply = msg.createReply();
try {
OrderedFrame actFrame = (OrderedFrame) slFrameCodec.decode(msg.getContent());
OrderedFrame sellFrame = (OrderedFrame) actFrame.elementAt(1);
QualifiedFrame bookFrame = (QualifiedFrame) sellFrame.elementAt(0);
String title = (String) bookFrame.get(BookTradingVocabulary.Book_TITLE);
PriceManager pm = (PriceManager) catalogue.get(title);
if (pm != null) {
// The requested book is available for sale
reply.setPerformative (ACLMessage.Propose);
OrderedFrame listFrame = new OrderedFrame(null);
listFrame.addElement (actFrame);
OrderedFrame costsFrame = new OrderedFrame(BookTradingVocabulary.COSTS);
costsFrame.addElement(bookFrame);
costsFrame.addElement(new Integer (pm.getCurrentPrice()));
listFrame.addElement(costsFrame);
reply.setContent (slFrameCodec.encode(listFrame));
}
else {
// The requested book is NOT available for sale.
reply.setPerformative(ACLMessage.REFUSE);
}
}
catch (FrameExpection fe) {
fe.printStackTrace();
reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
}
myAgent.send(reply);
}
}
} // End of inner class CallForOfferServer

===========================================================

public class BookTradingMIDlet extends MIDlet implements CommandListener {
ChoiceGroup choices;
Command okCommand = new Command ("OK", Command.OK, 1);

// MIDlet startup method
public void startApp() throws MIDletStateChangeException {
// Set the Agent.midlet static field
Agent.midlet = this;

// Show a form to let the user select the operation he wants to do
Form f = new Form ("Book Trading");
choices = ChoiceGroup ("Select an operation", Choice.EXCLUSIVE);
choices.append("BUY", null);
choices.append("SELL", null);
f.append(choices);
f.addCommand(okCommand);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}

// This is executed when the user clicks OK
public void commandAction (Command c, Displayable d) {
if (c == okCommand) {
int i = choices.getSelectedIndex();
String agentsOption = null;
if (i == 0) {
// The user selected BUY –> start a BookBuyerAgent
agentsOption = "%C-book-buyer:bookTrading.buyer.BookBuyerAgent";
}
else {
// The user selected SELL –> start a BookSellerAgent
agentsOption = "%C-book-seller:bookTrading.seller.BookSellerAgent";
}

// Launch JADE with the proper agent
Properties pp = new Properties();
pp.load("jad");
pp.setProperty(MicroRuntime.AGENTS_KEY, agentsOption);
MicroRuntime.startJADE(pp, null);
if (!MicroRuntime.isRunning()) {
// JADE startup failed for some reason. Handle the failure. . .
}
}
}
}

==========================================================

private Logger myLogger = Logger.getMyLogger(getClass().getName());
. . .
Properties pp = new Properties();
pp.load("jad");
pp.put(MicroRuntime.CONNECTION_LISTENER_KEY, new ConnectionListener() {
public void handleConnectionEvent(int code, Object info) {
switch (code) {
case ConnectionListener.DISCONNECTED:
myLogger.log(Logger.WARNING, "Connection down");
break;
case ConnectionListener.RECONNECTED:
myLogger.log(Logger.INFO, "Connection reestablished");
break;
}
}
} );
MicroRuntime.startJADE(pp, null);
if (!MicroRuntime.isRunning()( {
// JADE startup failed for some reason. Handle the failure. . .
}
. . .
[/java]