📄 messagetransportprotocol.java
字号:
/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A.
The updating of this file to JADE 2.0 has been partially supported by the IST-1999-10211 LEAP Project
GNU Lesser General Public License
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation,
version 2.1 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*****************************************************************/
package jade.mtp.iiop;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import FIPA.*; // OMG IDL Stubs
import jade.core.AID;
import jade.core.Profile;
import jade.mtp.InChannel;
import jade.mtp.OutChannel;
import jade.mtp.MTP;
import jade.mtp.MTPException;
import jade.mtp.TransportAddress;
import jade.domain.FIPAAgentManagement.Envelope;
import jade.domain.FIPAAgentManagement.ReceivedObject;
import jade.domain.FIPAAgentManagement.Property;
//FIXME: if we add the method getSize() to all the slots whose value is a set
// (e.g. intendedReceiver, userdefProperties, ...) we can improve a lot
// performance in marshalling and unmarshalling.
/**
Implementation of <code><b>fipa.mts.mtp.iiop.std</b></code>
specification for delivering ACL messages over the OMG IIOP
transport protocol.
@author Giovanni Rimassa - Universita' di Parma
@version $Date: 2004-03-11 17:03:29 +0100 (gio, 11 mar 2004) $ $Revision: 4905 $
*/
public class MessageTransportProtocol implements MTP {
private static class MTSImpl extends FIPA._MTSImplBase {
private final InChannel.Dispatcher dispatcher;
public MTSImpl(InChannel.Dispatcher disp) {
dispatcher = disp;
}
public void message(FipaMessage aFipaMessage) {
FIPA.Envelope[] envelopes = aFipaMessage.messageEnvelopes;
byte[] payload = aFipaMessage.messageBody;
Envelope env = new Envelope();
// Read all the envelopes sequentially, so that later slots
// overwrite earlier ones.
for(int e = 0; e < envelopes.length; e++) {
FIPA.Envelope IDLenv = envelopes[e];
// Read in the 'to' slot
if(IDLenv.to.length > 0)
env.clearAllTo();
for(int i = 0; i < IDLenv.to.length; i++) {
AID id = unmarshalAID(IDLenv.to[i]);
env.addTo(id);
}
// Read in the 'from' slot
if(IDLenv.from.length > 0) {
AID id = unmarshalAID(IDLenv.from[0]);
env.setFrom(id);
}
// Read in the 'intended-receiver' slot
if(IDLenv.intendedReceiver.length > 0)
env.clearAllIntendedReceiver();
for(int i = 0; i < IDLenv.intendedReceiver.length; i++) {
AID id = unmarshalAID(IDLenv.intendedReceiver[i]);
env.addIntendedReceiver(id);
}
// Read in the 'encrypted' slot
//if(IDLenv.encrypted.length > 0)
// env.clearAllEncrypted();
//for(int i = 0; i < IDLenv.encrypted.length; i++) {
// String word = IDLenv.encrypted[i];
// env.addEncrypted(word);
//}
// Read in the other slots
if(IDLenv.comments.length() > 0)
env.setComments(IDLenv.comments);
if(IDLenv.aclRepresentation.length() > 0)
env.setAclRepresentation(IDLenv.aclRepresentation);
if(IDLenv.payloadLength > 0)
env.setPayloadLength(new Long(IDLenv.payloadLength));
if(IDLenv.payloadEncoding.length() > 0)
env.setPayloadEncoding(IDLenv.payloadEncoding);
if(IDLenv.date.length > 0) {
Date d = unmarshalDateTime(IDLenv.date[0]);
env.setDate(d);
}
// Read in the 'received' stamp
if(IDLenv.received.length > 0)
env.addStamp(unmarshalReceivedObj(IDLenv.received[0]));
// Read in the 'user-defined properties' slot
if(IDLenv.userDefinedProperties.length > 0)
env.clearAllProperties();
for(int i = 0; i < IDLenv.userDefinedProperties.length; i++) {
env.addProperties(unmarshalProperty(IDLenv.userDefinedProperties[i]));
}
}
//String tmp = "\n\n"+(new java.util.Date()).toString()+" RECEIVED IIOP MESSAGE"+ "\n" + env.toString() + "\n" + new String(payload);
//System.out.println(tmp);
//MessageTransportProtocol.log(tmp); //Write in a log file for iiop incoming message
// Dispatch the message
dispatcher.dispatchMessage(env, payload);
}
private AID unmarshalAID(FIPA.AgentID id) {
AID result = new AID();
result.setName(id.name);
for(int i = 0; i < id.addresses.length; i++)
result.addAddresses(id.addresses[i]);
for(int i = 0; i < id.resolvers.length; i++)
result.addResolvers(unmarshalAID(id.resolvers[i]));
return result;
}
private Date unmarshalDateTime(FIPA.DateTime d) {
Date result = new Date();
return result;
}
private Property unmarshalProperty(FIPA.Property p) {
return new Property(p.keyword, p.value.extract_Value());
}
private ReceivedObject unmarshalReceivedObj(FIPA.ReceivedObject ro) {
ReceivedObject result = new ReceivedObject();
result.setBy(ro.by);
result.setFrom(ro.from);
result.setDate(unmarshalDateTime(ro.date));
result.setId(ro.id);
result.setVia(ro.via);
return result;
}
} // End of MTSImpl class
private static final String[] PROTOCOLS = new String[] { "IOR", "corbaloc", "corbaname" };
private final ORB myORB;
private MTSImpl server;
private static PrintWriter logFile;
public MessageTransportProtocol() {
myORB = ORB.init(new String[0], null);
}
public TransportAddress activate(InChannel.Dispatcher disp, Profile p) throws MTPException {
server = new MTSImpl(disp);
myORB.connect(server);
IIOPAddress iiop = new IIOPAddress(myORB, server);
/* //Open log file
String fileName = "iiop"+iiop.getHost()+iiop.getPort()+".log";
try{
logFile = new PrintWriter(new FileWriter(fileName,true));
}catch(java.io.IOException e){e .printStackTrace();}
*/
return iiop;
}
public void activate(InChannel.Dispatcher disp, TransportAddress ta, Profile p) throws MTPException {
// throw new MTPException("User supplied transport address not supported.");
// Do not throw, but modify the supplied address instead.
IIOPAddress iia = (IIOPAddress)ta;
IIOPAddress generated = (IIOPAddress)activate(disp, p);
iia.initFromIOR(generated.getIOR());
}
public void deactivate(TransportAddress ta) throws MTPException {
myORB.disconnect(server);
}
public void deactivate() throws MTPException {
myORB.disconnect(server);
}
public void deliver(String addr, Envelope env, byte[] payload) throws MTPException {
try {
TransportAddress ta = strToAddr(addr);
IIOPAddress iiopAddr = (IIOPAddress)ta;
FIPA.MTS objRef = iiopAddr.getObject();
// verifies if the server object really exists (useful if the IOR is
// valid, i.e corresponds to a good object) (e.g. old IOR)
// FIXME. To check if this call slows down performance
if (objRef._non_existent())
throw new MTPException("Bad IIOP server object reference:" + objRef.toString());
// Fill in the 'to' field of the IDL envelope
Iterator itTo = env.getAllTo();
List to = new ArrayList();
while(itTo.hasNext()) {
AID id = (AID)itTo.next();
to.add(marshalAID(id));
}
FIPA.AgentID[] IDLto = new FIPA.AgentID[to.size()];
for(int i = 0; i < to.size(); i++)
IDLto[i] = (FIPA.AgentID)to.get(i);
// Fill in the 'from' field of the IDL envelope
AID from = env.getFrom();
FIPA.AgentID[] IDLfrom = new FIPA.AgentID[] { marshalAID(from) };
// Fill in the 'intended-receiver' field of the IDL envelope
Iterator itIntendedReceiver = env.getAllIntendedReceiver();
List intendedReceiver = new ArrayList();
while(itIntendedReceiver.hasNext()) {
AID id = (AID)itIntendedReceiver.next();
intendedReceiver.add(marshalAID(id));
}
FIPA.AgentID[] IDLintendedReceiver = new FIPA.AgentID[intendedReceiver.size()];
for(int i = 0; i < intendedReceiver.size(); i++)
IDLintendedReceiver[i] = (FIPA.AgentID)intendedReceiver.get(i);
// Fill in the 'encrypted' field of the IDL envelope
//Iterator itEncrypted = env.getAllEncrypted();
//List encrypted = new ArrayList();
//while(itEncrypted.hasNext()) {
//String word = (String)itEncrypted.next();
//encrypted.add(word);
//}
String[] IDLencrypted = new String[0];
//String[] IDLencrypted = new String[encrypted.size()];
//for(int i = 0; i < encrypted.size(); i++)
//IDLencrypted[i] = (String)encrypted.get(i);
// Fill in the other fields of the IDL envelope ...
String IDLcomments = (env.getComments() != null)?env.getComments():"";
String IDLaclRepresentation = env.getAclRepresentation();
Long payloadLength = env.getPayloadLength();
int IDLpayloadLength = payloadLength.intValue();
String IDLpayloadEncoding = (env.getPayloadEncoding() != null)?env.getPayloadEncoding():"";
FIPA.DateTime[] IDLdate = new FIPA.DateTime[] { marshalDateTime(env.getDate()) };
FIPA.Property[][] IDLtransportBehaviour = new FIPA.Property[][] { };
// Fill in the 'userdefined-properties' field of the IDL envelope
Iterator itUserDefProps = env.getAllProperties();
List userDefProps = new ArrayList();
while(itUserDefProps.hasNext()) {
Property p = (Property)itUserDefProps.next();
userDefProps.add(marshalProperty(p));
}
FIPA.Property[] IDLuserDefinedProperties = new FIPA.Property[userDefProps.size()];
for(int i = 0; i < userDefProps.size(); i++)
IDLuserDefinedProperties[i] = (FIPA.Property)userDefProps.get(i);
// Fill in the list of 'received' stamps
/* FIXME: Maybe several IDL Envelopes should be generated, one for every 'received' stamp...
ReceivedObject[] received = env.getStamps();
FIPA.ReceivedObject[] IDLreceived = new FIPA.ReceivedObject[received.length];
for(int i = 0; i < received.length; i++)
IDLreceived[i] = marshalReceivedObj(received[i]);
*/
// FIXME: For now, only the current 'received' object is considered...
ReceivedObject received = env.getReceived();
FIPA.ReceivedObject[] IDLreceived;
if(received != null)
IDLreceived = new FIPA.ReceivedObject[] { marshalReceivedObj(received) };
else
IDLreceived = new FIPA.ReceivedObject[] { };
FIPA.Envelope IDLenv = new FIPA.Envelope(IDLto,
IDLfrom,
IDLcomments,
IDLaclRepresentation,
IDLpayloadLength,
IDLpayloadEncoding,
IDLdate,
IDLencrypted,
IDLintendedReceiver,
IDLreceived,
IDLtransportBehaviour,
IDLuserDefinedProperties);
FipaMessage msg = new FipaMessage(new FIPA.Envelope[] { IDLenv }, payload);
//String tmp = "\n\n"+(new java.util.Date()).toString()+" SENT IIOP MESSAGE"+ "\n" + env.toString() + "\n" + new String(payload);
//System.out.println(tmp);
//MessageTransportProtocol.log(tmp); // write in a log file for sent iiop message
objRef.message(msg);
}
catch(ClassCastException cce) {
cce.printStackTrace();
throw new MTPException("Address mismatch: this is not a valid IIOP address.");
}
catch(Exception cce2) {
cce2.printStackTrace();
throw new MTPException("Address mismatch: this is not a valid IIOP address.");
}
}
public TransportAddress strToAddr(String rep) throws MTPException {
return new IIOPAddress(myORB, rep); // FIXME: Should cache object references
}
public String addrToStr(TransportAddress ta) throws MTPException {
try {
IIOPAddress addr = (IIOPAddress)ta;
return addr.getIOR();
}
catch(ClassCastException cce) {
throw new MTPException("Address mismatch: this is not a valid IIOP address.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -