001         package com.croftsoft.apps.chat.server;
002    
003         import com.croftsoft.core.math.MathConstants;
004         import com.croftsoft.core.util.queue.Queue;
005    
006         import com.croftsoft.apps.chat.event.Event;
007         import com.croftsoft.apps.chat.event.NullEvent;
008         import com.croftsoft.apps.chat.request.Request;
009         import com.croftsoft.apps.chat.request.PullRequest;
010         import com.croftsoft.apps.chat.user.User;
011    
012         /*********************************************************************
013         * Serves a PullRequest.
014         *
015         * @version
016         *   2003-06-18
017         * @since
018         *   2003-06-07
019         * @author
020         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021         *********************************************************************/
022    
023         public final class  PullServer
024           extends AbstractServer
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029         public static final long  DEFAULT_QUEUE_PULL_TIMEOUT
030           = 30 * MathConstants.MILLISECONDS_PER_SECOND;
031    
032         //
033    
034         private final long  queuePullTimeout;
035    
036         //////////////////////////////////////////////////////////////////////
037         //////////////////////////////////////////////////////////////////////
038    
039         public  PullServer ( long  queuePullTimeout )
040         //////////////////////////////////////////////////////////////////////
041         {
042           this.queuePullTimeout = queuePullTimeout;
043         }
044    
045         public  PullServer ( )
046         //////////////////////////////////////////////////////////////////////
047         {
048           this ( DEFAULT_QUEUE_PULL_TIMEOUT );
049         }
050    
051         //////////////////////////////////////////////////////////////////////
052         //////////////////////////////////////////////////////////////////////
053    
054         public Object  serve (
055           User     user,
056           Request  request )
057         //////////////////////////////////////////////////////////////////////
058         {
059           Queue  messageQueue = user.getMessageQueue ( );
060    
061           Object  message = null;
062    
063           try
064           {
065             message = messageQueue.pull ( queuePullTimeout );
066           }
067           catch ( InterruptedException  ex )
068           {
069           }
070    
071           if ( message == null )
072           {
073             message = new NullEvent ( user.getEventIndex ( ) );
074           }
075           else if ( message instanceof Event )
076           {
077             ( ( Event ) message ).setEventIndex ( user.nextEventIndex ( ) );         
078           }
079    
080           return message;
081         }
082    
083         //////////////////////////////////////////////////////////////////////
084         //////////////////////////////////////////////////////////////////////
085         }