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