From: Volkan YAZICI
Subject: Imitating Socket-Like Streams
Date: 
Message-ID: <90d43097-4ec6-43be-8f8c-786dd8f5abcd@k8g2000yqn.googlegroups.com>
Hi,

I'm trying to imitate network-socket-like stream functionality in
Common Lisp with no success so far. What I'm trying to achieve is to
have some sort of stream that I can READ and WRITE. For this purpose,
I tried to use ECHO-STREAM and TWO-WAY-STREAM. Besides that I couldn't
make them to work, I also couldn't understand the difference between
them. (Why TWO-WAY-STREAM is a bidirectional "composite" stream, while
ECHO-STREAM is just a bidirectional stream?) Any kind of helps will be
really appreciated.


Regards.

From: Thomas A. Russ
Subject: Re: Imitating Socket-Like Streams
Date: 
Message-ID: <ymiskoyxw7c.fsf@blackcat.isi.edu>
Volkan YAZICI <·············@gmail.com> writes:

> Hi,
> 
> I'm trying to imitate network-socket-like stream functionality in
> Common Lisp with no success so far. What I'm trying to achieve is to
> have some sort of stream that I can READ and WRITE. For this purpose,
> I tried to use ECHO-STREAM and TWO-WAY-STREAM. Besides that I couldn't
> make them to work, I also couldn't understand the difference between
> them. (Why TWO-WAY-STREAM is a bidirectional "composite" stream, while
> ECHO-STREAM is just a bidirectional stream?) Any kind of helps will be
> really appreciated.

What did you try that didn't work?

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Volkan YAZICI
Subject: Re: Imitating Socket-Like Streams
Date: 
Message-ID: <a50ed6be-dd1a-4a17-9ce2-5365f1bcaab4@35g2000pry.googlegroups.com>
On Dec 8, 10:12 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> > I'm trying to imitate network-socket-like stream functionality in
> > Common Lisp with no success so far. What I'm trying to achieve is to
> > have some sort of stream that I can READ and WRITE. For this purpose,
> > I tried to use ECHO-STREAM and TWO-WAY-STREAM. Besides that I couldn't
> > make them to work, I also couldn't understand the difference between
> > them. (Why TWO-WAY-STREAM is a bidirectional "composite" stream, while
> > ECHO-STREAM is just a bidirectional stream?) Any kind of helps will be
> > really appreciated.
>
> What did you try that didn't work?

My ECHO-STREAM and TWO-WAY-STREAM eforts were far from having a bright
future -- as Pascal pointed out. I understand that this limitation of
standard Common Lisp streams is addressed by gray streams. (I once had
a similar experience while trying to extend STRING class.)

All my attempts were composed of trying to write to an output stream
(tried with both ECHO-STREAM and TWO-WAY-STREAM) and read from same
stream again. But as Pascal's diagrams pointed out, none of output
stream's data is fed into its input stream. That was the reason whay I
take END-OF-FILE exceptions when I try to issue a second READ on the
stream expecting my previously written data fed into the input stream.

What did I end up with? Having a FIFO queue implemented using linked
lists. From a complexity point of view, ironic.


Regards.
From: Pascal J. Bourguignon
Subject: Re: Imitating Socket-Like Streams
Date: 
Message-ID: <87oczmqqri.fsf@informatimago.com>
Volkan YAZICI <·············@gmail.com> writes:

> What did I end up with? Having a FIFO queue implemented using linked
> lists. From a complexity point of view, ironic.

In my experience, this is not a rare occurence, with any system,
framework or library.  (I don't have much experience with prolog or
ontologies though).

That's also a reason why having a lot of libraries doesn't matter
either, I think.



Namely, a library is designed with some exact (if unexpressed)
requirements.  If your own requirements are a subset of these original
requirement, you are lucky.  But most of the time, your requirements
will be a superset of the available library, or worse, their symetric
difference won't be empty.  If you don't realize it, you will be using
the library without implementing your own requirements, and your project
will fail.  If you realize it, you may try to write a "wrapper"
implementing the missing parts, or it may be the case that the "wrapper"
is 80% and the existing library is only 20% and that it would be much
simplier to just rewrite everything from scratch (giving probably less
work than the 80% wrapper, since you don't have to bend your
architecture around an existing 20% library).

Of course, if your requirements are to just fulfill whatever the
framework provides (eg. to implement the look-and-feel of a given GUI
environment), then it's ok.  But you have to realize that you aren't
writting your own application, with your own requirements, but you're
writting an application implemented by the framework designers, to their
own requirements.  If your users and customers are happy with that, so
good for you.



Now, thanks to open source, you can also wear the hat of the framework
designer, and extend the framework toward the requirements of your own
application.  In the case at hand that means that even without using
Gray Streams, you could add a SYSTEM:PIPE-STREAM to a couple of free CL
implementations, and write a CDR documenting their specifications.

