KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > datagram > ConnectedEndpoint


1 // $Id: ConnectedEndpoint.java 1546 2007-07-23 06:07:56Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.datagram;
23
24
25 import java.io.IOException JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.SocketAddress JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.concurrent.Executor JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34
35
36
37 /**
38  * connected endpoint implementation
39  *
40  * @author grro@xsocket.org
41  */

42 public final class ConnectedEndpoint extends AbstractChannelBasedEndpoint implements IConnectedEndpoint {
43     
44     private static final Logger JavaDoc LOG = Logger.getLogger(ConnectedEndpoint.class.getName());
45     
46     private SocketAddress JavaDoc remoteAddress = null;
47     
48     
49     
50     /**
51      * Constructs a <i>client/server</i> datagram socket and binds it to any
52      * available port on the local host machine. The socket
53      * will be bound to the wildcard address, an IP address
54      * chosen by the kernel. The local socket will be connected
55      * to the server by using the passed over addresses
56      *
57      * @param host the remote host
58      * @param port the remote port
59      * @throws IOException If some I/O error occurs
60      */

61     public ConnectedEndpoint(String JavaDoc host, int port) throws IOException JavaDoc {
62         this(new InetSocketAddress JavaDoc(host, port));
63     }
64
65     
66     
67     /**
68      * @deprecated
69      */

70     public ConnectedEndpoint(String JavaDoc host, int port, DatagramSocketConfiguration socketConfiguration) throws IOException JavaDoc {
71         this(new InetSocketAddress JavaDoc(host, port), socketConfiguration);
72     }
73     
74         
75     /**
76      * Constructs a <i>client/server</i> datagram socket and binds it to any
77      * available port on the local host machine. The socket
78      * will be bound to the wildcard address, an IP address
79      * chosen by the kernel. The local socket will be connected
80      * to the server by using the passed over addresses
81      *
82      * @throws IOException If some I/O error occurs
83      */

84     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress) throws IOException JavaDoc {
85         this(remoteAddress, -1);
86     }
87     
88     
89     /**
90      * @deprecated
91      */

92     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, DatagramSocketConfiguration socketConfiguration) throws IOException JavaDoc {
93         this(remoteAddress, socketConfiguration, -1);
94     }
95
96     
97
98     /**
99      * Constructs a <i>client/server</i> datagram socket and binds it to the given
100      * available port on the local host machine. The socket
101      * will be bound to the wildcard address, an IP address
102      * chosen by the kernel. The local socket will be connected
103      * to the server by using the passed over addresses
104      *
105      * @param remoteAddress the remote socket address
106      * @param receivePacketSize the receive packet size
107      * @throws IOException If some I/O error occurs
108      */

109     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, int receivePacketSize) throws IOException JavaDoc {
110         this(remoteAddress, receivePacketSize, null);
111     }
112     
113     
114     /**
115      * @deprecated
116      */

117     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize) throws IOException JavaDoc {
118         this(remoteAddress, socketConfiguration, receivePacketSize, null);
119     }
120     
121     
122     /**
123      * Constructs a <i>client/server</i> datagram socket and binds it to the given
124      * available port on the local host machine. The socket
125      * will be bound to the wildcard address, an IP address
126      * chosen by the kernel. The local socket will be connected
127      * to the server by using the passed over addresses
128      *
129      * @param host the remote host
130      * @param port the remote port
131      * @param receivePacketSize the receive packet size
132      * @throws IOException If some I/O error occurs
133      */

134     public ConnectedEndpoint(String JavaDoc host, int port, int receivePacketSize) throws IOException JavaDoc {
135         this(new InetSocketAddress JavaDoc(host, port), new DatagramSocketConfiguration(), receivePacketSize, null);
136     }
137     
138     
139     /**
140      * @deprecated
141      */

142     public ConnectedEndpoint(String JavaDoc host, int port, DatagramSocketConfiguration socketConfiguration, int receivePacketSize) throws IOException JavaDoc {
143         this(new InetSocketAddress JavaDoc(host, port), socketConfiguration, receivePacketSize, null);
144     }
145     
146     
147     
148     /**
149      * Constructs a <i>client/server</i> datagram socket and binds it to the given
150      * available port on the local host machine. The socket
151      * will be bound to the wildcard address, an IP address
152      * chosen by the kernel. The local socket will be connected
153      * to the server by using the passed over addresses
154      *
155      * @param remoteAddress the remote socket address
156      * @param receivePacketSize the receive packet size
157      * @param datagramHandler the datagram handler
158      * @throws IOException If some I/O error occurs
159      */

160     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, int receivePacketSize, IDatagramHandler datagramHandler) throws IOException JavaDoc {
161         this(remoteAddress, new DatagramSocketConfiguration(), receivePacketSize, datagramHandler);
162     }
163     
164
165     /**
166      * @deprecated
167      */

168     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize, IDatagramHandler datagramHandler) throws IOException JavaDoc {
169         this(remoteAddress, socketConfiguration, receivePacketSize, datagramHandler, getGlobalWorkerPool());
170     }
171
172     
173     /**
174      * @deprecated
175      */

176     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, DatagramSocketConfiguration socketConfiguration, int receivePacketSize, IDatagramHandler datagramHandler, Executor JavaDoc workerPool) throws IOException JavaDoc {
177         this(remoteAddress, socketConfiguration.toOptions(), receivePacketSize, datagramHandler, workerPool);
178     }
179     
180     
181     /**
182      * Constructs a <i>client/server</i> datagram socket and binds it to the given
183      * available port on the local host machine. The socket
184      * will be bound to the wildcard address, an IP address
185      * chosen by the kernel. The local socket will be connected
186      * to the server by using the passed over addresses
187      *
188      * @param remoteAddress the remote socket address
189      * @param socketOptions the socket options
190      * @param receivePacketSize the receive packet size
191      * @param datagramHandler the datagram handler
192      * @param workerPool the worker pool
193      * @throws IOException If some I/O error occurs
194      */

195     public ConnectedEndpoint(SocketAddress JavaDoc remoteAddress, Map JavaDoc<String JavaDoc, Object JavaDoc> options, int receivePacketSize, IDatagramHandler datagramHandler, Executor JavaDoc workerPool) throws IOException JavaDoc {
196         super(InetAddress.getLocalHost(), 0, options, datagramHandler, receivePacketSize, workerPool);
197         
198         this.remoteAddress = remoteAddress;
199         getChannel().connect(remoteAddress);
200     }
201     
202     
203     @Override JavaDoc
204     public void send(UserDatagram packet) throws IOException JavaDoc {
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     /**
220      * {@inheritDoc}
221      */

222     public SocketAddress JavaDoc getRemoteSocketAddress() {
223         return remoteAddress;
224     }
225     
226     
227     /**
228      * {@inheritDoc}
229      */

230     protected ConnectedEndpoint setOption(String JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
231         return (ConnectedEndpoint) super.setOption(name, value);
232     }
233 }
234
Popular Tags