This is a course page of David Casperson |
|
A Pipe
class can be found
on galaxy.unbc.ca
in the directory
/export/research/casper/cpsc101/
Pipes/
.
Directly in that directory can be found Pipe.h
and
a Pipe.o
files.
Under the src
subdirectory of that directory can be found the source code used to
create the Pipe
class, for those that wish to try to
compile it with a different compiler or operating
system. Warning: I know that the code does not compile
using gcc 2.95
.
Under the examples
subdirectory there are various
examples of how to use the Pipe
class. As of 2007-02-25,
the examples consisted of a simple echo class.
There is a very determinedly simple-minded computer opponent
in /export/research/casper/cpsc101/Pipes/examples/Opponents/
.
This class aborts whenever it doesn't receive a perfectly formed
command. Otherwise, it plays in a determinedly stupid fashion.
In /export/research/casper/cpsc101/Pipes/examples/tester
there is a simple test driver to test your computer opponent for
conformity to the communication protocol.
There is complete source code for the tester and
a Makefile
here. There is also a precompiled test
program test1
that takes one command-line argument, the
name of a computer opponent to test. For instance, the following commands
cd /export/research/casper/cpsc101/Pipes/examples/Opponents/ /export/research/casper/cpsc101/Pipes/examples/tester/test1 dumbOpponent
produces
testQuit passed. testRestart passed. testPlayColors passed. testFirstMove passed. testSecondMove passed.
This testing isn't definitive, but you may be able to write your own code to add tests to those given.
class Pipe { public: explicit Pipe(std::string progname) ; ~Pipe() ; std::istream * const getInputStream() const ; std::ostream * const getOutputStream() const ; pid_t getPid() const ; private: … } ;
The Pipe
class contains the following methods.
Name | Meaning |
Pipe |
The constructor begins executing the program named in
its string argument as a separate process. This
process receives as its standard input (cin ) any
information written to the output stream of the pipe. Any
information that it writes to standard output
(cout ) is accessible through the input stream of
the pipe.
The constructor aborts if it cannot create a pipe, cannot
|
~Pipe |
The destructor terminates the execution of the process started by the constructor if it hasn't already terminated execution. It also ensures that the input and output streams of the pipe are closed. |
getInputStream |
This method returns a |
getOutputStream |
This method returns a |
getPid |
This method returns the process id (an integer) of the program that the pipe is communicating with. |
Note that every time that a program creates a Pipe
object
it acquires access to one more istream
and one
more ostream
. Thus a tournament director that has four
current Pipe
objects has four istream
s and
four ostream
s, not including
std::cin
,
std::cout
, or
std::cerr
.
Note, on the other hand, that a program that is started by
a Pipe
object does not have direct access to the keyboard
or the display through std::cin
and std::cout
as these are connected directly to the pipe.
fall-2024