001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *                               ***
008 *
009 *   Community License: GPL 3.0
010 *
011 *   This file is free software: you can redistribute it and/or modify
012 *   it under the terms of the GNU General Public License as published
013 *   by the Free Software Foundation, either version 3 of the License,
014 *   or (at your option) any later version.
015 *
016 *   This file is distributed in the hope that it will be useful, but
017 *   WITHOUT ANY WARRANTY; without even the implied warranty of
018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019 *   GNU General Public License for more details.
020 *
021 *   You should have received a copy of the GNU General Public License
022 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
023 *
024 *                               ***
025 *
026 *   Available Commercial License: GraniteDS SLA 1.0
027 *
028 *   This is the appropriate option if you are creating proprietary
029 *   applications and you are not prepared to distribute and share the
030 *   source code of your application under the GPL v3 license.
031 *
032 *   Please visit http://www.granitedataservices.com/license for more
033 *   details.
034 */
035package org.granite.gravity.udp;
036
037import java.net.InetSocketAddress;
038
039import javax.servlet.http.HttpServletRequest;
040
041import org.granite.gravity.AbstractChannel;
042import org.granite.gravity.GravityConfig;
043import org.granite.gravity.udp.UdpReceiver;
044import org.granite.gravity.udp.UdpReceiverFactory;
045import org.granite.logging.Logger;
046
047import flex.messaging.messages.Message;
048
049/**
050 * @author Franck WOLFF
051 */
052public class UdpReceiverFactoryImpl implements UdpReceiverFactory {
053
054        private static final Logger log = Logger.getLogger(UdpReceiverFactoryImpl.class);
055
056    private int port = 0;
057        private boolean nio = true;
058        private boolean connected = false;
059        private int sendBufferSize = GravityConfig.DEFAULT_UDP_SEND_BUFFER_SIZE;
060        
061        private UdpChannelFactory udpChannelFactory = null;
062
063        public void setPort(int port) {
064                this.port = port;
065        }
066        public int getPort() {
067                return port;
068        }
069
070        public boolean isNio() {
071                return nio;
072        }
073        public void setNio(boolean nio) {
074                this.nio = nio;
075        }
076
077        public boolean isConnected() {
078                return connected;
079        }
080        public void setConnected(boolean connected) {
081                this.connected = connected;
082        }
083
084        public int getSendBufferSize() {
085                return sendBufferSize;
086        }
087        public void setSendBufferSize(int sendBufferSize) {
088                this.sendBufferSize = sendBufferSize;
089        }
090
091        public void start() {
092                log.info("Starting UDP Receiver Factory (port=%d, nio=%b, connected=%b, sendBufferSize=%d)", port, nio, connected, sendBufferSize);
093                
094                if (connected) {
095                        if (nio)
096                                udpChannelFactory = new UdpConnectedNIOChannelFactory(this);
097                        else
098                                udpChannelFactory = new UdpConnectedIOChannelFactory(this);
099                }
100                else {
101                        if (nio)
102                                udpChannelFactory = new UdpSharedNIOChannelFactory(this);
103                        else
104                                udpChannelFactory = new UdpSharedIOChannelFactory(this);
105                }
106                
107                udpChannelFactory.start();
108        }
109
110        @Override
111        public int getHeartBeatPort() {
112                return 0;
113        }
114        
115        public boolean isUdpConnectRequest(Message connect) {
116                return connect.headerExists(UdpReceiverImpl.GDS_CLIENT_UPD_PORT);
117        }
118
119        public UdpReceiver newReceiver(AbstractChannel channel, HttpServletRequest request, Message connect) {
120                Number port = (Number)connect.getHeader(UdpReceiverImpl.GDS_CLIENT_UPD_PORT);
121                InetSocketAddress address = new InetSocketAddress(request.getRemoteAddr(), port.intValue());
122                
123                log.debug("Creating UDP receiver for channel id %s and address %s", channel.getId(), address);
124                
125                return new UdpReceiverImpl(udpChannelFactory.newUdpChannel(channel, address), connect);
126        }
127
128        public void stop() {
129                log.info("Stopping UDP Receiver Factory");
130
131                try {
132                        udpChannelFactory.stop();
133                }
134                finally {
135                        udpChannelFactory = null;
136                }
137        }
138}