• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.network.kernel;
34 
35 import java.util.concurrent.ConcurrentLinkedQueue;
36 import java.util.concurrent.LinkedBlockingQueue;
37 import java.util.concurrent.atomic.AtomicLong;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 
41 /**
42  *  Base implementation of the Kernel interface providing several
43  *  useful default implementations of some methods.  This implementation
44  *  assumes that the kernel will be managing its own internal threads
45  *  and queuing any results for the caller to retrieve on their own
46  *  thread.
47  *
48  *  @version   $Revision: 8843 $
49  *  @author    Paul Speed
50  */
51 public abstract class AbstractKernel implements Kernel
52 {
53     static Logger log = Logger.getLogger(AbstractKernel.class.getName());
54 
55     private AtomicLong nextId = new AtomicLong(1);
56 
57     /**
58      *  Contains the pending endpoint events waiting for the caller
59      *  to retrieve them.
60      */
61     private ConcurrentLinkedQueue<EndpointEvent> endpointEvents = new ConcurrentLinkedQueue<EndpointEvent>();
62 
63     /**
64      *  Contains the pending envelopes waiting for the caller to
65      *  retrieve them.
66      */
67     private LinkedBlockingQueue<Envelope> envelopes = new LinkedBlockingQueue<Envelope>();
68 
AbstractKernel()69     protected AbstractKernel()
70     {
71     }
72 
reportError( Exception e )73     protected void reportError( Exception e )
74     {
75         // Should really be queued up so the outer thread can
76         // retrieve them.  For now we'll just log it.  FIXME
77         log.log( Level.SEVERE, "Unhanddled kernel error", e );
78     }
79 
nextEndpointId()80     protected long nextEndpointId()
81     {
82         return nextId.getAndIncrement();
83     }
84 
85     /**
86      *  Returns true if there are waiting envelopes.
87      */
hasEnvelopes()88     public boolean hasEnvelopes()
89     {
90         return !envelopes.isEmpty();
91     }
92 
93     /**
94      *  Removes one envelope from the received messages queue or
95      *  blocks until one is available.
96      */
read()97     public Envelope read() throws InterruptedException
98     {
99         return envelopes.take();
100     }
101 
102     /**
103      *  Removes and returnsn one endpoint event from the event queue or
104      *  null if there are no endpoint events.
105      */
nextEvent()106     public EndpointEvent nextEvent()
107     {
108         return endpointEvents.poll();
109     }
110 
addEvent( EndpointEvent e )111     protected void addEvent( EndpointEvent e )
112     {
113         endpointEvents.add( e );
114     }
115 
addEnvelope( Envelope env )116     protected void addEnvelope( Envelope env )
117     {
118         if( !envelopes.offer( env ) ) {
119             throw new KernelException( "Critical error, could not enqueue envelope." );
120         }
121     }
122 }
123