private MTPDescriptor installMTP(String address, String className) throws IMTPException, ServiceException, MTPException {
try {
// Create the MTP
Class c = Class.forName(className);
MTP proto = (MTP)c.newInstance();
InChannel.Dispatcher dispatcher = new InChannel.Dispatcher() {
public void dispatchMessage(Envelope env, byte[] payload) {
//log("Message from remote platform received", 2);
if (myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Message from remote platform received");
// To avoid message loops, make sure that the ID of this ACC does
// not appear in a previous 'received' stamp
ReceivedObject[] stamps = env.getStamps();
for(int i = 0; i < stamps.length; i++) {
String id = stamps[i].getBy();
if(CaseInsensitiveString.equalsIgnoreCase(id, accID)) {
System.err.println("ERROR: Message loop detected !!!");
System.err.println("Route is: ");
for(int j = 0; j < stamps.length; j++)
System.err.println("[" + j + "]" + stamps[j].getBy());
System.err.println("Message dispatch aborted.");
return;
}
}
// Put a 'received-object' stamp in the envelope
ReceivedObject ro = new ReceivedObject();
ro.setBy(accID);
ro.setDate(new Date());
env.setReceived(ro);
Iterator it = env.getAllIntendedReceiver();
// FIXME: There is a problem if no 'intended-receiver' is present,
// but this should not happen
while (it.hasNext()) {
AID rcv = (AID)it.next();
GenericMessage msg = new GenericMessage(env,payload);
String traceId = getTraceId(env);
if (traceId != null) {
myLogger.log(Logger.INFO, "MTP In-Channel handling message from the outside for receiver "+rcv.getName()+". TraceID = "+traceId);
msg.setTraceID(traceId);
}
myMessageManager.deliver(msg, rcv, MessagingService.this);
}
}
};
if(address == null) {
// Let the MTP choose the address
TransportAddress ta = proto.activate(dispatcher, myProfile);
address = proto.addrToStr(ta);
}
else {
// Convert the given string into a TransportAddress object and use it
TransportAddress ta = proto.strToAddr(address);
proto.activate(dispatcher, ta, myProfile);
}
MTPDescriptor result = new MTPDescriptor(proto.getName(), className, new String[] {address}, proto.getSupportedProtocols());
routes.addLocalMTP(address, proto, result);
String[] pp = result.getSupportedProtocols();
for (int i = 0; i < pp.length; ++i) {
//log("Added Route-Via-MTP for protocol "+pp[i], 1);