sipcapture / hep-java

HEP: JAVA implementation of HEP/EEP Encapsulation Protocol
http://sipcapture.org
GNU General Public License v3.0
5 stars 5 forks source link

Importable Library for HEP parsing #3

Closed nehararora closed 7 years ago

nehararora commented 7 years ago

Hi,

In writing an application to parse an incoming HEP stream, we discovered that changes were needed to the java reference implementation to make it usable as a library. Basic idea was to be able to import the library as a jar, instantiate the parser class, and be able to parse out the HEPStructure. Wanted to see if there is any interest in merging the changes in.

Here is an example of using the library in an application after the changes:


//...
//...
import org.homer.hep.ParserHEPv3;
import org.homer.hep.HEPStructure;
//...
//...

void consume(Socket socket) {
    // hep input stream      
    InputStream in = socket.getInputStream();

    // get header bytes
    byte[] header = new byte[4];
    byte[] expectedHeader = {0x48, 0x45, 0x50, 0x33};
    in.read(header);

    // check if header spells "HEP3"
    if (Arrays.equals(header, expectedHeader)){

        // get the total length bytes
        byte[] lenBytes = new byte[2]; 
        in.read(lenBytes);
        int len = ((lenBytes[0]<<8)+ lenBytes[1]);
        // get payload bytes
        byte[] bytes = new byte[len];
        in.read(bytes);

        // parse out HEP structure
        ParserHEPv3 parser = new ParserHEPv3();
        HEPStructure hepStructure = parser.parse(ByteBuffer.wrap(bytes), len, socket.getInetAddress().toString());
        // this prints out the HEP structure string representation
        System.out.println("HEP Structure: " + hepStructure);

        // grab payload from HEP structure
        String payload = new String(hepStructure.payloadByteMessage.array());
        //...
        //... do stuff with payload
        //...
    }
}
CLAassistant commented 7 years ago

CLA assistant check
All committers have signed the CLA.

adubovikov commented 7 years ago

Thank you very much