src/Pure/System/system_channel.scala
author wenzelm
Mon, 17 Oct 2011 11:24:22 +0200
changeset 45158 db4bf4fb5492
parent 45055 55274f7e306b
child 45243 27466646a7a3
permissions -rw-r--r--
always use sockets on Windows/Cygwin; discontinued special raw_dump facility;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/system_channel.scala
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     3
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
     4
Portable system channel for inter-process communication, based on
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
     5
named pipes or sockets.
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     6
*/
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     7
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     8
package isabelle
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
     9
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    10
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    11
import java.io.{InputStream, OutputStream, File, FileInputStream, FileOutputStream, IOException}
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    12
import java.net.{ServerSocket, InetAddress}
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    13
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    14
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    15
object System_Channel
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    16
{
45055
55274f7e306b explicit option for socket vs. fifo communication;
wenzelm
parents: 45028
diff changeset
    17
  def apply(use_socket: Boolean = false): System_Channel =
45158
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45055
diff changeset
    18
    if (Platform.is_windows) new Socket_Channel else new Fifo_Channel
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    19
}
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    20
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    21
abstract class System_Channel
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    22
{
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    23
  def isabelle_args: List[String]
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    24
  def rendezvous(): (OutputStream, InputStream)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    25
  def accepted(): Unit
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    26
}
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    27
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    28
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    29
/** named pipes **/
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    30
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    31
object Fifo_Channel
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    32
{
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    33
  private val next_fifo = new Counter
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    34
}
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    35
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    36
class Fifo_Channel extends System_Channel
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    37
{
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    38
  private def mk_fifo(): String =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    39
  {
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    40
    val i = Fifo_Channel.next_fifo()
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    41
    val script =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    42
      "FIFO=\"/tmp/isabelle-fifo-${PPID}-$$" + i + "\"\n" +
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    43
      "echo -n \"$FIFO\"\n" +
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    44
      "mkfifo -m 600 \"$FIFO\"\n"
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    45
    val (out, err, rc) = Isabelle_System.bash(script)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    46
    if (rc == 0) out else error(err.trim)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    47
  }
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    48
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    49
  private def rm_fifo(fifo: String): Boolean =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    50
    Isabelle_System.platform_file(
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    51
      Path.explode(if (Platform.is_windows) fifo + ".lnk" else fifo)).delete
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    52
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    53
  private def fifo_input_stream(fifo: String): InputStream =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    54
  {
45158
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45055
diff changeset
    55
    require(!Platform.is_windows)
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45055
diff changeset
    56
    new FileInputStream(fifo)
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    57
  }
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    58
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    59
  private def fifo_output_stream(fifo: String): OutputStream =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    60
  {
45158
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45055
diff changeset
    61
    require(!Platform.is_windows)
db4bf4fb5492 always use sockets on Windows/Cygwin;
wenzelm
parents: 45055
diff changeset
    62
    new FileOutputStream(fifo)
45027
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    63
  }
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    64
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    65
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    66
  private val fifo1 = mk_fifo()
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    67
  private val fifo2 = mk_fifo()
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    68
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    69
  val isabelle_args: List[String] = List ("-W", fifo1 + ":" + fifo2)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    70
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    71
  def rendezvous(): (OutputStream, InputStream) =
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    72
  {
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    73
    val output_stream = fifo_output_stream(fifo1)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    74
    val input_stream = fifo_input_stream(fifo2)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    75
    (output_stream, input_stream)
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    76
  }
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    77
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    78
  def accepted() { rm_fifo(fifo1); rm_fifo(fifo2) }
f459e93a038e more abstract wrapping of fifos as System_Channel;
wenzelm
parents:
diff changeset
    79
}
45028
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    80
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    81
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    82
/** sockets **/
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    83
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    84
class Socket_Channel extends System_Channel
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    85
{
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    86
  private val server = new ServerSocket(0, 2, InetAddress.getByName("127.0.0.1"))
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    87
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    88
  def isabelle_args: List[String] = List("-T", "127.0.0.1:" + server.getLocalPort)
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    89
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    90
  def rendezvous(): (OutputStream, InputStream) =
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    91
  {
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    92
    val socket = server.accept
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    93
    (socket.getOutputStream, socket.getInputStream)
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    94
  }
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    95
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    96
  def accepted() { server.close }
d608dd8cd409 alternative Socket_Channel;
wenzelm
parents: 45027
diff changeset
    97
}