001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.gravity.generic;
022
023import org.granite.gravity.AbstractChannel;
024import org.granite.gravity.AsyncHttpContext;
025import org.granite.gravity.Gravity;
026import org.granite.gravity.MessageReceivingException;
027import org.granite.logging.Logger;
028
029import flex.messaging.messages.AsyncMessage;
030
031/**
032 * @author William DRAI
033 */
034public class GenericChannel extends AbstractChannel {
035
036    private static final Logger log = Logger.getLogger(GenericChannel.class);
037
038    private WaitingContinuation continuation = null;
039
040    public GenericChannel(Gravity gravity, String id, GenericChannelFactory factory, String clientType) {
041        super(gravity, id, factory, clientType);
042    }
043
044    public void setContinuation(WaitingContinuation continuation) {
045        try {
046            if (this.continuation != null && this.continuation.isPending()) {
047                log.debug("Set pending continuation for client: %s", getId());
048                this.continuation.resume();
049            }
050        }
051        finally {
052            this.continuation = continuation;
053        }
054    }
055    
056    public void close() {
057        try {
058            if (this.continuation != null)
059                this.continuation.reset();
060        }
061        finally {
062            this.continuation = null;
063        }
064    }
065    
066
067    public void resume() {
068        try {
069            if (this.continuation != null) {
070                log.debug("Resume pending continuation for client: %s", getId());
071                this.continuation.resume();
072            }
073        }
074        finally {
075            this.continuation = null;
076        }
077    }
078    
079    @Override
080        public void receive(AsyncMessage message) throws MessageReceivingException {
081                if (message == null)
082                        throw new NullPointerException("message cannot be null");
083                
084                receivedQueueLock.lock();
085                try {
086                        receivedQueue.add(message);
087                }
088                catch (Exception e) {
089                        throw new MessageReceivingException(message, "Could not queue message", e);
090                }
091                finally {
092                        receivedQueueLock.unlock();
093                }
094                
095                synchronized (this) {
096                        resume();
097                }
098        }
099
100        @Override
101        protected boolean hasAsyncHttpContext() {
102                return false;
103        }
104
105        @Override
106        protected void releaseAsyncHttpContext(AsyncHttpContext context) {
107        }
108
109        @Override
110        protected AsyncHttpContext acquireAsyncHttpContext() {
111        return null;
112    }
113
114    public boolean isLocal() {
115        return true;
116    }
117
118        @Override
119        public void destroy() {
120                try {
121                        super.destroy();
122                }
123                finally {
124                        synchronized (this) {
125                                close();
126                        }
127                }
128        }
129}