From: sdl@linus.mitre.org (Steven D. Litvintchouk)
Subject: Re: Can this data be represented in Ada?
Organization: The MITRE Corporation, Bedford, MA
Date: Thu, 31 Dec 1992 23:22:21 GMT


In article <1992Dec31.014719.22174@nosc.mil> psm@nosc.mil (Scot Mcintosh) writes:

> I'm implementing a data communications protocol that has some data
> structural features that don't look possible to implement in Ada.
> Perhaps someone more experienced can suggest a way to accomplish
> what I'm trying to do (changing the protocol is not an option).
> 
> A Transmission Unit looks like:
> 
> +---------+----------+-----------+------+
> | Packet  |  Packet1 |  Packet2  | etc  |
> | Length  |          |           |  ... |
> +---------+----------+-----------+------+
>     |     |<-------->|<--------->|
>     |          ^          ^
>     |          |          |
>     |__________|__________+
> 
> 
> Each Packet looks like:
> 
> +---------+----------+--------------+
> | Data    |  Data    |  Padding     | 
> | Length  |          |(i.e. nothing)| 
> +---------+----------+--------------+
> |   |     |<-------->               |
> |   |          ^                    |
> |   |          |                    |
> |   |__________|                    |
> |                                   |
> |<--------------------------------->|
> 	Packet Length
> 
> For a given transmission, the Packet Length is fixed, but can be
> different in the next transmission. The Data Length within each
> Packet can be different for each Packet. 

You didn't happen to state how you know when you have read in all the
packets for a given Transmission Unit--is there some
"end-of-transmission" indicator?

You didn't happen to state how "data length" is related to "data".
I'll assume that "data" is in the form of a stream of bytes; and
that "data length" and "packet length" are two-byte integers giving
the number of bytes in Data and Packet, respectively.

   type BYTE is range 0 .. 255;
   for BYTE'SIZE use 8;
   subtype DATA_BYTE is BYTE;

   type LENGTH is range 0 .. 50000;      -- or whatever
   for LENGTH'SIZE use 16;

   DATA_LENGTH, PACKET_LENGTH:  LENGTH;
    
I suggest it's easier to simply define everything as an
undifferentiated stream of bytes, and use UNCHECKED_CONVERSION to
extract the more complicated, higher-level structure after you read in
the needed bytes.

The major use of UNCHECKED_CONVERSION, I have found, is to read in
low-level data representations (like bit streams or byte streams), and
then convert them to whatever high-level representation you wish after
you have interrogated them to find out their dynamic structure.

So you can read the first two bytes, and then use UNCHECKED_CONVERSION
to convert it to the integer type LENGTH.  Then that's the Packet
Length; you know you must read in that number of bytes for each
subsequent packet.  So for each subsequent packet, read in a number of
bytes given by Packet Length; use UNCHECKED_CONVERSION to convert the
first two bytes to an integer (which represents Data Length); then
process that number of subsequent bytes as Data, ignoring the rest.



--
Steven Litvintchouk
MITRE Corporation
202 Burlington Road
Bedford, MA  01730

Fone:  (617)271-7753
ARPA:  sdl@mitre.org
UUCP:  linus!sdl
	"Where does he get those wonderful toys?"
Newsgroups: comp.lang.ada