Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3658

Show
Ignore:
Timestamp:
06/22/08 17:59:20 (3 months ago)
Author:
kris
Message:

revised to host user-defined types

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/net/SocketPool.d

    r3623 r3658  
    3333*******************************************************************************/ 
    3434 
    35 private class SocketPool 
     35class SocketPool(T) 
    3636{  
    37         private alias void delegate (IConduit) Handler; 
    38         private alias void delegate (char[] msg, ...) Error; 
     37        public alias void delegate (T) Handler; 
     38        public alias T delegate (IConduit) Factory; 
     39        public alias void delegate (char[] msg, ...) Log; 
    3940 
    4041        private int                     size,  
     
    4344                                        online; 
    4445 
    45         private Error                   error
     46        private Factory                 factory
    4647        private InternetAddress         address; 
    4748        private Connection              freelist; 
    4849        private TimeSpan                timeout = TimeSpan.seconds(60); 
    4950 
     51 
    5052        /*********************************************************************** 
    5153 
     
    5456        ***********************************************************************/ 
    5557 
    56         this (InternetAddress address, Error error=null, bool nagle=true) 
     58        this (InternetAddress address, Factory factory, bool nagle=true) 
    5759        {       
    58                 this.online = true; 
    59                 this.error = error; 
    6060                this.nagle = nagle; 
     61                this.factory = factory; 
    6162                this.address = address; 
    6263        } 
     
    132133        ***********************************************************************/ 
    133134         
    134         final bool request (Handler send, Handler recv
     135        final bool request (Handler send, Handler recv, Log log = null
    135136        {        
    136137                Time time; 
    137138 
    138139                // get a connection to the server 
    139                 auto connect = borrow (time = Clock.now); 
     140                auto connection = borrow (time = Clock.now); 
    140141 
    141142                // talk to the server (try a few times if necessary) 
    142143                for (int attempts=3; attempts--;) 
    143144                     try { 
    144                          send (connect.conduit);  
     145                         send (connection.bound);  
    145146 
    146147                         // load the reply. Don't retry on 
     
    150151                         // otherwise  
    151152                         attempts = 0; 
    152                          recv (connect.conduit); 
     153                         recv (connection.bound); 
    153154 
    154155                         // return borrowed connection 
    155                          connect.done (time); 
     156                         connection.done (time); 
    156157 
    157158                         } catch (IOException x) 
    158159                                 { 
    159                                  if (error
    160                                      error ("IOException on request to server {} - {}", address, x); 
     160                                 if (log
     161                                     log ("IOException on request to server {} - {}", address, x); 
    161162 
    162163                                 // attempt to reconnect? 
    163                                  if (attempts is 0 || !connect.reset) 
     164                                 if (attempts is 0 || !connection.reset) 
    164165                                    { 
    165166                                    // that server is offline 
    166                                     if (error) 
    167                                         error ("disabling connection for server {}", address); 
    168167                                    close; 
     168 
     169                                    if (log) 
     170                                        log ("disabling connection for server {}", address); 
    169171   
    170172                                    // state that we failed 
     
    191193                SocketPool      parent;    
    192194                SocketConduit   socket; 
     195                T               bound; 
    193196 
    194197                /*************************************************************** 
     
    215218                final bool reset () 
    216219                { 
     220                        // new connection to host 
     221                        socket = new SocketConduit; 
     222 
     223                        // have callee create the binding 
     224                        bound = parent.factory (socket); 
     225 
    217226                        try { 
    218                             socket = new SocketConduit; 
    219  
    220227                            // apply Nagle settings 
    221228                            socket.socket.setNoDelay (parent.nagle is false); 
     
    228235 
    229236                            return parent.online = true; 
    230  
    231                             } catch (Object o) 
    232                                      if (! Runtime.isHalting && parent.error) 
    233                                            parent.error ("server {} is unavailable - {}", parent.address, o); 
     237                            } catch (Object o) {} 
     238                                      
    234239                        return false; 
    235                 } 
    236                    
    237                 /*************************************************************** 
    238  
    239                         Return the socket belonging to this connection 
    240  
    241                 ***************************************************************/ 
    242          
    243                 final SocketConduit conduit () 
    244                 { 
    245                         return socket; 
    246240                } 
    247241                   
     
    286280        import tango.io.Stdout; 
    287281        import tango.core.Thread; 
     282         
     283        alias SocketPool!(IConduit) Pool; 
    288284 
    289285        void main() 
    290286        { 
    291  
    292                 auto pool = new SocketPool (new InternetAddress("localhost:1111"), &Stdout.formatln); 
    293  
    294                 void send (Connection c) 
     287                IConduit create (IConduit c) 
     288                {        
     289                        return c; 
     290                } 
     291 
     292                void send (IConduit c) 
    295293                { 
    296294                        Stdout ("sending").newline; 
     
    300298                            throw new IOException ("failed to write"); 
    301299                } 
    302  
     300         
    303301                void recv (IConduit c) 
    304302                { 
    305303                } 
    306304 
     305                auto pool = new Pool (new InternetAddress("localhost:1111"), &create); 
    307306                while (true) 
    308307                      { 
    309                       if (! pool.request (&send, &recv)) 
     308                      if (! pool.request (&send, &recv, cast(Pool.Log) &Stdout.formatln)) 
    310309                            Stdout (">>> request failed").newline; 
    311310                      Thread.sleep (1);