KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > io > impl > IoSocketDispatcherPool


1 // $Id: IoSocketDispatcherPool.java 1304 2007-06-02 13:26:34Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream.io.impl;
23
24
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32
33 import org.xsocket.IDispatcher;
34
35
36 /**
37  * Dispatcher Pool
38  *
39  *
40  * @author grro@xsocket.org
41  */

42 final class IoSocketDispatcherPool {
43     
44     private static final Logger JavaDoc LOG = Logger.getLogger(IoSocketDispatcherPool.class.getName());
45     
46     private boolean isRunning = true;
47     
48
49     
50     // memory management
51
private int preallocationSize = 65536;
52     private boolean useDirect = false;
53     
54     
55     // listeners
56
private final List JavaDoc<IIoSocketDispatcherPoolListener> listeners = new ArrayList JavaDoc<IIoSocketDispatcherPoolListener>();
57
58     
59     
60     // dispatcher management
61
private final LinkedList JavaDoc<IoSocketDispatcher> dispatchers = new LinkedList JavaDoc<IoSocketDispatcher>();
62     private int size = 0;
63     private int pointer = 0;
64
65     
66     IoSocketDispatcherPool(int preallocationSize, boolean useDirect) {
67         this.preallocationSize = preallocationSize;
68         this.useDirect = useDirect;
69     }
70     
71     
72     synchronized void setSize(int size) {
73         this.size = size;
74         updateDispatcher();
75     }
76     
77     synchronized void addListener(IIoSocketDispatcherPoolListener listener) {
78         listeners.add(listener);
79     }
80     
81     
82     synchronized boolean removeListener(IIoSocketDispatcherPoolListener listener) {
83         return listeners.remove(listener);
84     }
85
86
87     void run() {
88         isRunning = true;
89         updateDispatcher();
90     }
91     
92     
93     
94     /**
95      * get the size of the preallocation buffer,
96      * for reading incomming data
97      *
98      * @return preallocation buffer size
99      */

100     synchronized int getReceiveBufferPreallocationSize() {
101         return preallocationSize;
102     }
103
104     /**
105      * set the size of the preallocation buffer,
106      * for reading incomming data
107      *
108      * @param size the preallocation buffer size
109      */

110     synchronized void setReceiveBufferPreallocationSize(int size) {
111         preallocationSize = size;
112     }
113     
114
115     @SuppressWarnings JavaDoc("unchecked")
116     List JavaDoc<IDispatcher<IoSocketHandler>> getDispatchers() {
117         List JavaDoc<IDispatcher<IoSocketHandler>> result = null;
118         synchronized (dispatchers) {
119             result = (List JavaDoc<IDispatcher<IoSocketHandler>>) dispatchers.clone();
120         }
121         return result;
122     }
123     
124     
125
126
127     @SuppressWarnings JavaDoc("unchecked")
128     private synchronized void updateDispatcher() {
129         if (isRunning) {
130             int currentRunning = dispatchers.size();
131             
132             if (currentRunning != size) {
133                 if (currentRunning > size) {
134                     for (int i = size; i < currentRunning; i++) {
135                         IDispatcher<IoSocketHandler> dispatcher = dispatchers.getLast();
136                         dispatchers.remove(dispatcher);
137                         try {
138                             dispatcher.close();
139                         } catch (IOException JavaDoc ioe) {
140                             if (LOG.isLoggable(Level.FINE)) {
141                                 LOG.fine("error occured by closing the dispatcher " + dispatcher + ". reason " + ioe.toString());
142                             }
143                         }
144                         
145                         for (IIoSocketDispatcherPoolListener listener : listeners) {
146                             listener.onDispatcherRemoved(dispatcher);
147                         }
148                     }
149         
150                 } else if ( currentRunning < size) {
151                     for (int i = currentRunning; i < size; i++) {
152                         IoSocketDispatcher dispatcher = new IoSocketDispatcher(new UnsynchronizedMemoryManager(preallocationSize, useDirect));
153                         dispatchers.addLast(dispatcher);
154         
155                         Thread JavaDoc t = new Thread JavaDoc(dispatcher);
156                         t.setDaemon(false);
157                         t.setName(IoSocketDispatcher.DISPATCHER_PREFIX + "#" + i);
158                         t.start();
159                         
160                         for (IIoSocketDispatcherPoolListener listener : listeners) {
161                             listener.onDispatcherAdded(dispatcher);
162                         }
163
164                     }
165                 }
166             }
167             
168             IDispatcher<IoSocketHandler>[] connectionDispatchers = new IDispatcher[dispatchers.size()];
169             for (int i = 0; i < connectionDispatchers.length; i++) {
170                 connectionDispatchers[i] = dispatchers.get(i);
171             }
172         }
173     }
174     
175
176     
177     /**
178      * shutdown the pool
179      *
180      */

181     void shutdown() {
182         isRunning = false;
183         
184         if (LOG.isLoggable(Level.FINER)) {
185             LOG.fine("terminate dispatchers");
186         }
187
188         for (IDispatcher<IoSocketHandler> dispatcher : dispatchers) {
189             try {
190                 dispatcher.close();
191                 
192                 for (IIoSocketDispatcherPoolListener listener : listeners) {
193                     listener.onDispatcherRemoved(dispatcher);
194                 }
195                 
196             } catch (IOException JavaDoc ioe) {
197                 if (LOG.isLoggable(Level.FINE)) {
198                     LOG.fine("error occured by closing the dispatcher " + dispatcher + ". reason " + ioe.toString());
199                 }
200             }
201         }
202         
203         dispatchers.clear();
204     }
205     
206
207
208     
209     IoSocketDispatcher nextDispatcher() {
210         // round-robin approach
211
pointer++;
212         if (pointer >= size) {
213             pointer = 0;
214         }
215
216         return dispatchers.get(pointer);
217     }
218     
219     
220
221     @SuppressWarnings JavaDoc("unchecked")
222     long getNumberOfConnectionTimeouts() {
223         long timeouts = 0;
224         
225         LinkedList JavaDoc<IoSocketDispatcher> copy = (LinkedList JavaDoc<IoSocketDispatcher>) dispatchers.clone();
226         for (IoSocketDispatcher dispatcher : copy) {
227             timeouts += dispatcher.getCountConnectionTimeout();
228         }
229         return timeouts;
230     }
231     
232     
233     @SuppressWarnings JavaDoc("unchecked")
234     public long getNumberOfIdleTimeouts() {
235         long timeouts = 0;
236         
237         LinkedList JavaDoc<IoSocketDispatcher> copy = (LinkedList JavaDoc<IoSocketDispatcher>) dispatchers.clone();
238         for (IoSocketDispatcher dispatcher : copy) {
239             timeouts += dispatcher.getCountIdleTimeout();
240         }
241         return timeouts;
242     }
243     
244 }
245
Popular Tags