Esempio n. 1
0
  public DNSMultiCast(Jenkins hudson) {
    if (disabled) return; // escape hatch

    try {
      this.jmdns = JmDNS.create();

      Map<String, String> props = new HashMap<String, String>();
      String rootURL = hudson.getRootUrl();
      if (rootURL != null) props.put("url", rootURL);
      try {
        props.put("version", String.valueOf(Jenkins.getVersion()));
      } catch (IllegalArgumentException e) {
        // failed to parse the version number
      }

      TcpSlaveAgentListener tal = hudson.getTcpSlaveAgentListener();
      if (tal != null) props.put("slave-port", String.valueOf(tal.getPort()));

      props.put("server-id", Util.getDigestOf(hudson.getSecretKey()));

      URL jenkins_url = new URL(rootURL);
      int jenkins_port = jenkins_url.getPort();
      if (jenkins_port == -1) {
        jenkins_port = 80;
      }
      if (jenkins_url.getPath().length() > 0) {
        props.put("path", jenkins_url.getPath());
      }

      jmdns.registerService(
          ServiceInfo.create(
              "_hudson._tcp.local.",
              "hudson",
              jenkins_port,
              0,
              0,
              props)); // for backward compatibility
      jmdns.registerService(
          ServiceInfo.create("_jenkins._tcp.local.", "jenkins", jenkins_port, 0, 0, props));

      // Make Jenkins appear in Safari's Bonjour bookmarks
      jmdns.registerService(
          ServiceInfo.create("_http._tcp.local.", "Jenkins", jenkins_port, 0, 0, props));
    } catch (IOException e) {
      LOGGER.log(Level.WARNING, "Failed to advertise the service to DNS multi-cast", e);
    }
  }
Esempio n. 2
0
  @Override
  public void createService(String name, String type, int port) {
    if (!type.contains(TYPE_PADDING)) {
      type = type + TYPE_PADDING;
    }
    mType = type;

    acquireMulticastLock();
    try {
      if (null != mJmDNS) {
        destroyService(null, false);
      }
      if (mHost) {
        mJmDNS = JmDNS.create(mWifiEnable);
      } else {
        if (null != mAddress) {
          mJmDNS = JmDNS.create(mAddress, name);
        } else {
          mJmDNS = JmDNS.create(mWifiEnable);
        }
      }
    } catch (Exception e) {
      Log.e(TAG, "failed to create the service", e);
      if (null != mServiceListener) {
        mServiceListener.onServiceRegisteredFailed(new ZeroServiceInfo(null, null));
      }
    }
    if (null != mJmDNS) {
      try {
        ServiceInfo si =
            ServiceInfo.create(
                mType,
                name,
                port,
                LOCAL_SERVER_WEIGHT,
                LOCAL_SERVER_PROIORITY,
                true,
                "android_jmdns_service");
        mJmDNS.registerService(si);
      } catch (Exception e) {
        Log.e(TAG, "failed to cast the service", e);
      }
    }
  }
Esempio n. 3
0
  @Override
  public PluginResult execute(String action, JSONArray data, String callbackId) {
    // Log.d("OSCManager", "executing something " + action);
    PluginResult result = null;
    try {
      System.out.println("EXECUTING BONJOUR ********************************************");
      if (action.equals("start") || action.equals("browse")) {
        System.out.println("STARTING BONJOUR ********************************************");

        ServiceInfo[] infos = jmdns.list("_osc._udp.local.");
        for (int i = 0; i < infos.length; i++) {
          String jsString =
              "javascript:destinationManager.addDestination('"
                  + infos[i].getHostAddress()
                  + "',"
                  + infos[i].getPort()
                  + ", 0, 0);";
          System.out.println(jsString);
          webView.loadUrl(jsString);
          System.out.println("after sending to js");
        }
        if (action.equals("start")) {
          ServiceInfo serviceInfo =
              ServiceInfo.create(
                  "_osc._udp.local.",
                  "Control_" + (Math.round(Math.random() * 100000)),
                  8080,
                  "OSC reception for device running Control");
          jmdns.registerService(serviceInfo);
        }
      }
    } catch (Exception e) {
      System.out.println("after sending to js");
    }
    return result;
  }
