home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / ada / 3855 < prev    next >
Encoding:
Text File  |  1993-01-03  |  3.8 KB  |  116 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!att!att!fang!tarpit!cs.ucf.edu!crigler
  3. From: crigler@cs.ucf.edu (James Crigler)
  4. Subject: Re: Can this data be represented in Ada?
  5. Message-ID: <crigler.726037560@eola.cs.ucf.edu>
  6. Sender: news@cs.ucf.edu (News system)
  7. Organization: University of Central Florida
  8. References: <1992Dec31.014719.22174@nosc.mil>
  9. Date: Sun, 3 Jan 1993 05:06:00 GMT
  10. Lines: 104
  11.  
  12. psm@nosc.mil (Scot Mcintosh) writes:
  13.  
  14. >I'm implementing a data communications protocol that has some data
  15. >structural features that don't look possible to implement in Ada.
  16. >Perhaps someone more experienced can suggest a way to accomplish
  17. >what I'm trying to do (changing the protocol is not an option).
  18.  
  19. >A Transmission Unit looks like:
  20.  
  21. [Picture deleted]
  22.  
  23.  
  24. >Each Packet looks like:
  25.  
  26. [Picture deleted]
  27.  
  28. >For a given transmission, the Packet Length is fixed, but can be
  29. >different in the next transmission. The Data Length within each
  30. >Packet can be different for each Packet. I've tried to figure out how
  31. >to define Ada types for the Transmission Unit and Packet that
  32. >allow for the variability of the field sizes and haven't succeeded.
  33. >Any ideas? Thanks in advance.
  34.  
  35. I've done this kind of thing before, and it's not easy to map this kind
  36. of structure at compile time:  Ada just won't let you do it.  However,
  37. it may be possible to create your structure at run time by use of blocks
  38. contained in procedures.  Below is a little idea.  I've filled in some
  39. stuff by assumption.
  40.  
  41. procedure CREATE_TRANSMISSION (PACKET_SIZE : in POSITIVE,
  42.                    [other parms as required]) is
  43.  
  44.   subtype PACKET_LENGTH is POSITIVE range 1 .. 255; -- or whatever
  45.   subtype DATUM         is NATURAL  range 0 .. 255; -- or whatever
  46.   type PACKET_DATA is array (1 .. PACKET_SIZE - 1) of DATUM;
  47.   type PACKET is record
  48.     DATA_SIZE : POSITIVE;
  49.     DATA      : PACKET_DATA;
  50.   end record;
  51.  
  52.   TRANSIMISSION_UNIT_SIZE : constant := 1024;
  53.  
  54.   PACKET_COUNT : constant := TRANSMISSION_UNIT_SIZE / PACKET_SIZE;
  55.   -- But if it can be an even multiple, subtract 1.
  56.  
  57.   type TRANSMISSION_UNIT is record
  58.     PACKET_SIZE : PACKET_LENGTH;
  59.     PACKET      : array (1 .. PACKET_COUNT) of PACKET;
  60.   end record;
  61.  
  62.   -- Put necessary procedures to fill in data here, followed by
  63.   -- main body of procedure.
  64.  
  65.  
  66. At the receiving end, use a system like this:
  67.  
  68. with SYSTEM;
  69.  
  70. package body FRED is
  71.  
  72. subtype PACKET_LENGTH is POSITIVE range 1 .. 255; -- or whatever
  73. subtype DATUM         is NATURAL  range 0 .. 255; -- or whatever
  74. TRANSIMISSION_UNIT_SIZE : constant := 1024;
  75.  
  76. type RECEPTION_UNIT is record
  77.   PACKET_SIZE : PACKET_LENGTH;
  78.   DATA        : ARRAY (1 .. TRANMISSION_UNIT_SIZE - 1) of DATUM;
  79. end record;
  80.  
  81. procedure DECODE_DATA (DATA : in RECEPTION_UNIT) is
  82.  
  83.   type PACKET_DATA is array (1 .. DATA.PACKET_SIZE - 1) of DATUM;
  84.   type PACKET_REC is record
  85.     DATA_SIZE : PACKET_LENGTH;
  86.     DATA      : PACKET_DATA;
  87.   end record;
  88.  
  89.   PACKET_COUNT : constant := TRANSMISSION_UNIT_SIZE / DATA.PACKET_SIZE;
  90.   -- But if it can be an even multiple, subtract 1.
  91.  
  92.   type PACKET_ARRAY is array (1 .. PACKET_COUNT) of PACKET_REC;
  93.   PACKET : PACKET_ARRAY; for PACKET use at DATA.DATA'ADDRESS;
  94.   [other declarations and procedure body]
  95.  
  96.  
  97.  
  98. I haven't used anything like this in a long time but I think it should
  99. work.  The trick here is that the in mode parameter PACKET_SIZE is
  100. a constant at run time from the procedure's point of view.  There are
  101. two problems:
  102.  
  103.     1.  A hit in run time if there is a lot of range checking to do.
  104.         Remember that the language is required to do a lot of "safety"
  105.     checking behind your back, so you may have to program to
  106.     defend yourself against the language.
  107.  
  108.     2.  You don't get a global "type" that you can put in a package
  109.     spec for the world to see.
  110.  
  111. Hope this sheds a little lux et veritas.
  112.  
  113. Jim Crigler
  114. ------------------------------------------------------
  115. !PC
  116.