[AWS] AWS memory streams

Wiljan Derks wiljan.derks at nxp.com
Thu Sep 24 19:48:50 CEST 2009


Kalle,

You wrote:
>I planning a small app which would mostly read data from database,  
>format it a bit and present it as a html table, and a memory stream  
>seems like a good way of doing this. But how the streams should be  
>used? The example in the guide (3.3.11) doesn't quite seem to work  
>since AWS.Response.Stream expects access to Stream_Type'Class and in  
>the example it is given AWS.Resources.Streams.Memory.Stream_Type.
>Any examples available? Or should some other way be considered?

I am doing this in my application.
To use this concept, I also implemented a package AWS.Resources.Streams.Inline.
It is now not part of AWS, but very usefull to create this kind of streams.

Here a fragment from my code to response with a csv stream from my server (hope this help you to get started).
I hope this explains how to use the attached packages.
Basically you only need to implement an Add_Data routine that either adds some data to the stream or
calls the done procedure to tell that the stream is ready.
The Aws framework will deallocate the stream object automatically when it is finsihed.

   type Csv_Stream_Type is new AWS.Resources.Streams.Inline.Stream_Type with
      record
         Dcf    : Pfile_Info;
         Dcf_Nr : Integer;
         Device : Integer := 0;
      end record;
   type Pcsv_Stream_Type is access all Csv_Stream_Type;
   procedure Add_Data (Resource : in out Csv_Stream_Type);

   procedure Add_Data (Resource : in out Csv_Stream_Type) is

      procedure Add (S : String) is
      begin
         Add (Resource, S);
      end Add;

      procedure Line is
      begin
         Add (ASCII.CR & ASCII.LF);
      end Line;

      procedure Add_Float (N : Float) is
      begin
         if Valid_Real (N) then
            Add (Float_Image (N));
         end if;
      end Add_Float;

      procedure Add_Limit (Lim : Name_Type) is
         L : Float := Float_Value (Value (Lim), True);
      begin
         if Valid_Real (L) then
            Add_Float (L);
         else
            Add (Value (Lim));
         end if;
      end Add_Limit;

   begin
      Validate (Resource.Dcf_Nr);
      if Resource.Device = 0 then
         Add ("Data,Name");
         for I in Resource.Dcf.Symbols'Range loop
            Add (",");
            Add (Value (Resource.Dcf.Symbols (I).Name));
         end loop;
         Line;
         Add ("Low Limit,");
         for I in Resource.Dcf.Symbols'Range loop
            Add (",");
            Add_Limit (Resource.Dcf.Symbols (I).Low_Limit);
         end loop;
         Line;
         Add ("High Limit,");
         for I in Resource.Dcf.Symbols'Range loop
            Add (",");
            Add_Limit (Resource.Dcf.Symbols (I).High_Limit);
         end loop;
         Line;
         Add ("Unit,");
         for I in Resource.Dcf.Symbols'Range loop
            Add (",");
            Add (Value (Resource.Dcf.Symbols (I).Unit));
         end loop;
         Line;
         Resource.Device := 1;
      elsif Resource.Device in Resource.Dcf.Data'Range then
         Add (Itos (Resource.Dcf.Data (Resource.Device).Device_Id.Device_Nr));
         Add (",");
         if Status_Oke (Resource.Dcf.Data (Resource.Device).Status) then
            Add ("P");
         else
            Add ("F");
         end if;
         Add (Itos (Resource.Dcf.Data (Resource.Device).Type_Result));
         for I in Resource.Dcf.Symbols'Range loop
            Add (",");
            if Resource.Dcf.Symbols (I).Idx in
               Resource.Dcf.Data (Resource.Device).Data'Range then
               Add_Float (Resource.Dcf.Data (Resource.Device).Data
                          (Resource.Dcf.Symbols (I).Idx).R);
            end if;
         end loop;
         Line;
         Resource.Device := Resource.Device + 1;
      else
         Done (Resource);
      end if;
   end Add_Data;

   function Dcfcsv_Response (Request : in AWS.Status.Data)
                             return AWS.Response.Data is
      URI   : constant String := AWS.Status.URI (Request);
      DcfId : constant String := URI (6 .. URI'Last);
      Str   : Pcsv_Stream_Type;
      Idx   : Integer;
   begin
      Idx := Find_File (DcfId);
      if Idx /= 0 then
         Validate (Idx);
         Str := new Csv_Stream_Type;
         Str.Dcf_Nr := Idx;
         Str.Dcf := Files (Idx);
         return Stream
           (Content_Type => "application/csv",
            Handle => Str,
            Cache_Control => AWS.Messages.Unspecified,
            Encoding      => AWS.Status.Preferred_Coding (Request),
            Disposition => Attachment,
            User_Filename => Get_Filename (Str.Dcf.all) & ".csv");
      end if;
      return AWS.Response.Empty;
   end Dcfcsv_Response;


Greetings,

Wiljan



-----Original Message-----
From: aws-bounces at lists.adacore.com [mailto:aws-bounces at lists.adacore.com] On Behalf Of kvkoskin at cc.hut.fi
Sent: 2009 Sep 24 1:56 PM
To: aws at lists.adacore.com
Subject: [AWS] AWS memory streams

Hello!

I planning a small app which would mostly read data from database,  
format it a bit and present it as a html table, and a memory stream  
seems like a good way of doing this. But how the streams should be  
used? The example in the guide (3.3.11) doesn't quite seem to work  
since AWS.Response.Stream expects access to Stream_Type'Class and in  
the example it is given AWS.Resources.Streams.Memory.Stream_Type.

Any examples available? Or should some other way be considered?

    -Kalle


_______________________________________________
AWS mailing list
AWS at lists.adacore.com
/no-more-mailman.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: aws-resources-streams-inline.adb
Type: application/octet-stream
Size: 1714 bytes
Desc: aws-resources-streams-inline.adb
Url : /pipermail/aws/attachments/20090924/02670ae2/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: aws-resources-streams-inline.ads
Type: application/octet-stream
Size: 1063 bytes
Desc: aws-resources-streams-inline.ads
Url : /pipermail/aws/attachments/20090924/02670ae2/attachment-0001.obj 


More information about the AWS mailing list