Esempio n. 4
0
  @Override
  public void run() {
    try {
      final JmDNS jmdns = JmDNS.create();
      jmdns.addServiceListener(TOUCH_ABLE_TYPE, this);
      jmdns.addServiceListener(DACP_TYPE, this);

      final HashMap<String, String> values = new HashMap<String, String>();
      byte[] number = new byte[4];
      random.nextBytes(number);
      values.put("DvNm", "Android-" + toHex(number));
      values.put("RemV", "10000");
      values.put("DvTy", "iPod");
      values.put("RemN", "Remote");
      values.put("txtvers", "1");
      byte[] pair = new byte[8];
      random.nextBytes(pair);
      values.put("Pair", toHex(pair));

      while (_running) {
        ServerSocket server = new ServerSocket(0);

        byte[] name = new byte[20];
        random.nextBytes(name);
        System.out.println("Requesting pairing for " + toHex(name));
        ServiceInfo pairservice =
            ServiceInfo.create(REMOTE_TYPE, toHex(name), server.getLocalPort(), 0, 0, values);
        jmdns.registerService(pairservice);

        System.out.println("Waiting for pass code");
        final Socket socket = server.accept();
        OutputStream output = null;

        try {
          output = socket.getOutputStream();

          // output the contents for debugging
          final BufferedReader br =
              new BufferedReader(new InputStreamReader(socket.getInputStream()));
          while (br.ready()) {
            String line = br.readLine();
            System.out.println(line);
          }

          // edit our local PAIRING_RAW to return the correct guid
          byte[] code = new byte[8];
          random.nextBytes(code);
          System.out.println("Device guid: " + toHex(code));
          System.arraycopy(code, 0, PAIRING_RAW, 16, 8);

          byte[] header =
              String.format(
                      "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n",
                      new Integer(PAIRING_RAW.length))
                  .getBytes();
          byte[] reply = new byte[header.length + PAIRING_RAW.length];

          System.arraycopy(header, 0, reply, 0, header.length);
          System.arraycopy(PAIRING_RAW, 0, reply, header.length, PAIRING_RAW.length);

          System.out.println("Response: " + new String(reply));

          output.write(reply);
          output.flush();

          System.out.println("someone paired with me!");

          jmdns.unregisterService(pairservice);
        } finally {
          if (output != null) {
            output.close();
          }

          System.out.println("Closing Socket");
          if (!server.isClosed()) {
            server.close();
          }
          _running = false;
        }
      }
      Thread.sleep(6000);
      System.out.println("Closing JmDNS");
      jmdns.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 5
0
  public static void main(final String[] args) throws Exception {
    /* Make sure AirReceiver shuts down gracefully */
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    onShutdown();
                  }
                }));

    /* Create about dialog */
    final Dialog aboutDialog = new Dialog((Dialog) null);
    final GridBagLayout aboutLayout = new GridBagLayout();
    aboutDialog.setLayout(aboutLayout);
    aboutDialog.setVisible(false);
    aboutDialog.setTitle("About AirReceiver");
    aboutDialog.setResizable(false);
    {
      /* Message */
      final TextArea title = new TextArea(AboutMessage.split("\n").length + 1, 64);
      title.setText(AboutMessage);
      title.setEditable(false);
      final GridBagConstraints titleConstraints = new GridBagConstraints();
      titleConstraints.gridx = 1;
      titleConstraints.gridy = 1;
      titleConstraints.fill = GridBagConstraints.HORIZONTAL;
      titleConstraints.insets = new Insets(0, 0, 0, 0);
      aboutLayout.setConstraints(title, titleConstraints);
      aboutDialog.add(title);
    }
    {
      /* Done button */
      final Button aboutDoneButton = new Button("Done");
      aboutDoneButton.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent evt) {
              aboutDialog.setVisible(false);
            }
          });
      final GridBagConstraints aboutDoneConstraints = new GridBagConstraints();
      aboutDoneConstraints.gridx = 1;
      aboutDoneConstraints.gridy = 2;
      aboutDoneConstraints.anchor = GridBagConstraints.PAGE_END;
      aboutDoneConstraints.fill = GridBagConstraints.NONE;
      aboutDoneConstraints.insets = new Insets(0, 0, 0, 0);
      aboutLayout.setConstraints(aboutDoneButton, aboutDoneConstraints);
      aboutDialog.add(aboutDoneButton);
    }
    aboutDialog.setVisible(false);
    aboutDialog.setLocationByPlatform(true);
    aboutDialog.pack();

    /* Create tray icon */
    final URL trayIconUrl = AirReceiver.class.getClassLoader().getResource("icon_32.png");
    final TrayIcon trayIcon = new TrayIcon((new ImageIcon(trayIconUrl, "AirReceiver").getImage()));
    trayIcon.setToolTip("AirReceiver");
    trayIcon.setImageAutoSize(true);
    final PopupMenu popupMenu = new PopupMenu();
    final MenuItem aboutMenuItem = new MenuItem("About");
    aboutMenuItem.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent evt) {
            aboutDialog.setLocationByPlatform(true);
            aboutDialog.setVisible(true);
          }
        });
    popupMenu.add(aboutMenuItem);
    final MenuItem exitMenuItem = new MenuItem("Quit");
    exitMenuItem.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent evt) {
            onShutdown();
            System.exit(0);
          }
        });
    popupMenu.add(exitMenuItem);
    trayIcon.setPopupMenu(popupMenu);
    SystemTray.getSystemTray().add(trayIcon);

    /* Create AirTunes RTSP server */
    final ServerBootstrap airTunesRtspBootstrap =
        new ServerBootstrap(new NioServerSocketChannelFactory(ExecutorService, ExecutorService));
    airTunesRtspBootstrap.setPipelineFactory(new RaopRtspPipelineFactory());
    airTunesRtspBootstrap.setOption("reuseAddress", true);
    airTunesRtspBootstrap.setOption("child.tcpNoDelay", true);
    airTunesRtspBootstrap.setOption("child.keepAlive", true);
    s_allChannels.add(
        airTunesRtspBootstrap.bind(
            new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), AirtunesServiceRTSPPort)));
    s_logger.info("Launched RTSP service on port " + AirtunesServiceRTSPPort);

    /* Create mDNS responders. */
    synchronized (s_jmDNSInstances) {
      for (final NetworkInterface iface :
          Collections.list(NetworkInterface.getNetworkInterfaces())) {
        if (iface.isLoopback()) continue;
        if (iface.isPointToPoint()) continue;
        if (!iface.isUp()) continue;

        for (final InetAddress addr : Collections.list(iface.getInetAddresses())) {
          if (!(addr instanceof Inet4Address) && !(addr instanceof Inet6Address)) continue;

          try {
            /* Create mDNS responder for address */
            final JmDNS jmDNS = JmDNS.create(addr, HostName + "-jmdns");
            s_jmDNSInstances.add(jmDNS);

            /* Publish RAOP service */
            final ServiceInfo airTunesServiceInfo =
                ServiceInfo.create(
                    AirtunesServiceType,
                    HardwareAddressString + "@" + HostName + " (" + iface.getName() + ")",
                    AirtunesServiceRTSPPort,
                    0 /* weight */,
                    0 /* priority */,
                    AirtunesServiceProperties);
            jmDNS.registerService(airTunesServiceInfo);
            s_logger.info(
                "Registered AirTunes service '" + airTunesServiceInfo.getName() + "' on " + addr);
          } catch (final Throwable e) {
            s_logger.log(Level.SEVERE, "Failed to publish service on " + addr, e);
          }
        }
      }
    }
  }
