Search in sources :

Example 1 with ServiceDescription

use of com.connectsdk.service.config.ServiceDescription in project butter-android by butterproject.

the class DeviceService method getService.

@SuppressWarnings("unchecked")
public static DeviceService getService(JSONObject json) {
    Class<DeviceService> newServiceClass;
    try {
        String className = json.optString(KEY_CLASS);
        if (className.equalsIgnoreCase("DLNAService"))
            return null;
        if (className.equalsIgnoreCase("Chromecast"))
            return null;
        newServiceClass = (Class<DeviceService>) Class.forName(DeviceService.class.getPackage().getName() + "." + className);
        Constructor<DeviceService> constructor = newServiceClass.getConstructor(ServiceDescription.class, ServiceConfig.class);
        JSONObject jsonConfig = json.optJSONObject(KEY_CONFIG);
        ServiceConfig serviceConfig = null;
        if (jsonConfig != null)
            serviceConfig = ServiceConfig.getConfig(jsonConfig);
        JSONObject jsonDescription = json.optJSONObject(KEY_DESC);
        ServiceDescription serviceDescription = null;
        if (jsonDescription != null)
            serviceDescription = ServiceDescription.getDescription(jsonDescription);
        if (serviceConfig == null || serviceDescription == null)
            return null;
        return constructor.newInstance(serviceDescription, serviceConfig);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ServiceDescription(com.connectsdk.service.config.ServiceDescription) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) ServiceConfig(com.connectsdk.service.config.ServiceConfig)

Example 2 with ServiceDescription

use of com.connectsdk.service.config.ServiceDescription in project butter-android by butterproject.

the class SSDPDiscoveryProvider method notifyListenersOfLostService.

private void notifyListenersOfLostService(ServiceDescription service) {
    List<String> serviceIds = serviceIdsForFilter(service.getServiceFilter());
    for (String serviceId : serviceIds) {
        ServiceDescription _newService = service.clone();
        _newService.setServiceID(serviceId);
        final ServiceDescription newService = _newService;
        Util.runOnUI(new Runnable() {

            @Override
            public void run() {
                for (DiscoveryProviderListener listener : serviceListeners) {
                    listener.onServiceRemoved(SSDPDiscoveryProvider.this, newService);
                }
            }
        });
    }
}
Also used : ServiceDescription(com.connectsdk.service.config.ServiceDescription) DiscoveryProviderListener(com.connectsdk.discovery.DiscoveryProviderListener)

Example 3 with ServiceDescription

use of com.connectsdk.service.config.ServiceDescription in project butter-android by butterproject.

the class SSDPDiscoveryProvider method notifyListenersOfNewService.

private void notifyListenersOfNewService(ServiceDescription service) {
    List<String> serviceIds = serviceIdsForFilter(service.getServiceFilter());
    for (String serviceId : serviceIds) {
        ServiceDescription _newService = service.clone();
        _newService.setServiceID(serviceId);
        final ServiceDescription newService = _newService;
        Util.runOnUI(new Runnable() {

            @Override
            public void run() {
                for (DiscoveryProviderListener listener : serviceListeners) {
                    listener.onServiceAdded(SSDPDiscoveryProvider.this, newService);
                }
            }
        });
    }
}
Also used : ServiceDescription(com.connectsdk.service.config.ServiceDescription) DiscoveryProviderListener(com.connectsdk.discovery.DiscoveryProviderListener)

Example 4 with ServiceDescription

use of com.connectsdk.service.config.ServiceDescription in project butter-android by butterproject.

the class FireTVDiscoveryProvider method stop.

/**
 * Safely stop discovery and remove all found FireTV services because they don't work when
 * discovery is stopped. Ignore if it's already stopped.
 */
@Override
public void stop() {
    if (isRunning) {
        discoveryController.stop();
        isRunning = false;
    }
    for (ServiceDescription serviceDescription : foundServices.values()) {
        notifyListenersThatServiceLost(serviceDescription);
    }
    foundServices.clear();
}
Also used : ServiceDescription(com.connectsdk.service.config.ServiceDescription)

Example 5 with ServiceDescription

use of com.connectsdk.service.config.ServiceDescription in project butter-android by butterproject.

the class SSDPDiscoveryProvider method handleSSDPPacket.

private void handleSSDPPacket(SSDPPacket ssdpPacket) {
    if (ssdpPacket == null || ssdpPacket.getData().size() == 0 || ssdpPacket.getType() == null)
        return;
    String serviceFilter = ssdpPacket.getData().get(ssdpPacket.getType().equals(SSDPClient.NOTIFY) ? "NT" : "ST");
    if (serviceFilter == null || SSDPClient.MSEARCH.equals(ssdpPacket.getType()) || !isSearchingForFilter(serviceFilter))
        return;
    String usnKey = ssdpPacket.getData().get("USN");
    if (usnKey == null || usnKey.length() == 0)
        return;
    Matcher m = uuidReg.matcher(usnKey);
    if (!m.find())
        return;
    String uuid = m.group();
    if (SSDPClient.BYEBYE.equals(ssdpPacket.getData().get("NTS"))) {
        final ServiceDescription service = foundServices.get(uuid);
        if (service != null) {
            foundServices.remove(uuid);
            notifyListenersOfLostService(service);
        }
    } else {
        String location = ssdpPacket.getData().get("LOCATION");
        if (location == null || location.length() == 0)
            return;
        ServiceDescription foundService = foundServices.get(uuid);
        ServiceDescription discoverdService = discoveredServices.get(uuid);
        boolean isNew = foundService == null && discoverdService == null;
        if (isNew) {
            foundService = new ServiceDescription();
            foundService.setUUID(uuid);
            foundService.setServiceFilter(serviceFilter);
            foundService.setIpAddress(ssdpPacket.getDatagramPacket().getAddress().getHostAddress());
            foundService.setPort(3001);
            discoveredServices.put(uuid, foundService);
            getLocationData(location, uuid, serviceFilter);
        }
        if (foundService != null)
            foundService.setLastDetection(new Date().getTime());
    }
}
Also used : Matcher(java.util.regex.Matcher) ServiceDescription(com.connectsdk.service.config.ServiceDescription) Date(java.util.Date)

Aggregations

ServiceDescription (com.connectsdk.service.config.ServiceDescription)6 DiscoveryProviderListener (com.connectsdk.discovery.DiscoveryProviderListener)2 Date (java.util.Date)2 ServiceConfig (com.connectsdk.service.config.ServiceConfig)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Matcher (java.util.regex.Matcher)1 JSONObject (org.json.JSONObject)1