upnp Device-Discovery im Netzwerk

Status
Nicht offen für weitere Antworten.

Mufasa

Mitglied
Hallo alle zusammen, ich habe mich eben als blutjunger Anfänger hier angemeldet und hoffe Ihr könnte mir bei meinem Riesenproblem etwas helfen. Also ich habe die Aufgabenstellung ein Programm zu schreiben, dass alle upnp-Geräte im Netzwerk erkennt und auflistet. Netzwerkprogrammierung hab ich bisher nur mit VB6 gemacht und dafür hab ich nichts gefunden.
Folgendes JAVA-Programm hab ich gefunden, nur weiß ich jetzt nicht so ganz wie ich es zum laufen bringen kann (Eclipse hab ich heruntergeladen).

Java:
package net.sbbi.upnp;

import java.net.*;
import java.io.*;
import java.util.*;

import net.sbbi.upnp.devices.*;
import org.apache.commons.logging.*;

/**
 * Class to discover an UPNP device on the network.</br>
 * A multicast socket will be created to discover devices, the binding port for this socket is set to 1901,
 * if this is causing a problem you can use the net.sbbi.upnp.Discovery.bindPort system property 
 * to specify another port.
 * The discovery methods only accept matching device description and broadcast message response IP
 * to avoid a security flaw with the protocol. If you are not happy with such behaviour
 * you can set the net.sbbi.upnp.ddos.matchip system property to false to avoid this check.
 * @author <a href="mailto:superbonbon@sbbi.net">SuperBonBon</a>
 * @version 1.0
 */

public class Discovery {

  private final static Log log = LogFactory.getLog( Discovery.class );

  public final static String ROOT_DEVICES = "upnp:rootdevice";
  public final static String ALL_DEVICES = "ssdp:all";

  public static final int DEFAULT_MX = 3;
  public static final int DEFAULT_TTL = 4;
  public static final int DEFAULT_TIMEOUT = 1500;
  public static final String DEFAULT_SEARCH = ALL_DEVICES;
  public static final int DEFAULT_SSDP_SEARCH_PORT = 1901;
  
  public final static String SSDP_IP = "239.255.255.250";
  public final static int SSDP_PORT = 1900;
  
  /**
   * Devices discovering on all network interfaces with default values, all root devices will be searched
   * @return an array of UPNP Root device or null if nothing found with the default timeout.
   *         Null does NOT means that no UPNP device is available on the network. It only means
   *         that for this default timeout no devices responded or that effectively no devices
   *         are available at all.
   * @throws IOException if some IOException occurs during discovering
   */
  public static UPNPRootDevice[] discover() throws IOException {
    return discover( DEFAULT_TIMEOUT, DEFAULT_TTL, DEFAULT_MX, DEFAULT_SEARCH );
  }
  
  /**
   * Devices discovering on all network interfaces with a given root device to search
   * @param searchTarget the device URI to search
   * @return an array of UPNP Root device that matches the search or null if nothing found with the default timeout.
   *         Null does NOT means that no UPNP device is available on the network. It only means
   *         that for this given timeout no devices responded or that effectively no devices
   *         are available at all.
   * @throws IOException if some IOException occurs during discovering
   */
  public static UPNPRootDevice[] discover( String searchTarget ) throws IOException {
    return discover( DEFAULT_TIMEOUT, DEFAULT_TTL, DEFAULT_MX, searchTarget );
  }

  /**
   * Devices discovering on all network interfaces with a given timeout and a given root device to search
   * @param timeOut the time allowed for a device to give a response
   * @param searchTarget the device URI to search
   * @return an array of UPNP Root device that matches the search or null if nothing found with the given timeout.
   *         Null does NOT means that no UPNP device is available on the network. It only means
   *         that for this given timeout no devices responded or that effectively no devices
   *         are available at all.
   * @throws IOException if some IOException occurs during discovering
   */
  public static UPNPRootDevice[] discover( int timeOut, String searchTarget ) throws IOException {
    return discover( timeOut, DEFAULT_TTL, DEFAULT_MX, searchTarget );
  }