Esempio n. 6
0
 /** Start advertising the service. */
 public void publish() {
   if (!isPublished()) {
     ZeroConfService.services.put(this.key(), this);
     this.listeners
         .stream()
         .forEach(
             (listener) -> {
               listener.serviceQueued(new ZeroConfServiceEvent(this, null));
             });
     boolean useIPv4 =
         ProfileUtils.getPreferences(
                 ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
             .getBoolean(ZeroConfService.IPv4, true);
     boolean useIPv6 =
         ProfileUtils.getPreferences(
                 ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
             .getBoolean(ZeroConfService.IPv6, true);
     for (JmDNS netService : ZeroConfService.netServices().values()) {
       ZeroConfServiceEvent event;
       ServiceInfo info;
       try {
         if (netService.getInetAddress() instanceof Inet6Address && !useIPv6) {
           // Skip if address is IPv6 and should not be advertised on
           log.debug("Ignoring IPv6 address {}", netService.getInetAddress().getHostAddress());
           continue;
         }
         if (netService.getInetAddress() instanceof Inet4Address && !useIPv4) {
           // Skip if address is IPv4 and should not be advertised on
           log.debug("Ignoring IPv4 address {}", netService.getInetAddress().getHostAddress());
           continue;
         }
         try {
           log.debug(
               "Publishing ZeroConfService for '{}' on {}",
               key(),
               netService.getInetAddress().getHostAddress());
         } catch (IOException ex) {
           log.debug(
               "Publishing ZeroConfService for '{}' with IOException {}",
               key(),
               ex.getLocalizedMessage(),
               ex);
         }
         // JmDNS requires a 1-to-1 mapping of serviceInfo to InetAddress
         if (!this.serviceInfos.containsKey(netService.getInetAddress())) {
           try {
             info = this.serviceInfo();
             netService.registerService(info);
             log.debug(
                 "Register service '{}' on {} successful.",
                 this.key(),
                 netService.getInetAddress().getHostAddress());
           } catch (IllegalStateException ex) {
             // thrown if the reference serviceInfo object is in use
             try {
               log.debug(
                   "Initial attempt to register '{}' on {} failed.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               info = this.addServiceInfo(netService);
               log.debug(
                   "Retrying register '{}' on {}.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               netService.registerService(info);
             } catch (IllegalStateException ex1) {
               // thrown if service gets registered on interface by
               // the networkListener before this loop on interfaces
               // completes, so we only ensure a later notification
               // is not posted continuing to next interface in list
               log.debug(
                   "'{}' is already registered on {}.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               continue;
             }
           }
         } else {
           log.debug(
               "skipping '{}' on {}, already in serviceInfos.",
               this.key(),
               netService.getInetAddress().getHostAddress());
         }
         event = new ZeroConfServiceEvent(this, netService);
       } catch (IOException ex) {
         log.error("Unable to publish service for '{}': {}", key(), ex.getMessage());
         continue;
       }
       this.listeners
           .stream()
           .forEach(
               (listener) -> {
                 listener.servicePublished(event);
               });
     }
   }
 }