src/Pure/System/message_channel.ML
changeset 56333 38f1422ef473
parent 52800 1baa5d19ac44
child 56733 f7700146678d
equal deleted inserted replaced
56327:3e62e68fb342 56333:38f1422ef473
     5 *)
     5 *)
     6 
     6 
     7 signature MESSAGE_CHANNEL =
     7 signature MESSAGE_CHANNEL =
     8 sig
     8 sig
     9   type message
     9   type message
    10   val message: string -> Properties.T -> string -> message
    10   val message: string -> Properties.T -> string list -> message
    11   type T
    11   type T
    12   val send: T -> message -> unit
    12   val send: T -> message -> unit
    13   val shutdown: T -> unit
    13   val shutdown: T -> unit
    14   val make: System_Channel.T -> T
    14   val make: System_Channel.T -> T
    15 end;
    15 end;
    19 
    19 
    20 (* message *)
    20 (* message *)
    21 
    21 
    22 datatype message = Message of string list;
    22 datatype message = Message of string list;
    23 
    23 
    24 fun chunk s = [string_of_int (size s), "\n", s];
    24 fun chunk ss =
       
    25   string_of_int (fold (Integer.add o size) ss 0) :: "\n" :: ss;
    25 
    26 
    26 fun message name raw_props body =
    27 fun message name raw_props body =
    27   let
    28   let
    28     val robust_props = map (pairself YXML.embed_controls) raw_props;
    29     val robust_props = map (pairself YXML.embed_controls) raw_props;
    29     val header = YXML.string_of (XML.Elem ((name, robust_props), []));
    30     val header = YXML.string_of (XML.Elem ((name, robust_props), []));
    30   in Message (chunk header @ chunk body) end;
    31   in Message (chunk [header] @ chunk body) end;
    31 
    32 
    32 fun output_message channel (Message ss) =
    33 fun output_message channel (Message ss) =
    33   List.app (System_Channel.output channel) ss;
    34   List.app (System_Channel.output channel) ss;
    34 
    35 
    35 
    36 
    36 (* flush *)
    37 (* flush *)
    37 
    38 
    38 fun flush channel = ignore (try System_Channel.flush channel);
    39 fun flush channel = ignore (try System_Channel.flush channel);
    39 
    40 
    40 val flush_message = message Markup.protocolN Markup.flush "";
    41 val flush_message = message Markup.protocolN Markup.flush [];
    41 fun flush_output channel = (output_message channel flush_message; flush channel);
    42 fun flush_output channel = (output_message channel flush_message; flush channel);
    42 
    43 
    43 
    44 
    44 (* channel *)
    45 (* channel *)
    45 
    46