From: wintwrd@dutrun2.tudelft.nl (Paul v.d. Weerd)
Subject: Re: IPC between Smalltalk and C?
Organization: Delft University of Technology
Date: Mon, 5 Oct 1992 12:46:51 GMT

Dear David,

At Delft University of Technology (DUT), Faculty of Technical
Mathematics and Informatics, we have built a multi-process
dispatcher that will control a flexible assembly cell, using
Smalltalk-80 version 2.5 on Sun 3/60 and Sun Sparc Unix
workstations.

For this purpose, we try to do I/O in Smalltalk-80 via a so-called
'named pipe', which is a Unix directory-entry with actual pipe-
properties. The pipe must be created with the Unix shell command:
'/etc/mknod pipeName p'.

To test writing into such a pipe, you can open a shell, and type:
'cat > pipeName'. Everything you type afterwards will be put into
the named pipe. To test reading from such a pipe, you can open a
shell, and type: 'cat pipeName'. Everything that is put into the
named pipe will now be listed in the shell. It does not make any
difference in which order you do it: either put some text into the
pipe first, and then get it from the pipe; or try to get some text
from the pipe, and then put it into the pipe.

Normally there is no way that a program (C) can tell whether it is
performing I/O with a normal file or a named pipe. The only
difference in behaviour is: when no input is available in the
named pipe, the process (the running C-program) reading the pipe
is suspended until input has arrived in the pipe. We would like to
realize that same behaviour in Smalltalk as well.

The point we have reached so far, is that Smalltalk I/O via pipes
is actually possible, but ONLY WHEN WE HAVE FIRST CONNECTED THE
NAMED PIPES with the appropriate producers and consumers, e.g. by
using 'cat' in the way described above.

Once we try to put some text into a named pipe in Smalltalk first,
having the intention to connect a consuming 'cat' to the named
pipes afterwards, the Smalltalk program crashes immediately with
an error message: 'Unhandled exception: Transfer error' at the
moment that the message 'writeStream' is sent.

Once we try to get some text from a named pipe in Smalltalk first,
having the intention to connect a producing 'cat' to the named
pipes afterwards, the Smalltalk program does not crash, but does
not wait for the pipe's text either.

We would be very helped with a solution to this problem. It is
rather difficult to get information about this kind of pipe I/O,
because we did not find any hints in the manuals or any Smalltalk
code comments. If you do receive other suggestions, please let me
know. I have recently left DUT, but I am still involved in the
project described above.

The St80 code we are using is fairly straightforward, we only had
to write an extra instance method for the class Filename, called
'copyToString' (listed below).

----

"This code puts some text in a named pipe, which must be made
with: '/etc/mknod robotCom p'."

| commandStream |
commandStream := (Filename named: 'robotCom') writeStream.
commandStream nextPutAll: 'some text for pipe robotCom'.
commandStream nextPut: Character cr.
commandStream flush.

----

"This code gets a return-string from a named pipe, which must be
made with: '/etc/mknod robotRet p'."

| returnBuffer returnString |
returnBuffer := Filename named: 'robotRet'.
returnString := returnBuffer copyToString.
returnString printOn: Transcript.
Transcript cr; endEntry.

----

"This is the added method to get text from a pipe."

!Filename methodsFor: 'file utilities'!

copyToString
        "Copy the contents of a file (or pipe) to a string."

        | sourceFile buffer bufferSize amountRead string |
        string := ''.
        sourceFile := IOAccessor openFileReadOnly: self.
        [bufferSize := sourceFile bufferSize.
        buffer := ByteArray new: bufferSize.
        [(amountRead := sourceFile readInto: buffer) = bufferSize
                whileTrue: [string := string , buffer asByteString].
        amountRead > 0 ifTrue:
                [string := string , ((ByteString new: amountRead)
                        replaceFrom: 1
                        to: amountRead
                        withByteArray: buffer
                        startingAt: 1)]]
                valueOnUnwindDo: [sourceFile close].
        ^string! !

----
Paul van der Weerd
Delft Geotechnics,  PO Box 69,  2600 AB Delft,  The Netherlands
Tel +31 15 693500,  Fax +31 15 610821,  E-mail wrd@delftgeot.nl
----

Xref: sparky comp.lang.smalltalk:1969 comp.object:3777 comp.lang.eiffel:1195