⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagetransportprotocol.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      }

      codecStrategy.writeString(typeName);

      // Write '1' as profiles sequence length
      codecStrategy.writeLong(1);

      codecStrategy.writeLong(TAG_INTERNET_IOP);
      CDRCodec profileBodyCodec;
      switch(endianness) {
      case BIG_ENDIAN:
	profileBodyCodec = new BigEndianCodec(new byte[0]);
	break;
      case LITTLE_ENDIAN:
	profileBodyCodec = new LittleEndianCodec(new byte[0]);
	break;
      default:
	throw new MTPException("Invalid endianness specifier");
      }

      // Write IIOP 1.0 profile to auxiliary CDR codec
      profileBodyCodec.writeOctet(IIOP_MAJOR);
      profileBodyCodec.writeOctet(IIOP_MINOR);
      profileBodyCodec.writeString(host);
      profileBodyCodec.writeShort(port);
      try {
	byte[] objKey = objectKey.getBytes("US-ASCII");

	// Remove all the RFC 2396 escape sequences...
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	for(int i = 0; i < objKey.length; i++) {
	  byte b = objKey[i];
	  if(b != ASCII_PERCENT)
	    buf.write(b);
	  else {
	    // Get the hex value represented by the two bytes after '%'
	    try {
	      String hexPair = new String(objKey, i + 1, 2, "US-ASCII");
	      short sh = Short.parseShort(hexPair, 16);
	      if(sh > Byte.MAX_VALUE)
		b = (byte)(sh + 2*Byte.MIN_VALUE); // Conversion from unsigned to signed
	      else
		b = (byte)sh;
	    }
	    catch(UnsupportedEncodingException uee) {
	      b = 0;
	    }
	    buf.write(b);
	    i += 2;
	  }
	}

	profileBodyCodec.writeOctetSequence(buf.toByteArray());

	byte[] encapsulatedProfile = profileBodyCodec.writtenBytes();

	// Write encapsulated profile to main IOR codec
	codecStrategy.writeOctetSequence(encapsulatedProfile);

	String hexString = codecStrategy.writtenString();
	ior = "IOR:" + hexString;

	codecStrategy = null;

      }
      catch(UnsupportedEncodingException uee) {
	// It should never happen
	uee.printStackTrace();
      }

    }

    // This method returns true if and only if the supplied byte,
    // interpreted as an US-ASCII encoded character, corresponds to an
    // unreserved URI character. See RFC 2396 for details.
    private boolean isUnreservedURIChar(byte b) {
      // An upper case letter?
      if((ASCII_UPPER_A <= b)&&(ASCII_UPPER_Z >= b))
	return true;
      // A lower case letter?
      if((ASCII_LOWER_A <= b)&&(ASCII_LOWER_Z >= b))
	return true;
      // A decimal digit?
      if((ASCII_ZERO <= b)&&(ASCII_NINE >= b))
	return true;
      // An unreserved, but not alphanumeric character?
      if((b == ASCII_MINUS)||(b == ASCII_UNDERSCORE)||(b == ASCII_DOT)||(b == ASCII_BANG)||(b == ASCII_TILDE)||
	 (b == ASCII_STAR)||(b == ASCII_QUOTE)||(b == ASCII_OPEN_BRACKET)||(b == ASCII_CLOSED_BRACKET))
	return true;

      // Anything else is not allowed
      return false;
    }

    public String getURL() {
      int portNum = port;
      if(portNum < 0)
	portNum += 65536;
      return "corbaloc::" + host + ":" + portNum + "/" + objectKey;
    }

    public String getIOR() {
      return ior;
    }

    public FIPA.MTS getObject() {
      return FIPA.MTSHelper.narrow(orb.string_to_object(ior));
    }

    private static abstract class CDRCodec {

      protected byte[] readBuffer;
      protected StringBuffer writeBuffer;
      protected int readIndex = 0;
      protected int writeIndex = 0;

      protected CDRCodec(String hexString) {
	// Put all Hex digits into a byte array
	readBuffer = bytesFromHexString(hexString);
	readIndex = 1;
	writeBuffer = new StringBuffer(255);
      }

      protected CDRCodec(byte[] hexDigits) {
	readBuffer = new byte[hexDigits.length];
	System.arraycopy(hexDigits, 0, readBuffer, 0, readBuffer.length);
	readIndex = 1;
	writeBuffer = new StringBuffer(255);
      }

      public String writtenString() {
	return new String(writeBuffer);
      }

      public byte[] writtenBytes() {
	return bytesFromHexString(new String(writeBuffer));
      }

      public byte readOctet() {
	return readBuffer[readIndex++];
      }

      public byte[] readOctetSequence() {
	int seqLen = readLong();
	byte[] result = new byte[seqLen];
	System.arraycopy(readBuffer, readIndex, result, 0, seqLen);
	readIndex += seqLen;
	return result;
      }

      public String readString() { 
	  int strLen = readLong(); // This includes '\0' terminator
	  String result = new String(readBuffer, readIndex, strLen - 1);
	  readIndex += strLen;
	  return result;
      }

      // These depend on endianness, so are deferred to subclasses
      public abstract short readShort();   // 16 bits
      public abstract int readLong();      // 32 bits
      public abstract long readLongLong(); // 64 bits

      // Writes a couple of hexadecimal digits representing the given byte.
      // All other marshalling operations ultimately use this method to modify
      // the write buffer
      public void writeOctet(byte b) {
	char[] digits = new char[2];
	digits[0] = HEX[(b & 0xF0) >> 4]; // High nibble
	digits[1] = HEX[b & 0x0F]; // Low nibble
	writeBuffer.append(digits);
	writeIndex++;
      }

      public void writeOctetSequence(byte[] seq) {
	int seqLen = seq.length;
	writeLong(seqLen);
	for(int i = 0; i < seqLen; i++)
	  writeOctet(seq[i]);
      }

      public void writeString(String s) {
	int strLen = s.length() + 1; // This includes '\0' terminator
	writeLong(strLen);
	byte[] bytes = s.getBytes();
	for(int i = 0; i < s.length(); i++)
	  writeOctet(bytes[i]);
	writeOctet((byte)0x00);
      }

      // These depend on endianness, so are deferred to subclasses
      public abstract void writeShort(short s);   // 16 bits
      public abstract void writeLong(int i);      // 32 bits
      public abstract void writeLongLong(long l); // 64 bits

      protected void setReadAlignment(int align) {
	while((readIndex % align) != 0)
	  readIndex++;
      }

      protected void setWriteAlignment(int align) {
	while(writeIndex % align != 0)
	  writeOctet((byte)0x00);
      }

      private byte[] bytesFromHexString(String hexString) {
	int hexLen = hexString.length() / 2;
	byte[] result = new byte[hexLen];

	for(int i = 0; i < hexLen; i ++) {
	  String currentDigit = hexString.substring(2*i, 2*(i + 1));
	  Short s = Short.valueOf(currentDigit, 16);
	  result[i] = s.byteValue();
	}

	return result;
      }

    } // End of CDRCodec class

    private static class BigEndianCodec extends CDRCodec {

      public BigEndianCodec(String ior) {
	super(ior);
	writeOctet((byte)0x00); // Writes 'Big Endian' magic number
      }

      public BigEndianCodec(byte[] hexDigits) {
	super(hexDigits);
	writeOctet((byte)0x00); // Writes 'Big Endian' magic number
      }

      public short readShort() {
	setReadAlignment(2);
	short result = (short)((readBuffer[readIndex++] << 8) + readBuffer[readIndex++]);
	return result;
      }

      public int readLong() {
	setReadAlignment(4);
	int result = (readBuffer[readIndex++] << 24) + (readBuffer[readIndex++] << 16);
	result += (readBuffer[readIndex++] << 8) + readBuffer[readIndex++];
	return result;
      }

      public long readLongLong() {
	setReadAlignment(8);
	long result = (readBuffer[readIndex++] << 56) + (readBuffer[readIndex++] << 48);
	result += (readBuffer[readIndex++] << 40) + (readBuffer[readIndex++] << 32);
	result += (readBuffer[readIndex++] << 24) + (readBuffer[readIndex++] << 16);
	result += (readBuffer[readIndex++] << 8) + readBuffer[readIndex++];
	return result;
      }

      public void writeShort(short s) {
	setWriteAlignment(2);
	writeOctet((byte)((s & 0xFF00) >> 8));
	writeOctet((byte)(s & 0x00FF));
      }

      public void writeLong(int i) {
	setWriteAlignment(4);
	writeOctet((byte)((i & 0xFF000000) >> 24));
	writeOctet((byte)((i & 0x00FF0000) >> 16));
	writeOctet((byte)((i & 0x0000FF00) >> 8));
	writeOctet((byte)(i & 0x000000FF));
      }

      public void writeLongLong(long l) {
	setWriteAlignment(8);
	writeOctet((byte)((l & 0xFF00000000000000L) >> 56));
	writeOctet((byte)((l & 0x00FF000000000000L) >> 48));
	writeOctet((byte)((l & 0x0000FF0000000000L) >> 40));
	writeOctet((byte)((l & 0x000000FF00000000L) >> 32));
	writeOctet((byte)((l & 0x00000000FF000000L) >> 24));
	writeOctet((byte)((l & 0x0000000000FF0000L) >> 16));
	writeOctet((byte)((l & 0x000000000000FF00L) >> 8));
	writeOctet((byte)(l & 0x00000000000000FFL));
      }

    } // End of BigEndianCodec class

    private static class LittleEndianCodec extends CDRCodec {

      public LittleEndianCodec(String ior) {
	super(ior);
	writeOctet((byte)0x01); // Writes 'Little Endian' magic number
      }

      public LittleEndianCodec(byte[] hexDigits) {
	super(hexDigits);
	writeOctet((byte)0x01); // Writes 'Little Endian' magic number
      }

      public short readShort() {
	setReadAlignment(2);
	short result = (short)(readBuffer[readIndex++] + (readBuffer[readIndex++] << 8));
	return result;
      }

      public int readLong() {
	setReadAlignment(4);
	int result = readBuffer[readIndex++] + (readBuffer[readIndex++] << 8) + (readBuffer[readIndex++] << 16) + (readBuffer[readIndex++] << 24);
	return result;
      }

      public long readLongLong() {
	setReadAlignment(8);
	long result = readBuffer[readIndex++] + (readBuffer[readIndex++] << 8);
	result += (readBuffer[readIndex++] << 16) + (readBuffer[readIndex++] << 24);
	result += (readBuffer[readIndex++] << 32) + (readBuffer[readIndex++] << 40);
	result += (readBuffer[readIndex++] << 48) + (readBuffer[readIndex++] << 56);
	return result;
      }

      public void writeShort(short s) {
	setWriteAlignment(2);
	writeOctet((byte)(s & 0x00FF));
	writeOctet((byte)((s & 0xFF00) >> 8));
      }

      public void writeLong(int i) {
	setWriteAlignment(4);
	writeOctet((byte)(i & 0x000000FF));
	writeOctet((byte)((i & 0x0000FF00) >> 8));
	writeOctet((byte)((i & 0x00FF0000) >> 16));
	writeOctet((byte)((i & 0xFF000000) >> 24));
      }

      public void writeLongLong(long l) {
	setWriteAlignment(8);
	writeOctet((byte)(l & 0x00000000000000FFL));
	writeOctet((byte)((l & 0x000000000000FF00L) >> 8));
	writeOctet((byte)((l & 0x0000000000FF0000L) >> 16));
	writeOctet((byte)((l & 0x00000000FF000000L) >> 24));
	writeOctet((byte)((l & 0x000000FF00000000L) >> 32));
	writeOctet((byte)((l & 0x0000FF0000000000L) >> 40));
	writeOctet((byte)((l & 0x00FF000000000000L) >> 48));
	writeOctet((byte)((l & 0xFF00000000000000L) >> 56));
      }

    }  // End of LittleEndianCodec class

    public String getProto() {
      return "iiop";
    }

    public String getHost() {
      return host;
    }

    public String getPort() {
      return Short.toString(port);
    }

    public String getFile() {
      return objectKey;
    }

    public String getAnchor() {
      return anchor;
    }
    
    

  } // End of IIOPAddress class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -