src/Pure/System/system_channel.ML
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 45158 db4bf4fb5492
child 50800 c0fb2839d1a9
permissions -rw-r--r--
more official command specifications, including source position;
wenzelm@45029
     1
(*  Title:      Pure/System/system_channel.ML
wenzelm@45029
     2
    Author:     Makarius
wenzelm@45029
     3
wenzelm@45029
     4
Portable system channel for inter-process communication, based on
wenzelm@45029
     5
named pipes or sockets.
wenzelm@45029
     6
*)
wenzelm@45029
     7
wenzelm@45029
     8
signature SYSTEM_CHANNEL =
wenzelm@45029
     9
sig
wenzelm@45029
    10
  type T
wenzelm@45029
    11
  val input_line: T -> string option
wenzelm@45029
    12
  val inputN: T -> int -> string
wenzelm@45029
    13
  val output: T -> string -> unit
wenzelm@45029
    14
  val flush: T -> unit
wenzelm@45029
    15
  val fifo_rendezvous: string -> string -> T
wenzelm@45029
    16
  val socket_rendezvous: string -> T
wenzelm@45029
    17
end;
wenzelm@45029
    18
wenzelm@45029
    19
structure System_Channel: SYSTEM_CHANNEL =
wenzelm@45029
    20
struct
wenzelm@45029
    21
wenzelm@45029
    22
datatype T = System_Channel of
wenzelm@45029
    23
 {input_line: unit -> string option,
wenzelm@45029
    24
  inputN: int -> string,
wenzelm@45029
    25
  output: string -> unit,
wenzelm@45029
    26
  flush: unit -> unit};
wenzelm@45029
    27
wenzelm@45029
    28
fun input_line (System_Channel {input_line = f, ...}) = f ();
wenzelm@45029
    29
fun inputN (System_Channel {inputN = f, ...}) n = f n;
wenzelm@45029
    30
fun output (System_Channel {output = f, ...}) s = f s;
wenzelm@45029
    31
fun flush (System_Channel {flush = f, ...}) = f ();
wenzelm@45029
    32
wenzelm@45029
    33
wenzelm@45029
    34
(* named pipes *)
wenzelm@45029
    35
wenzelm@45029
    36
fun fifo_rendezvous fifo1 fifo2 =
wenzelm@45029
    37
  let
wenzelm@45029
    38
    val in_stream = TextIO.openIn fifo1;
wenzelm@45029
    39
    val out_stream = TextIO.openOut fifo2;
wenzelm@45029
    40
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream out_stream, IO.BLOCK_BUF);
wenzelm@45029
    41
  in
wenzelm@45029
    42
    System_Channel
wenzelm@45029
    43
     {input_line = fn () => TextIO.inputLine in_stream,
wenzelm@45029
    44
      inputN = fn n => TextIO.inputN (in_stream, n),
wenzelm@45029
    45
      output = fn s => TextIO.output (out_stream, s),
wenzelm@45029
    46
      flush = fn () => TextIO.flushOut out_stream}
wenzelm@45029
    47
  end;
wenzelm@45029
    48
wenzelm@45029
    49
wenzelm@45158
    50
(* sockets *)  (* FIXME proper buffering via BinIO (after Poly/ML 5.4.1) *)
wenzelm@45058
    51
wenzelm@45058
    52
local
wenzelm@45029
    53
wenzelm@45058
    54
fun readN socket n =
wenzelm@45029
    55
  let
wenzelm@45058
    56
    fun read i buf =
wenzelm@45058
    57
      let
wenzelm@45058
    58
        val s = Byte.bytesToString (Socket.recvVec (socket, n - i));
wenzelm@45058
    59
        val m = size s;
wenzelm@45058
    60
        val i' = i + m;
wenzelm@45058
    61
        val buf' = Buffer.add s buf;
wenzelm@45058
    62
      in if m > 0 andalso n > i' then read i' buf' else Buffer.content buf' end;
wenzelm@45058
    63
  in read 0 Buffer.empty end;
wenzelm@45058
    64
wenzelm@45058
    65
fun read_line socket =
wenzelm@45058
    66
  let
wenzelm@45058
    67
    fun result cs = implode (rev ("\n" :: cs));
wenzelm@45029
    68
    fun read cs =
wenzelm@45058
    69
      (case readN socket 1 of
wenzelm@45058
    70
        "" => if null cs then NONE else SOME (result cs)
wenzelm@45058
    71
      | "\n" => SOME (result cs)
wenzelm@45058
    72
      | c => read (c :: cs));
wenzelm@45029
    73
  in read [] end;
wenzelm@45029
    74
wenzelm@45058
    75
fun write socket =
wenzelm@45058
    76
  let
wenzelm@45058
    77
    fun send buf =
wenzelm@45058
    78
      if Word8VectorSlice.isEmpty buf then ()
wenzelm@45058
    79
      else
wenzelm@45058
    80
        let
wenzelm@45058
    81
          val n = Int.min (Word8VectorSlice.length buf, 4096);
wenzelm@45058
    82
          val m = Socket.sendVec (socket, Word8VectorSlice.subslice (buf, 0, SOME n));
wenzelm@45058
    83
          val buf' = Word8VectorSlice.subslice (buf, m, NONE);
wenzelm@45058
    84
        in send buf' end;
wenzelm@45058
    85
  in fn s => send (Word8VectorSlice.full (Byte.stringToBytes s)) end;
wenzelm@45058
    86
wenzelm@45058
    87
in
wenzelm@45058
    88
wenzelm@45029
    89
fun socket_rendezvous name =
wenzelm@45029
    90
  let
wenzelm@45058
    91
    fun err () = error ("Bad socket name: " ^ quote name);
wenzelm@45058
    92
    val (host, port) =
wenzelm@45058
    93
      (case space_explode ":" name of
wenzelm@45058
    94
        [h, p] =>
wenzelm@45058
    95
         (case NetHostDB.getByName h of SOME host => host | NONE => err (),
wenzelm@45058
    96
          case Int.fromString p of SOME port => port | NONE => err ())
wenzelm@45058
    97
      | _ => err ());
wenzelm@45058
    98
    val socket: Socket.active INetSock.stream_sock = INetSock.TCP.socket ();
wenzelm@45058
    99
    val _ = Socket.connect (socket, INetSock.toAddr (NetHostDB.addr host, port));
wenzelm@45029
   100
  in
wenzelm@45029
   101
    System_Channel
wenzelm@45058
   102
     {input_line = fn () => read_line socket,
wenzelm@45058
   103
      inputN = fn n => readN socket n,
wenzelm@45058
   104
      output = fn s => write socket s,
wenzelm@45058
   105
      flush = fn () => ()}
wenzelm@45029
   106
  end;
wenzelm@45029
   107
wenzelm@45029
   108
end;
wenzelm@45029
   109
wenzelm@45058
   110
end;
wenzelm@45058
   111