Skip to content

Commit 0d89b09

Browse files
committed
Full alpha functionality
1 parent da107df commit 0d89b09

27 files changed

+206
-466
lines changed

src/main/java/org/ros/RosCore.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ public static RosCore newPublic(int port) {
5050
return new RosCore(BindAddress.newPublic(port), AdvertiseAddress.newPublic(port));
5151
}
5252

53-
public static RosCore newPrivate(int port) {
54-
return new RosCore(BindAddress.newPrivate(port), AdvertiseAddress.newPrivate(port));
55-
}
5653

5754
public static RosCore newPrivate() {
5855
BindAddress ba = BindAddress.newPrivate();
59-
return new RosCore(ba, AdvertiseAddress.newPrivate(ba.toInetSocketAddress().getPort()));
56+
return new RosCore(ba, AdvertiseAddress.newPrivate());
6057
}
6158

6259
private RosCore(BindAddress bindAddress, AdvertiseAddress advertiseAddress) {

src/main/java/org/ros/address/AdvertiseAddress.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.ros.exception.RosRuntimeException;
2121

22+
import java.io.Serializable;
2223
import java.net.InetAddress;
2324
import java.net.InetSocketAddress;
2425
import java.net.URI;
@@ -34,16 +35,19 @@
3435
* the advertised port.
3536
*
3637
* @author damonkohler@google.com (Damon Kohler)
38+
* @author jg
3739
*/
38-
public class AdvertiseAddress {
39-
40-
private final String host;
41-
private final int port;
40+
public class AdvertiseAddress implements Serializable {
41+
private static final long serialVersionUID = -8488174967781048482L;
42+
private String host;
43+
private int port;
4244

4345
private Callable<Integer> portCallable;
4446

45-
public static AdvertiseAddress newPrivate(int port) {
46-
return new PrivateAdvertiseAddressFactory().newDefault(port);
47+
public AdvertiseAddress() { }
48+
49+
public static AdvertiseAddress newPrivate() {
50+
return new PrivateAdvertiseAddressFactory().newDefault();
4751
}
4852

4953
/**
@@ -63,6 +67,21 @@ public AdvertiseAddress(String host, final int port) {
6367
this.port = port;
6468
}
6569

70+
public AdvertiseAddress(InetAddress host, int port) {
71+
this.host = host.getCanonicalHostName();
72+
this.port = port;
73+
}
74+
75+
public AdvertiseAddress(String loopback) {
76+
InetSocketAddress addr = InetSocketAddressFactory.newFromHostString(loopback);
77+
this.host = addr.getHostName();
78+
this.port = addr.getPort();
79+
}
80+
81+
public AdvertiseAddress(InetSocketAddress addr) {
82+
this.host = addr.getHostName();
83+
this.port = addr.getPort();
84+
}
6685

6786
public String getHost() {
6887
return host;

src/main/java/org/ros/address/AdvertiseAddressFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
*/
2222
public interface AdvertiseAddressFactory {
2323

24-
AdvertiseAddress newDefault(int port);
24+
AdvertiseAddress newDefault();
2525
}

src/main/java/org/ros/address/BindAddress.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
package org.ros.address;
1818

19+
import java.io.IOException;
20+
import java.net.InetAddress;
1921
import java.net.InetSocketAddress;
20-
import java.util.concurrent.atomic.AtomicInteger;
22+
import java.net.ServerSocket;
23+
24+
import org.ros.exception.RosRuntimeException;
2125

2226
/**
2327
* A wrapper for {@link InetSocketAddress} that emphasizes the difference
@@ -29,7 +33,6 @@
2933
public class BindAddress {
3034

3135
private final InetSocketAddress address;
32-
private final static AtomicInteger portSelector = new AtomicInteger(8090);
3336

3437
private BindAddress(InetSocketAddress address) {
3538
this.address = address;
@@ -41,11 +44,16 @@ private BindAddress(InetSocketAddress address) {
4144
* to all network interfaces on the host
4245
*/
4346
public static BindAddress newPublic(int port) {
44-
return new BindAddress(new InetSocketAddress(port));
47+
return new BindAddress(new InetSocketAddress(InetSocketAddressFactory.newNonLoopback(), port));
4548
}
4649

4750
public static BindAddress newPublic() {
48-
return newPublic(portSelector.getAndAdd(2));
51+
InetAddress host = InetSocketAddressFactory.newNonLoopback();
52+
try {
53+
return new BindAddress(new InetSocketAddress(host, findPortOnAddress(host)));
54+
} catch (IOException e) {
55+
throw new RosRuntimeException(e);
56+
}
4957
}
5058

5159
/**
@@ -58,7 +66,7 @@ public static BindAddress newPrivate(int port) {
5866
}
5967

6068
public static BindAddress newPrivate() {
61-
return newPrivate(portSelector.getAndAdd(2));
69+
return new BindAddress(InetSocketAddressFactory.newLoopback());
6270
}
6371

6472
@Override
@@ -89,5 +97,12 @@ public boolean equals(Object obj) {
8997
} else if (!address.equals(other.address)) return false;
9098
return true;
9199
}
92-
100+
101+
private static Integer findPortOnAddress(InetAddress host) throws IOException {
102+
try (
103+
ServerSocket socket = new ServerSocket(0,0,host);
104+
) {
105+
return socket.getLocalPort();
106+
}
107+
}
93108
}

src/main/java/org/ros/address/InetSocketAddressFactory.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import org.ros.exception.RosRuntimeException;
2020

21+
import java.io.IOException;
2122
import java.net.InetAddress;
2223
import java.net.InetSocketAddress;
2324
import java.net.NetworkInterface;
25+
import java.net.ServerSocket;
2426
import java.net.SocketException;
2527
import java.net.UnknownHostException;
2628
import java.util.ArrayList;
@@ -36,7 +38,6 @@
3638
* @author jg
3739
*/
3840
public class InetSocketAddressFactory {
39-
private static AtomicInteger portSelector = new AtomicInteger(8090); // add and get by 2 each call, so first is 8090
4041

4142
private InetSocketAddressFactory() {
4243
// Utility class
@@ -101,40 +102,17 @@ private static Collection<InetAddress> getAllInetAddressByName(String host) {
101102
* If the specified host name is {@code Address.LOCALHOST}, this method
102103
* returns a loopback address.
103104
*
104-
* In all cases the port is incremented by 2 each new address for a master and parameter server port
105-
* combination;
106105
*
107106
* @param host
108107
* @return an {@link InetAddress} with both an IP and a host set (no further resolving will take place)
109108
*/
110109
public static InetSocketAddress newFromHostString(String host) {
111-
try {
112-
InetAddress target = null;
113-
if (InetAddress.getByName(host) != null) {
114-
return new InetSocketAddress(InetAddress.getByAddress(host, InetAddress.getByName(host).getAddress()),
115-
portSelector.addAndGet(2));
116-
}
117-
if (host.equals(Address.LOCALHOST)) {
118-
return new InetSocketAddress(InetAddress.getByAddress(Address.LOCALHOST, InetAddress.getByName(Address.LOOPBACK).getAddress()),
119-
portSelector.addAndGet(2));
120-
}
121-
} catch (UnknownHostException e) {
122-
throw new RosRuntimeException(e);
123-
}
124-
Collection<InetAddress> allAddressesByName = getAllInetAddressByName(host);
125-
// First, try to find a non-loopback IPv4 address.
126-
for (InetAddress address : allAddressesByName) {
127-
if (!address.isLoopbackAddress() && isIpv4(address)) {
128-
return new InetSocketAddress(address, portSelector.addAndGet(2));
129-
}
130-
}
131-
// Return a loopback IPv4 address as a last resort.
132-
for (InetAddress address : allAddressesByName) {
133-
if (isIpv4(address)) {
134-
return new InetSocketAddress(address, portSelector.addAndGet(2));
135-
}
136-
}
137-
throw new RosRuntimeException("Unable to construct InetAddress for host: " + host);
110+
try {
111+
InetAddress hostaddr = InetAddress.getByName(host);
112+
return new InetSocketAddress(hostaddr, findPortOnAddress(hostaddr));
113+
} catch (IOException e) {
114+
throw new RosRuntimeException(e);
115+
}
138116
}
139117

140118
public static InetSocketAddress newLoopback() {
@@ -144,4 +122,12 @@ public static InetSocketAddress newLoopback() {
144122
public static InetSocketAddress newLoopback(int port) {
145123
return new InetSocketAddress(Address.LOOPBACK, port);
146124
}
125+
126+
private static Integer findPortOnAddress(InetAddress host) throws IOException {
127+
try (
128+
ServerSocket socket = new ServerSocket(0,0,host);
129+
) {
130+
return socket.getLocalPort();
131+
}
132+
}
147133
}

src/main/java/org/ros/address/PrivateAdvertiseAddressFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public class PrivateAdvertiseAddressFactory implements AdvertiseAddressFactory {
2323

2424
@Override
25-
public AdvertiseAddress newDefault(int port) {
26-
return new AdvertiseAddress(Address.LOOPBACK, port);
25+
public AdvertiseAddress newDefault() {
26+
return new AdvertiseAddress(Address.LOOPBACK);
2727
}
2828
}

src/main/java/org/ros/address/PublicAdvertiseAddressFactory.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,45 @@
1616

1717
package org.ros.address;
1818

19+
import java.net.InetAddress;
20+
import java.net.InetSocketAddress;
21+
import java.net.UnknownHostException;
22+
23+
import org.ros.exception.RosRuntimeException;
24+
1925
/**
2026
* An {@link AdvertiseAddressFactory} which creates public (non-loopback) addresses.
2127
*
2228
* @author damonkohler@google.com (Damon Kohler)
29+
* @author jg
2330
*/
2431
public class PublicAdvertiseAddressFactory implements AdvertiseAddressFactory {
2532

26-
private final String host;
27-
33+
private final InetAddress host;
2834

2935
public PublicAdvertiseAddressFactory() {
30-
this(InetSocketAddressFactory.newNonLoopback().getCanonicalHostName());
36+
this(InetSocketAddressFactory.newNonLoopback());
3137
}
3238

33-
public PublicAdvertiseAddressFactory(String host) {
39+
public PublicAdvertiseAddressFactory(InetAddress host) {
3440
this.host = host;
3541
}
3642

43+
public PublicAdvertiseAddressFactory(String host) {
44+
try {
45+
this.host = InetAddress.getByName(host);
46+
} catch (UnknownHostException e) {
47+
throw new RosRuntimeException("Host name "+host+" cannot be resolved");
48+
}
49+
}
3750

38-
@Override
3951
public AdvertiseAddress newDefault(int port) {
4052
return new AdvertiseAddress(host, port);
4153
}
54+
55+
@Override
56+
public AdvertiseAddress newDefault() {
57+
InetSocketAddress isa = InetSocketAddressFactory.newLoopback();
58+
return new AdvertiseAddress(isa);
59+
}
4260
}

src/main/java/org/ros/internal/loader/CommandLineLoader.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
/**
4343
* Create {@link NodeConfiguration} instances using a ROS command-line and
44-
* environment specification.
44+
* environment specification. When starting a node through RosRun, this class is used
45+
* to process the command line remappings.
4546
*
4647
* @author kwc@willowgarage.com (Ken Conley)
4748
* @author damonkohler@google.com (Damon Kohler)
@@ -115,7 +116,7 @@ public List<String> getNodeArguments() {
115116
public NodeConfiguration build() {
116117
parseRemappingArguments();
117118
// TODO(damonkohler): Add support for starting up a private node.
118-
NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(getHost(), getPort());
119+
NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(getHost(), getMasterUri());
119120
nodeConfiguration.setParentResolver(buildParentResolver());
120121
nodeConfiguration.setRosRoot(getRosRoot());
121122
nodeConfiguration.setRosPackagePath(getRosPackagePath());
@@ -171,7 +172,7 @@ private NameResolver buildParentResolver() {
171172
* </ol>
172173
*/
173174
private String getHost() {
174-
String host = InetSocketAddressFactory.newLoopback().getAddress().getHostAddress();
175+
String host = InetSocketAddressFactory.newLoopback().getAddress().getCanonicalHostName();
175176
if (specialRemappings.containsKey(CommandLineVariables.ROS_IP)) {
176177
host = specialRemappings.get(CommandLineVariables.ROS_IP);
177178
} else if (environment.containsKey(EnvironmentVariables.ROS_IP)) {
@@ -182,17 +183,6 @@ private String getHost() {
182183
return host;
183184
}
184185

185-
private int getPort() {
186-
int port = InetSocketAddressFactory.newLoopback().getPort();
187-
if (specialRemappings.containsKey(CommandLineVariables.ROS_IP_PORT)) {
188-
port = Integer.valueOf(specialRemappings.get(CommandLineVariables.ROS_IP_PORT));
189-
} else if (environment.containsKey(EnvironmentVariables.ROS_IP_PORT)) {
190-
port = Integer.valueOf(environment.get(EnvironmentVariables.ROS_IP_PORT));
191-
} else if (environment.containsKey(EnvironmentVariables.ROS_HOSTPORT)) {
192-
port = Integer.valueOf(environment.get(EnvironmentVariables.ROS_HOSTPORT));
193-
}
194-
return port;
195-
}
196186
/**
197187
* Precedence:
198188
*

src/main/java/org/ros/internal/node/DefaultNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class DefaultNode implements ConnectedNode {
119119
* {@link DefaultNodeFactory}.
120120
*
121121
* @param nodeConfiguration
122-
* the {@link NodeConfiguration} for this {@link Node}
122+
* the {@link NodeConfiguration} for this {@link Node}, rpc bind/advertise and tcpros bind/advertise from here
123123
* @param nodeListeners
124124
* a {@link Collection} of {@link NodeListener}s that will be added
125125
* to this {@link Node} before it starts

src/main/java/org/ros/internal/node/response/InetAddressListResultFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ public class InetAddressListResultFactory implements ResultFactory<List<InetAddr
3333

3434
@Override
3535
public List<InetAddress> newFromValue(Object value) {
36+
/*
3637
List<Object> values = Arrays.asList(value);
3738
List<InetAddress> uris = new ArrayList<InetAddress>();
3839
for (Object uri : values) {
3940
try {
40-
uris.add(InetAddress.getByName((String) uri));
41+
uris.add( InetAddress.getByName((String)uri));
4142
} catch (UnknownHostException e) {
4243
throw new RosRuntimeException(e);
4344
}
4445
}
4546
return uris;
47+
*/
48+
return (List<InetAddress>)value;
4649
}
4750
}

0 commit comments

Comments
 (0)