Two Protocols, One Wire

It’s not that I hate CPN Tools, but friends do not force friends to write code like the below.  The problem is that I have a couple programs.  They communicate over a channel using a given packet format.  Unfortunately CPN Tools misunderstood the format of the packets for one specific kind of packets, misrepresenting them as packets in another format.  Basically, I have programs communicating with two protocols over the same wire.

I cannot just go and change one of the formats, as both protocols have been used in the past (though by different programs that never sent/received packets in more than one of the formats).  Now, I’m writing a program that needs to handle both kinds of packets, and so I need to distinguish the two kinds of packets (as they need completely different parsers).  To do that, I need to unify the fields of both kinds of packets, escaping as soon as I know the format, but continuing the calculation until I know which format to parse.

I need to inspect the first 9 integers on the line, each adding more assumptions that I need to check.  Also, this all has to be changed is just one of the protocols ever change (and they are both designed to be extensible…).  Right now my matching code is this beaut, but it actually relies on the fact that I don’t expect rtype to ever be 8; if it is, the code is actually wrong (in that it may wrongly decide that a legal GFC packet is instead a BIS format packet).  Of course, the initial prefix of the packet would then be 2, 10, 8, 1, 1, 1, 1/2/3, 2, 2, which is a GFC packet with command 2, 10 parameters, return type byte array, first parameter an input value of type integer with value 1, and second parameter an parameter of type string with length 2 or an output parameter of type string followed by another out parameter.  At the same time, this packet is also a valid BIS packet with 2 booleans, 10 integers, 8 strings, the two booleans being both true, and the first integers being 1, 1/2/3, 2, and 2.  Let’s just assume that rtype cannot be 8, as this makes things much easier than trying to distinguish these kinds of packets.

So, now I’ve spent most of my day productively writing that kind of crap code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.