Search in sources :

Example 1 with ServiceType

use of ch.ethz.iks.slp.ServiceType in project ecf by eclipse.

the class JSLPNamespace method createInstance.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object[])
	 */
public ID createInstance(Object[] parameters) {
    // error case
    if (parameters == null || parameters.length < 1 || parameters.length > 2) {
        // $NON-NLS-1$
        throw new IDCreateException("Parameters cannot be null and must be of length 1 or 2");
    // error case
    } else if (parameters[0] == null || parameters[0].equals("")) {
        // $NON-NLS-1$
        throw new IDCreateException("First parameter cannot be null or empty String");
    // create by jSLP ServiceURL
    } else if (parameters.length == 2 && parameters[0] instanceof ServiceURL) {
        final ServiceURL anURL = (ServiceURL) parameters[0];
        final String[] scopes = (String[]) parameters[1];
        return new JSLPServiceTypeID(this, anURL, scopes);
    // final String serviceName = (String) (parameters[1] != null ? parameters[1] : anURL.getHost());
    // return null /*new JSLPServiceID(this, stid, serviceName)*/;
    // conversion call where conversion isn't necessary
    } else if (parameters.length == 1 && parameters[0] instanceof JSLPServiceID) {
        return (ID) parameters[0];
    // convert from IServiceID to IServiceTypeID, String
    } else if (parameters.length == 1 && parameters[0] instanceof IServiceID) {
        final IServiceID anId = (IServiceID) parameters[0];
        final Object[] newParams = new Object[2];
        newParams[0] = anId.getServiceTypeID();
        newParams[1] = anId.getName();
        return createInstance(newParams);
    // create by ECF discovery generic IServiceTypeID (but not JSLPServiceID!!!)
    } else if (parameters[0] instanceof IServiceTypeID) {
        final IServiceTypeID stid = (IServiceTypeID) parameters[0];
        parameters[0] = stid.getName();
        return createInstance(parameters);
    // create by jSLP ServiceType
    } else if (parameters.length == 1 && parameters[0] instanceof ServiceType) {
        return new JSLPServiceTypeID(this, (ServiceType) parameters[0]);
    // return new JSLPServiceID(this, stid, (String) parameters[1]);
    // create by jSLP ServiceType String representation (from external)
    } else if (parameters[0] instanceof String && ((String) parameters[0]).startsWith("service:")) {
        // $NON-NLS-1$
        parameters[0] = new ServiceType((String) parameters[0]);
        return createInstance(parameters);
    // create IServiceID by ECF discovery generic String representation
    } else if (parameters.length == 2 && parameters[0] instanceof String && ((String) parameters[0]).startsWith("_") && parameters[1] instanceof URI) {
        // $NON-NLS-1$
        final String type = (String) parameters[0];
        final URI anURI = (URI) parameters[1];
        final JSLPServiceTypeID serviceType = new JSLPServiceTypeID(this, new ServiceTypeID(this, type));
        return new JSLPServiceID(this, serviceType, anURI);
    // create IServiceTypeID by ECF discovery generic ServiceType
    } else if (parameters.length == 1 && parameters[0] instanceof String && ((String) parameters[0]).startsWith("_")) {
        // $NON-NLS-1$
        final String type = (String) parameters[0];
        return new JSLPServiceTypeID(this, new ServiceTypeID(this, type));
    // error case second parameter not a String
    } else if (parameters.length == 2 && parameters[1] != null && !(parameters[1] instanceof String)) {
        // $NON-NLS-1$
        throw new IDCreateException("Second parameter must be of type String");
    // error case
    } else {
        // $NON-NLS-1$
        throw new IDCreateException("Wrong JSLPServiceID creation parameters");
    }
}
Also used : URI(java.net.URI) ServiceType(ch.ethz.iks.slp.ServiceType) ServiceURL(ch.ethz.iks.slp.ServiceURL)

Example 2 with ServiceType

use of ch.ethz.iks.slp.ServiceType in project ecf by eclipse.

the class SLPCore method init.

