1 /*******************************************************************************
2 
3     The pool of connections handled by a SelectListener.
4 
5     Copyright:
6         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.net.server.connpool.SelectListenerPool;
17 
18 
19 
20 
21 import ocean.net.server.connpool.ISelectListenerPoolInfo;
22 
23 import ocean.net.server.connection.IConnectionHandler;
24 
25 import ocean.util.container.pool.ObjectPool : AutoCtorPool;
26 
27 
28 
29 /*******************************************************************************
30 
31     SelectListenerPool class template.
32 
33     Extends AutoCtorPool with the additional methods demanded by
34     ISelectListenerPoolInfo.
35 
36     The additional T constructor argument parameters must appear after those for
37     the mandatory IConnectionHandler constructor.
38 
39     Params:
40         T    = connection handler class
41         Args = additional constructor arguments for T
42 
43 *******************************************************************************/
44 
45 public class SelectListenerPool ( T, Args ... ) :
46     AutoCtorPool!(T, IConnectionHandler.FinalizeDg, Args), ISelectListenerPoolInfo
47 {
48     /***************************************************************************
49 
50         Constructor.
51 
52         Params:
53             finalize_dg = delegate for a connection to call when finished
54                 (should recycle it into this pool)
55             args = T constructor arguments to be used each time an
56                    object is created
57 
58     ***************************************************************************/
59 
60     public this ( scope IConnectionHandler.FinalizeDg finalize_dg, Args args )
61     {
62         super(finalize_dg, args);
63     }
64 
65 
66     /***************************************************************************
67 
68         foreach iterator over informational interfaces to the active connections
69         in the pool.
70 
71     ***************************************************************************/
72 
73     public int opApply ( scope int delegate ( ref IConnectionHandlerInfo ) dg )
74     {
75         int ret;
76         scope it = this.new BusyItemsIterator;
77         foreach ( conn; it )
78         {
79             auto conn_info = cast(IConnectionHandlerInfo)conn;
80             ret = dg(conn_info);
81             if ( ret ) break;
82         }
83         return ret;
84     }
85 
86 
87     /***************************************************************************
88 
89         foreach iterator over informational interfaces to the active connections
90         in the pool, and their indices.
91 
92     ***************************************************************************/
93 
94     public int opApply ( scope int delegate ( ref size_t, ref IConnectionHandlerInfo ) dg )
95     {
96         int ret;
97         scope it = this.new BusyItemsIterator;
98         foreach ( i, conn; it )
99         {
100             auto conn_info = cast(IConnectionHandlerInfo)conn;
101             ret = dg(i, conn_info);
102             if ( ret ) break;
103         }
104         return ret;
105     }
106 
107 
108     /***************************************************************************
109 
110         IPoolInfo method, wrapper to super class implementation.
111 
112         Returns:
113             limit of items in pool
114 
115     ***************************************************************************/
116 
117     public override size_t limit ( )
118     {
119         return super.limit();
120     }
121 
122 
123     /***************************************************************************
124 
125         IPoolInfo method, wrapper to super class implementation.
126 
127         Returns:
128             true if the number of items in the pool is limited or false
129             otherwise
130 
131     ***************************************************************************/
132 
133     public override bool is_limited ( )
134     {
135         return super.is_limited();
136     }
137 }
138