Search in sources :

Example 1 with ServiceLocationException

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

the class JSLPDiscoveryJob method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
protected IStatus run(final IProgressMonitor monitor) {
    Assert.isNotNull(monitor);
    try {
        final Map availableServices = Activator.getDefault().getLocator().getServiceURLs();
        final Map removedServices = new HashMap(services);
        for (final Iterator itr = availableServices.entrySet().iterator(); itr.hasNext() && !monitor.isCanceled(); ) {
            final Map.Entry entry = (Map.Entry) itr.next();
            final ServiceURL url = (ServiceURL) entry.getKey();
            // do we know the service already?
            if (removedServices.containsKey(url)) {
                removedServices.remove(url);
            } else {
                // we don't know the service, so we need to create the
                final ServicePropertiesAdapter spa = new ServicePropertiesAdapter((List) entry.getValue());
                final String serviceName = spa.getServiceName() == null ? url.toString() : spa.getServiceName();
                final IServiceInfo serviceInfo = new JSLPServiceInfo(serviceName, new ServiceURLAdapter(url), spa.getPriority(), spa.getWeight(), spa);
                services.put(url, serviceInfo);
                discoveryContainer.fireServiceTypeDiscovered(serviceInfo.getServiceID().getServiceTypeID());
                discoveryContainer.fireServiceDiscovered(serviceInfo);
            }
            monitor.worked(1);
        }
        // at this point removedServices only contains stale services
        for (final Iterator itr = removedServices.entrySet().iterator(); itr.hasNext() && !monitor.isCanceled(); ) {
            final Map.Entry entry = (Map.Entry) itr.next();
            final Object key = entry.getKey();
            final IServiceInfo value = (IServiceInfo) entry.getValue();
            discoveryContainer.fireServiceUndiscovered(value);
            services.remove(key);
            monitor.worked(1);
        }
    } catch (final ServiceLocationException e) {
        // TODO-mkuppe if the advertiser is gone, we run into this exception
        // but we have to let the listeners know about the gone services
        // too
        // $NON-NLS-1$
        Trace.catching(Activator.PLUGIN_ID, JSLPDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "run", e);
    }
    // check if the JSLPDiscoveryContainer has been disconnected or disposed
    if (discoveryContainer.getConnectedID() != null) {
        this.schedule(JSLPDiscoveryContainer.REDISCOVER);
    }
    return Status.OK_STATUS;
}
Also used : JSLPServiceInfo(org.eclipse.ecf.provider.jslp.container.JSLPServiceInfo) IServiceInfo(org.eclipse.ecf.discovery.IServiceInfo) ServiceURL(ch.ethz.iks.slp.ServiceURL) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 2 with ServiceLocationException

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

the class JSLPDiscoveryContainer method registerService.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#registerService(org.eclipse.ecf.discovery.IServiceInfo)
	 */
public void registerService(IServiceInfo aServiceInfo) {
    Assert.isNotNull(aServiceInfo);
    try {
        JSLPServiceInfo si = new JSLPServiceInfo(aServiceInfo);
        IServiceTypeID stid = si.getServiceID().getServiceTypeID();
        Activator.getDefault().getAdvertiser().register(si.getServiceURL(), Arrays.asList(stid.getScopes()), new ServicePropertiesAdapter(si).toProperties());
    } catch (ServiceLocationException e) {
        // $NON-NLS-1$
        Trace.catching(Activator.PLUGIN_ID, JSLPDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "registerService(IServiceInfo)", e);
        throw new ECFRuntimeException(e.getMessage(), e);
    }
}
Also used : ECFRuntimeException(org.eclipse.ecf.core.util.ECFRuntimeException) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 3 with ServiceLocationException

use of ch.ethz.iks.slp.ServiceLocationException 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 4 with ServiceLocationException

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

the class SLPCore method setupReceiverThread.

/**
 * setup a new receiver thread for a socket.
 *
 * @param socket
 *            the <code>DatagramSocket</code> for which the receiver
 *            thread is set up.
 * @param minLifetime
 *            the minimum lifetime of the receiver thread.
 */
