locked
How do I write a parser to iterate through an array? RRS feed

  • Question

  • I'm writing a parser for NM 3.3. First off, I must say that getting a basic parser working was very easy, so kudos on that. Anyway, my protocol contains a variable number of "sections", and each section has a section identifier within it, along with some section-specific data. I've set up the array of sections like this:

     
    struct Section {
      UINT16 section_type
      switch( section_type ) {
         case 0 : /* section 0 data */;
         case 1 : /* section 1 data */;
         ...
      }
    }

    Then in the Protocol section:

    UINT16 num_sections;
    Section sections[num_sections];

    This is working fine, but I would like the description of each packet (i.e. Protocol MyProtocol = <something>) to include the object name, which is contained in one of the sections. But the sections can occur in any order and some may or may not be present, so I can't use an array index. How can I iterate through the array of sections looking for the one with the right section number and return the string in that section?
    • Edited by Scott_Matthews Thursday, October 15, 2009 5:41 PM Slight formatting fix
    Thursday, October 15, 2009 4:30 PM

Answers

  • Hi Scott,

    Seems like you've gotten off to a great start.  There's a couple of ways to accomplish what you'd like.  I'm thinking you could try something like the following (I've inlined the array just for brevity):

    Protocol MyProtocol = FormatString("%s", MyProtocolDescription)
    {
    UINT16 num_sections;
    Struct sections [ num_sections ]
    {
      UINT16 section_type
      switch( section_type ) {
         case 0 : /* section 0 data */;
             [MyProtocolDescription = MyProtocolDescription + " section 0 " + this] //this should be the somedata
             UINT16 somedata;
         case 1 : /* section 1 data */;
         ...
      }
    };
    }

    It's a little hard to test without your data and npl, but the above should give you a good idea.  This way you can pull out the information you hit in the array into the description line of the protocol.  You can get fancy with some other markers in properties or looking for fields and inline conditional operators (?:) to filter out the information you'd like to display.

    If you look at some other protocols, like HTTP for instance, you can see how they modify their descriptions in the conversation view, as well as all the information they bubble to the top.

    Hope that helps.

    Thanks,
    Michael Hawker | Program Manager | Network Monitor
    • Marked as answer by Scott_Matthews Thursday, October 15, 2009 11:38 PM
    Thursday, October 15, 2009 8:45 PM

All replies

  • Hi Scott,

    Seems like you've gotten off to a great start.  There's a couple of ways to accomplish what you'd like.  I'm thinking you could try something like the following (I've inlined the array just for brevity):

    Protocol MyProtocol = FormatString("%s", MyProtocolDescription)
    {
    UINT16 num_sections;
    Struct sections [ num_sections ]
    {
      UINT16 section_type
      switch( section_type ) {
         case 0 : /* section 0 data */;
             [MyProtocolDescription = MyProtocolDescription + " section 0 " + this] //this should be the somedata
             UINT16 somedata;
         case 1 : /* section 1 data */;
         ...
      }
    };
    }

    It's a little hard to test without your data and npl, but the above should give you a good idea.  This way you can pull out the information you hit in the array into the description line of the protocol.  You can get fancy with some other markers in properties or looking for fields and inline conditional operators (?:) to filter out the information you'd like to display.

    If you look at some other protocols, like HTTP for instance, you can see how they modify their descriptions in the conversation view, as well as all the information they bubble to the top.

    Hope that helps.

    Thanks,
    Michael Hawker | Program Manager | Network Monitor
    • Marked as answer by Scott_Matthews Thursday, October 15, 2009 11:38 PM
    Thursday, October 15, 2009 8:45 PM
  • Thanks Michael - that's basically what I did. The "Writing a Parser from Wire to Window" document was extremely helpful and gave me about 3/4 of the information I needed to do what I wanted, and then I found the HTTP example and that pretty much gave me the rest. I'm sure I'll have more questions, so I'll keep this forum bookmarked!

    Thanks again
    Scott

    Thursday, October 15, 2009 11:48 PM
  • Just to show another example, here I defined the Section separately as a public struct.  Using this method I can give each array element a description and the use a concatonation, like Michael did above, to fill your protocol description.

    // 01 02 03 04 05

    Number UINT8
    {
     size = 1
    }

    [property.MyProtHeading = property.MyProtHeading + Section.ToString]
    Struct Section = FormatString("value is %d", a)
    {
     UINT8 a;
    }

    protocol frame = MyProtHeading
    {
     Section sect[5];
    }



    The output looks like this:

    - frame: value is 1value is 2value is 3value is 4value is 5
      + sect: value is 1
      + sect: value is 2
      + sect: value is 3
      + sect: value is 4
      + sect: value is 5

    Monday, October 19, 2009 4:59 PM