UnPackUDPData.java

  1. package org.opentrafficsim.road.network.factory.opendrive.communicationRTI;

  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;

  6. import org.opentrafficsim.road.network.factory.opendrive.data.RTIToOTSData;

  7. /** */
  8. public class UnPackUDPData
  9. {
  10.     /**
  11.      * @param inputStream input stream of data
  12.      * @return RTIToOTSData element
  13.      * @throws IOException on i/o error
  14.      */
  15.     static RTIToOTSData unPack(DataInputStream inputStream) throws IOException
  16.     {

  17.         RTIToOTSData simData = new RTIToOTSData();

  18.         simData.setTimeStamp(parseFloat(inputStream));
  19.         simData.setDeltaT(parseFloat(inputStream));

  20.         simData.getEgoPos().setX(parseFloat(inputStream));
  21.         simData.getEgoPos().setY(parseFloat(inputStream));
  22.         simData.getEgoPos().setZ(parseFloat(inputStream));

  23.         simData.getEgoOri().setYaw(parseFloat(inputStream));
  24.         simData.getEgoOri().setPitch(parseFloat(inputStream));
  25.         simData.getEgoOri().setRoll(parseFloat(inputStream));

  26.         simData.getEgoVel().setVx(parseFloat(inputStream));
  27.         simData.getEgoVel().setVy(parseFloat(inputStream));
  28.         simData.getEgoVel().setVz(parseFloat(inputStream));

  29.         simData.getEgoAngVel().setYawRate(parseFloat(inputStream));
  30.         simData.getEgoAngVel().setPitchRate(parseFloat(inputStream));
  31.         simData.getEgoAngVel().setRollRate(parseFloat(inputStream));

  32.         simData.setIntersection_type(parseInt(inputStream));
  33.         simData.setIntersection_phase(parseInt(inputStream));
  34.         simData.setIntersection_distance(parseFloat(inputStream));

  35.         // System.out.println(simData);

  36.         return simData;
  37.         // fos.write(receivePacket.getData(), 0, receivePacket.getLength());
  38.         // fos.flush();
  39.         // System.out.println(new String(receivePacket.getData()));

  40.         // break;
  41.         // InetAddress IPAddress = receivePacket.getAddress();
  42.         // int port = receivePacket.getPort();
  43.         // String capitalizedSentence = sentence.toUpperCase();
  44.         /*
  45.          * sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
  46.          * IPAddress, port); serverSocket.send(sendPacket);
  47.          */

  48.     }

  49.     /**
  50.      * @param inputStream input data stream
  51.      * @return float
  52.      * @throws IOException on i/o error
  53.      */
  54.     private static float parseFloat(DataInputStream inputStream) throws IOException
  55.     {
  56.         byte[] floats = new byte[4];
  57.         for (int i = 0; i < 4; i++)
  58.             floats[i] = inputStream.readByte();

  59.         /*
  60.          * for(int i = 0; i < floats.length / 2; i++) { byte temp = floats[i]; floats[i] = floats[floats.length - i - 1];
  61.          * floats[floats.length - i - 1] = temp; }
  62.          */
  63.         return ByteBuffer.wrap(floats).order(ByteOrder.LITTLE_ENDIAN).getFloat();
  64.     }

  65.     /**
  66.      * @param inputStream input data stream
  67.      * @return int
  68.      * @throws IOException on i/o error
  69.      */
  70.     private static int parseInt(DataInputStream inputStream) throws IOException
  71.     {
  72.         byte[] intBytes = new byte[4];
  73.         for (int i = 0; i < 4; i++)
  74.             intBytes[i] = inputStream.readByte();

  75.         /*
  76.          * byte[] newIntBytes = new byte[4]; newIntBytes[0] = intBytes[3]; newIntBytes[1] = intBytes[2]; newIntBytes[2] =
  77.          * intBytes[1]; newIntBytes[3] = intBytes[0];
  78.          */

  79.         return ByteBuffer.wrap(intBytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public final String toString()
  84.     {
  85.         return "UnPackUDPData []";
  86.     }
  87. }