-- 
__Pascal Bourguignon__
From: Pascal J. Bourguignon
Subject: Re: Imitating Socket-Like Streams
Date: 
Message-ID: <87wseaqxyv.fsf@informatimago.com>
Volkan YAZICI <·············@gmail.com> writes:
> I'm trying to imitate network-socket-like stream functionality in
> Common Lisp with no success so far. What I'm trying to achieve is to
> have some sort of stream that I can READ and WRITE. For this purpose,
> I tried to use ECHO-STREAM and TWO-WAY-STREAM. Besides that I couldn't
> make them to work, I also couldn't understand the difference between
> them. (Why TWO-WAY-STREAM is a bidirectional "composite" stream, while
> ECHO-STREAM is just a bidirectional stream?) Any kind of helps will be
> really appreciated.

Sorry, but it's not clear what you want to archieve.

Any bidirectional stream, you can READ and WRITE.



An ECHO-STREAM is an input stream like this:

                       +-----------------+
                       |    echo stream  |
         -----------------\              +--\
      -->  input-stream    >----->+---------->
         -----------------/       |      +--/
                       |          |      |
         -----------------/       |      |
       <-- output-stream <--------+      |
         -----------------\              | 
                       |                 |
                       +-----------------+

When you read from an echo stream, it reads from its input stream, and
writes a copy of the data to its output stream.  Then it returns the
data read from the input stream.



An TWO-WAY-STREAM is a bidirectional stream like this:

                       +-----------------+
                       |    2way stream  |
         -----------------\              +--\
      -->  input-stream    >------>---------->
         -----------------/              +--/
                       |                 |
         -----------------/              +--/
       <-- output-stream <--------<--------<
         -----------------\              +--\
                       |                 |
                       +-----------------+

It only allows you to handle two independent streams, one input and one
output, as if they were part of the same bidirectional stream.


You can draw similarly BROADCAST-STREAM,  CONCATENATED-STREAM, and
SYNONYM-STREAM.



Then if what you want is to implement a pipe, it would be something like
this:

                       +-----------------+
                       |    pipe stream  |
                       |                 +--\
                       |          +---------->
                       |          |      +--/
                       |          ^      |
                       |          |      +--/
                       |          +--------<
                       |                 +--\
                       |                 |
                       +-----------------+

You can try to combine the other construction blocks as you want, you
will never get something like a pipe stream.  It is not possible in
standard Common Lisp.


But you could do it if your implementation provides Gray Streams.
Another implementation specific consideration is whether you want to use
this pipe stream as a synchronization mechanism between threads.  In
this case, you must have a buffer size parameter.  Otherwise you will
need to handle a possibly unbounded buffer.  A degenerate case of the
later would be to store the data to a file:


[25]> (handler-bind ((file-error (lambda (err) (invoke-restart 'continue))))
        (with-open-file (out "/tmp/pipe" :direction :output
                             :if-does-not-exist :create
                             :if-exists :supersede)
          (with-open-file (inp "/tmp/pipe" :direction :input
                               :if-does-not-exist :error)
            (prin1 '(Hello World)   out)  (finish-output out)
            (prin1 '(How do you do) out)  (finish-output out)
            (print (read inp)) 
            (prin1 '(Fine thank you) out) (finish-output out)
            (print (read inp))
            (print (read inp))))
        (delete-file "/tmp/pipe"))

(HELLO WORLD) 
(HOW DO YOU DO) 
(FINE THANK YOU) 
T
[26]> 



Or you could implement your own ADT.  After all, a pipe is a mere FIFO.
Do you really need it to use the standard CL stream API?


-- 
__Pascal Bourguignon__
From: ···@informatimago.com
Subject: Re: Imitating Socket-Like Streams
Date: 
Message-ID: <c1408264-a366-49ad-9bc6-8fa5d1591882@v42g2000yqv.googlegroups.com>
On 8 dic, 20:16, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> Volkan YAZICI <·············@gmail.com> writes:
> > I'm trying to imitate network-socket-like stream functionality in
> > Common Lisp with no success so far. What I'm trying to achieve is to
> > have some sort of stream that I can READ and WRITE. For this purpose,
> > I tried to use ECHO-STREAM and TWO-WAY-STREAM. Besides that I couldn't
> > make them to work, I also couldn't understand the difference between
> > them. (Why TWO-WAY-STREAM is a bidirectional "composite" stream, while
> > ECHO-STREAM is just a bidirectional stream?) Any kind of helps will be
> > really appreciated.
>
> Sorry, but it's not clear what you want to archieve.
>
> Any bidirectional stream, you can READ and WRITE.
>
> An ECHO-STREAM is an input stream like this:

Of course, I meant:

   An ECHO-STREAM is a bidirectional stream like this:

and to draw this:

                       +-----------------+
                       |    echo stream  |
         -----------------\              +--\
      -->  input-stream    >----->+---------->
         -----------------/       |      +--/
                       |          v      |
         -----------------/       |      +---/
       <-- output-stream <--------+<--------<
         -----------------\              +---\
                       |                 |
                       +-----------------+


--
__Pascal Bourguignon__