Skip to content

Commit

Permalink
Added favored port option
Browse files Browse the repository at this point in the history
  • Loading branch information
keenan-v1 committed Sep 8, 2018
1 parent bd9ec61 commit 53ad0ce
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Net Helper

[![Release](https://jitpack.io/v/xorith/nethelper.svg)](https://jitpack.io/#xorith/nethelper)
[![Build Status](https://travis-ci.org/xorith/nethelper.svg?branch=master)](https://travis-ci.org/xorith/nethelper)

Net Helper is a small library that provides helper methods to make networking in Java easier.

## 1.1.1 Changes ##
Added a favored port setting on ports. The helper will attempt to use this port first if possible before falling back on the other possible ports allowed for a given port object.

Add the dependency:
```xml
<dependency>
<groupId>coffee.wantsmore</groupId>
<artifactId>nethelper</artifactId>
<version>1.0</version>
</dependency>
```
## Usage ##

To use, follow the instructions under Set Me Up! on [Bintray](https://bintray.com/xorith/coffee/NetHelper#)

Example:
```java
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<description>A suite of helpful network tools, including upnp port mapping</description>
<url>https://www.github.com/xorith/nethelper</url>
<packaging>jar</packaging>
<version>1.1.0</version>
<version>1.1.1</version>

<licenses>
<license>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/coffee/keenan/network/helpers/port/Port.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class Port extends ErrorTracking
{
private final List<Integer> ports = new ArrayList<>();
private Integer favortedPort;
private final Protocol protocols;
private final InetAddress address;
private final Set<IPortValidator> validators = new HashSet<>();
Expand Down Expand Up @@ -39,6 +40,17 @@ public Port(final InetAddress address, final Protocol protocol)
}
}

public Port setFavoredPort(final int port)
{
this.favortedPort = port;
return this;
}

public Integer getFavoredPort()
{
return this.favortedPort;
}

public Port addPort(final int port)
{
this.ports.add(port);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/coffee/keenan/network/helpers/port/PortHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ public static Port assignPort(@NotNull final Port port)
return assignPort(port, new DefaultConfiguration());
}

public Port assignFavoredPort(@NotNull final Port port)
{
final Integer p = port.getFavoredPort();
if (p != null && validatePort(port.getAddress(), p, port.getValidators()))
port.setAssignedPort(p);
return port;
}

@SuppressWarnings("WeakerAccess")
public static Port assignPort(@NotNull final Port port, @NotNull final IConfiguration configuration)
{
final PortHelper portHelper = new PortHelper(configuration);
portHelper.assignFavoredPort(port);
if (port.getAssignedPort() != 0) return port;

for (final int p : port.getPorts())
{
if (portHelper.validatePort(port.getAddress(), p, port.getValidators()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,39 @@ public Exception getException()
assertEquals(o, p);
assertEquals(0, p.getAssignedPort());
}

@Test
public void assignFavoredPort()
{
Port o = new Port(InetAddress.getLoopbackAddress(), Protocol.TCP)
.setDescription("Test")
.addPortRange(1000, 1200)
.setFavoredPort(1234)
.toMap();
Port p = PortHelper.assignPort(o);
assertEquals(o, p);
assertEquals(1234, p.getAssignedPort());
o = new Port(InetAddress.getLoopbackAddress(), Protocol.TCP)
.setDescription("Test")
.addPortRange(1000, 1200)
.setFavoredPort(1234)
.toMap()
.addValidators(new IPortValidator()
{
@Override
public boolean validate(InetAddress address, IConfiguration configuration, int port)
{
return false;
}

@Override
public Exception getException()
{
return new Exception("failure");
}
});
p = PortHelper.assignPort(o);
assertEquals(o, p);
assertEquals(0, p.getAssignedPort());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

public class PortTest
{
@Test
public void addFavoredPort()
{
Port p = new Port(InetAddress.getLoopbackAddress(), Protocol.TCP);
assertNull(p.getFavoredPort());
p.setFavoredPort(1234);
assertNotNull(p.getFavoredPort());
assertEquals(1234, p.getFavoredPort().intValue());
}

@Test
public void addPort()
Expand Down

0 comments on commit 53ad0ce

Please sign in to comment.