1/* File: canny/hdx.pl 2 Author: Roy Ratcliffe 3 Created: Sep 24 2022 4 Purpose: Canny Half-Duplex 5 6Copyright (c) 2022, Roy Ratcliffe, Northumberland, United Kingdom 7 8Permission is hereby granted, free of charge, to any person obtaining a 9copy of this software and associated documentation files (the 10"Software"), to deal in the Software without restriction, including 11without limitation the rights to use, copy, modify, merge, publish, 12distribute, sublicense, and/or sell copies of the Software, and to 13permit persons to whom the Software is furnished to do so, subject to 14the following conditions: 15 16 The above copyright notice and this permission notice shall be 17 included in all copies or substantial portions of the Software. 18 19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 27*/ 28 29:- module(canny_hdx, 30 [ hdx/4, % +StreamPair,+Term,-Codes,+TimeOut 31 hdx/3, % +In,-Codes,+TimeOut 32 hdx/2 % +Out,+Term 33 ]).
Filling a stream buffer blocks the calling thread if there is no input ready. Pending read operations also block for the same reason. Hence the wait_for_input/3 must precede them.
58hdx(StreamPair, Term, Codes, TimeOut) :- 59 stream_pair(StreamPair, In, Out), 60 hdx(Out, Term), 61 hdx(In, Codes, TimeOut). 62 63/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 64 65No need to distinguish between command and query at the lower layers of 66duplex communication. The upper-level stream determines the mode: either 67both halves of the half-duplex cycle: write then read for the query 68stream; else write only for the command stream. 69 70- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 71 72hdx(Out, Term) :- 73 write(Out, Term), 74 flush_output(Out). 75 76hdx(In, Codes, TimeOut) :- 77 wait_for_input([In], [Ready], TimeOut), 78 fill_buffer(Ready), 79 read_pending_codes(Ready, Codes, [])