android.net.nsd.NsdServiceInfo Java Examples

The following examples show how to use android.net.nsd.NsdServiceInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MirrorInfo.java    From mirror with GNU General Public License v3.0 6 votes vote down vote up
public MirrorInfo(NsdServiceInfo nsdServiceInfo) {
    Pattern pattern = Pattern.compile("\\((.*?)\\)");
    Matcher matcher = pattern.matcher(nsdServiceInfo.getServiceName());

    mName = "";
    while (matcher.find()) {
        mName = matcher.group(1);
    }

    mHost = "";
    if (nsdServiceInfo.getHost() != null) {
        mHost = nsdServiceInfo.getHost().getHostAddress();
    }

    mPort = nsdServiceInfo.getPort();
    mNsdServiceInfo = nsdServiceInfo;
}
 
Example #2
Source File: DiscoverResolverTest.java    From tinydnssd with MIT License 6 votes vote down vote up
public void testServiceLost() throws IOException, InterruptedException {
    startDiscoveryOnMainThread();
    mDiscoveryListener.onDiscoveryStarted(SERVICE_TYPE);
    NsdServiceInfo serviceInfo = newNsdServiceInfo("device-1234", "_example._tcp.");
    MDNSDiscover.Result result = new MDNSDiscover.Result();
    when(mMockResolver.resolve(eq("device-1234._example._tcp.local"), anyInt())).thenReturn(result);
    mLatch = new CountDownLatch(1);
    mDiscoveryListener.onServiceFound(serviceInfo);
    mLatch.await();
    Map<String, MDNSDiscover.Result> expectedMap = new HashMap<>();
    expectedMap.put("device-1234._example._tcp.local", result);
    verify(mMockListener).onServicesChanged(eq(expectedMap));
    mLatch = new CountDownLatch(1);
    mDiscoveryListener.onServiceLost(serviceInfo);
    mLatch.await();
    expectedMap.clear();
    verify(mMockListener).onServicesChanged(eq(expectedMap));
    stopDiscoveryOnMainThread();
}
 
Example #3
Source File: ThetaDeviceDetectionFromLAN.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onServiceLost(final NsdServiceInfo serviceInfo) {
    mLogger.info("onServiceLost: serviceInfo=" + serviceInfo);

    ThetaDevice deleted = null;
    synchronized (mDetectedDevices) {
        for (Iterator<ThetaDevice> it = mDetectedDevices.iterator(); it.hasNext(); ) {
            ThetaDevice device = it.next();
            if (device.getName().equals(serviceInfo.getServiceName())) {
                deleted = device;
                it.remove();
                break;
            }
        }
    }
    if (deleted != null) {
        notifyOnThetaLost(deleted);
    }
}
 
Example #4
Source File: DiscoverResolverTest.java    From tinydnssd with MIT License 6 votes vote down vote up
/**
 * Resolve a service but inhibit the callback by having the main thread blocked. Stop the
 * DiscoverResolver as soon as the main thread is unblocked, then check the callback does not
 * occur after being stopped.
 * @throws IOException
 * @throws InterruptedException
 */
public void testNoChangesAfterStop() throws IOException, InterruptedException {
    MDNSDiscover.Result result = new MDNSDiscover.Result();
    when(mMockResolver.resolve(eq("device-1234._example._tcp.local"), anyInt())).thenReturn(result);

    startDiscoveryOnMainThread();
    mDiscoveryListener.onDiscoveryStarted(SERVICE_TYPE);
    NsdServiceInfo serviceInfo = newNsdServiceInfo("device-1234", "_example._tcp.");
    blockMainThread();
    new Handler(Looper.getMainLooper()).post(DISCOVER_STOP); // queue this to happen straight after unblockMainThread()
    mDiscoveryListener.onServiceFound(serviceInfo);
    Thread.sleep(100);  // give the resolver some time
    verify(mMockResolver).resolve(eq("device-1234._example._tcp.local"), anyInt());
    verify(mMockListener, never()).onServicesChanged(anyMap());
    unblockMainThread();
    Thread.sleep(100);
    verify(mMockListener, never()).onServicesChanged(anyMap());
}
 
Example #5
Source File: ThetaDeviceFactory.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
public static ThetaDevice createDeviceFromNsdServiceInfo(final NsdServiceInfo serviceInfo) {
    InetAddress host = serviceInfo.getHost();
    if (host == null) {
        return null;
    }
    String serviceName = serviceInfo.getServiceName();
    if (serviceName == null) {
        return null;
    }
    // サービス名がSSIDと同一である前提
    ThetaDeviceModel model = parseModel(serviceName);
    switch (model) {
        case THETA_V:
            String password = parsePasswordForDigestAuthentication(serviceName);
            Credentials credentials = new Credentials(serviceName, password);
            return new ThetaV(serviceName, host.getHostAddress(), credentials, null);
        default:
            return null;
    }
}
 
