[Improvements to web documentation sydow@chalmers.se**20081116201927] { move ./tests/PingTimeServers.t ./examples/PingTimeServers.t hunk ./examples/Makefile 3 -EXAMPLES = Echo Echo2 Echo3 EchoServer EchoServer2 MasterMind Primes Reflex TCPClient +EXAMPLES = Echo Echo2 Echo3 EchoServer EchoServer2 MasterMind Primes Reflex TCPClient PingTimeServers hunk ./examples/PingTimeServers.t 14 -check neterror report sock = class +client neterror report sock = class hunk ./examples/PingTimeServers.t 17 - sock.outFile.write "Hi!" hunk ./examples/PingTimeServers.t 38 - env.inet.tcp.connect (Host (env.argv!i)) port (check (report i) (report i)) + env.inet.tcp.connect (Host (env.argv!i)) port (client (report i) (report i)) hunk ./examples/Reflex.t 20 - msg <- after (sec 2 + millisec (r `mod` 2000)) action + waitingTime = sec 2 + millisec (r `mod` 2000) + msg <- after waitingTime action hunk ./web/Echo3_descr.html 49 - is no ackumulating drift in the program, even if a particular tick may be delayed in + is no accumulating drift in the program, even if a particular tick may be delayed in hunk ./web/Echo_descr.html 75 -Echo2 for a more careful description.) +Echo2 for a more careful description.) hunk ./web/MasterMind_descr.html 8 -MasterMind is a board game with two players; in this +MasterMind is a board game with two players; in this hunk ./web/MasterMind_descr.html 35 -
+Another network client, that asks a number of network time servers, +listed on the command line, for current time. Here is a sample +run: +
+examples> ./PingTimeServers time.nist.gov time-a.nist.gov time.ien.it dummy localhost +dummy: Name lookup error +localhost: Connection failed +time-a.nist.gov: +54786 08-11-16 16:51:48 00 0 0 254.7 UTC(NIST) * + +time.ien.it: Sun Nov 16 17:51:47 2008 + +time.nist.gov: no response +examples> ++
The program connects to servers that use the old Daytime Protocol, +listening on TCP port 13. +The three first arguments above are existing time servers. Two of +these responded quickly with time in this run, while +time.nist.gov +did not respond within two seconds. The fourth argument is a +nonsense URL that the name server will not identify and the final +argument, localhost, is a valid host which will probably not run +a time server. +
+Here is the program: +
+module PingTimeServers where + +import POSIX +import Data.Functional.List + +port = Port 13 -- standard port for time servers + +client neterror report sock = class + + established = action + sock.inFile.installR report + + close = request result () + + result Connection {..} + + +root env = class + + args = [1..size env.argv-1] + print i mess = env.stdout.write + (env.argv!i ++ ": "++ mess ++ "\n") + + outstanding := args + + report i mess = action + outstanding := delete i outstanding + print i mess + if (null outstanding) then env.exit 0 + + result action + forall i <- args do + env.inet.tcp.connect (Host (env.argv!i)) + port + (client (report i) (report i)) + after (sec 2) action + forall i <- outstanding do + print i "no response" + env.exit 0 + ++
+Comments to the code: +
+all elements are initialised to the value of the second argument). +
+
+ We note also that the correctness of the program depends on two mathematical facts: +
+instance var :: type where + bind+ ++
+This is generalized in the expected way to other programs; +the --make option makes sure that all hunk ./web/examples.html 51 +
+The POSIX environment provides basic support for network programming +over TCP sockets. We repeat part of module POSIX: +
+module POSIX where + +... + +data Host = Host String +data Port = Port Int + +struct Internet where + tcp :: Sockets + +struct Socket < Closable where + remoteHost :: Host + remotePort :: Port + inFile :: RFile + outFile :: WFile + +struct Connection < Closable where + established :: Action + neterror :: String -> Action + +struct Sockets where + connect :: Host -> Port -> + (Socket -> Class Connection) -> Request() + listen :: Port -> + (Socket -> Class Connection) -> Request Closable + +instance showHost :: Show Host +showHost = struct + show (Host nm) = nm ++
+Typically, a network program consists of two program parts, a client +and a server, executing on different hosts, connected to +the internet. The parts communicate over a socket, an +abstraction of a communication channel. A socket has selectors to give +the host and port of the remote peer and two files, an RFile +for reading and a WFile for writing. The main body of both the +server and the client is a function of type Socket -> Class Connection. +This function has access to a socket and must define methods +describing how to handle the events a network program must react to: + an established message when the cnnection has been established, a +neterror message in case of problems and a close +message from the remote peer. +
+The client tries to connect to a server by specifying a +host and a port; the server just specifies a port on which to +listen. Whenever a client connects to a host on a port +and there is a server listening on that port, a socket is created +for each party with files set up for communication and an +established +message is sent to both parties. Typically, code in the body of +established will register callbacks with the +infiles. +
For examples, see EchoServer +and TCPClient. +
Presently, only TCP sockets are supported. A future release may +provide support for UDP sockets. + hunk ./web/menu_examples.html 27 +
+The startTime denotes a Time value, representing the time elapsed since +the Epoch. One main use of this value is that its least significant part can +serve as random generator seed. +
The
-
-after expr expr
-before expr expr
-
-Here the first expr must be a time interval and the second an action; the latter is given a new baseline (the after case)
-or deadline (the before case).
+After/before expressions are often used as statements in this way.
hunk ./web/timberc.html 22
-