home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk374.lzh / IPDevice / IP_Device.DOC < prev    next >
Text File  |  1990-10-08  |  22KB  |  453 lines

  1.  
  2.                             The "IP:" Pipe Device
  3.                             _____________________
  4.  
  5.                              -- Pete Goodeve --
  6.                                 August 1990
  7.  
  8. Overview
  9. ________
  10.  
  11. "IP:" is a handler that may be mounted in your system like any other
  12. AmigaDOS device, to provide you with both standard "pipe" type facilities
  13. and a number of features not available elsewhere.  In particular, as a
  14. pipe, but unlike other pipes available for the Amiga, it has "unbuffered
  15. pass-through"; in other words when a writer process sends a packet it
  16. is immediately available to the reader.  (The manual description of the
  17. 1.3 pipe claims that it also does this, but in fact it does not -- the
  18. reader gets no data until either the buffer is full or the writer closes
  19. its end of the pipe.)  This feature makes it possible for processes to
  20. actually interact via these pipes: sending a command line, for example.
  21. will get an immediate response from the recipient.
  22.  
  23. In addition, an IP channel need not close when the program at either
  24. end goes away.  It will remain open as long as there are ANY processes
  25. (readers or writers) referencing it.  Normally it will send an end-of-file
  26. to the reader (usually causing it to exit) when the last writer closes
  27. its reference to the channel, but it is easy to inhibit this so that the
  28. reader will remain active and waiting for input, whether or not there are
  29. any writers, until a specific QUIT request is sent (see below).
  30.  
  31. Further, although only one reader may be attached at a time, you can have
  32. multiple writer processes sending output to a single IP channel at the
  33. same time -- a "Funnel", rather than a Pipe.
  34.  
  35. There is a special convention for opening both an input AND an output
  36. channel with the same specification, so that you can for example attach
  37. a Shell process to piped input and output instead of a window.  As IP
  38. pipes don't buffer, this becomes a workable arrangement.
  39.  
  40. The IP device is actually based on the "PPIPC" protocol, and the "channel
  41. names" are actually IPCPorts, so it provides a general mechanism for
  42. programs that use this scheme to send text or other unstructured data to
  43. other programs that don't know about PPIPC.  It also means that control
  44. information may be sent (via IPCMessage) to an IP channel; the only
  45. such that is publicly available at this time is the 'QUIT' message
  46. mentioned above, but other capabilities can be provided when appropriate
  47. programs are available.  (A separate program to send the QUIT is provided.)
  48.  
  49. Because all data is passed via publicly named IPCPorts, you can easily
  50. interpose programs to extend performance in various ways.  Possibilities
  51. (not actually written yet!) include things like a "tee", to make data
  52. simultaneously available amongst several processes, and a "tank" that
  53. would dynamically buffer data to decouple writer and reader more
  54. completely (allowing a certain number of unread packets to accumulate
  55. before blocking the writer).
  56.  
  57.  
  58. Requirements and Installation
  59. _____________________________
  60.  
  61. The IP device needs a number of elements to be set up for its correct
  62. operation.  Most fundamentally it requires 'ppipc.library' to be present
  63. in LIBS:, and of course it needs the handler code itself ('IP-Handler')
  64. in the L: directory.  To allow it to be mounted, it will need a suitable
  65. entry in DEVS:MountList, and for permanent installation you'll also want
  66. a "Mount IP:" command in your Startup-Sequence.  For convenience, you will
  67. probably want "IPCQuit" in C:.
  68.  
  69. This can all be done for you with minimum fuss -- but with your permission
  70. at each step -- by the installation icon "Install_IP_Device".  This runs a
  71. script that goes through all the above steps, keeping you informed and
  72. asking permission at each stage.  Once you have installed the library, you
  73. can start up the device without actually installing anything else, if you
  74. like, by answering "NO" to the query regarding installation of the handler,
  75. and then "YES" to the question about a temporary trial.
  76.  
  77. If you prefer to do it yourself by hand, "Install_IP_Device" is actually a
  78. directory (only accessible through the Shell or CLI) containing all the
  79. necessary pieces.  (It is completely self-contained, by the way; you can
  80. copy the whole system to any desired location simply by dragging the icon.)
  81.  
  82. You don't really need to know anything about PPIPC to use this device,
  83. but if you want full details (and I definitely encourage this!) the
  84. complete package is on Fish Disk 290.
  85.  
  86. The code itself should run under any WorkBench from about 1.2 on (including
  87. 2.0 -- it has been tested there without encountering any problems).  The
  88. installation script uses a couple of command options not available under
  89. 1.2 (and has not been tested there) so it may not work completely, but
  90. probably should go most of the way; it is perfectly happy under 2.0.
  91.  
  92.  
  93. Use as a standard Pipe
  94. ______________________
  95.  
  96. Once the IP device is installed and mounted as above, you use it just
  97. like other AmigaDOS pipes.  Each channel (that is simultaneously open)
  98. must have its own name, specified in the usual Amiga device:name format,
  99. for example 'IP:xyz' or 'ip:MyChan'.  (These are not true "filenames"
  100. of course.  In particular, there is no "directory structure"; '/'
  101. separators have a different meaning -- see later.)  As distinct from
  102. most AmigaDOS names, the name part is case-sensitive: 'xyz' and 'XYZ'
  103. are different channels.  This is because the channels are actually
  104. IPCPorts, where case is important.
  105.  
  106. To pass data between two processes, you simply open a particular channel
  107. for writing by one and reading by the other.  Thus as a trivial example:
  108.  
  109.                 RUN LIST >IP:xyz
  110.                 TYPE IP:xyz
  111.  
  112. or equivalently:
  113.  
  114.                 RUN TYPE IP:xyz
  115.                 LIST >IP:xyz
  116.  
  117. Either reader or writer may be started first; it will block on its first
  118. I/O request until the other end is ready.  (It is important that at least
  119. the first command be invoked by RUN -- or of course from a separate CLI
  120. window if you prefer -- because it will not continue until the connection
  121. is made.)
  122.  
  123. Once made, the connection will remain open until the writer closes it,
  124. at which point an end-of-file is sent to the reader, which can in turn
  125. close its end.  This is the conventional sequence, but it may differ at
  126. the option of either program.  If the reader chooses not to exit when
  127. sent an EOF, the channel will remain open for further input from other
  128. processes (further details in the next section).
  129.  
  130. On the other hand, the reader might terminate on some signal other than
  131. EOF, in which case another reader could attach itself to the channel to
  132. process further output from the writer.  Note, however, that any output
  133. sent to a IP channel which has no current reader -- except when a
  134. connection is being first established -- is DISCARDED; a write error is
  135. actually also returned to the sender, but the majority of programs
  136. unfortunately ignore this.
  137.  
  138.  
  139. Use as a "Funnel"
  140. ________________
  141.  
  142. As mentioned you can have as many processes writing to an IP channel
  143. as you want.  These can be either running in parallel or concatenated
  144. sequentially, as appropriate; in the latter case you must inhibit automatic
  145. closing of the channel as described below.  Data is passed by packet as
  146. written by each sender (usually a line at a time, though some programs --
  147. such as COPY -- may use packets of a different size);  if more than one
  148. process is writing at once, packets from each will be interleaved in the
  149. order that they arrive.
  150.  
  151. Writer processes may attach and detach themselves on a channel as they
  152. require, without disturbing the reader, until the last writer closes,
  153. at which time the reader gets an end-of-file, causing it too to close
  154. (usually).  An EOF, by the way, is -- as normal in AmigaDOS -- an empty
  155. data packet.
  156.  
  157. If you wish to keep the channel open on a semi-permanent basis, you can
  158. suppress this automatic EOF.  To do so, you prefix the channel name that
  159. you open for reading by "!":
  160.  
  161.                 TYPE IP:!xyz
  162.  
  163. The exclamation mark is NOT a part of the channel name: writers still
  164. normally access it as "IP:xyz"  (for writing, the mark has a different
  165. meaning: "exclusive write" -- see below).
  166.  
  167. If you do open a reader in this mode, when you eventually want to close it
  168. you will have to do so by some other method: either terminate it directly
  169. somehow -- by some recognized command, say -- or explicitly cause an EOF by
  170. sending the channel a QUIT message.  Using the IPCQuit command (a separate
  171. program provided with the package), you specify the name of the channel(s)
  172. you wish to terminate, WITHOUT the "IP:" prefix:
  173.  
  174.                 IPCQUIT xyz
  175.  
  176.  
  177.  
  178. Exclusive Write Mode
  179. ____________________
  180.  
  181. You can prevent a writer process that intends to open a particular channel
  182. from actually opening it if it already exists by specifying "exclusive
  183. write".  The exclamation mark is again used to specify this:
  184.  
  185.                 LIST >IP:!xyz
  186.  
  187. If ANY process (reader or writer) currently has this channel open, the
  188. request will be rejected.  Note that this mode only affects the specific
  189. request in which it is used.  Any later request that does not include
  190. the "!" will connect normally.
  191.  
  192. A caution:  the channel is actually opened momentarily to make the check;
  193. if there happens to be just a single reader (in non-funnel mode) on the
  194. channel at that time, it will get an EOF as a result.  This scheme should
  195. only be used where the write end is always opened first.
  196.  
  197. This mode will probably be of use mainly in things like ARexx scripts
  198. that may have several simultaneous multitasking incarnations.  The script
  199. can loop through a sequence of generated channel names until it successfully
  200. opens one, which it then knows is unique and safe to use.
  201.  
  202.  
  203. Bidirectional Data
  204. __________________
  205.  
  206. There is an option for IP channel name specification by which you may
  207. specify both input and output channel at the same time.  This is ONLY
  208. useful for programs that normally expect a "CON:" window specification at
  209. that point -- in other words it is really only useful for NEWCLI or
  210. NEWSHELL.
  211.  
  212. The convention for this is to specify the input and output channel names
  213. -- which must of course be different -- separated by a slash ("/")
  214. character:
  215.  
  216.                 NEWSHELL IP:inchan/outchan
  217.  
  218. The exclamation mark modifier may be used on either part, but it is
  219. superfluous on the input side because the Shell and CLI ignore EOFs
  220. on that channel.  In fact it is probably undesirable since it would
  221. prevent programs running under the shell from receiving an EOF either!
  222. "Exclusive Write" could well be useful on the output side, though, as
  223. NEWSHELL will fail (with return code 20) if the connection fails, so
  224. whatever script has invoked it could detect the condition.
  225.  
  226.  
  227. Clogged Pipes
  228. _____________
  229.  
  230. A word or two may be useful on what to do if a program at one end or other
  231. of your pipe fails to behave as expected, and leaves the other end hanging.
  232. In general all you'll need to restore normality is an available Shell/CLI
  233. window from which to give a command or two.
  234.  
  235. If you start a reader successfully, but the writer process never gets
  236. going, the reader will just keep waiting.  If you just made some typing
  237. error in the writer command line, you can just try again with a corrected
  238. line.  If you can't do this, and as long as you didn't open it as a
  239. maintained "funnel" (with "!"), all it needs is some process to first open
  240. a writer then close it, to send an EOF and let it terminate. A simple way
  241. would be:
  242.  
  243.                 ECHO >IP:xyz ""
  244.  
  245. More convenient still is to send a QUIT message, and this also works
  246. for funnels as explained above:
  247.  
  248.                 IPCQUIT xyz
  249.  
  250. If it's the reader side that fails, the writer will be the one to be
  251. left hanging in the air, waiting for somewhere to send its output.  The
  252. best way to clear this is to start some other process to take the output
  253. from that channel.  You could either type it to the screen, or if it is
  254. voluminous or non-text, send it off to NIL:
  255.  
  256.                 TYPE IP:xyz to NIL:
  257.  
  258. Then of course there's the situation where you mistyped the channel name
  259. in one of the two commands, so you have TWO processes going nowhere and
  260. talking to no one.  The most direct solution is to clear one or both
  261. processes as described above, but if you're really desperate -- or just
  262. want to play -- you could simply start a THIRD process, to form a bridge
  263. between the two loose ends, with a command like the following!
  264.  
  265.                 TYPE IP:xyz to IP:xzz
  266.  
  267. Use TYPE rather than COPY, by the way, because the latter does some odd
  268. things when it opens a destination -- checking for existence and so on --
  269. with multiple opens and closes that will result in your destination channel
  270. getting shut before it ever gets started.  For this reason you should also,
  271. if you ever want to COPY to an IP channel, either start the COPY command
  272. FIRST or use maintained funnel mode on the receiver.
  273.  
  274. One other problem that might crop up if you have two programs communicating
  275. with each other by a pair of IP pipes is circular lock-up.  If one of the
  276. two reads an input line from one pipe, processes it, and puts some output
  277. on the other, it will then hang until that data is read by the other program
  278. -- the one that is sending IT input.  Each program therefore must be sure
  279. to read ALL the current output of the other at each cycle before attempting
  280. to send any of its own, or the other end will never reach to point of
  281. reading it, and there will be a lock up.  If this does occur, judicious
  282. application of IPCQuit should break the lock.
  283.  
  284.  
  285. Packet Management
  286. _________________
  287.  
  288. As this is a DOS device, data is passed into, and taken out of, an IP
  289. channel as standard DOS packets.  The (maximum) amount of data a packet can
  290. contain is determined by the program doing the I/O.
  291.  
  292. When a packet is written to an IP channel, the whole packet contents is
  293. always passed (as an IPCMessage) to the IPCPort acting as the rendezvous.
  294. There is no copying at this stage -- just a pointer is passed; the sender
  295. is suspended until the data has been accepted by the reader.
  296.  
  297. If the packet supplied by the reader to receive the data is large enough
  298. to take it all, the data is copied to the receiving packet and the sender's
  299. packet is released.  If the receiving packet is too small, as much data
  300. as possible will be copied, and the sender will remain suspended until
  301. further reads have dealt with all its data.
  302.  
  303. Symmetrically, of course, if a reader sends a packet (makes a read request)
  304. to the IP device when there is no packet from a writer waiting, it will
  305. be suspended until one arrives.
  306.  
  307.  
  308. PPIPC Specifications
  309. ____________________
  310.  
  311. It is a little ironic that while one of the specific advantages claimed for
  312. PPIPC is its concern for the structure of data, this application essentially
  313. passes unformatted byte streams.  However this is of course dictated by
  314. the expectations of the programs that will connect to the IP device.
  315. The application does make good use of the "safe connection" aspect of
  316. PPIPC, though.  This section describes the format of IPCMessages sent
  317. and recognized by the IP device.  Knowledge of the PPIPC protocol will
  318. be needed -- see Fish Disk 290.
  319.  
  320.  
  321. IPCPorts:               Each IP device channel uses an IPCPort to pass
  322.                         its data through. The name of a channel's port is
  323.     the string following the "IP:" device identifier prefix (and remember
  324.     case is significant).  Any other PPIPC-capable device can communicate
  325.     with this port, without having to know that it is an IP channel
  326.     (provided that its messages have suitable IDs).
  327.  
  328.     A request to open an IP channel for reading will attach that IP
  329.     process to the port as a Server (therefore there may be only one
  330.     reader at a time).  A request to write will attach it as a Client:
  331.     the first connection attempt is made with LoadIPCPort(), so that if
  332.     it is not already served and there is a Broker running, the appropriate
  333.     startup action can be taken; if this fails, the connection is simply
  334.     made with GetIPCPort(), BUT -- if there is no server -- the 'IPP_LOADING'
  335.     flag is also set so that the first message sent will not bounce.  This
  336.     assumes that eventually a server WILL arrive!
  337.  
  338.     When an AmigaDOS 'close file' request is sent, a writer will simply
  339.     drop its port and quit.  The reader first 'shuts' the port, ensures
  340.     that any unprocessed messages are returned with 'IPC_REJECT' set,
  341.     and then quits.  The Server will detect the disappearance of its
  342.     last Client and -- unless opened as a "funnel" -- will automatically
  343.     send an EOF (null packet) to its reader process.
  344.  
  345.  
  346. IPCMessages Sent:       An IP output connection itself will send only
  347.                         one kind of message.  Its ipc_Id field is 'DATA'
  348.     and it has one IPCItem whose ii_Id field is also 'DATA'.  ii_Size is
  349.     set to the number of bytes in the packet received from the writer, and
  350.     ii_Ptr points to that packet data buffer.  No assumptions are made as
  351.     to the nature of the data block.  The message must of course be replied
  352.     when processed by the reader.
  353.  
  354.  
  355. IPCMessages Received:   The reader (Server) end can handle several other
  356.                         message formats besides the one above.  It expects
  357.     ipc_Id to be one of 'DATA', 'TEXT', 'CMND', or 'QUIT'.  (A fifth ID
  358.     'SETP' is also recognized.  It is used to set up some other parameters
  359.     that are used by an as yet unreleased application.  It will not be
  360.     described further at the moment.)
  361.  
  362.     All messages are processed in the order that they arrive.  The first
  363.     three message IDs are treated identically.  'QUIT' causes an EOF
  364.     (null packet) to be sent to the reader.  Any data message should
  365.     contain an item with one of three ii_Id's: 'DATA', 'TEXT', or 'LINE'.
  366.     In the current implementation only the first item with one of these IDs
  367.     will be found in a message -- any preceding irrelevant items, and
  368.     anything following the first found, will be ignored.
  369.  
  370.     The contents of a 'DATA' item is passed to the reader's AmigaDOS packet
  371.     exactly as received (broken into suitable pieces if it is too big).
  372.     Its size is taken from the ii_Size field of the item.
  373.  
  374.     A 'TEXT' item is treated similarly, except that it is assumed to be
  375.     a null-terminated (ASCII) string, and its size is calculated up to
  376.     the first null byte.  (It is not allowed to exceed ii_Size, however.)
  377.     The null byte is NOT copied to the DOS packet!  (Text length is
  378.     indicated by the return value in the packet.)
  379.  
  380.     A 'LINE' is a null terminated ASCII line of text WITHOUT the
  381.     terminating newline.  The missing newline is added to the packet
  382.     before passing it on.
  383.  
  384.     Normally the Server will reply all messages that it receives, but
  385.     a Client (this won't be an IP process naturally) may also send one that
  386.     is not to be replied, IF it ensures that the following is true: a) the
  387.     data being passed is in the user data area of the message itself, b)
  388.     the ReplyPort field is NULL.  (For completeness, 'IPC_TRANSFER' should
  389.     be set in ipc_Flags, but this is not currently checked.)  If an IP Server
  390.     does receive a message with no replyport, it ASSUMES that it can safely
  391.     delete it, and does so without further fuss.
  392.  
  393.                             + + + + +
  394.  
  395. Coda
  396. ____
  397.  
  398. I suspect many people's reaction to the IP Device may be "Cute... but so
  399. what?".  Its utility may not be immediately obvious, but think about it
  400. and you may find that it's exactly what you need.  It is just another
  401. building block that in certain places will turn out to be a perfect fit.
  402.  
  403. Most straightforwardly you should be able to replace all use of the 1.3
  404. pipes (provided that you don't rely on buffering to pass data from one
  405. program to another under the same CLI).  I have been frustrated by the
  406. erratic behaviour of the 1.3 pipes -- they will fail to open for no
  407. apparent reason, or irretrievably lock up.  Any "lock-up" of the IP
  408. device should be easily clearable (see "Clogged Pipes", above), and as
  409. far as I can tell it behaves completely reliably.
  410.  
  411. In addition you get the non-buffered immediate response, and the ability to
  412. concatenate output from more than one program, which are not available
  413. from other pipes.  I haven't included any examples or demos here, because
  414. although the IP dvice is now central to my environment, the scripts I
  415. use are also tailored too much to my needs to be much use to others.
  416. And, quite frankly, most of the appplications I envision for it I haven't
  417. even tried yet.
  418.  
  419. One such use is as a link in a "SuperShell".  You could have a Rexx script
  420. (or Tcl or something -- let's not restrict ourselves) that keeps an IP
  421. channel open to a Shell process.  Commands would be examined by the script
  422. and processed in some really intelligent fashion before being passed to the
  423. Shell for execution.  (ARexx can of course send command strings to AmigaDOS,
  424. but a new process is opened for each one, so you can't do something as
  425. simple as change a directory.)
  426.  
  427. The "tee" and "tank" modules mentioned in the overview will add to the
  428. capabilities when they arrive.  In the meantime, see what you can do
  429. with the current ones.
  430.  
  431. If you have any comments, requests or suggestions, you can contact me
  432. at the address below.
  433.  
  434.                             + + + + +
  435.  
  436. The IP Device and this manual are Copyright 1990 by Peter Goodeve.
  437. There is no restriction on their non-commercial use or copying.
  438. If you wish to incorporate the IP device in a commercial product or
  439. distribution, please contact the author.  (By 'commercial', I mean
  440. a distribution for which more than a nominal materials and copying
  441. fee is charged.)
  442.  
  443.                             + + + + +
  444.  
  445.                                             Pete Goodeve
  446.                                             3012 Deakin Street #D
  447.                                             Berkeley, Calif. 94705
  448.                                             U. S. A.
  449.  
  450.                                             net: pete@violet.Berkeley.EDU
  451.  
  452.  
  453.