connections.anubis 2.73 KB

 *Project*                             The Anubis Project
   
 *Title*                     Unifying the handling of connections.
   
 *Copyright*                     Copyright (c) Alain Prouté 2003. 


 *Author*       Alain Prouté


   

 *Overview*
   Anubis knows several sorts of connections:

     * connections to files (on disk)
     * TCP/IP connections
     * SSL connections.


   Unfortunatly,  the   system  (at   least  for  version   1)  has  not   been  conceived
   consistently. Actually, file and TCP/IP  connections are of type ?Addr(Int8), while SSL
   connections are of type SSL_Connection.


   In  order to  unify the  handling of  all  sorts of  connections, we  first define  the
   following type:


public type Connection:
   file(RAddr(Int8)), 
   file(WAddr(Int8)), 
   file(RWAddr(Int8)), 
   tcp(RWAddr(Int8)),
   ssl(SSL_Connection). 


   The operations on connections are:


     - opening connections (closing is automatic), 
     - writing to a connection
     - reading from a connection. 


   For   opening  connections,   see  'file',   'connect'  and   'open_SSL_connection'  in
   'predefined.anubis'.

   Now, here are the unified tools for reading and writing: 


public define Maybe(ByteArray)       // result of reading
   read
     (
       Connection c,
       Int32 n,                      // maximal number of bytes to read
       Int32 timeout                 // in seconds
     ). 

  
public define Maybe(Int32)           // number of bytes actually written
   write
     (
       Connection c,
       ByteArray ba
     ). 


   --- That's all for the public part. ---------------------------------------------------



public define Maybe(ByteArray)              // result of reading
   read
     (
       Connection c,
       Int32 n,     
       Int32 timeout
     ) =

   if c is 
     {
       file(ra)    then read(ra,n,timeout),
       file(wa)    then alert, 
       file(rwa)   then read(weaken(rwa),n,timeout),
       tcp(a)      then read(weaken(a),n,timeout),
       ssl(a)      then read(a,n,timeout) 
     }.
  

public define Maybe(Int32)                  // number of bytes actually written
   write
     (
       Connection c,
       ByteArray ba
     ) = 

   if c is 
     {
       file(ra)    then alert,
       file(wa)    then write(wa,ba), 
       file(rwa)   then write(weaken(rwa),ba),
       tcp(a)      then write(weaken(a),ba),
       ssl(a)      then write(a,ba) 
     }.
  

public define (Int32,Int32)   
   remote_IP_address_and_port
     (
       Connection c
     ) =
   if c is 
     {
       file(ra)    then alert,
       file(wa)    then alert,
       file(rwa)   then alert,
       tcp(a)      then remote_IP_address_and_port(a),
       ssl(a)      then remote_SSL_address_and_port(a)
     }.