comp.lang.ada
 help / color / mirror / Atom feed
* How to implement I/O using Streams
@ 1997-04-01  0:00 Matthew Heaney
  1997-04-02  0:00 ` Robert Dewar
  0 siblings, 1 reply; 2+ messages in thread
From: Matthew Heaney @ 1997-04-01  0:00 UTC (permalink / raw)



The package Ada.Streams.Stream_IO is example of using a stream to do
(heterogeneous) file I/O.  I want to do network I/O - using BSD Sockets,
say - and use a streams interface to do it.

How do I do this?  The example in RM95 shows how a file object gives the
client a pointer to a stream (through which the I/O is performed), but is
silent about how the stream and file connect to each other.  How does the
stream know about the file? 

If I have a socket descriptor, how can I use a stream to do the I/O over
the socket?

How do I implement the Read and Write operations of my type that derives
from Root_Stream?

declare
   sd : int;  -- or perhaps I'll use a higher-level abstraction
   The_Stream : Stream_Access;
   The_Data : My_Record;
begin
   <create socket and connect to server>

   The_Stream := <get a stream connected to the socket descriptor sd>; -- ???

   My_Record'Write (The_Stream, The_Data);  -- more or less done for me
end;

Does anyone know how the implement that middle line?  Has anyone
implemented a streams interface, ie used something other than
Ada.Streams.Stream_IO or Ada.Text_IO.Text_Streams?

Here's a trial attempt:

type My_Stream (sd : access int) is
   new Root_Stream_Type with null record;

procedure Read (Stream ...);
-- reads from the socket descriptor discriminent

procedure Write (Stream ...);
-- writes to the socket descriptor discriminent

Suppose I built a higher-level abstraction.  Would this work:

   type Socket_File is limited private;

   type Stream_Access is access all Root_Stream_Type'Class;

   function Stream (File : Socket_File) return Stream_Access;
...
private
 
   type My_Stream (File : access Socket_File) is
      new Root_Stream_Type with null record;

   type Socket_File is limited
      record
         sd : int;
         Stream : aliased My_Stream (Socket_File'Access);
      end record;

   procedure Read (Stream : in out My_Stream...);
   procedure Write (Stream : in out My_Stream ...);

end;

Well, is that the way?  Is that how the language designers intended a
stream to be implemented?

Thank you much!
Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to implement I/O using Streams
  1997-04-01  0:00 How to implement I/O using Streams Matthew Heaney
@ 1997-04-02  0:00 ` Robert Dewar
  0 siblings, 0 replies; 2+ messages in thread
From: Robert Dewar @ 1997-04-02  0:00 UTC (permalink / raw)



Matthew said

<<The package Ada.Streams.Stream_IO is example of using a stream to do
(heterogeneous) file I/O.  I want to do network I/O - using BSD Sockets,
say - and use a streams interface to do it.

How do I do this?  The example in RM95 shows how a file object gives the
client a pointer to a stream (through which the I/O is performed), but is
silent about how the stream and file connect to each other.  How does the
stream know about the file?>>

The answer to your last question is that you write the Read and Write
procedures and they know what ever they need to know. This is simply
a straightforward programming problem with no magic. Remember that
Root_Stream_Type is an abstract type -- you have to supply all the
necessary code for a given instance.

If you want to look at examples of how this is done, look at the GLADE
code which precisely does what you are talking about (sends stream data
via sockets). In fact, while you are at it, check to see whether annex
E and GLADE might not solve your problem at a much higher level ...





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-04-02  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-01  0:00 How to implement I/O using Streams Matthew Heaney
1997-04-02  0:00 ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox