REBOL [ Title: "Message relaying for messaging.r" Purpose: { Allows message relaying with messaging.r } Author: "Gabriele Santilli" EMail: giesse@rebol.it File: %relays.r License: { Copyright (c) 2005, Gabriele Santilli All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of Gabriele Santilli may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. } Date: 28-Jan-2005 Version: 1.8.0 ; majorv.minorv.status ; status: 0: unfinished; 1: testing; 2: stable History: [ 21-Jan-2005 1.1.0 "History start" 21-Jan-2005 1.2.0 "Fixed a bug" 21-Jan-2005 1.3.0 "Fixed a bug" 24-Jan-2005 1.4.0 "Handler was not letting non-relay message pass thru" 27-Jan-2005 1.5.0 "Debugging" 27-Jan-2005 1.6.0 "Added relayed nodes timeouts" 27-Jan-2005 1.7.0 "Removed some debug prints" 28-Jan-2005 1.8.0 "Trying to debug an endless loop problem" ] ] relays: [ ] relay!: context [ request-queue: [ ] last-callback: "func [request] [...]" last-response: none ] relay-message!: context [ request: "Request message" send-response: "func [response] [...]" ] enqueue-request: func [relay request' callback] [ debug true ["enqueue-request" mold :request'] insert tail relay/request-queue make relay-message! [ request: :request' send-response: :callback ] ] make-relay: does [ insert tail relays make relay! [last-response: now/precise] length? relays ] insert tail handlers func [port message callback /local response relay relay-id request] [ ;print ["Relay handler:" mold :message] if any [ not block? :message not parse message [ 'please 'relay end (callback reduce ['your 'relay 'id make-relay]) | 'relay set relay integer! set response skip end ( debug true ["Relay response from" relay ":" mold :message] either relay: pick relays relay [ relay/last-response: now/precise if all [:response not empty? relay/request-queue] [ debug true ["sending response:" mold :response] relay/request-queue/1/send-response :response remove relay/request-queue ] either empty? relay/request-queue [ relay/last-callback: :callback ] [ relay/last-callback: none debug true ["sending request:" mold get in relay/request-queue/1 'request] callback get in relay/request-queue/1 'request ] ] [ debug true "Unknown relay ID" callback [ERROR NO-RELAY {Unknown relay ID}] ] ) | set relay-id integer! set request skip end ( debug true ["Relay request for" relay-id ":" mold :message] either relay: pick relays relay-id [ either all [none? get in relay 'last-callback 0:00:04 < difference now/precise relay/last-response] [ ; relayed node seems to have stopped pinging us debug true ["Trying to clear the queue for" relay-id] foreach request relay/request-queue [ request/send-response [ERROR NO-RELAY {Unknown relay ID}] ] poke relays relay-id none callback [ERROR NO-RELAY {Unknown relay ID}] ] [ enqueue-request relay :request :callback ; will be a NOP unless the other side is actually waiting ; for a message relay/last-callback :request ] ] [ debug true "Unknown relay ID" callback [ERROR NO-RELAY {Unknown relay ID}] ] ) ] ] [ callback none ] ]