protected static void init() {
    if (isInitialized) {
        return;
    }
    isInitialized = true;
    platform.logDebug("jSLP is running on the following interfaces: " + java.util.Arrays.asList(myIPs));
    platform.logDebug("jSLP is using port: " + SLP_PORT);
    String[] daAddresses = CONFIG.getDaAddresses();
    if (daAddresses == null) {
        if (noDiscovery) {
            throw new IllegalArgumentException("Configuration 'net.slp.noDaDiscovery=true' requires a non-empty list of preconfigured DAs");
        }
    } else {
        try {
            // process the preconfigured DAs
            final ServiceRequest req = new ServiceRequest(new ServiceType(SLP_DA_TYPE), null, null, null);
            req.port = SLP_PORT;
            for (int i = 0; i < daAddresses.length; i++) {
                try {
                    req.address = InetAddress.getByName(daAddresses[i]);
                    DAAdvertisement daa = (DAAdvertisement) sendMessage(req, true);
                    String[] scopes = (String[]) daa.scopeList.toArray(new String[daa.scopeList.size()]);
                    for (int j = 0; j < scopes.length; j++) {
                        platform.logDebug("jSLP is adding DA, " + daAddresses[i] + " for the Scope, " + scopes[j]);
                        SLPUtils.addValue(dAs, scopes[i].toLowerCase(), daAddresses[i]);
                    }
                } catch (ServiceLocationException e) {
                    platform.logWarning("Error communitcating with " + daAddresses[i], e);
                } catch (UnknownHostException e) {
                    platform.logWarning("Unknown net.slp.DAAddresses address: " + daAddresses[i], e);
                }
            }
        } catch (IllegalArgumentException ise) {
            platform.logDebug("May never happen", ise);
        }
    }
    if (!noDiscovery) {
        // perform an initial lookup
        try {
            List scopes = new ArrayList();
            scopes.add("default");
            daLookup(scopes);
        } catch (Exception e) {
            platform.logError("Exception in initial DA lookup", e);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) ServiceType(ch.ethz.iks.slp.ServiceType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 3 with ServiceType

use of ch.ethz.iks.slp.ServiceType in project ecf by eclipse.

the class SelfDiscoveryTest method testService.

/**
 * Test method for
 * {@link ch.ethz.iks.slp.Locator}.
 */
public void testService() throws Exception {
    int count = 0;
    for (ServiceLocationEnumeration services = TestActivator.locator.findServices(new ServiceType("service:osgi"), null, null); services.hasMoreElements(); ) {
        assertEquals(services.next().toString(), "service:osgi://" + HOST_AND_PORT);
        count++;
    }
    assertEquals(1, count);
}
Also used : ServiceType(ch.ethz.iks.slp.ServiceType) ServiceLocationEnumeration(ch.ethz.iks.slp.ServiceLocationEnumeration)

Example 4 with ServiceType

use of ch.ethz.iks.slp.ServiceType in project ecf by eclipse.

the class SelfDiscoveryTest method testFilter.

/**
 * Test method for
 * {@link ch.ethz.iks.slp.Locator}.
 */
public void testFilter() throws Exception {
    int count = 0;
    for (ServiceLocationEnumeration services = TestActivator.locator.findServices(new ServiceType("service:osgi"), null, "(attr=false)"); services.hasMoreElements(); ) {
        assertEquals(services.next().toString(), "service:osgi://" + HOST_AND_PORT);
        count++;
    }
    assertEquals(1, count);
}
Also used : ServiceType(ch.ethz.iks.slp.ServiceType) ServiceLocationEnumeration(ch.ethz.iks.slp.ServiceLocationEnumeration)

Example 5 with ServiceType

use of ch.ethz.iks.slp.ServiceType in project ecf by eclipse.

the class SLPCore method daLookup.

/**
 * find DAs for the scopes by sending a multicast service request for
 * service <i>service:directory-agent</i>.
 *
 * @param scopes
 *            a <code>List</code> of scopes.
 * @throws ServiceLocationException
 *             in case of network errors.
 */
static void daLookup(final List scopes) throws ServiceLocationException {
    int i = 0;
    try {
        // altered by Jan to be backwards compatible with Java 2
        for (; i < myIPs.length; i++) {
            // create a socket bound to the next ip address
            final InetAddress addr = InetAddress.getByName(myIPs[i]);
            DatagramSocket socket = new DatagramSocket(0, addr);
            ServiceRequest sreq = new ServiceRequest(new ServiceType(SLP_DA_TYPE), scopes, null, SLPCore.DEFAULT_LOCALE);
            sreq.xid = SLPCore.nextXid();
            sreq.scopeList = scopes;
            sreq.address = MCAST_ADDRESS;
            sreq.multicast = true;
            byte[] bytes = sreq.getBytes();
            DatagramPacket d = new DatagramPacket(bytes, bytes.length, MCAST_ADDRESS, SLP_PORT);
            platform.logTraceMessage("SENT " + sreq + "(udp multicast)");
            setupReceiverThread(socket, CONFIG.getWaitTime(), sreq);
            try {
                socket.send(d);
            } catch (SocketException se) {
                // blacklist address
                final List remaining = new ArrayList(java.util.Arrays.asList(myIPs));
                final String faulty = myIPs[i];
                remaining.remove(faulty);
                myIPs = (String[]) remaining.toArray(new String[remaining.size()]);
                platform.logDebug("Blacklisting IP " + faulty);
            }
        }
    } catch (IllegalArgumentException ise) {
        platform.logDebug("May never happen, no filter set", ise);
    } catch (UnknownHostException uhe) {
        platform.logWarning("Unknown net.slp.interfaces address: " + myIPs[i], uhe);
        throw new ServiceLocationException(ServiceLocationException.NETWORK_ERROR, uhe.getMessage());
    } catch (IOException e) {
        platform.logWarning("Error connecting to: " + myIPs[i], e);
        throw new ServiceLocationException(ServiceLocationException.NETWORK_ERROR, e.getMessage());
    }
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DatagramSocket(java.net.DatagramSocket) ServiceType(ch.ethz.iks.slp.ServiceType) DatagramPacket(java.net.DatagramPacket) ArrayList(java.util.ArrayList) List(java.util.List) InetAddress(java.net.InetAddress) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Aggregations

ServiceType (ch.ethz.iks.slp.ServiceType)7 ServiceLocationEnumeration (ch.ethz.iks.slp.ServiceLocationEnumeration)3 ServiceLocationException (ch.ethz.iks.slp.ServiceLocationException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ServiceURL (ch.ethz.iks.slp.ServiceURL)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 BindException (java.net.BindException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 ProtocolException (java.net.ProtocolException)1 URI (java.net.URI)1 Iterator (java.util.Iterator)1