Permalink
Browse files

- moved createServerSocket from ConnectionMap --> Util

- using new method in ConnectionMap and FD_SOCK (https://issues.jboss.org/browse/JGRP-1288)
  • Loading branch information...
1 parent aa75349 commit 71edec906440b697069649e55efc8e26ac2bd4cc @belaban committed Feb 14, 2011
View
36 src/org/jgroups/blocks/TCPConnectionMap.java
@@ -84,7 +84,7 @@ public TCPConnectionMap(String service_name,
this.conn_expire_time = conn_expire_time;
if(socket_factory != null)
this.socket_factory=socket_factory;
- this.srv_sock=createServerSocket(service_name, srv_port, max_port);
+ this.srv_sock=Util.createServerSocket(socket_factory, service_name, bind_addr, srv_port, max_port);
if(external_addr != null)
local_addr=new IpAddress(external_addr, srv_sock.getLocalPort());
@@ -191,41 +191,7 @@ public void stop() {
}
}
- /**
- * Finds first available port starting at start_port and returns server
- * socket. Will not bind to port >end_port. Sets srv_port
- */
- protected ServerSocket createServerSocket(String service_name, int start_port, int end_port) throws Exception {
- ServerSocket ret=null;
- while(true) {
- try {
- if(bind_addr == null)
- ret=socket_factory.createServerSocket(service_name, start_port);
- else {
- // changed (bela Sept 7 2007): we accept connections on all NICs
- ret=socket_factory.createServerSocket(service_name, start_port, 20, bind_addr);
- }
- }
- catch(BindException bind_ex) {
- if(start_port == end_port)
- throw new BindException("No available port to bind to");
- if(bind_addr != null) {
- NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
- if(nic == null)
- log.warn("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
- }
- start_port++;
- continue;
- }
- catch(IOException io_ex) {
- if(log.isErrorEnabled())
- log.error("exception binding to " + bind_addr + "::" + start_port + ": " + io_ex);
- }
- break;
- }
- return ret;
- }
private void setSocketParameters(Socket client_sock) throws SocketException {
if(log.isTraceEnabled())
View
13 src/org/jgroups/protocols/FD_SOCK.java
@@ -287,8 +287,13 @@ public Object down(Event evt) {
case Event.CONNECT_WITH_STATE_TRANSFER:
case Event.CONNECT_USE_FLUSH:
case Event.CONNECT_WITH_STATE_TRANSFER_USE_FLUSH:
- Object ret=down_prot.down(evt);
- startServerSocket();
+ Object ret=down_prot.down(evt);
+ try {
+ startServerSocket();
+ }
+ catch(Exception e) {
+ throw new IllegalArgumentException("failed to created server socket", e);
+ }
return ret;
case Event.DISCONNECT:
@@ -552,9 +557,9 @@ void sendPingSignal(int signal) {
- void startServerSocket() {
+ void startServerSocket() throws Exception {
srv_sock=Util.createServerSocket(getSocketFactory(),
- Global.FD_SOCK_SRV_SOCK, bind_addr, start_port); // grab a random unused port above 10000
+ Global.FD_SOCK_SRV_SOCK, bind_addr, start_port, start_port+port_range); // grab a random unused port above 10000
srv_sock_addr=new IpAddress(bind_addr, srv_sock.getLocalPort());
if(srv_sock_handler != null) {
srv_sock_handler.start(); // won't start if already running
View
36 src/org/jgroups/util/Util.java
@@ -2989,6 +2989,42 @@ public static ServerSocket createServerSocket(SocketFactory factory, String serv
}
+ /**
+ * Finds first available port starting at start_port and returns server
+ * socket. Will not bind to port >end_port. Sets srv_port
+ */
+ public static ServerSocket createServerSocket(SocketFactory factory, String service_name, InetAddress bind_addr,
+ int start_port, int end_port) throws Exception {
+ ServerSocket ret=null;
+ int original_start_port=start_port;
+
+ while(true) {
+ try {
+ if(bind_addr == null)
+ ret=factory.createServerSocket(service_name, start_port);
+ else {
+ // changed (bela Sept 7 2007): we accept connections on all NICs
+ ret=factory.createServerSocket(service_name, start_port, 50, bind_addr);
+ }
+ }
+ catch(BindException bind_ex) {
+ if(start_port == end_port)
+ throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]");
+ if(bind_addr != null) {
+ NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr);
+ if(nic == null)
+ throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex);
+ }
+ start_port++;
+ continue;
+ }
+ break;
+ }
+ return ret;
+ }
+
+
+
/**
* Creates a DatagramSocket bound to addr. If addr is null, socket won't be bound. If address is already in use,

0 comments on commit 71edec9

Please sign in to comment.