/* ---------------------------------------------------------------- */
/* streamclient in cooperation with streamserver can be used to     */
/* redirect standard streams to other terminal windows              */
/* ---------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */                                                             
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */ 
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with a date and a description.                    */
/*                                                                  */
/* ---------------------------------------------------------------- */
/*  Parameter(s):                                                   */ 
/*                                                                  */
/*    hostname - fully qualified or ip-address,                     */
/*               use loclhost for same machine.                     */
/*                                                                  */
/*    port - port number to use for the socket connection.          */
/*                                                                  */
/*  Usage for instance to redirect trace output to a new terminal   */
/*    out = .streamclient~new(localhost,726576))                    */
/*    out~open(.context~name "- ooRexx Standard Output")            */
/*    pdest = .traceoutput~destination(out)                         */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2015/04/08 - Initial version                                     */
/* ---------------------------------------------------------------- */

::class streamclient public subclass streamsocket

::method init
  use strict arg hostname, port, bufsize = 4096
  -- pass arguments on to super class
  self~init:super(hostname, port, bufsize)
  -- check if streamserver is already running
  socket = .socket~new('AF_INET', 'SOCK_STREAM', 0)
  rc = socket~connect(.inetaddress~new(hostname,port))
  -- negative return code will start the streamserver.
  -- obviously, this will only work on localhost, NOT for a remote hostname
  -- and only on Windows, NOT on Linux etc. For those cases start the 
  -- streamserver manually before using streamclient.
  if rc<0 then do
    parse source os .
    if os~pos("Windows")>0 & hostname=="localhost"
      then address CMD 'start rexxpaws streamserver.rex' hostname port
      else do
        socket~close
        raise syntax 93.900 array ("Could not connect to" hostname":"port"!")
      end
  end
  -- close the socket anyway
  socket~close

 
::method open
  use strict arg header = ''
  self~open:super
  self~say(header~center(80,'='))

::requires 'streamsocket.cls'