📄 messagetransportprotocol.java
字号:
}
}
public String getName() {
return jade.domain.FIPANames.MTP.IIOP;
}
public String[] getSupportedProtocols() {
return PROTOCOLS;
}
private FIPA.Property marshalProperty(jade.domain.FIPAAgentManagement.Property p) {
org.omg.CORBA.Any value = myORB.create_any();
java.lang.Object v = p.getValue();
if (v instanceof java.io.Serializable) {
value.insert_Value((Serializable) v);
}
else {
if (v != null) {
value.insert_Value(v.toString());
}
}
return new FIPA.Property(p.getName(), value);
}
private FIPA.AgentID marshalAID(AID id) {
String name = id.getName();
String[] addresses = id.getAddressesArray();
AID[] resolvers = id.getResolversArray();
FIPA.Property[] userDefinedProperties = new FIPA.Property[] { };
int numOfResolvers = resolvers.length;
FIPA.AgentID result = new FIPA.AgentID(name, addresses, new AgentID[numOfResolvers], userDefinedProperties);
for(int i = 0; i < numOfResolvers; i++) {
result.resolvers[i] = marshalAID(resolvers[i]); // Recursively marshal all resolvers, which are, in turn, AIDs.
}
return result;
}
private FIPA.DateTime marshalDateTime(Date d) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
short year = (short)cal.get(Calendar.YEAR);
short month = (short)cal.get(Calendar.MONTH);
short day = (short)cal.get(Calendar.DAY_OF_MONTH);
short hour = (short)cal.get(Calendar.HOUR_OF_DAY);
short minutes = (short)cal.get(Calendar.MINUTE);
short seconds = (short)cal.get(Calendar.SECOND);
short milliseconds = 0; // FIXME: This is truncated to the second
char typeDesignator = ' '; // FIXME: Uses local timezone ?
FIPA.DateTime result = new FIPA.DateTime(year,
month,
day,
hour,
minutes,
seconds,
milliseconds,
typeDesignator);
return result;
}
private FIPA.ReceivedObject marshalReceivedObj(ReceivedObject ro) {
FIPA.ReceivedObject result = new FIPA.ReceivedObject();
result.by = ro.getBy();
result.from = ro.getFrom();
result.date = marshalDateTime(ro.getDate());
result.id = ro.getId();
result.via = ro.getVia();
return result;
}
//Method to write on a file the iiop message log file
/*public static synchronized void log(String str) {
logFile.println(str);
logFile.flush();
}*/
} // End of class MessageTransportProtocol
/**
This class represents an IIOP address.
Three syntaxes are allowed for an IIOP address (all case-insensitive):
<code>
IIOPAddress ::= "ior:" (HexDigit HexDigit+)
| "corbaname://" NSHost ":" NSPort "/" NSObjectID "#" objectName
| "corbaloc:" HostName ":" portNumber "/" objectID
</code>
Notice that, in the third case, BIG_ENDIAN is assumed by default. In the first and second case, instead, the endianess information is contained within the IOR definition.
**/
class IIOPAddress implements TransportAddress {
public static final byte BIG_ENDIAN = 0;
public static final byte LITTLE_ENDIAN = 1;
private static final String FIPA_2000_TYPE_ID = "IDL:FIPA/MTS:1.0";
private static final String NS_TYPE_ID = "IDL:omg.org/CosNaming/NamingContext";
private static final int TAG_INTERNET_IOP = 0;
private static final byte IIOP_MAJOR = 1;
private static final byte IIOP_MINOR = 0;
private static final byte ASCII_PERCENT = getASCIIByte("%");
private static final byte ASCII_UPPER_A = getASCIIByte("A");
private static final byte ASCII_UPPER_Z = getASCIIByte("Z");
private static final byte ASCII_LOWER_A = getASCIIByte("a");
private static final byte ASCII_LOWER_Z = getASCIIByte("z");
private static final byte ASCII_ZERO = getASCIIByte("0");
private static final byte ASCII_NINE = getASCIIByte("9");
private static final byte ASCII_MINUS = getASCIIByte("-");
private static final byte ASCII_UNDERSCORE = getASCIIByte("_");
private static final byte ASCII_DOT = getASCIIByte(".");
private static final byte ASCII_BANG = getASCIIByte("!");
private static final byte ASCII_TILDE = getASCIIByte("~");
private static final byte ASCII_STAR = getASCIIByte("*");
private static final byte ASCII_QUOTE = getASCIIByte("'");
private static final byte ASCII_OPEN_BRACKET = getASCIIByte("(");
private static final byte ASCII_CLOSED_BRACKET = getASCIIByte("$");
private static final char[] HEX = {
'0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f'
};
private static final byte getASCIIByte(String ch) {
try {
return (ch.getBytes("US-ASCII"))[0];
}
catch(UnsupportedEncodingException uee) {
return 0;
}
}
private final ORB orb;
private String ior;
private String host;
private short port;
private String objectKey;
private String anchor;
private CDRCodec codecStrategy;
public IIOPAddress(ORB anOrb, FIPA.MTS objRef) throws MTPException {
this(anOrb, anOrb.object_to_string(objRef));
}
public IIOPAddress(ORB anOrb, String s) throws MTPException {
orb = anOrb;
if(s.toLowerCase().startsWith("ior:"))
initFromIOR(s);
else if(s.toLowerCase().startsWith("corbaloc:"))
initFromURL(s, BIG_ENDIAN);
else if(s.toLowerCase().startsWith("corbaname:"))
initFromNS(s);
else
throw new MTPException("Invalid string prefix");
}
void initFromIOR(String s) throws MTPException {
parseIOR(s, FIPA_2000_TYPE_ID);
anchor = "";
}
private void initFromURL(String s, short endianness) throws MTPException {
// Remove 'corbaloc:' prefix to get URL host, port and file
s = s.substring(9);
if(s.toLowerCase().startsWith("iiop:")) {
// Remove an explicit IIOP specification
s = s.substring(5);
}
else if(s.startsWith(":")) {
// Remove implicit IIOP specification
s = s.substring(1);
}
else
throw new MTPException("Invalid 'corbaloc' URL: neither 'iiop:' nor ':' was specified.");
buildIOR(s, FIPA_2000_TYPE_ID, endianness);
}
private void initFromNS(String s) throws MTPException {
// First perform a 'corbaloc::' resolution to get the IOR of the Naming Service.
// Replace 'corbaname:' with 'corbaloc::'
StringBuffer buf = new StringBuffer(s);
// Use 'corbaloc' support to build a reference on the NamingContext
// where the real object reference will be looked up.
buf.replace(0, 11, "corbaloc::");
buildIOR(s.substring(11), NS_TYPE_ID, BIG_ENDIAN);
org.omg.CORBA.Object o = orb.string_to_object(ior);
NamingContext ctx = NamingContextHelper.narrow(o);
try {
// Transform the string after the '#' sign into a COSNaming::Name.
StringTokenizer lexer = new StringTokenizer(anchor, "/.", true);
List name = new ArrayList();
while(lexer.hasMoreTokens()) {
String tok = lexer.nextToken();
NameComponent nc = new NameComponent();
nc.id = tok;
name.add(nc);
if(!lexer.hasMoreTokens())
break; // Out of the while loop
tok = lexer.nextToken();
if(tok.equals(".")) { // An (id, kind) pair
tok = lexer.nextToken();
nc.kind = tok;
}
else if(!tok.equals("/")) // No separator other than '.' or '/' is allowed
throw new MTPException("Ill-formed path into the Naming Service: Unknown separator.");
}
// Get the object reference stored into the naming service...
NameComponent[] path = (NameComponent[])name.toArray(new NameComponent[name.size()]);
o = ctx.resolve(path);
// Stringify it and use the resulting IOR to initialize yourself
String realIOR = orb.object_to_string(o);
initFromIOR(realIOR);
}
catch(NoSuchElementException nsee) {
throw new MTPException("Ill-formed path into the Naming Service.", nsee);
}
catch(UserException ue) {
throw new MTPException("CORBA Naming Service user exception.", ue);
}
catch(SystemException se) {
throw new MTPException("CORBA Naming Service system exception.", se);
}
}
private void parseIOR(String s, String typeName) throws MTPException {
try {
// Store stringified IOR
ior = new String(s.toUpperCase());
// Remove 'IOR:' prefix to get Hex digits
String hexString = ior.substring(4);
short endianness = Short.parseShort(hexString.substring(0, 2), 16);
switch(endianness) {
case BIG_ENDIAN:
codecStrategy = new BigEndianCodec(hexString);
break;
case LITTLE_ENDIAN:
codecStrategy = new LittleEndianCodec(hexString);
break;
default:
throw new MTPException("Invalid endianness specifier");
}
try {
// Read 'string type_id' field
String typeID = codecStrategy.readString();
if(!typeID.equalsIgnoreCase(typeName))
throw new MTPException("Invalid type ID" + typeID);
}
catch (Exception e) { // all exceptions are converted into MTPException
throw new MTPException("Invalid type ID");
}
// Read 'sequence<TaggedProfile> profiles' field
// Read sequence length
int seqLen = codecStrategy.readLong();
for(int i = 0; i < seqLen; i++) {
// Read 'ProfileId tag' field
int tag = codecStrategy.readLong();
byte[] profile = codecStrategy.readOctetSequence();
if(tag == TAG_INTERNET_IOP) {
// Process IIOP profile
CDRCodec profileBodyCodec;
switch(profile[0]) {
case BIG_ENDIAN:
profileBodyCodec = new BigEndianCodec(profile);
break;
case LITTLE_ENDIAN:
profileBodyCodec = new LittleEndianCodec(profile);
break;
default:
throw new MTPException("Invalid endianness specifier");
}
// Read IIOP version
byte versionMajor = profileBodyCodec.readOctet();
byte versionMinor = profileBodyCodec.readOctet();
if(versionMajor != 1)
throw new MTPException("IIOP version not supported");
try {
// Read 'string host' field
host = profileBodyCodec.readString();
}
catch (Exception e) {
throw new MTPException("Invalid host string");
}
// Read 'unsigned short port' field
port = profileBodyCodec.readShort();
// Read 'sequence<octet> object_key' field and convert it
// into a String object
byte[] keyBuffer = profileBodyCodec.readOctetSequence();
ByteArrayOutputStream buf = new ByteArrayOutputStream();
// Escape every forbidden character, as for RFC 2396 (URI: Generic Syntax)
for(int ii = 0; ii < keyBuffer.length; ii++) {
byte b = keyBuffer[ii];
if(isUnreservedURIChar(b)) {
// Write the character 'as is'
buf.write(b);
}
else {
// Escape it using '%'
buf.write(ASCII_PERCENT);
buf.write(HEX[(b & 0xF0) >> 4]); // High nibble
buf.write(HEX[b & 0x0F]); // Low nibble
}
}
objectKey = buf.toString("US-ASCII");
codecStrategy = null;
}
}
}
catch (Exception e) { // all exceptions are converted into MTPException
throw new MTPException(e.getMessage());
}
}
private void buildIOR(String s, String typeName, short endianness) throws MTPException {
int colonPos = s.indexOf(':');
int slashPos = s.indexOf('/');
int poundPos = s.indexOf('#');
if((colonPos == -1) || (slashPos == -1))
throw new MTPException("Invalid URL string");
host = new String(s.substring(0, colonPos));
port = Short.parseShort(s.substring(colonPos + 1, slashPos));
if(poundPos == -1) {
objectKey = new String(s.substring(slashPos + 1, s.length()));
anchor = "";
}
else {
objectKey = new String(s.substring(slashPos + 1, poundPos));
anchor = new String(s.substring(poundPos + 1, s.length()));
}
switch(endianness) {
case BIG_ENDIAN:
codecStrategy = new BigEndianCodec(new byte[0]);
break;
case LITTLE_ENDIAN:
codecStrategy = new LittleEndianCodec(new byte[0]);
break;
default:
throw new MTPException("Invalid endianness specifier");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -