 |
Changeset 3658
- 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
| r3623 |
r3658 |
|
| 33 | 33 | *******************************************************************************/ |
|---|
| 34 | 34 | |
|---|
| 35 | | private class SocketPool |
|---|
| | 35 | class SocketPool(T) |
|---|
| 36 | 36 | { |
|---|
| 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; |
|---|
| 39 | 40 | |
|---|
| 40 | 41 | private int size, |
|---|
| … | … | |
| 43 | 44 | online; |
|---|
| 44 | 45 | |
|---|
| 45 | | private Error error; |
|---|
| | 46 | private Factory factory; |
|---|
| 46 | 47 | private InternetAddress address; |
|---|
| 47 | 48 | private Connection freelist; |
|---|
| 48 | 49 | private TimeSpan timeout = TimeSpan.seconds(60); |
|---|
| 49 | 50 | |
|---|
| | 51 | |
|---|
| 50 | 52 | /*********************************************************************** |
|---|
| 51 | 53 | |
|---|
| … | … | |
| 54 | 56 | ***********************************************************************/ |
|---|
| 55 | 57 | |
|---|
| 56 | | this (InternetAddress address, Error error=null, bool nagle=true) |
|---|
| | 58 | this (InternetAddress address, Factory factory, bool nagle=true) |
|---|
| 57 | 59 | { |
|---|
| 58 | | this.online = true; |
|---|
| 59 | | this.error = error; |
|---|
| 60 | 60 | this.nagle = nagle; |
|---|
| | 61 | this.factory = factory; |
|---|
| 61 | 62 | this.address = address; |
|---|
| 62 | 63 | } |
|---|
| … | … | |
| 132 | 133 | ***********************************************************************/ |
|---|
| 133 | 134 | |
|---|
| 134 | | final bool request (Handler send, Handler recv) |
|---|
| | 135 | final bool request (Handler send, Handler recv, Log log = null) |
|---|
| 135 | 136 | { |
|---|
| 136 | 137 | Time time; |
|---|
| 137 | 138 | |
|---|
| 138 | 139 | // get a connection to the server |
|---|
| 139 | | auto connect = borrow (time = Clock.now); |
|---|
| | 140 | auto connection = borrow (time = Clock.now); |
|---|
| 140 | 141 | |
|---|
| 141 | 142 | // talk to the server (try a few times if necessary) |
|---|
| 142 | 143 | for (int attempts=3; attempts--;) |
|---|
| 143 | 144 | try { |
|---|
| 144 | | send (connect.conduit); |
|---|
| | 145 | send (connection.bound); |
|---|
| 145 | 146 | |
|---|
| 146 | 147 | // load the reply. Don't retry on |
|---|
| … | … | |
| 150 | 151 | // otherwise |
|---|
| 151 | 152 | attempts = 0; |
|---|
| 152 | | recv (connect.conduit); |
|---|
| | 153 | recv (connection.bound); |
|---|
| 153 | 154 | |
|---|
| 154 | 155 | // return borrowed connection |
|---|
| 155 | | connect.done (time); |
|---|
| | 156 | connection.done (time); |
|---|
| 156 | 157 | |
|---|
| 157 | 158 | } catch (IOException x) |
|---|
| 158 | 159 | { |
|---|
| 159 | | if (error) |
|---|
| 160 | | error ("IOException on request to server {} - {}", address, x); |
|---|
| | 160 | if (log) |
|---|
| | 161 | log ("IOException on request to server {} - {}", address, x); |
|---|
| 161 | 162 | |
|---|
| 162 | 163 | // attempt to reconnect? |
|---|
| 163 | | if (attempts is 0 || !connect.reset) |
|---|
| | 164 | if (attempts is 0 || !connection.reset) |
|---|
| 164 | 165 | { |
|---|
| 165 | 166 | // that server is offline |
|---|
| 166 | | if (error) |
|---|
| 167 | | error ("disabling connection for server {}", address); |
|---|
| 168 | 167 | close; |
|---|
| | 168 | |
|---|
| | 169 | if (log) |
|---|
| | 170 | log ("disabling connection for server {}", address); |
|---|
| 169 | 171 | |
|---|
| 170 | 172 | // state that we failed |
|---|
| … | … | |
| 191 | 193 | SocketPool parent; |
|---|
| 192 | 194 | SocketConduit socket; |
|---|
| | 195 | T bound; |
|---|
| 193 | 196 | |
|---|
| 194 | 197 | /*************************************************************** |
|---|
| … | … | |
| 215 | 218 | final bool reset () |
|---|
| 216 | 219 | { |
|---|
| | 220 | // new connection to host |
|---|
| | 221 | socket = new SocketConduit; |
|---|
| | 222 | |
|---|
| | 223 | // have callee create the binding |
|---|
| | 224 | bound = parent.factory (socket); |
|---|
| | 225 | |
|---|
| 217 | 226 | try { |
|---|
| 218 | | socket = new SocketConduit; |
|---|
| 219 | | |
|---|
| 220 | 227 | // apply Nagle settings |
|---|
| 221 | 228 | socket.socket.setNoDelay (parent.nagle is false); |
|---|
| … | … | |
| 228 | 235 | |
|---|
| 229 | 236 | 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 | |
|---|
| 234 | 239 | 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; |
|---|
| 246 | 240 | } |
|---|
| 247 | 241 | |
|---|
| … | … | |
| 286 | 280 | import tango.io.Stdout; |
|---|
| 287 | 281 | import tango.core.Thread; |
|---|
| | 282 | |
|---|
| | 283 | alias SocketPool!(IConduit) Pool; |
|---|
| 288 | 284 | |
|---|
| 289 | 285 | void main() |
|---|
| 290 | 286 | { |
|---|
| 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) |
|---|
| 295 | 293 | { |
|---|
| 296 | 294 | Stdout ("sending").newline; |
|---|
| … | … | |
| 300 | 298 | throw new IOException ("failed to write"); |
|---|
| 301 | 299 | } |
|---|
| 302 | | |
|---|
| | 300 | |
|---|
| 303 | 301 | void recv (IConduit c) |
|---|
| 304 | 302 | { |
|---|
| 305 | 303 | } |
|---|
| 306 | 304 | |
|---|
| | 305 | auto pool = new Pool (new InternetAddress("localhost:1111"), &create); |
|---|
| 307 | 306 | while (true) |
|---|
| 308 | 307 | { |
|---|
| 309 | | if (! pool.request (&send, &recv)) |
|---|
| | 308 | if (! pool.request (&send, &recv, cast(Pool.Log) &Stdout.formatln)) |
|---|
| 310 | 309 | Stdout (">>> request failed").newline; |
|---|
| 311 | 310 | Thread.sleep (1); |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic