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.io.IOException; 038import java.net.InetSocketAddress; 039import java.nio.channels.DatagramChannel; 040 041import org.granite.gravity.AbstractChannel; 042import org.granite.gravity.udp.UdpReceiverFactory; 043import org.granite.logging.Logger; 044 045/** 046 * @author Franck WOLFF 047 */ 048public class UdpSharedNIOChannelFactory extends AbstractUdpChannelFactory { 049 050 private static final Logger log = Logger.getLogger(UdpSharedNIOChannelFactory.class); 051 052 private DatagramChannel channel = null; 053 054 public UdpSharedNIOChannelFactory(UdpReceiverFactory factory) { 055 super(factory); 056 } 057 058 @Override 059 public void start() { 060 super.start(); 061 062 int port = factory.getPort(); 063 064 if (port == 0) 065 log.info("Creating shared UDP channel on port * (any available port)..."); 066 else 067 log.info("Creating shared UDP channel on port %d...", port); 068 069 try { 070 channel = DatagramChannel.open(); 071 channel.socket().setSendBufferSize(factory.getSendBufferSize()); 072 channel.socket().bind(new InetSocketAddress(port)); 073 } 074 catch (Exception e) { 075 throw new RuntimeException("Could not create data channel on port: " + port, e); 076 } 077 078 log.info("UDP shared channel bound to port: %d", channel.socket().getLocalPort()); 079 } 080 081 @Override 082 public void stop() { 083 super.stop(); 084 085 log.info("Closing UDP channel..."); 086 087 if (channel != null) { 088 try { 089 channel.close(); 090 } 091 catch (IOException e) { 092 log.error(e, "Could not close UDP channel"); 093 } 094 finally { 095 channel = null; 096 } 097 } 098 099 log.info("UDP channel closed."); 100 } 101 102 @Override 103 public UdpChannel newUdpChannel(AbstractChannel gravityChannel, InetSocketAddress address) { 104 return new UdpNIOChannel(this, gravityChannel, channel, address); 105 } 106}