Example #6
Source File: ServerDiscovery.java    From habpanelviewer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized void onServiceFound(final NsdServiceInfo service) {
    executor.submit(() -> {
        Log.v(TAG, "starting to resolve service " + service.getServiceName()
                + service.getHost() + ":" + service.getPort() + "...");

        CountDownLatch finishLatch = new CountDownLatch(1);
        mNsdManager.resolveService(service, new ResolveListener(finishLatch, mListener));

        try {
            finishLatch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        Log.v(TAG, "fisnihed resolving service " + service.getServiceName()
                + service.getHost() + ":" + service.getPort() + "...");
    });
}
 
Example #7
Source File: ServerDiscovery.java    From habpanelviewer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
    final int port = serviceInfo.getPort();
    final String host = serviceInfo.getHost().getHostName();

    Log.v(TAG, "service resolved: name= " + serviceInfo.getServiceName()
            + ", host=" + host + ", port=" + port);

    synchronized (mUrls) {
        if (serviceInfo.getServiceName().contains("openhab-ssl")) {
            if (mUrls.add("https://" + host + ":" + port)) {
                mListener.found("https://" + host + ":" + port);
            }
        } else {
            if (mUrls.add("http://" + host + ":" + port)) {
                mListener.found("http://" + host + ":" + port);
            }
        }
    }

    mLatch.countDown();
}
 
Example #8
Source File: MainActivity.java    From Rucky with GNU General Public License v3.0 6 votes vote down vote up
private void initPiResolveListener() {
    piResolveListener = new NsdManager.ResolveListener() {
        @Override
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            piSocketConnected = false;
            piConnected = false;
        }

        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            mServiceInfo = serviceInfo;
            piIp = mServiceInfo.getHost().getHostAddress();
            piSocketConnected = false;
            piConnected = true;
        }
    };
}
 
Example #9
Source File: MirrorService.java    From mirror with GNU General Public License v3.0 6 votes vote down vote up
private void onResolveSuccess(NsdServiceInfo nsdServiceInfo) {
    MirrorInfo mirrorInfo = new MirrorInfo(nsdServiceInfo);
    PreferenceUtils.setMirror(this, MirrorUtils.buildMirrorHttpUrl(mirrorInfo));
    RxBus.getInstance().post(new MirrorResolveSuccessEvent(mirrorInfo));

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(MirrorUtils.MIRROR_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
            .readTimeout(MirrorUtils.MIRROR_READ_TIMEOUT, TimeUnit.MILLISECONDS)
            .retryOnConnectionFailure(true)
            .build();
    Request request = new Request.Builder()
            .url(MirrorUtils.buildMirrorWebSocketUrl(mirrorInfo))
            .build();
    mWebSocketCall = WebSocketCall.create(client, request);
    mWebSocketCall.enqueue(this);
}
 
Example #10
Source File: NsdHelper.java    From snapdroid with GNU General Public License v3.0 6 votes vote down vote up
private void initializeResolveListener() {
    mResolveListener = new NsdManager.ResolveListener() {
        @Override
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Log.d(TAG, "Resolve failed");
        }

        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            NsdServiceInfo info = serviceInfo;
            // sometimes it returns an IPv6 address...
            if (!info.getHost().getCanonicalHostName().contains(":"))
                listener.onResolved(NsdHelper.this, serviceInfo);
        }
    };
}
 
Example #11
Source File: ThetaDeviceDetectionFromLAN.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
    mLogger.info("onServiceResolved: serviceInfo=" + serviceInfo);

    ThetaDevice device = ThetaDeviceFactory.createDeviceFromNsdServiceInfo(serviceInfo);
    if (device != null) {
        mLogger.info("Created THETA service: serviceInfo=" + serviceInfo);
        synchronized (mDetectedDevices) {
            mDetectedDevices.add(device);
        }
        notifyOnThetaDetected(device);
    } else {
        mLogger.warning("Failed to create THETA service: serviceInfo=" + serviceInfo);
    }
}
 
Example #12
Source File: YokeActivity.java    From yoke-android with MIT License 5 votes vote down vote up
@Override
public void onServiceFound(NsdServiceInfo service) {
    log(res.getString(R.string.log_service_found) + service);
    mServiceMap.put(service.getServiceName(), service);
    mServiceNames.add(service.getServiceName());
    this.runOnUiThread(() -> {
        if (mSpinner.getSelectedItem().toString().equals(service.getServiceName()))
            return;
        mAdapter.add(service.getServiceName());
    });

}
 
