View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.polling;
21  
22  import java.net.SocketAddress;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Queue;
30  import java.util.Set;
31  import java.util.concurrent.ConcurrentLinkedQueue;
32  import java.util.concurrent.Executor;
33  import java.util.concurrent.Executors;
34  
35  import org.apache.mina.core.RuntimeIoException;
36  import org.apache.mina.core.filterchain.IoFilter;
37  import org.apache.mina.core.future.IoFuture;
38  import org.apache.mina.core.service.AbstractIoAcceptor;
39  import org.apache.mina.core.service.IoAcceptor;
40  import org.apache.mina.core.service.IoHandler;
41  import org.apache.mina.core.service.IoProcessor;
42  import org.apache.mina.core.service.SimpleIoProcessorPool;
43  import org.apache.mina.core.session.AbstractIoSession;
44  import org.apache.mina.core.session.IoSession;
45  import org.apache.mina.core.session.IoSessionConfig;
46  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
47  import org.apache.mina.util.ExceptionMonitor;
48  
49  /**
50   * A base class for implementing transport using a polling strategy. The
51   * underlying sockets will be checked in an active loop and woke up when an
52   * socket needed to be processed. This class handle the logic behind binding,
53   * accepting and disposing the server sockets. An {@link Executor} will be used
54   * for running client accepting and an {@link AbstractPollingIoProcessor} will
55   * be used for processing client I/O operations like reading, writing and
56   * closing.
57   * 
58   * All the low level methods for binding, accepting, closing need to be provided
59   * by the subclassing implementation.
60   * 
61   * @see NioSocketAcceptor for a example of implementation
62   * 
63   * @author The Apache MINA Project (dev@mina.apache.org)
64   * @version $Rev: 751504 $, $Date: 2008-06-26 17:58:30 +0200 (jeu, 26 jun 2008)
65   *          $
66   */
67  public abstract class AbstractPollingIoAcceptor<T extends AbstractIoSession, H>
68          extends AbstractIoAcceptor {
69  
70      private final IoProcessor<T> processor;
71  
72      private final boolean createdProcessor;
73  
74      private final Object lock = new Object();
75  
76      private final Queue<AcceptorOperationFuture> registerQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
77  
78      private final Queue<AcceptorOperationFuture> cancelQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
79  
80      private final Map<SocketAddress, H> boundHandles = Collections
81              .synchronizedMap(new HashMap<SocketAddress, H>());
82  
83      private final ServiceOperationFuture disposalFuture = new ServiceOperationFuture();
84  
85      /** A flag set when the acceptor has been created and initialized */
86      private volatile boolean selectable;
87  
88      /** The thread responsible of accepting incoming requests */ 
89      private Acceptor acceptor;
90  
91      /**
92       * Constructor for {@link AbstractPollingIoAcceptor}. You need to provide a default
93       * session configuration, a class of {@link IoProcessor} which will be instantiated in a
94       * {@link SimpleIoProcessorPool} for better scaling in multiprocessor systems. The default
95       * pool size will be used.
96       * 
97       * @see SimpleIoProcessorPool
98       * 
99       * @param sessionConfig
100      *            the default configuration for the managed {@link IoSession}
101      * @param processorClass a {@link Class} of {@link IoProcessor} for the associated {@link IoSession}
102      *            type.
103      */
104     protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
105             Class<? extends IoProcessor<T>> processorClass) {
106         this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass),
107                 true);
108     }
109 
110     /**
111      * Constructor for {@link AbstractPollingIoAcceptor}. You need to provide a default
112      * session configuration, a class of {@link IoProcessor} which will be instantiated in a
113      * {@link SimpleIoProcessorPool} for using multiple thread for better scaling in multiprocessor
114      * systems.
115      * 
116      * @see SimpleIoProcessorPool
117      * 
118      * @param sessionConfig
119      *            the default configuration for the managed {@link IoSession}
120      * @param processorClass a {@link Class} of {@link IoProcessor} for the associated {@link IoSession}
121      *            type.
122      * @param processorCount the amount of processor to instantiate for the pool
123      */
124     protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
125             Class<? extends IoProcessor<T>> processorClass, int processorCount) {
126         this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass,
127                 processorCount), true);
128     }
129 
130     /**
131      * Constructor for {@link AbstractPollingIoAcceptor}. You need to provide a default
132      * session configuration, a default {@link Executor} will be created using
133      * {@link Executors#newCachedThreadPool()}.
134      * 
135      * {@see AbstractIoService#AbstractIoService(IoSessionConfig, Executor)}
136      * 
137      * @param sessionConfig
138      *            the default configuration for the managed {@link IoSession}
139      * @param processor the {@link IoProcessor} for processing the {@link IoSession} of this transport, triggering 
140      *            events to the bound {@link IoHandler} and processing the chains of {@link IoFilter} 
141      */
142     protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
143             IoProcessor<T> processor) {
144         this(sessionConfig, null, processor, false);
145     }
146 
147     /**
148      * Constructor for {@link AbstractPollingIoAcceptor}. You need to provide a default
149      * session configuration and an {@link Executor} for handling I/O events. If a
150      * null {@link Executor} is provided, a default one will be created using
151      * {@link Executors#newCachedThreadPool()}.
152      * 
153      * {@see AbstractIoService#AbstractIoService(IoSessionConfig, Executor)}
154      * 
155      * @param sessionConfig
156      *            the default configuration for the managed {@link IoSession}
157      * @param executor
158      *            the {@link Executor} used for handling asynchronous execution of I/O
159      *            events. Can be <code>null</code>.
160      * @param processor the {@link IoProcessor} for processing the {@link IoSession} of this transport, triggering 
161      *            events to the bound {@link IoHandler} and processing the chains of {@link IoFilter} 
162      */
163     protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
164             Executor executor, IoProcessor<T> processor) {
165         this(sessionConfig, executor, processor, false);
166     }
167 
168     /**
169      * Constructor for {@link AbstractPollingIoAcceptor}. You need to provide a default
170      * session configuration and an {@link Executor} for handling I/O events. If a
171      * null {@link Executor} is provided, a default one will be created using
172      * {@link Executors#newCachedThreadPool()}.
173      * 
174      * {@see AbstractIoService#AbstractIoService(IoSessionConfig, Executor)}
175      * 
176      * @param sessionConfig
177      *            the default configuration for the managed {@link IoSession}
178      * @param executor
179      *            the {@link Executor} used for handling asynchronous execution of I/O
180      *            events. Can be <code>null</code>.
181      * @param processor the {@link IoProcessor} for processing the {@link IoSession} of 
182      * this transport, triggering events to the bound {@link IoHandler} and processing 
183      * the chains of {@link IoFilter}
184      * @param createdProcessor tagging the processor as automatically created, so it 
185      * will be automatically disposed 
186      */
187     private AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
188             Executor executor, IoProcessor<T> processor,
189             boolean createdProcessor) {
190         super(sessionConfig, executor);
191 
192         if (processor == null) {
193             throw new NullPointerException("processor");
194         }
195 
196         this.processor = processor;
197         this.createdProcessor = createdProcessor;
198 
199         try {
200             // Initialize the selector
201             init();
202             
203             // The selector is now ready, we can switch the
204             // flag to true so that incoming connection can be accepted
205             selectable = true;
206         } catch (RuntimeException e) {
207             throw e;
208         } catch (Exception e) {
209             throw new RuntimeIoException("Failed to initialize.", e);
210         } finally {
211             if (!selectable) {
212                 try {
213                     destroy();
214                 } catch (Exception e) {
215                     ExceptionMonitor.getInstance().exceptionCaught(e);
216                 }
217             }
218         }
219     }
220 
221     /**
222      * Initialize the polling system, will be called at construction time.
223      * @throws Exception any exception thrown by the underlying system calls  
224      */
225     protected abstract void init() throws Exception;
226 
227     /**
228      * Destroy the polling system, will be called when this {@link IoAcceptor}
229      * implementation will be disposed.  
230      * @throws Exception any exception thrown by the underlying systems calls
231      */
232     protected abstract void destroy() throws Exception;
233 
234     /**
235      * Check for acceptable connections, interrupt when at least a server is ready for accepting.
236      * All the ready server socket descriptors need to be returned by {@link #selectedHandles()}
237      * @return The number of sockets having got incoming client
238      * @throws Exception any exception thrown by the underlying systems calls
239      */
240     protected abstract int select() throws Exception;
241 
242     /**
243      * Interrupt the {@link #select()} method. Used when the poll set need to be modified.
244      */
245     protected abstract void wakeup();
246 
247     /**
248      * {@link Iterator} for the set of server sockets found with acceptable incoming connections
249      *  during the last {@link #select()} call.
250      * @return the list of server handles ready
251      */
252     protected abstract Iterator<H> selectedHandles();
253 
254     /**
255      * Open a server socket for a given local address.
256      * @param localAddress the associated local address
257      * @return the opened server socket
258      * @throws Exception any exception thrown by the underlying systems calls
259      */
260     protected abstract H open(SocketAddress localAddress) throws Exception;
261 
262     /**
263      * Get the local address associated with a given server socket
264      * @param handle the server socket
265      * @return the local {@link SocketAddress} associated with this handle
266      * @throws Exception any exception thrown by the underlying systems calls
267      */
268     protected abstract SocketAddress localAddress(H handle) throws Exception;
269 
270     /**
271      * Accept a client connection for a server socket and return a new {@link IoSession}
272      * associated with the given {@link IoProcessor}
273      * @param processor the {@link IoProcessor} to associate with the {@link IoSession}  
274      * @param handle the server handle
275      * @return the created {@link IoSession}
276      * @throws Exception any exception thrown by the underlying systems calls
277      */
278     protected abstract T accept(IoProcessor<T> processor, H handle)
279             throws Exception;
280 
281     /**
282      * Close a server socket.
283      * @param handle the server socket
284      * @throws Exception any exception thrown by the underlying systems calls
285      */
286     protected abstract void close(H handle) throws Exception;
287 
288     /**
289      * {@inheritDoc}
290      */
291     @Override
292     protected IoFuture dispose0() throws Exception {
293         unbind();
294         if (!disposalFuture.isDone()) {
295             startupAcceptor();
296             wakeup();
297         }
298         return disposalFuture;
299     }
300 
301     /**
302      * {@inheritDoc}
303      */
304     @Override
305     protected final Set<SocketAddress> bindInternal(
306             List<? extends SocketAddress> localAddresses) throws Exception {
307         // Create a bind request as a Future operation. When the selector
308         // have handled the registration, it will signal this future.
309         AcceptorOperationFuture request = new AcceptorOperationFuture(
310                 localAddresses);
311 
312         // adds the Registration request to the queue for the Workers
313         // to handle
314         registerQueue.add(request);
315 
316         // creates the Acceptor instance and has the local
317         // executor kick it off.
318         startupAcceptor();
319         
320         // As we just started the acceptor, we have to unblock the select()
321         // in order to process the bind request we just have added to the 
322         // registerQueue.
323         wakeup();
324         
325         // Now, we wait until this request is completed.
326         request.awaitUninterruptibly();
327 
328         if (request.getException() != null) {
329             throw request.getException();
330         }
331 
332         // Update the local addresses.
333         // setLocalAddresses() shouldn't be called from the worker thread
334         // because of deadlock.
335         Set<SocketAddress> newLocalAddresses = new HashSet<SocketAddress>();
336         
337         for (H handle:boundHandles.values()) {
338             newLocalAddresses.add(localAddress(handle));
339         }
340 
341         return newLocalAddresses;
342     }
343 
344     /**
345      * This method is called by the doBind() and doUnbind()
346      * methods.  If the acceptor is null, the acceptor object will
347      * be created and kicked off by the executor.  If the acceptor
348      * object is null, probably already created and this class
349      * is now working, then nothing will happen and the method
350      * will just return.
351      */
352     private void startupAcceptor() {
353         // If the acceptor is not ready, clear the queues
354         // TODO : they should already be clean : do we have to do that ?
355         if (!selectable) {
356             registerQueue.clear();
357             cancelQueue.clear();
358         }
359 
360         // start the acceptor if not already started
361         synchronized (lock) {
362             if (acceptor == null) {
363                 acceptor = new Acceptor();
364                 executeWorker(acceptor);
365             }
366         }
367     }
368 
369     /**
370      * {@inheritDoc}
371      */
372     @Override
373     protected final void unbind0(List<? extends SocketAddress> localAddresses)
374             throws Exception {
375         AcceptorOperationFuture future = new AcceptorOperationFuture(
376                 localAddresses);
377 
378         cancelQueue.add(future);
379         startupAcceptor();
380         wakeup();
381 
382         future.awaitUninterruptibly();
383         if (future.getException() != null) {
384             throw future.getException();
385         }
386     }
387 
388     /**
389      * This class is called by the startupAcceptor() method and is
390      * placed into a NamePreservingRunnable class.
391      * It's a thread accepting incoming connections from clients.
392      * The loop is stopped when all the bound handlers are unbound.
393      */
394     private class Acceptor implements Runnable {
395         public void run() {
396             int nHandles = 0;
397 
398             while (selectable) {
399                 try {
400                     // Detect if we have some keys ready to be processed
401                     // The select() will be woke up if some new connection
402                     // have occurred, or if the selector has been explicitly
403                     // woke up
404                     int selected = select();
405 
406                     // this actually sets the selector to OP_ACCEPT,
407                     // and binds to the port on which this class will
408                     // listen on
409                     nHandles += registerHandles();
410 
411                     if (selected > 0) {
412                         // We have some connection request, let's process 
413                         // them here. 
414                         processHandles(selectedHandles());
415                     }
416 
417                     // check to see if any cancellation request has been made.
418                     nHandles -= unregisterHandles();
419 
420                     // Now, if the number of registred handles is 0, we can
421                     // quit the loop: we don't have any socket listening
422                     // for incoming connection.
423                     if (nHandles == 0) {
424                         synchronized (lock) {
425                             if (registerQueue.isEmpty()
426                                     && cancelQueue.isEmpty()) {
427                                 acceptor = null;
428                                 break;
429                             }
430                         }
431                     }
432                 } catch (Throwable e) {
433                     ExceptionMonitor.getInstance().exceptionCaught(e);
434 
435                     try {
436                         Thread.sleep(1000);
437                     } catch (InterruptedException e1) {
438                         ExceptionMonitor.getInstance().exceptionCaught(e1);
439                     }
440                 }
441             }
442 
443             // Cleanup all the processors, and shutdown the acceptor.
444             if (selectable && isDisposing()) {
445                 selectable = false;
446                 try {
447                     if (createdProcessor) {
448                         processor.dispose();
449                     }
450                 } finally {
451                     try {
452                         synchronized (disposalLock) {
453                             if (isDisposing()) {
454                                 destroy();
455                             }
456                         }
457                     } catch (Exception e) {
458                         ExceptionMonitor.getInstance().exceptionCaught(e);
459                     } finally {
460                         disposalFuture.setDone();
461                     }
462                 }
463             }
464         }
465 
466         /**
467          * This method will process new sessions for the Worker class.  All
468          * keys that have had their status updates as per the Selector.selectedKeys()
469          * method will be processed here.  Only keys that are ready to accept
470          * connections are handled here.
471          * <p/>
472          * Session objects are created by making new instances of SocketSessionImpl
473          * and passing the session object to the SocketIoProcessor class.
474          */
475         @SuppressWarnings("unchecked")
476         private void processHandles(Iterator<H> handles) throws Exception {
477             while (handles.hasNext()) {
478                 H handle = handles.next();
479                 handles.remove();
480 
481                 // Associates a new created connection to a processor,
482                 // and get back a session
483                 T session = accept(processor, handle);
484                 
485                 if (session == null) {
486                     break;
487                 }
488 
489                 initSession(session, null, null);
490 
491                 // add the session to the SocketIoProcessor
492                 session.getProcessor().add(session);
493             }
494         }
495     }
496 
497     /**
498      * Sets up the socket communications.  Sets items such as:
499      * <p/>
500      * Blocking
501      * Reuse address
502      * Receive buffer size
503      * Bind to listen port
504      * Registers OP_ACCEPT for selector
505      */
506     private int registerHandles() {
507         for (;;) {
508             // The register queue contains the list of services to manage
509             // in this acceptor.
510             AcceptorOperationFuture future = registerQueue.poll();
511             
512             if (future == null) {
513                 return 0;
514             }
515 
516             // We create a temporary map to store the bound handles,
517             // as we may have to remove them all if there is an exception
518             // during the sockets opening.
519             Map<SocketAddress, H> newHandles = new HashMap<SocketAddress, H>();
520             List<SocketAddress> localAddresses = future.getLocalAddresses();
521 
522             try {
523                 // Process all the addresses
524                 for (SocketAddress a : localAddresses) {
525                     H handle = open(a);
526                     newHandles.put(localAddress(handle), handle);
527                 }
528 
529                 // Everything went ok, we can now update the map storing
530                 // all the bound sockets.
531                 boundHandles.putAll(newHandles);
532 
533                 // and notify.
534                 future.setDone();
535                 return newHandles.size();
536             } catch (Exception e) {
537                 // We store the exception in the future
538                 future.setException(e);
539             } finally {
540                 // Roll back if failed to bind all addresses.
541                 if (future.getException() != null) {
542                     for (H handle : newHandles.values()) {
543                         try {
544                             close(handle);
545                         } catch (Exception e) {
546                             ExceptionMonitor.getInstance().exceptionCaught(e);
547                         }
548                     }
549                     
550                     // TODO : add some comment : what is the wakeup() waking up ?
551                     wakeup();
552                 }
553             }
554         }
555     }
556 
557     /**
558      * This method just checks to see if anything has been placed into the
559      * cancellation queue.  The only thing that should be in the cancelQueue
560      * is CancellationRequest objects and the only place this happens is in
561      * the doUnbind() method.
562      */
563     private int unregisterHandles() {
564         int cancelledHandles = 0;
565         for (;;) {
566             AcceptorOperationFuture future = cancelQueue.poll();
567             if (future == null) {
568                 break;
569             }
570 
571             // close the channels
572             for (SocketAddress a : future.getLocalAddresses()) {
573                 H handle = boundHandles.remove(a);
574                 if (handle == null) {
575                     continue;
576                 }
577 
578                 try {
579                     close(handle);
580                     wakeup(); // wake up again to trigger thread death
581                 } catch (Throwable e) {
582                     ExceptionMonitor.getInstance().exceptionCaught(e);
583                 } finally {
584                     cancelledHandles++;
585                 }
586             }
587 
588             future.setDone();
589         }
590 
591         return cancelledHandles;
592     }
593 
594     /**
595      * {@inheritDoc}
596      */
597     public final IoSession newSession(SocketAddress remoteAddress,
598             SocketAddress localAddress) {
599         throw new UnsupportedOperationException();
600     }
601 }