Example #1
0
  /**
   * Verifies that the default encoding (ISO8859_1) works for char, char arrays, and strings.
   * Reading the string forces alignment of the 4-byte length, and ignores any null terminator.
   */
  @Test
  public void testDefaultEncodingChar() throws Exception {
    byte[] codedText = {'a', 's', 'd', 'f', 'x', 'y', 'z', '*', 0, 0, 0, 5, 'C', 'A', 'F', 'E', 0};
    CDRInputStream stream = new CDRInputStream(orb, codedText);
    assertEquals("char 1", 'a', stream.read_char());
    assertEquals("char 2", 's', stream.read_char());
    assertEquals("char 3", 'd', stream.read_char());
    assertEquals("char 4", 'f', stream.read_char());

    char[] buffer = new char[4];
    buffer[0] = 'w';
    stream.read_char_array(buffer, 1, 3);
    assertEquals("char array", "wxyz", new String(buffer));
    assertEquals("string value", "CAFE", stream.read_string());
    stream.close();
  }
Example #2
0
  /**
   * Run method. Inherited from runnable, this method is used to stay listening to the socket for
   * new messages.
   */
  @Override
  public void run() {
    // create incomplete table if doesn't exist
    if (incompleteMessages == null) {
      incompleteMessages = new HashMap<String, FragmentedMessage>();
    }

    // allocates a buffer
    byte[] buffer = new byte[packetMaxSize];

    while (connected) {
      try {
        // if the number of messages in incomplete table is greater than a
        // specified threshold we inspect this table to clear incomplete packets
        // collections.
        if (incompleteMessages.size() > incompleteMessagesThreshold) {
          dropIncompleteMessages();
        }

        // creates a new datagram to be read
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        try {
          // wait for the datagram
          socket.receive(packet);
        } catch (SocketTimeoutException ste) {
          continue;
        } catch (InterruptedIOException e) {
          throw new org.omg.CORBA.TRANSIENT("Interrupted I/O: " + e);
        } catch (IOException se) {
          throw to_COMM_FAILURE(se);
        }
        // the packet was received successfully.
        CDRInputStream in = new CDRInputStream(configuration.getORB(), packet.getData());

        // Read the header
        //
        // Manually read in the stream rather than using the generated
        // PacketHeader_1_0Helper
        // as we may need to alter endian half way through.
        org.omg.MIOP.PacketHeader_1_0 header = new org.omg.MIOP.PacketHeader_1_0();
        header.magic = new char[4];
        in.read_char_array(header.magic, 0, 4);

        // Verify the message is MIOP
        if (!MulticastUtil.matchMIOPMagic(header.magic)) {
          // if it isn't a MIOP message I can ignore it
          continue;
        }

        // We know it is MIOP from now on.
        header.hdr_version = in.read_octet();
        header.flags = in.read_octet();

        // Set endian for the stream
        in.setLittleEndian((0x01 & header.flags) != 0);

        header.packet_length = in.read_ushort();
        header.packet_number = in.read_ulong();
        header.number_of_packets = in.read_ulong();
        header.Id = org.omg.MIOP.UniqueIdHelper.read(in);

        int pos = in.get_pos();
        // difference to next MulticastUtil.BOUNDARY (which is an 8 byte boundary)
        int header_padding = MulticastUtil.BOUNDARY - (pos % MulticastUtil.BOUNDARY);
        header_padding = (header_padding == MulticastUtil.BOUNDARY) ? 0 : header_padding;

        // skip header_padding bytes anyway, because if no body is
        // present, nobody will try to read it
        in.skip(header_padding);

        // read the GIOP data
        byte data[] = new byte[header.packet_length];
        if (in.available() < data.length) {
          throw new MARSHAL(
              "Impossible length in MIOP header. Header denotes length of "
                  + header.packet_length
                  + " but only "
                  + in.available()
                  + " is available.");
        }
        in.read_octet_array(data, 0, header.packet_length);

        String messageId = new String(header.Id);
        FragmentedMessage message = incompleteMessages.get(messageId);

        // verify if it's the first message to arrive
        if (message == null) {
          // If this is the first fragment of the message create a  fragmented message
          message = new FragmentedMessage();
          try {
            message.configure(configuration);
          } catch (ConfigurationException e) {
            logger.error("couldn't create a Fragmented message", e);
            throw new IllegalArgumentException("wrong configuration: " + e);
          }
          incompleteMessages.put(messageId, message);
        }

        if (logger.isDebugEnabled()) {
          logger.debug(
              "Received message number "
                  + (header.packet_number + 1)
                  + " out of "
                  + header.number_of_packets
                  + " and adding fragment of size "
                  + data.length);
        }
        message.addFragment(header, data);

        // verify if it's the last message to arrive
        if (message.isComplete()) {
          synchronized (this) {
            incompleteMessages.remove(messageId);
            fullMessages.addLast(message.buildMessage());
            notifyAll();
          }
        }
      } catch (COMM_FAILURE e) {
        if (logger.isDebugEnabled()) {
          logger.debug("Transport to " + connection_info + ": stream closed " + e.getMessage());
        }
        if (connected) {
          close();
        }
      } catch (SystemException e) {
        if (logger.isWarnEnabled()) {
          logger.warn("ServerMIOPConnection caught exception.", e);
        }
      } catch (Throwable e) {
        if (logger.isErrorEnabled()) {
          logger.error("ServerMIOPConnection caught exception.", e);
        }
      }
    }
  }