本文整理汇总了Java中org.bitlet.weupnp.GatewayDiscover.discover方法的典型用法代码示例。如果您正苦于以下问题:Java GatewayDiscover.discover方法的具体用法?Java GatewayDiscover.discover怎么用?Java GatewayDiscover.discover使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bitlet.weupnp.GatewayDiscover
的用法示例。
在下文中一共展示了GatewayDiscover.discover方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.bitlet.weupnp.GatewayDiscover; //导入方法依赖的package包/类
/**
* Initialize the UPnP support
*/
private static void init() {
initDone = true;
//
// Discover the gateway devices on the local network
//
try {
Logger.logInfoMessage("Looking for UPnP gateway device...");
GatewayDiscover discover = new GatewayDiscover();
Map<InetAddress, GatewayDevice> gatewayMap = discover.discover();
if (gatewayMap == null || gatewayMap.isEmpty()) {
Logger.logDebugMessage("There are no UPnP gateway devices");
} else {
gatewayMap.forEach((addr, device) ->
Logger.logDebugMessage("UPnP gateway device found on " + addr.getHostAddress()));
gateway = discover.getValidGateway();
if (gateway == null) {
Logger.logDebugMessage("There is no connected UPnP gateway device");
} else {
localAddress = gateway.getLocalAddress();
externalAddress = InetAddress.getByName(gateway.getExternalIPAddress());
Logger.logDebugMessage("Using UPnP gateway device on " + localAddress.getHostAddress());
Logger.logInfoMessage("External IP address is " + externalAddress.getHostAddress());
}
}
} catch (Exception exc) {
Logger.logErrorMessage("Unable to discover UPnP gateway devices: " + exc.toString());
}
}
示例2: run
import org.bitlet.weupnp.GatewayDiscover; //导入方法依赖的package包/类
/**
* args:<br>
* [0] - port number
* @param args
* @throws Exception
*/
public void run(String[] args) throws Exception{
PORT = Integer.parseInt(args[0]);
boolean LIST_ALL_MAPPINGS = false;
addLogLine("Starting weupnp");
GatewayDiscover gatewayDiscover = new GatewayDiscover();
addLogLine("Looking for Gateway Devices...");
Map<InetAddress, GatewayDevice> gateways = gatewayDiscover.discover();
if (gateways.isEmpty()) {
addLogLine("No gateways found");
addLogLine("Stopping weupnp");
return;
}
addLogLine(gateways.size()+" gateway(s) found\n");
int counter=0;
for (GatewayDevice gw: gateways.values()) {
counter++;
addLogLine("Listing gateway details of device #" + counter+
"\n\tFriendly name: " + gw.getFriendlyName()+
"\n\tPresentation URL: " + gw.getPresentationURL()+
"\n\tModel name: " + gw.getModelName()+
"\n\tModel number: " + gw.getModelNumber()+
"\n\tLocal interface address: " + gw.getLocalAddress().getHostAddress()+"\n");
}
// Choose the first active gateway for the tests
activeGW = gatewayDiscover.getValidGateway();
if (null != activeGW) {
addLogLine("Using gateway: " + activeGW.getFriendlyName());
} else {
addLogLine("No active gateway device found");
addLogLine("Stopping weupnp");
return;
}
/* get local and external IPs*/
InetAddress localAddress = activeGW.getLocalAddress();
localIPAddress = activeGW.getLocalAddress().toString();
addLogLine("Using local address: "+ localAddress.getHostAddress());
externalIPAddress = activeGW.getExternalIPAddress();
addLogLine("External address: "+ externalIPAddress);
/* Check if port is mapped */
addLogLine("Attempting to map port " + PORT);
PortMappingEntry portMapping = new PortMappingEntry();
if (activeGW.getSpecificPortMappingEntry(PORT,"TCP",portMapping)) {
addLogLine("Port " + PORT + " is already mapped. Aborting test.");
return;
} else {
addLogLine("Mapping free. Sending port mapping request for port " + PORT);
if (activeGW.addPortMapping(PORT, PORT,localAddress.getHostAddress(), "TCP", "BTCAuthenticator_Mapping")) {
addLogLine("Mapping SUCCESSFUL");
}
else
addLogLine("Port mapping attempt failed");
}
}
示例3: enableNATInboundConnections
import org.bitlet.weupnp.GatewayDiscover; //导入方法依赖的package包/类
protected void enableNATInboundConnections() throws Exception {
GatewayDiscover discover = new GatewayDiscover();
LOGGER.debug("Looking for gateway devices");
discover.discover();
final GatewayDevice natDevice = discover.getValidGateway();
if (null != natDevice) {
LOGGER.debug("Gateway device found {} {} ", natDevice.getModelName(), natDevice.getModelDescription());
} else {
LOGGER.debug("No valid gateway device found, doing nothing.");
return;
}
// String externalIPAddress = natDevice.getExternalIPAddress();
// LOGGER.debug("Our external address is {}", externalIPAddress);
// server1.setListeningAddress(externalIPAddress);
final int localPortToMap=getListeningPort();
LOGGER.debug("Querying device to see if a mapping for port {} already exists", localPortToMap);
PortMappingEntry portMapping = new PortMappingEntry();
if (natDevice.getSpecificPortMappingEntry(localPortToMap, "TCP", portMapping)) {
LOGGER.error("Port {} was already mapped by {}. Aborting...", localPortToMap, portMapping.getPortMappingDescription()); //TODO Maybe we should retry with another port number. Do not forget to update the local gossip info with the new port
} else {
LOGGER.debug("External port {} is available, sending mapping request", localPortToMap);
InetAddress localAddress = natDevice.getLocalAddress();
if (natDevice.addPortMapping(localPortToMap, localPortToMap, localAddress.getHostAddress(), "TCP", getClass().getName())) {
LOGGER.info("Port mapping successfull");
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
try {
LOGGER.debug("deleting port mapping {}", localPortToMap);
natDevice.deletePortMapping(localPortToMap, "TCP");
} catch (Exception e) {
e.printStackTrace();
}
};
});
} else {
LOGGER.error("Port mapping failed");
}
}
}
示例4: test
import org.bitlet.weupnp.GatewayDiscover; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
GatewayDiscover discover = new GatewayDiscover();
System.out.println("Looking for Gateway Devices");
discover.discover();
GatewayDevice d = discover.getValidGateway();
if (null != d) {
System.out.println("Gateway device found "+d.getModelName()+" " +d.getModelDescription());
} else {
System.out.println("No valid gateway device found.");
return;
}
InetAddress localAddress = d.getLocalAddress();
System.out.println("Using local address: "+ localAddress);
String externalIPAddress = d.getExternalIPAddress();
System.out.println("External address: "+ externalIPAddress);
PortMappingEntry portMapping = new PortMappingEntry();
System.out.println("Attempting to map port {0}" + SAMPLE_PORT);
System.out.println("Querying device to see if mapping for port {0} already exists"+ SAMPLE_PORT);
if (!d.getSpecificPortMappingEntry(SAMPLE_PORT, "TCP", portMapping)) {
System.out.println("Sending port mapping request");
if (d.addPortMapping(SAMPLE_PORT, SAMPLE_PORT, localAddress.getHostAddress(), "TCP", "libminiupnpc")) {
System.out.println("Mapping succesful: waiting {0} seconds before removing mapping." + WAIT_TIME);
Thread.sleep(1000 * WAIT_TIME);
d.deletePortMapping(SAMPLE_PORT, "TCP");
System.out.println("Port mapping removed");
System.out.println("Test SUCCESSFUL");
} else {
System.out.println("Port mapping failed");
fail();
}
} else {
System.out.println("Port was already mapped. Aborting test.");
fail();
}
System.out.println("Stopping weupnp");
}