示例#1
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);
          }
        }
      }
    }
  }