Example #13
Source File: DiscoverResolver.java    From tinydnssd with MIT License 5 votes vote down vote up
@Override
public void onServiceLost(final NsdServiceInfo serviceInfo) {
    Log.d(TAG, "onServiceLost() serviceInfo = [" + serviceInfo + "]");
    synchronized (DiscoverResolver.this) {
        if (mStarted) {
            String name = serviceInfo.getServiceName() + "." + serviceInfo.getServiceType() + "local";
            mDebouncer.put(name, null);
        }
    }
}
 
Example #14
Source File: MainActivity.java    From Rucky with GNU General Public License v3.0 5 votes vote down vote up
private void initPiDiscoveryListener() {
    piDiscoveryListener = new NsdManager.DiscoveryListener() {
        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            mNsdManager.stopServiceDiscovery(this);
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            mNsdManager.stopServiceDiscovery(this);
        }

        @Override
        public void onDiscoveryStarted(String serviceType) {
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
        }

        @Override
        public void onServiceFound(NsdServiceInfo serviceInfo) {
            String name = serviceInfo.getServiceName();
            String type = serviceInfo.getServiceType();
            if (type.equals(SERVICE_TYPE) && name.contains("Rucky")) {
                mNsdManager.resolveService(serviceInfo, piResolveListener);
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo serviceInfo) {
        }
    };
}
 
Example #15
Source File: ServerDialogFragment.java    From snapdroid with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    NsdHelper.getInstance(getContext()).startListening("_snapcast._tcp.", "Snapcast", new NsdHelper.NsdHelperListener() {
        @Override
        public void onResolved(NsdHelper nsdHelper, NsdServiceInfo serviceInfo) {
            Log.d(TAG, "onResolved: " + serviceInfo.getHost().getCanonicalHostName());
            setHost(serviceInfo.getHost().getCanonicalHostName(), serviceInfo.getPort(), serviceInfo.getPort() + 1);
        }
    });
}
 
Example #16
Source File: YokeActivity.java    From yoke-android with MIT License 5 votes vote down vote up
@Override
public void onServiceLost(NsdServiceInfo service) {
    log(res.getString(R.string.log_service_lost) + service);
    mServiceMap.remove(service.getServiceName());
    mServiceNames.remove(service.getServiceName());
    this.runOnUiThread(() -> {
        if (mSpinner.getSelectedItem().toString().equals(service.getServiceName()))
            return;

        mAdapter.remove(service.getServiceName());
    });

}
 
Example #17
Source File: DiscoverResolver.java    From tinydnssd with MIT License 5 votes vote down vote up
@Override
public void onServiceFound(final NsdServiceInfo serviceInfo) {
    Log.d(TAG, "onServiceFound() serviceInfo = [" + serviceInfo + "]");
    synchronized (DiscoverResolver.this) {
        if (mStarted) {
            String name = serviceInfo.getServiceName() + "." + serviceInfo.getServiceType() + "local";
            mDebouncer.put(name, DUMMY);
        }
    }
}
 
Example #18
Source File: ShareActivity.java    From nitroshare-android with MIT License 5 votes vote down vote up
@Override
public void onServiceFound(NsdServiceInfo serviceInfo) {
    if (serviceInfo.getServiceName().equals(mThisDeviceName)) {
        return;
    }
    Log.d(TAG, String.format("found \"%s\"; queued for resolving", serviceInfo.getServiceName()));
    boolean resolve;
    synchronized (mQueue) {
        resolve = mQueue.size() == 0;
        mQueue.add(serviceInfo);
    }
    if (resolve) {
        resolveNextService();
    }
}
 
Example #19
Source File: DiscoverResolverTest.java    From tinydnssd with MIT License 5 votes vote down vote up
public void testResolve() throws IOException, InterruptedException {
    startDiscoveryOnMainThread();
    mDiscoveryListener.onDiscoveryStarted(SERVICE_TYPE);
    NsdServiceInfo serviceInfo = newNsdServiceInfo("device-1234", "_example._tcp.");
    MDNSDiscover.Result result = new MDNSDiscover.Result();
    when(mMockResolver.resolve(eq("device-1234._example._tcp.local"), anyInt())).thenReturn(result);
    mLatch = new CountDownLatch(1);
    mDiscoveryListener.onServiceFound(serviceInfo);
    mLatch.await();
    verify(mMockResolver).resolve(eq("device-1234._example._tcp.local"), anyInt());
    Map<String, MDNSDiscover.Result> expectedMap = new HashMap<>();
    expectedMap.put("device-1234._example._tcp.local", result);
    verify(mMockListener).onServicesChanged(eq(expectedMap));
    stopDiscoveryOnMainThread();
}
 
Example #20
Source File: ShareActivity.java    From nitroshare-android with MIT License 5 votes vote down vote up
@Override
public void onServiceLost(final NsdServiceInfo serviceInfo) {
    Log.d(TAG, String.format("lost \"%s\"", serviceInfo.getServiceName()));
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            remove(serviceInfo.getServiceName());
            mDevices.remove(serviceInfo.getServiceName());
        }
    });
}
 
