public void deactivate() {
   unregisterAllServices();
   try {
     jmdns.close();
     logger.debug("mDNS service has been stopped");
   } catch (IOException e) {
     logger.error(e.getMessage());
   }
 }
 private void stopMDNS() throws IOException {
   if (jmdns != null) {
     for (JmDNS mdns : jmdns) {
       if (mdns == null) {
         continue;
       }
       Log.i(TAG, "Stopping GameClient discovery " + mdns.getName());
       mdns.removeServiceListener(SERVICE_TYPE, GameClient.this);
       mdns.close();
       mdns = null;
     }
   }
 }
 @Override
 public void inetAddressRemoved(InetAddress address) {
   try (JmDNS jmDNS = this.mappedJmDNSs.remove(address)) {
     if (jmDNS != null) {
       try {
         jmDNS.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   }
 }
 private static void stopServicesMDNS() {
   synchronized (lock) {
     if (jmdns != null) {
       for (JmDNS mdns : jmdns) {
         if (mdns != null) {
           try {
             mdns.unregisterAllServices();
             mdns.close();
             mdns = null;
           } catch (IOException ex) {
             Log.e(TAG, "", ex);
           }
         }
       }
     }
     waiter.release();
     Log.i(TAG, "mDNS released");
   }
 }
 private void destroyService(ZeroServiceInfo zsi, boolean notify) {
   if (null == mJmDNS) {
     if (notify) {
       mServiceListener.onServiceUnregistered(zsi);
     }
     return;
   }
   try {
     releaseMulticastLock();
     mJmDNS.removeServiceListener(mType, mJmDNSListener);
     mJmDNS.unregisterAllServices();
     mJmDNS.close();
     mJmDNS = null;
     if (null != mServiceListener && notify) {
       mServiceListener.onServiceUnregistered(zsi);
     }
   } catch (Exception e) {
     Log.e(TAG, "failed to close the JmDNS service");
     if (null != mServiceListener && notify) {
       mServiceListener.onServiceUnregisteredFailed(zsi);
     }
   }
 }
Exemple #6
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();
    }
  }