  /**
   * Devices discovering on all network interfaces with a given timeout and a given root device to search, as well as a ttl and mx param
   * @param timeOut the timeout for the a device to give a reponse
   * @param ttl the UDP socket packets time to live
   * @param mx discovery message mx http header field value
   * @param searchTarget the device URI to search
   * @return an array of UPNP Root device that matches the search or null if nothing found within the given timeout.
   *         Null return does NOT means that no UPNP device is available on the network. It only means
   *         that for this given timeout no devices responded or that effectively no devices
   *         are available at all.
   * @throws IOException if some IOException occurs during discovering
   */
  public static UPNPRootDevice[] discover( int timeOut, int ttl, int mx, String searchTarget ) throws IOException {
    return discoverDevices( timeOut, ttl, mx, searchTarget, null );
  }
  
  /**
   * Devices discovering with a given timeout and a given root device to search on an given network interface, as well as a ttl and mx param
   * @param timeOut the timeout for the a device to give a reponse
   * @param ttl the UDP socket packets time to live
   * @param mx discovery message mx http header field value
   * @param searchTarget the device URI to search
   * @param ni the networkInterface where to search devices, null to lookup all interfaces
   * @return an array of UPNP Root device that matches the search or null if nothing found within the given timeout.
   *         Null return does NOT means that no UPNP device is available on the network. It only means
   *         that for this given timeout no devices responded or that effectively no devices
   *         are available at all.
   * @throws IOException if some IOException occurs during discovering
   */
  public static UPNPRootDevice[] discover( int timeOut, int ttl, int mx, String searchTarget, NetworkInterface ni ) throws IOException {
    return discoverDevices( timeOut, ttl, mx, searchTarget, ni );
  }
 
  private static UPNPRootDevice[] discoverDevices( int timeOut, int ttl, int mx, String searchTarget, NetworkInterface ni ) throws IOException {
    if ( searchTarget == null || searchTarget.trim().length() == 0  ) {
      throw new IllegalArgumentException( "Illegal searchTarget" );
    }
    
    final Map devices = new HashMap();
    
    DiscoveryResultsHandler handler = new DiscoveryResultsHandler() {

      public void discoveredDevice( String usn, String udn, String nt, String maxAge, URL location, String firmware ) {
        synchronized( devices ) {
          if ( ! devices.containsKey( usn ) ) {
            try {
              UPNPRootDevice device = new UPNPRootDevice( location, maxAge, firmware, usn, udn );
              devices.put( usn, device );
            } catch ( Exception ex ) {
              log.error( "Error occured during upnp root device object creation from location " + location, ex );
            }
          }
        }
      }
    };
    
    DiscoveryListener.getInstance().registerResultsHandler( handler, searchTarget );
    if ( ni == null ) {
      for ( Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
        NetworkInterface intf = (NetworkInterface)e.nextElement();
        for ( Enumeration adrs = intf.getInetAddresses(); adrs.hasMoreElements(); ) {
          InetAddress adr = (InetAddress)adrs.nextElement();
          if ( adr instanceof Inet4Address && !adr.isLoopbackAddress()  ) {
            sendSearchMessage( adr, ttl, mx, searchTarget );
          }
        }
      }
    } else {
      for ( Enumeration adrs = ni.getInetAddresses(); adrs.hasMoreElements(); ) {
        InetAddress adr = (InetAddress)adrs.nextElement();
        if ( adr instanceof Inet4Address && !adr.isLoopbackAddress()  ) {
          sendSearchMessage( adr, ttl, mx, searchTarget );
        }
      }
    }

    try {
        Thread.sleep( timeOut );
    } catch ( InterruptedException ex ) {
      // don't care
    }

    DiscoveryListener.getInstance().unRegisterResultsHandler( handler, searchTarget );
    
    if ( devices.size() == 0 ) {
      return null;
    }
    int j = 0;
    UPNPRootDevice[] rootDevices = new UPNPRootDevice[devices.size()];
    for ( Iterator i = devices.values().iterator(); i.hasNext(); ) {
      rootDevices[j++] = (UPNPRootDevice)i.next();
    }
    return rootDevices;
   
  }
  