Example #21
Source File: NsdListenerResolve.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
    mNsdHelper.logMsg(String.format("Service resolved -> %s, %s/%s, port %d, %s",
            serviceInfo.getServiceName(), serviceInfo.getHost().getHostName(),
            serviceInfo.getHost().getHostAddress(), serviceInfo.getPort(), serviceInfo.getServiceType()));
    mNsdHelper.onNsdServiceResolved(serviceInfo);
}
 
Example #22
Source File: ResolveQueue.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
private void run() {
    NsdServiceInfo nsdServiceInfo = mTasks.pollFirst();
    if (nsdServiceInfo != null)
        mNsdHelper.resolveService(nsdServiceInfo);
    else
        mIsRunning = false;
}
 
Example #23
Source File: NsdHelper.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
void onNsdServiceFound(NsdServiceInfo foundService) {
    mDiscoveryTimer.reset();
    if (mNsdListener != null)
        mNsdListener.onNsdServiceFound(new NsdService(foundService));
    if (mAutoResolveEnabled)
        mResolveQueue.enqueue(foundService);
}
 
Example #24
Source File: ThetaDeviceDetectionFromLAN.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onServiceFound(final NsdServiceInfo serviceInfo) {
    mLogger.info("onServiceFound: serviceInfo=" + serviceInfo);
    if (serviceInfo != null) {
        NsdManager nsdManager = mNsdManager;
        if (nsdManager != null) {
            nsdManager.resolveService(serviceInfo, mResolveListener);
        }
    }
}
 
Example #25
Source File: NsdListenerDiscovery.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onServiceFound(NsdServiceInfo serviceInfo) {
    if (serviceInfo.getServiceType().equals(mNsdHelper.getDiscoveryServiceType())) {
        if (!serviceInfo.getServiceName().equals(mNsdHelper.getRegisteredServiceInfo().getServiceName())) {
            if (mNsdHelper.getDiscoveryServiceName() == null || serviceInfo.getServiceName().toLowerCase().equals(mNsdHelper.getDiscoveryServiceName().toLowerCase())) {
                mNsdHelper.logMsg("Service found -> " + serviceInfo.getServiceName());
                mNsdHelper.onNsdServiceFound(serviceInfo);
            }
        }
    }
}
 
Example #26
Source File: NsdHelper.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve service host and port.
 *
 * @param nsdService Service to be resolved.
 */
public void resolveService(NsdService nsdService) {
    NsdServiceInfo serviceInfo = new NsdServiceInfo();
    serviceInfo.setServiceName(nsdService.getName());
    serviceInfo.setServiceType(nsdService.getType());
    mResolveQueue.enqueue(serviceInfo);
}
 
Example #27
Source File: NsdHelper.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
/**
 * Register new service with given serivce name, type and port.
 *
 * @param desiredServiceName Desired service name. If the name already exists in network it will be change to something like 'AppChat (1)'.
 * @param serviceType        Service type.
 * @param port               Service port.
 */
public void registerService(String desiredServiceName, String serviceType, int port) {
    if (port == 0) return;

    mRegisteredServiceInfo = new NsdServiceInfo();
    mRegisteredServiceInfo.setServiceName(desiredServiceName);
    mRegisteredServiceInfo.setServiceType(serviceType);
    mRegisteredServiceInfo.setPort(port);

    mRegistrationListener = new NsdListenerRegistration(this);
    mNsdManager.registerService(mRegisteredServiceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
 
Example #28
Source File: NsdService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean registerService(int regId, NsdServiceInfo service) {
    if (DBG) {
        Slog.d(TAG, "registerService: " + regId + " " + service);
    }
    String name = service.getServiceName();
    String type = service.getServiceType();
    int port = service.getPort();
    byte[] textRecord = service.getTxtRecord();
    String record = Base64.encodeToString(textRecord, Base64.DEFAULT).replace("\n", "");
    return mDaemon.execute("register", regId, name, type, port, record);
}
 
Example #29
Source File: NsdService.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
public NsdService(NsdServiceInfo nsdServiceInfo) {
    this.name = nsdServiceInfo.getServiceName();
    this.type = nsdServiceInfo.getServiceType();
    this.hostIp = nsdServiceInfo.getHost() == null ? null : nsdServiceInfo.getHost().getHostAddress();
    this.hostName = nsdServiceInfo.getHost() == null ? null : nsdServiceInfo.getHost().getHostName();
    this.port = nsdServiceInfo.getPort();
    this.host = nsdServiceInfo.getHost();
}
 
Example #30
Source File: ResolveQueue.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
public void enqueue(NsdServiceInfo serviceInfo) {
    mTasks.add(serviceInfo);
    if (!mIsRunning) {
        mIsRunning = true;
        run();
    }
}