Skip to content

Commit

Permalink
removing cling, adding wifi check to upnp check, moving upnp check to…
Browse files Browse the repository at this point in the history
… settings
  • Loading branch information
cyberb committed Feb 22, 2015
1 parent 81870c0 commit 708cf3d
Show file tree
Hide file tree
Showing 40 changed files with 291 additions and 935 deletions.
2 changes: 0 additions & 2 deletions android.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
Expand Down
4 changes: 2 additions & 2 deletions apps/apps.iml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
Expand Down
2 changes: 0 additions & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.3'
compile 'com.google.guava:guava:18.0'
compile 'log4j:log4j:1.2.17'
compile 'org.fourthline.cling:cling-core:2.0.1'
compile 'org.fourthline.cling:cling-support:2.0.1'
compile 'org.bitlet:weupnp:0.1.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.10'
Expand Down
10 changes: 2 additions & 8 deletions common/common.iml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
Expand All @@ -29,15 +29,9 @@
<orderEntry type="library" exported="" name="jackson-databind-2.3.3" level="project" />
<orderEntry type="library" exported="" name="guava-18.0" level="project" />
<orderEntry type="library" exported="" name="log4j-1.2.17" level="project" />
<orderEntry type="library" exported="" name="cling-core-2.0.1" level="project" />
<orderEntry type="library" exported="" name="cling-support-2.0.1" level="project" />
<orderEntry type="library" exported="" name="weupnp-0.1.2" level="project" />
<orderEntry type="library" exported="" name="jackson-annotations-2.3.0" level="project" />
<orderEntry type="library" exported="" name="jackson-core-2.3.3" level="project" />
<orderEntry type="library" exported="" name="seamless-util-1.1.0" level="project" />
<orderEntry type="library" exported="" name="seamless-http-1.1.0" level="project" />
<orderEntry type="library" exported="" name="seamless-xml-1.1.0" level="project" />
<orderEntry type="library" exported="" name="seamless-swing-1.1.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="junit-4.10" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-core-1.1" level="project" />
</component>
Expand Down
130 changes: 125 additions & 5 deletions common/src/main/java/org/syncloud/common/upnp/Router.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,133 @@
package org.syncloud.common.upnp;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;

public interface Router {
String getName();
import org.apache.log4j.Logger;
import org.bitlet.weupnp.GatewayDevice;
import org.bitlet.weupnp.PortMappingEntry;

Optional<String> getExternalIP();
import java.util.ArrayList;
import java.util.List;

int getPortMappingsCount();
import static com.google.common.collect.FluentIterable.from;

boolean canManipulatePorts(String myIp);
public class Router {

private Logger logger = Logger.getLogger(Router.class);

private GatewayDevice device;

public Router(GatewayDevice device) {
this.device = device;
}

public String getName() {
return device.getModelName();
}

public Optional<String> getExternalIP() {
try {
return Optional.of(device.getExternalIPAddress());
} catch (Exception e) {
logger.error("unable to get external address, " + e.getMessage());
}
return Optional.absent();
}

public int getPortMappingsCount() {
return getPortMappings().size();
}

private List<PortMappingEntry> getPortMappings() {
List<PortMappingEntry> mappings = new ArrayList<PortMappingEntry>();
int i = 0;
while (true) {

PortMappingEntry entry = new PortMappingEntry();
boolean exists = false;
try {
exists = device.getGenericPortMappingEntry(i, entry);
} catch (Exception e) {
logger.error("unable to get mapping: " + e.getMessage());
}

if (exists) {
mappings.add(entry);
} else {
break;
}

i++;
}
return mappings;
}

private Optional<Integer> getAvailableExternalPort() {
ImmutableList<Integer> ports = from(getPortMappings())
.transform(new Function<PortMappingEntry, Integer>() {
@Override
public Integer apply(PortMappingEntry input) {
return input.getExternalPort();
}
}).toList();

for (int i = 10000; i < 65536; i++) {
if (!ports.contains(i)) {
return Optional.of(i);
}
}

return Optional.absent();
}

public boolean canManipulatePorts(String myIp) {
final Optional<Integer> availableExternalPort = getAvailableExternalPort();
if(!availableExternalPort.isPresent())
return false;

Integer port = availableExternalPort.get();
logger.info("first available port: " + port);

PortMappingEntry portMapping = new PortMappingEntry();
portMapping.setExternalPort(port);
portMapping.setInternalClient(myIp);
portMapping.setProtocol("TCP");

try {
if(!device.addPortMapping(port, port, myIp, "TCP", "syncloud"))
return false;
} catch (Exception e) {
logger.error("unable to add mapping: " + port);
return false;
}

Optional<PortMappingEntry> foundAdded = findPortMapping(port);
if(!foundAdded.isPresent())
return false;

logger.debug("found added: " + foundAdded.get().getExternalPort());

try {
if(!device.deletePortMapping(port, "TCP"))
return false;
} catch (Exception e) {
logger.error("unable to add mapping: " + port);
return false;
}

return !findPortMapping(port).isPresent();
}

private Optional<PortMappingEntry> findPortMapping(final int externalPort) {
List<PortMappingEntry> portMappings = getPortMappings();
return from(portMappings).firstMatch(new Predicate<PortMappingEntry>() {
@Override
public boolean apply(PortMappingEntry input) {
return input.getExternalPort() == externalPort;
}
});
}
}
27 changes: 23 additions & 4 deletions common/src/main/java/org/syncloud/common/upnp/UPnP.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@

import com.google.common.base.Optional;

public interface UPnP {
Optional<? extends Router> find();
import org.apache.log4j.Logger;
import org.bitlet.weupnp.GatewayDevice;
import org.syncloud.common.upnp.weupnp.GatewayDiscover;

void start();
void shutdown();
public class UPnP {

private Logger logger = Logger.getLogger(UPnP.class);

static {
System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
}

public Optional<Router> find() {
GatewayDiscover discover = new GatewayDiscover();
try {
discover.discover();
GatewayDevice device = discover.getValidGateway();
if (device != null)
return Optional.of(new Router(device));
} catch (Exception e) {
logger.error("unable to find upnp router, " + e.getMessage());
}
return Optional.absent();
}
}
18 changes: 0 additions & 18 deletions common/src/main/java/org/syncloud/common/upnp/UPnPFactory.java

This file was deleted.

Loading

0 comments on commit 708cf3d

Please sign in to comment.