private static void setupReceiverThread(final DatagramSocket socket, final long minLifetime, final SLPMessage msg) {
    new Thread() {

        public void run() {
            // prepare an empty datagram for receiving
            DatagramPacket packet;
            byte[] bytes = new byte[SLPCore.CONFIG.getMTU()];
            // calculate the end of lifetime
            long timeout = System.currentTimeMillis() + minLifetime + 1000;
            // while lifetime is not expired
            while (System.currentTimeMillis() < timeout) {
                // set socket timeout
                try {
                    long l = timeout - System.currentTimeMillis();
                    int soTimeout = (int) (l < 0 ? 1 : l);
                    socket.setSoTimeout(soTimeout);
                } catch (SocketException e1) {
                    platform.logError("Exception in mcast receiver thread", e1);
                    return;
                }
                packet = new DatagramPacket(bytes, bytes.length);
                try {
                    // try to receive a datagram packet
                    socket.receive(packet);
                } catch (InterruptedIOException iioe) {
                    continue;
                } catch (IOException e) {
                    platform.logDebug(e.getMessage(), e);
                    return;
                }
                final DataInputStream in = new DataInputStream(new ByteArrayInputStream(packet.getData()));
                try {
                    // and delegate it to the SLPCore
                    handleMessage(SLPMessage.parse(packet.getAddress(), packet.getPort(), in, false));
                } catch (ProtocolException pe) {
                    // Overflow, try to use TCP
                    try {
                        msg.address = packet.getAddress();
                        msg.port = packet.getPort();
                        msg.multicast = false;
                        handleMessage(sendMessageTCP(msg));
                    } catch (ServiceLocationException e) {
                    }
                } catch (ServiceLocationException e) {
                    platform.logDebug(e.getMessage(), e);
                }
            }
            // close the socket
            socket.close();
        }
    }.start();
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) ProtocolException(java.net.ProtocolException) ByteArrayInputStream(java.io.ByteArrayInputStream) DatagramPacket(java.net.DatagramPacket) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 5 with ServiceLocationException

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

the class SLPCore method sendMessageTCP.

/**
 * send a unicast message over TCP.
 *
 * @param msg
 *            the message.
 * @return the reply.
 * @throws ServiceLocationException
 *             in case of network errors.
 */
static ReplyMessage sendMessageTCP(final SLPMessage msg) throws ServiceLocationException {
    try {
        if (msg.xid == 0) {
            msg.xid = nextXid();
        }
        Socket socket = new Socket(msg.address, msg.port);
        socket.setSoTimeout(CONFIG.getTCPTimeout());
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        DataInputStream in = new DataInputStream(socket.getInputStream());
        out.write(msg.getBytes());
        final ReplyMessage reply = (ReplyMessage) SLPMessage.parse(msg.address, msg.port, in, true);
        socket.close();
        return reply;
    } catch (Exception e) {
        throw new ServiceLocationException(ServiceLocationException.NETWORK_ERROR, e.getMessage());
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) MulticastSocket(java.net.MulticastSocket) 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)

Aggregations

ServiceLocationException (ch.ethz.iks.slp.ServiceLocationException)29 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 InterruptedIOException (java.io.InterruptedIOException)6 Iterator (java.util.Iterator)6 DataOutputStream (java.io.DataOutputStream)5 ProtocolException (java.net.ProtocolException)5 SocketException (java.net.SocketException)5 ServiceURL (ch.ethz.iks.slp.ServiceURL)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DatagramPacket (java.net.DatagramPacket)4 UnknownHostException (java.net.UnknownHostException)4 ServiceType (ch.ethz.iks.slp.ServiceType)3 DataInputStream (java.io.DataInputStream)3 DatagramSocket (java.net.DatagramSocket)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BindException (java.net.BindException)2 InetAddress (java.net.InetAddress)2 MulticastSocket (java.net.MulticastSocket)2