  /**
   * Sends an SSDP search message on the network
   * @param src the sender ip
   * @param ttl the time to live
   * @param mx the mx field
   * @param searchTarget the search target
   * @throws IOException if some IO errors occurs during search
   */
  public static void sendSearchMessage( InetAddress src, int ttl, int mx, String searchTarget ) throws IOException {
   
    int bindPort = DEFAULT_SSDP_SEARCH_PORT;
    String port = System.getProperty( "net.sbbi.upnp.Discovery.bindPort" );
    if ( port != null ) {
      bindPort = Integer.parseInt( port );
    }
    InetSocketAddress adr = new InetSocketAddress( InetAddress.getByName( Discovery.SSDP_IP ), Discovery.SSDP_PORT );
    
    java.net.MulticastSocket skt = new java.net.MulticastSocket( null );
    skt.bind( new InetSocketAddress( src, bindPort ) );
    skt.setTimeToLive( ttl );
    StringBuffer packet = new StringBuffer();
    packet.append( "M-SEARCH * HTTP/1.1\r\n" );
    packet.append( "HOST: 239.255.255.250:1900\r\n" );
    packet.append( "MAN: \"ssdp:discover\"\r\n" );
    packet.append( "MX: ").append( mx ).append( "\r\n" );
    packet.append( "ST: " ).append( searchTarget ).append( "\r\n" ).append( "\r\n" );
    if ( log.isDebugEnabled() ) log.debug( "Sending discovery message on 239.255.255.250:1900 multicast address form ip " + src.getHostAddress() + ":\n" + packet.toString() );
    String toSend = packet.toString();
    byte[] pk = toSend.getBytes();
    skt.send( new DatagramPacket( pk, pk.length, adr ) );
    skt.disconnect();
    skt.close();
  }
 
}

Wäre super wenn mir jemand eine kleine Schritt-für-Schritt Anleitung für dieses Prog geben könnte wie ich es in Eclipse zum Laufen bekomme, was ich machen muss.

Ansonsten bin ich auch für jede andere Hilfestellung bezüglich meines Problems upnp-Discovery dankbar!!!
 

sparrow

Top Contributor
Das ist ja nicht wirklich eine Netzwerkfrage sondern du fragst hier nach Grundlagenerklärung.

Wenn du bisher noch gar nicht mit Java gearbeitet hast empfehle ich dir "Das Handbuch der Java-Programmierung" und "Java ist auch eine Insel".
Für beide gibt es die Möglichkeit die Bücher auch kostenlos zu lesen und sie bieten eine ausführliche Schritt-für-Schritt Einführung in die Programmiersprache Java. Eclipse ist für das Entwickeln von Java-Programmen ürigens nicht nötig, daher solltest du erstmal mit den Grundlagen anfangen.

Gruß
Sparrow
 

Mufasa

Mitglied
Hi sparrow,

ja Grundlagenerklärung ist ja schon irgendwo richtig..... andererseits geht es bei diesem Programm ja um eine Netzwerkangelegenheit, deswegen hab ich das hier gepostet. Es ist ja nicht so, dass ich noch nie Java gesehen hab, aber das ist schon Jahre her leider... ich muss das alles auffrischen und vieles neu erlernen....dafür werde ich mir auf jeden Fall die beiden Bücher anschauen. Danke schon mal für die Tipps!

Aber hierbei ist es wichtig ob Ihr mir sagen könntet ob dieses Programm etwas bringt oder nicht und wie ich es zum Laufen bringen könnte...... damit ich bei meinem Chef einigermaßen gut dastehen kann...... Das wäre super!! :)

Gruß
Mufasa
 
M

maki

Gast
Hmmm... nix für ungut, aber für mich sieht das wirklich so aus als ob du Java noch nie gesehen hättest.

Das ist kein Programm, sondern eine Klasse, was es bringt musst du beurteilen.
Ein Bild von der JavaDoc zu machen ist echt danaben, warum nicht gleich den Link zu HTML ausgabe?
Was sollen wir eigentlich mit der Javadoc anfangen??

damit ich bei meinem Chef einigermaßen gut dastehen kann...... Das wäre super!!
Ähm.. nö.
 

Mufasa

Mitglied
Ganz ehrlich Java hab ich das letzte mal gesehen vor..... ca. 5 Jahren....und das war auch nur so ganz klein HelloWorld und so....also echt nix großartiges.

Die HTML-Ausgabe ist in dieser Datei zum Herunterladen, die ich verlinkt habe. Weil es nicht online ausgegeben wird hab ich ein Bild gemacht. Dachte nur Ihr könntet mir sagen ob es so das ist was ich suche/brauche.....

Hmmm...... danke!
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen

Ähnliche Java Themen

Neue Themen


Oben