1 22 package org.xsocket.datagram; 23 24 25 import java.io.IOException ; 26 import java.net.InetAddress ; 27 import java.net.InetSocketAddress ; 28 import java.net.SocketAddress ; 29 import java.util.Map ; 30 import java.util.concurrent.Executor ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 35 36 37 42 public final class ConnectedEndpoint extends AbstractChannelBasedEndpoint implements IConnectedEndpoint { 43 44 private static final Logger LOG = Logger.getLogger(ConnectedEndpoint.class.getName()); 45 46 private SocketAddress remoteAddress = null; 47 48 49 50 61 public ConnectedEndpoint(String host, int port) throws IOException { 62 this(new InetSocketAddress (host, port)); 63 } 64 65 66 67 70 public ConnectedEndpoint(String host, int port, DatagramSocketConfiguration socketConfiguration) throws IOException { 71 this(new InetSocketAddress (host, port), socketConfiguration); 72 } 73 74 75 84 public ConnectedEndpoint(SocketAddress remoteAddress) throws IOException { 85 this(remoteAddress, -1); 86 } 87 88 89 92 public ConnectedEndpoint(SocketAddress remoteAddress, DatagramSocketConfiguration socketConfiguration) throws IOException { 93 this(remoteAddress, socketConfiguration, -1); 94 } 95 96 97 98 109 public ConnectedEndpoint(SocketAddress remoteAddress, int receivePacketSize) throws IOException { 110 this(remoteAddress, receivePacketSize, null); 111 } 112 113 114 117 public ConnectedEndpoint(SocketAddress remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize) throws IOException { 118 this(remoteAddress, socketConfiguration, receivePacketSize, null); 119 } 120 121 122 134 public ConnectedEndpoint(String host, int port, int receivePacketSize) throws IOException { 135 this(new InetSocketAddress (host, port), new DatagramSocketConfiguration(), receivePacketSize, null); 136 } 137 138 139 142 public ConnectedEndpoint(String host, int port, DatagramSocketConfiguration socketConfiguration, int receivePacketSize) throws IOException { 143 this(new InetSocketAddress (host, port), socketConfiguration, receivePacketSize, null); 144 } 145 146 147 148 160 public ConnectedEndpoint(SocketAddress remoteAddress, int receivePacketSize, IDatagramHandler datagramHandler) throws IOException { 161 this(remoteAddress, new DatagramSocketConfiguration(), receivePacketSize, datagramHandler); 162 } 163 164 165 168 public ConnectedEndpoint(SocketAddress remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize, IDatagramHandler datagramHandler) throws IOException { 169 this(remoteAddress, socketConfiguration, receivePacketSize, datagramHandler, getGlobalWorkerPool()); 170 } 171 172 173 176 public ConnectedEndpoint(SocketAddress remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize, IDatagramHandler datagramHandler, Executor workerPool) throws IOException { 177 this(remoteAddress, socketConfiguration.toOptions(), receivePacketSize, datagramHandler, workerPool); 178 } 179 180 181 195 public ConnectedEndpoint(SocketAddress remoteAddress, Map <String , Object > options, int receivePacketSize, IDatagramHandler datagramHandler, Executor workerPool) throws IOException { 196 super(InetAddress.getLocalHost(), 0, options, datagramHandler, receivePacketSize, workerPool); 197 198 this.remoteAddress = remoteAddress; 199 getChannel().connect(remoteAddress); 200 } 201 202 203 @Override 204 public void send(UserDatagram packet) throws IOException { 205 if (LOG.isLoggable(Level.FINER)) { 206 if (packet.getRemoteSocketAddress() != null) { 207 LOG.fine("remote address of given packet is already set with " 208 + packet.getRemoteSocketAddress() + ". this value will be overriden by " 209 + remoteAddress); 210 } 211 } 212 213 packet.setRemoteAddress(remoteAddress); 214 super.send(packet); 215 } 216 217 218 219 222 public SocketAddress getRemoteSocketAddress() { 223 return remoteAddress; 224 } 225 226 227 230 protected ConnectedEndpoint setOption(String name, Object value) throws IOException { 231 return (ConnectedEndpoint) super.setOption(name, value); 232 } 233 } 234 | Popular Tags |